blob: 7aa661f7689151f1b9d5d14ca15a23404160614e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010042#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/interrupt.h>
44#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020045#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020047#include <linux/of.h>
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +020048#include <linux/gpio/consumer.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010049
Boris Brezillon348d56a2018-09-07 00:38:48 +020050#include "internals.h"
51
Boris Brezillon41b207a2016-02-03 19:06:15 +010052/* Define default oob placement schemes for large and small page devices */
53static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
54 struct mtd_oob_region *oobregion)
55{
56 struct nand_chip *chip = mtd_to_nand(mtd);
57 struct nand_ecc_ctrl *ecc = &chip->ecc;
58
59 if (section > 1)
60 return -ERANGE;
61
62 if (!section) {
63 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020064 if (mtd->oobsize == 16)
65 oobregion->length = 4;
66 else
67 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010068 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020069 if (mtd->oobsize == 8)
70 return -ERANGE;
71
Boris Brezillon41b207a2016-02-03 19:06:15 +010072 oobregion->offset = 6;
73 oobregion->length = ecc->total - 4;
74 }
75
76 return 0;
77}
78
79static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
80 struct mtd_oob_region *oobregion)
81{
82 if (section > 1)
83 return -ERANGE;
84
85 if (mtd->oobsize == 16) {
86 if (section)
87 return -ERANGE;
88
89 oobregion->length = 8;
90 oobregion->offset = 8;
91 } else {
92 oobregion->length = 2;
93 if (!section)
94 oobregion->offset = 3;
95 else
96 oobregion->offset = 6;
97 }
98
99 return 0;
100}
101
102const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
103 .ecc = nand_ooblayout_ecc_sp,
104 .free = nand_ooblayout_free_sp,
105};
106EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
107
108static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
109 struct mtd_oob_region *oobregion)
110{
111 struct nand_chip *chip = mtd_to_nand(mtd);
112 struct nand_ecc_ctrl *ecc = &chip->ecc;
113
Miquel Raynal882fd152017-08-26 17:19:15 +0200114 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100115 return -ERANGE;
116
117 oobregion->length = ecc->total;
118 oobregion->offset = mtd->oobsize - oobregion->length;
119
120 return 0;
121}
122
123static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
124 struct mtd_oob_region *oobregion)
125{
126 struct nand_chip *chip = mtd_to_nand(mtd);
127 struct nand_ecc_ctrl *ecc = &chip->ecc;
128
129 if (section)
130 return -ERANGE;
131
132 oobregion->length = mtd->oobsize - ecc->total - 2;
133 oobregion->offset = 2;
134
135 return 0;
136}
137
138const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
139 .ecc = nand_ooblayout_ecc_lp,
140 .free = nand_ooblayout_free_lp,
141};
142EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200143
Alexander Couzens6a623e02017-05-02 12:19:00 +0200144/*
145 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
146 * are placed at a fixed offset.
147 */
148static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
149 struct mtd_oob_region *oobregion)
150{
151 struct nand_chip *chip = mtd_to_nand(mtd);
152 struct nand_ecc_ctrl *ecc = &chip->ecc;
153
154 if (section)
155 return -ERANGE;
156
157 switch (mtd->oobsize) {
158 case 64:
159 oobregion->offset = 40;
160 break;
161 case 128:
162 oobregion->offset = 80;
163 break;
164 default:
165 return -EINVAL;
166 }
167
168 oobregion->length = ecc->total;
169 if (oobregion->offset + oobregion->length > mtd->oobsize)
170 return -ERANGE;
171
172 return 0;
173}
174
175static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
176 struct mtd_oob_region *oobregion)
177{
178 struct nand_chip *chip = mtd_to_nand(mtd);
179 struct nand_ecc_ctrl *ecc = &chip->ecc;
180 int ecc_offset = 0;
181
182 if (section < 0 || section > 1)
183 return -ERANGE;
184
185 switch (mtd->oobsize) {
186 case 64:
187 ecc_offset = 40;
188 break;
189 case 128:
190 ecc_offset = 80;
191 break;
192 default:
193 return -EINVAL;
194 }
195
196 if (section == 0) {
197 oobregion->offset = 2;
198 oobregion->length = ecc_offset - 2;
199 } else {
200 oobregion->offset = ecc_offset + ecc->total;
201 oobregion->length = mtd->oobsize - oobregion->offset;
202 }
203
204 return 0;
205}
206
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100207static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200208 .ecc = nand_ooblayout_ecc_lp_hamming,
209 .free = nand_ooblayout_free_lp_hamming,
210};
211
Boris Brezillon08136212018-11-11 08:55:03 +0100212static int check_offs_len(struct nand_chip *chip, loff_t ofs, uint64_t len)
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530213{
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530214 int ret = 0;
215
216 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300217 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700218 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530219 ret = -EINVAL;
220 }
221
222 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530228 return ret;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/**
Boris Brezillon1d017852018-11-11 08:55:14 +0100232 * nand_select_target() - Select a NAND target (A.K.A. die)
233 * @chip: NAND chip object
234 * @cs: the CS line to select. Note that this CS id is always from the chip
235 * PoV, not the controller one
236 *
237 * Select a NAND target so that further operations executed on @chip go to the
238 * selected NAND target.
239 */
240void nand_select_target(struct nand_chip *chip, unsigned int cs)
241{
242 /*
243 * cs should always lie between 0 and chip->numchips, when that's not
244 * the case it's a bug and the caller should be fixed.
245 */
246 if (WARN_ON(cs > chip->numchips))
247 return;
248
Boris Brezillonae2294b2018-11-11 08:55:15 +0100249 chip->cur_cs = cs;
Boris Brezillon1d017852018-11-11 08:55:14 +0100250 chip->select_chip(chip, cs);
251}
252EXPORT_SYMBOL_GPL(nand_select_target);
253
254/**
255 * nand_deselect_target() - Deselect the currently selected target
256 * @chip: NAND chip object
257 *
258 * Deselect the currently selected NAND target. The result of operations
259 * executed on @chip after the target has been deselected is undefined.
260 */
261void nand_deselect_target(struct nand_chip *chip)
262{
263 chip->select_chip(chip, -1);
Boris Brezillonae2294b2018-11-11 08:55:15 +0100264 chip->cur_cs = -1;
Boris Brezillon1d017852018-11-11 08:55:14 +0100265}
266EXPORT_SYMBOL_GPL(nand_deselect_target);
267
268/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * nand_release_device - [GENERIC] release chip
Boris Brezillon08136212018-11-11 08:55:03 +0100270 * @chip: NAND chip object
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000271 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800272 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Boris Brezillon08136212018-11-11 08:55:03 +0100274static void nand_release_device(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200276 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 spin_lock(&chip->controller->lock);
278 chip->controller->active = NULL;
279 chip->state = FL_READY;
280 wake_up(&chip->controller->wq);
281 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Boris Brezillonc17556f2018-09-06 14:05:25 +0200286 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700287 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000289 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200291static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200293 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900294 int page, page_end, res;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900295 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Brian Norris5fb15492011-05-31 16:31:21 -0700297 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700298 ofs += mtd->erasesize - mtd->writesize;
299
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100300 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900301 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100302
Masahiro Yamadac120e752017-03-23 05:07:01 +0900303 for (; page < page_end; page++) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200304 res = chip->ecc.read_oob(chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530305 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900306 return res;
307
308 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000309
Brian Norriscdbec052012-01-13 18:11:48 -0800310 if (likely(chip->badblockbits == 8))
311 res = bad != 0xFF;
312 else
313 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900314 if (res)
315 return res;
316 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200317
Masahiro Yamadac120e752017-03-23 05:07:01 +0900318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Boris Brezillon99f33512018-11-11 08:55:04 +0100321static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
322{
323 if (chip->legacy.block_bad)
324 return chip->legacy.block_bad(chip, ofs);
325
326 return nand_block_bad(chip, ofs);
327}
328
329/**
330 * panic_nand_get_device - [GENERIC] Get chip for selected access
331 * @chip: the nand chip descriptor
332 * @new_state: the state which is requested
333 *
334 * Used when in panic, no locks are taken.
335 */
336static void panic_nand_get_device(struct nand_chip *chip, int new_state)
337{
338 /* Hardware controller shared among independent devices */
339 chip->controller->active = chip;
340 chip->state = new_state;
341}
342
343/**
344 * nand_get_device - [GENERIC] Get chip for selected access
345 * @chip: NAND chip structure
346 * @new_state: the state which is requested
347 *
348 * Get the device and lock it for exclusive access
349 */
350static int
351nand_get_device(struct nand_chip *chip, int new_state)
352{
353 spinlock_t *lock = &chip->controller->lock;
354 wait_queue_head_t *wq = &chip->controller->wq;
355 DECLARE_WAITQUEUE(wait, current);
356retry:
357 spin_lock(lock);
358
359 /* Hardware controller shared among independent devices */
360 if (!chip->controller->active)
361 chip->controller->active = chip;
362
363 if (chip->controller->active == chip && chip->state == FL_READY) {
364 chip->state = new_state;
365 spin_unlock(lock);
366 return 0;
367 }
368 if (new_state == FL_PM_SUSPENDED) {
369 if (chip->controller->active->state == FL_PM_SUSPENDED) {
370 chip->state = FL_PM_SUSPENDED;
371 spin_unlock(lock);
372 return 0;
373 }
374 }
375 set_current_state(TASK_UNINTERRUPTIBLE);
376 add_wait_queue(wq, &wait);
377 spin_unlock(lock);
378 schedule();
379 remove_wait_queue(wq, &wait);
380 goto retry;
381}
382
383/**
384 * nand_check_wp - [GENERIC] check if the chip is write protected
385 * @chip: NAND chip object
386 *
387 * Check, if the device is write protected. The function expects, that the
388 * device is already selected.
389 */
390static int nand_check_wp(struct nand_chip *chip)
391{
392 u8 status;
393 int ret;
394
395 /* Broken xD cards report WP despite being writable */
396 if (chip->options & NAND_BROKEN_XD)
397 return 0;
398
399 /* Check the WP bit */
400 ret = nand_status_op(chip, &status);
401 if (ret)
402 return ret;
403
404 return status & NAND_STATUS_WP ? 0 : 1;
405}
406
407/**
408 * nand_fill_oob - [INTERN] Transfer client buffer to oob
409 * @oob: oob data buffer
410 * @len: oob data write length
411 * @ops: oob ops structure
412 */
413static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
414 struct mtd_oob_ops *ops)
415{
416 struct mtd_info *mtd = nand_to_mtd(chip);
417 int ret;
418
419 /*
420 * Initialise to all 0xFF, to avoid the possibility of left over OOB
421 * data from a previous OOB read.
422 */
423 memset(chip->oob_poi, 0xff, mtd->oobsize);
424
425 switch (ops->mode) {
426
427 case MTD_OPS_PLACE_OOB:
428 case MTD_OPS_RAW:
429 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
430 return oob + len;
431
432 case MTD_OPS_AUTO_OOB:
433 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
434 ops->ooboffs, len);
435 BUG_ON(ret);
436 return oob + len;
437
438 default:
439 BUG();
440 }
441 return NULL;
442}
443
444/**
445 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
446 * @chip: NAND chip object
447 * @to: offset to write to
448 * @ops: oob operation description structure
449 *
450 * NAND write out-of-band.
451 */
452static int nand_do_write_oob(struct nand_chip *chip, loff_t to,
453 struct mtd_oob_ops *ops)
454{
455 struct mtd_info *mtd = nand_to_mtd(chip);
456 int chipnr, page, status, len;
457
458 pr_debug("%s: to = 0x%08x, len = %i\n",
459 __func__, (unsigned int)to, (int)ops->ooblen);
460
461 len = mtd_oobavail(mtd, ops);
462
463 /* Do not allow write past end of page */
464 if ((ops->ooboffs + ops->ooblen) > len) {
465 pr_debug("%s: attempt to write past end of page\n",
466 __func__);
467 return -EINVAL;
468 }
469
470 chipnr = (int)(to >> chip->chip_shift);
471
472 /*
473 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
474 * of my DiskOnChip 2000 test units) will clear the whole data page too
475 * if we don't do this. I have no clue why, but I seem to have 'fixed'
476 * it in the doc2000 driver in August 1999. dwmw2.
477 */
478 nand_reset(chip, chipnr);
479
Boris Brezillon1d017852018-11-11 08:55:14 +0100480 nand_select_target(chip, chipnr);
Boris Brezillon99f33512018-11-11 08:55:04 +0100481
482 /* Shift to get page */
483 page = (int)(to >> chip->page_shift);
484
485 /* Check, if it is write protected */
486 if (nand_check_wp(chip)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100487 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100488 return -EROFS;
489 }
490
491 /* Invalidate the page cache, if we write to the cached page */
492 if (page == chip->pagebuf)
493 chip->pagebuf = -1;
494
495 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
496
497 if (ops->mode == MTD_OPS_RAW)
498 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
499 else
500 status = chip->ecc.write_oob(chip, page & chip->pagemask);
501
Boris Brezillon1d017852018-11-11 08:55:14 +0100502 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100503
504 if (status)
505 return status;
506
507 ops->oobretlen = ops->ooblen;
508
509 return 0;
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700513 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200514 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700515 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700517 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700518 * specific driver. It provides the details for writing a bad block marker to a
519 * block.
520 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200521static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700522{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200523 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700524 struct mtd_oob_ops ops;
525 uint8_t buf[2] = { 0, 0 };
526 int ret = 0, res, i = 0;
527
Brian Norris0ec56dc2015-02-28 02:02:30 -0800528 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700529 ops.oobbuf = buf;
530 ops.ooboffs = chip->badblockpos;
531 if (chip->options & NAND_BUSWIDTH_16) {
532 ops.ooboffs &= ~0x01;
533 ops.len = ops.ooblen = 2;
534 } else {
535 ops.len = ops.ooblen = 1;
536 }
537 ops.mode = MTD_OPS_PLACE_OOB;
538
539 /* Write to first/last page(s) if necessary */
540 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
541 ofs += mtd->erasesize - mtd->writesize;
542 do {
Boris Brezillon08136212018-11-11 08:55:03 +0100543 res = nand_do_write_oob(chip, ofs, &ops);
Brian Norris5a0edb22013-07-30 17:52:58 -0700544 if (!ret)
545 ret = res;
546
547 i++;
548 ofs += mtd->writesize;
549 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
550
551 return ret;
552}
553
554/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200555 * nand_markbad_bbm - mark a block by updating the BBM
556 * @chip: NAND chip object
557 * @ofs: offset of the block to mark bad
558 */
559int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
560{
561 if (chip->legacy.block_markbad)
562 return chip->legacy.block_markbad(chip, ofs);
563
564 return nand_default_block_markbad(chip, ofs);
565}
566
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200567/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700568 * nand_block_markbad_lowlevel - mark a block bad
Boris Brezillon08136212018-11-11 08:55:03 +0100569 * @chip: NAND chip object
Brian Norris5a0edb22013-07-30 17:52:58 -0700570 * @ofs: offset from device start
571 *
572 * This function performs the generic NAND bad block marking steps (i.e., bad
573 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200574 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700575 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700576 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300577 *
Brian Norrise2414f42012-02-06 13:44:00 -0800578 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700579 * (2) write bad block marker to OOB area of affected block (unless flag
580 * NAND_BBT_NO_OOB_BBM is present)
581 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300582 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700583 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800584 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585*/
Boris Brezillon08136212018-11-11 08:55:03 +0100586static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Boris Brezillon08136212018-11-11 08:55:03 +0100588 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisb32843b2013-07-30 17:52:59 -0700589 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000590
Brian Norrisb32843b2013-07-30 17:52:59 -0700591 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800592 struct erase_info einfo;
593
594 /* Attempt erase before marking OOB */
595 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800596 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300597 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200598 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800599
Brian Norrisb32843b2013-07-30 17:52:59 -0700600 /* Write bad block marker to OOB */
Boris Brezillon08136212018-11-11 08:55:03 +0100601 nand_get_device(chip, FL_WRITING);
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200602 ret = nand_markbad_bbm(chip, ofs);
Boris Brezillon08136212018-11-11 08:55:03 +0100603 nand_release_device(chip);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200604 }
Brian Norrise2414f42012-02-06 13:44:00 -0800605
Brian Norrisb32843b2013-07-30 17:52:59 -0700606 /* Mark block bad in BBT */
607 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200608 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800609 if (!ret)
610 ret = res;
611 }
612
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200613 if (!ret)
614 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300615
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200616 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000619/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800620 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700621 * @mtd: MTD device structure
622 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300623 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800624 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300625 */
626static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
627{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100628 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300629
630 if (!chip->bbt)
631 return 0;
632 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200633 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300634}
635
636/**
637 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Boris Brezillon08136212018-11-11 08:55:03 +0100638 * @chip: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300639 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700640 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 *
642 * Check, if the block is bad. Either by reading the bad block table or
643 * calling of the scan function.
644 */
Boris Brezillon08136212018-11-11 08:55:03 +0100645static int nand_block_checkbad(struct nand_chip *chip, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200648 if (chip->bbt)
649 return nand_isbad_bbt(chip, ofs, allowbbt);
650
651 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200654/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100655 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
656 * @chip: NAND chip structure
657 * @timeout_ms: Timeout in ms
658 *
659 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
660 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
661 * returned.
662 *
663 * This helper is intended to be used when the controller does not have access
664 * to the NAND R/B pin.
665 *
666 * Be aware that calling this helper from an ->exec_op() implementation means
667 * ->exec_op() must be re-entrant.
668 *
669 * Return 0 if the NAND chip is ready, a negative error otherwise.
670 */
671int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
672{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200673 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100674 u8 status = 0;
675 int ret;
676
677 if (!chip->exec_op)
678 return -ENOTSUPP;
679
Boris Brezillon3057fce2018-05-04 21:24:31 +0200680 /* Wait tWB before polling the STATUS reg. */
681 timings = nand_get_sdr_timings(&chip->data_interface);
682 ndelay(PSEC_TO_NSEC(timings->tWB_max));
683
Miquel Raynal8878b122017-11-09 14:16:45 +0100684 ret = nand_status_op(chip, NULL);
685 if (ret)
686 return ret;
687
688 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
689 do {
690 ret = nand_read_data_op(chip, &status, sizeof(status), true);
691 if (ret)
692 break;
693
694 if (status & NAND_STATUS_READY)
695 break;
696
697 /*
698 * Typical lowest execution time for a tR on most NANDs is 10us,
699 * use this as polling delay before doing something smarter (ie.
700 * deriving a delay from the timeout value, timeout_ms/ratio).
701 */
702 udelay(10);
703 } while (time_before(jiffies, timeout_ms));
704
705 /*
706 * We have to exit READ_STATUS mode in order to read real data on the
707 * bus in case the WAITRDY instruction is preceding a DATA_IN
708 * instruction.
709 */
710 nand_exit_status_op(chip);
711
712 if (ret)
713 return ret;
714
715 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
716};
717EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
718
719/**
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +0200720 * nand_gpio_waitrdy - Poll R/B GPIO pin until ready
721 * @chip: NAND chip structure
722 * @gpiod: GPIO descriptor of R/B pin
723 * @timeout_ms: Timeout in ms
724 *
725 * Poll the R/B GPIO pin until it becomes ready. If that does not happen
726 * whitin the specified timeout, -ETIMEDOUT is returned.
727 *
728 * This helper is intended to be used when the controller has access to the
729 * NAND R/B pin over GPIO.
730 *
731 * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise.
732 */
733int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
734 unsigned long timeout_ms)
735{
736 /* Wait until R/B pin indicates chip is ready or timeout occurs */
737 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
738 do {
739 if (gpiod_get_value_cansleep(gpiod))
740 return 0;
741
742 cond_resched();
743 } while (time_before(jiffies, timeout_ms));
744
745 return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT;
746};
747EXPORT_SYMBOL_GPL(nand_gpio_waitrdy);
748
749/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700750 * panic_nand_wait - [GENERIC] wait until the command is done
Brian Norris8b6e50c2011-05-25 14:59:01 -0700751 * @chip: NAND chip structure
752 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200753 *
754 * Wait for command done. This is a helper function for nand_wait used when
755 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400756 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200757 */
Boris Brezillon3d4af7c2018-09-07 00:38:49 +0200758void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200759{
760 int i;
761 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200762 if (chip->legacy.dev_ready) {
763 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200764 break;
765 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100766 int ret;
767 u8 status;
768
769 ret = nand_read_data_op(chip, &status, sizeof(status),
770 true);
771 if (ret)
772 return;
773
774 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200775 break;
776 }
777 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200778 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200779}
780
Miquel Raynal789157e2018-03-19 14:47:28 +0100781static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +0100782{
Miquel Raynal789157e2018-03-19 14:47:28 +0100783 return (chip->parameters.supports_set_get_features &&
784 test_bit(addr, chip->parameters.get_feature_list));
785}
786
787static bool nand_supports_set_features(struct nand_chip *chip, int addr)
788{
789 return (chip->parameters.supports_set_get_features &&
790 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +0100791}
792
793/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200794 * nand_reset_data_interface - Reset data interface and timings
795 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100796 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200797 *
798 * Reset the Data interface and timings to ONFI mode 0.
799 *
800 * Returns 0 for success or negative error code otherwise.
801 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100802static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200803{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200804 int ret;
805
806 if (!chip->setup_data_interface)
807 return 0;
808
809 /*
810 * The ONFI specification says:
811 * "
812 * To transition from NV-DDR or NV-DDR2 to the SDR data
813 * interface, the host shall use the Reset (FFh) command
814 * using SDR timing mode 0. A device in any timing mode is
815 * required to recognize Reset (FFh) command issued in SDR
816 * timing mode 0.
817 * "
818 *
819 * Configure the data interface in SDR mode and set the
820 * timings to timing mode 0.
821 */
822
Miquel Raynal17fa8042017-11-30 18:01:31 +0100823 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon858838b2018-09-06 14:05:33 +0200824 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200825 if (ret)
826 pr_err("Failed to configure data interface to SDR timing mode 0\n");
827
828 return ret;
829}
830
831/**
832 * nand_setup_data_interface - Setup the best data interface and timings
833 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100834 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200835 *
836 * Find and configure the best data interface and NAND timings supported by
837 * the chip and the driver.
838 * First tries to retrieve supported timing modes from ONFI information,
839 * and if the NAND chip does not support ONFI, relies on the
840 * ->onfi_timing_mode_default specified in the nand_ids table.
841 *
842 * Returns 0 for success or negative error code otherwise.
843 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100844static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200845{
Miquel Raynal97baea12018-03-19 14:47:20 +0100846 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
847 chip->onfi_timing_mode_default,
848 };
Boris Brezillond8e725d2016-09-15 10:32:50 +0200849 int ret;
850
Miquel Raynal17fa8042017-11-30 18:01:31 +0100851 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200852 return 0;
853
Miquel Raynal993447b2018-03-19 14:47:21 +0100854 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +0100855 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100856 nand_select_target(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +0100857 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
858 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100859 nand_deselect_target(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200860 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +0100861 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +0200862 }
863
Miquel Raynal97baea12018-03-19 14:47:20 +0100864 /* Change the mode on the controller side */
Boris Brezillon858838b2018-09-06 14:05:33 +0200865 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +0100866 if (ret)
867 return ret;
868
869 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +0100870 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +0100871 return 0;
872
873 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon1d017852018-11-11 08:55:14 +0100874 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100875 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
876 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100877 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100878 if (ret)
879 goto err_reset_chip;
880
881 if (tmode_param[0] != chip->onfi_timing_mode_default) {
882 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
883 chip->onfi_timing_mode_default);
884 goto err_reset_chip;
885 }
886
887 return 0;
888
889err_reset_chip:
890 /*
891 * Fallback to mode 0 if the chip explicitly did not ack the chosen
892 * timing mode.
893 */
894 nand_reset_data_interface(chip, chipnr);
Boris Brezillon1d017852018-11-11 08:55:14 +0100895 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100896 nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +0100897 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100898
Boris Brezillond8e725d2016-09-15 10:32:50 +0200899 return ret;
900}
901
902/**
903 * nand_init_data_interface - find the best data interface and timings
904 * @chip: The NAND chip
905 *
906 * Find the best data interface and NAND timings supported by the chip
907 * and the driver.
908 * First tries to retrieve supported timing modes from ONFI information,
909 * and if the NAND chip does not support ONFI, relies on the
910 * ->onfi_timing_mode_default specified in the nand_ids table. After this
911 * function nand_chip->data_interface is initialized with the best timing mode
912 * available.
913 *
914 * Returns 0 for success or negative error code otherwise.
915 */
916static int nand_init_data_interface(struct nand_chip *chip)
917{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200918 int modes, mode, ret;
919
920 if (!chip->setup_data_interface)
921 return 0;
922
923 /*
924 * First try to identify the best timings from ONFI parameters and
925 * if the NAND does not support ONFI, fallback to the default ONFI
926 * timing mode.
927 */
Boris Brezillon462f35d2018-09-07 00:38:47 +0200928 if (chip->parameters.onfi) {
929 modes = chip->parameters.onfi->async_timing_mode;
930 } else {
Boris Brezillond8e725d2016-09-15 10:32:50 +0200931 if (!chip->onfi_timing_mode_default)
932 return 0;
933
934 modes = GENMASK(chip->onfi_timing_mode_default, 0);
935 }
936
Boris Brezillond8e725d2016-09-15 10:32:50 +0200937 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +0100938 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200939 if (ret)
940 continue;
941
Miquel Raynald787b8b2017-12-22 18:12:41 +0100942 /*
943 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
944 * controller supports the requested timings.
945 */
Boris Brezillon858838b2018-09-06 14:05:33 +0200946 ret = chip->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +0100947 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +0100948 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200949 if (!ret) {
950 chip->onfi_timing_mode_default = mode;
951 break;
952 }
953 }
954
955 return 0;
956}
957
Boris Brezillond8e725d2016-09-15 10:32:50 +0200958/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100959 * nand_fill_column_cycles - fill the column cycles of an address
960 * @chip: The NAND chip
961 * @addrs: Array of address cycles to fill
962 * @offset_in_page: The offset in the page
963 *
964 * Fills the first or the first two bytes of the @addrs field depending
965 * on the NAND bus width and the page size.
966 *
967 * Returns the number of cycles needed to encode the column, or a negative
968 * error code in case one of the arguments is invalid.
969 */
970static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
971 unsigned int offset_in_page)
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100972{
Miquel Raynal8878b122017-11-09 14:16:45 +0100973 struct mtd_info *mtd = nand_to_mtd(chip);
974
975 /* Make sure the offset is less than the actual page size. */
976 if (offset_in_page > mtd->writesize + mtd->oobsize)
977 return -EINVAL;
978
979 /*
980 * On small page NANDs, there's a dedicated command to access the OOB
981 * area, and the column address is relative to the start of the OOB
982 * area, not the start of the page. Asjust the address accordingly.
983 */
984 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
985 offset_in_page -= mtd->writesize;
986
987 /*
988 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
989 * wide, then it must be divided by 2.
990 */
991 if (chip->options & NAND_BUSWIDTH_16) {
992 if (WARN_ON(offset_in_page % 2))
993 return -EINVAL;
994
995 offset_in_page /= 2;
996 }
997
998 addrs[0] = offset_in_page;
999
1000 /*
1001 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1002 * need 2
1003 */
1004 if (mtd->writesize <= 512)
1005 return 1;
1006
1007 addrs[1] = offset_in_page >> 8;
1008
1009 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Miquel Raynal8878b122017-11-09 14:16:45 +01001012static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1013 unsigned int offset_in_page, void *buf,
1014 unsigned int len)
1015{
1016 struct mtd_info *mtd = nand_to_mtd(chip);
1017 const struct nand_sdr_timings *sdr =
1018 nand_get_sdr_timings(&chip->data_interface);
1019 u8 addrs[4];
1020 struct nand_op_instr instrs[] = {
1021 NAND_OP_CMD(NAND_CMD_READ0, 0),
1022 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1023 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1024 PSEC_TO_NSEC(sdr->tRR_min)),
1025 NAND_OP_DATA_IN(len, buf, 0),
1026 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001027 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001028 int ret;
1029
1030 /* Drop the DATA_IN instruction if len is set to 0. */
1031 if (!len)
1032 op.ninstrs--;
1033
1034 if (offset_in_page >= mtd->writesize)
1035 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1036 else if (offset_in_page >= 256 &&
1037 !(chip->options & NAND_BUSWIDTH_16))
1038 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1039
1040 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1041 if (ret < 0)
1042 return ret;
1043
1044 addrs[1] = page;
1045 addrs[2] = page >> 8;
1046
1047 if (chip->options & NAND_ROW_ADDR_3) {
1048 addrs[3] = page >> 16;
1049 instrs[1].ctx.addr.naddrs++;
1050 }
1051
1052 return nand_exec_op(chip, &op);
1053}
1054
1055static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1056 unsigned int offset_in_page, void *buf,
1057 unsigned int len)
1058{
1059 const struct nand_sdr_timings *sdr =
1060 nand_get_sdr_timings(&chip->data_interface);
1061 u8 addrs[5];
1062 struct nand_op_instr instrs[] = {
1063 NAND_OP_CMD(NAND_CMD_READ0, 0),
1064 NAND_OP_ADDR(4, addrs, 0),
1065 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1066 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1067 PSEC_TO_NSEC(sdr->tRR_min)),
1068 NAND_OP_DATA_IN(len, buf, 0),
1069 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001070 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001071 int ret;
1072
1073 /* Drop the DATA_IN instruction if len is set to 0. */
1074 if (!len)
1075 op.ninstrs--;
1076
1077 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1078 if (ret < 0)
1079 return ret;
1080
1081 addrs[2] = page;
1082 addrs[3] = page >> 8;
1083
1084 if (chip->options & NAND_ROW_ADDR_3) {
1085 addrs[4] = page >> 16;
1086 instrs[1].ctx.addr.naddrs++;
1087 }
1088
1089 return nand_exec_op(chip, &op);
1090}
1091
1092/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001093 * nand_read_page_op - Do a READ PAGE operation
1094 * @chip: The NAND chip
1095 * @page: page to read
1096 * @offset_in_page: offset within the page
1097 * @buf: buffer used to store the data
1098 * @len: length of the buffer
1099 *
1100 * This function issues a READ PAGE operation.
1101 * This function does not select/unselect the CS line.
1102 *
1103 * Returns 0 on success, a negative error code otherwise.
1104 */
1105int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1106 unsigned int offset_in_page, void *buf, unsigned int len)
1107{
1108 struct mtd_info *mtd = nand_to_mtd(chip);
1109
1110 if (len && !buf)
1111 return -EINVAL;
1112
1113 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1114 return -EINVAL;
1115
Miquel Raynal8878b122017-11-09 14:16:45 +01001116 if (chip->exec_op) {
1117 if (mtd->writesize > 512)
1118 return nand_lp_exec_read_page_op(chip, page,
1119 offset_in_page, buf,
1120 len);
1121
1122 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1123 buf, len);
1124 }
1125
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001126 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001127 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001128 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001129
1130 return 0;
1131}
1132EXPORT_SYMBOL_GPL(nand_read_page_op);
1133
1134/**
1135 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1136 * @chip: The NAND chip
1137 * @page: parameter page to read
1138 * @buf: buffer used to store the data
1139 * @len: length of the buffer
1140 *
1141 * This function issues a READ PARAMETER PAGE operation.
1142 * This function does not select/unselect the CS line.
1143 *
1144 * Returns 0 on success, a negative error code otherwise.
1145 */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02001146int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1147 unsigned int len)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001148{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001149 unsigned int i;
1150 u8 *p = buf;
1151
1152 if (len && !buf)
1153 return -EINVAL;
1154
Miquel Raynal8878b122017-11-09 14:16:45 +01001155 if (chip->exec_op) {
1156 const struct nand_sdr_timings *sdr =
1157 nand_get_sdr_timings(&chip->data_interface);
1158 struct nand_op_instr instrs[] = {
1159 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1160 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1161 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1162 PSEC_TO_NSEC(sdr->tRR_min)),
1163 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1164 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001165 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001166
1167 /* Drop the DATA_IN instruction if len is set to 0. */
1168 if (!len)
1169 op.ninstrs--;
1170
1171 return nand_exec_op(chip, &op);
1172 }
1173
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001174 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001175 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001176 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001177
1178 return 0;
1179}
1180
1181/**
1182 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1183 * @chip: The NAND chip
1184 * @offset_in_page: offset within the page
1185 * @buf: buffer used to store the data
1186 * @len: length of the buffer
1187 * @force_8bit: force 8-bit bus access
1188 *
1189 * This function issues a CHANGE READ COLUMN operation.
1190 * This function does not select/unselect the CS line.
1191 *
1192 * Returns 0 on success, a negative error code otherwise.
1193 */
1194int nand_change_read_column_op(struct nand_chip *chip,
1195 unsigned int offset_in_page, void *buf,
1196 unsigned int len, bool force_8bit)
1197{
1198 struct mtd_info *mtd = nand_to_mtd(chip);
1199
1200 if (len && !buf)
1201 return -EINVAL;
1202
1203 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1204 return -EINVAL;
1205
Miquel Raynal8878b122017-11-09 14:16:45 +01001206 /* Small page NANDs do not support column change. */
1207 if (mtd->writesize <= 512)
1208 return -ENOTSUPP;
1209
1210 if (chip->exec_op) {
1211 const struct nand_sdr_timings *sdr =
1212 nand_get_sdr_timings(&chip->data_interface);
1213 u8 addrs[2] = {};
1214 struct nand_op_instr instrs[] = {
1215 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1216 NAND_OP_ADDR(2, addrs, 0),
1217 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1218 PSEC_TO_NSEC(sdr->tCCS_min)),
1219 NAND_OP_DATA_IN(len, buf, 0),
1220 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001221 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001222 int ret;
1223
1224 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1225 if (ret < 0)
1226 return ret;
1227
1228 /* Drop the DATA_IN instruction if len is set to 0. */
1229 if (!len)
1230 op.ninstrs--;
1231
1232 instrs[3].ctx.data.force_8bit = force_8bit;
1233
1234 return nand_exec_op(chip, &op);
1235 }
1236
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001237 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001238 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001239 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001240
1241 return 0;
1242}
1243EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1244
1245/**
1246 * nand_read_oob_op - Do a READ OOB operation
1247 * @chip: The NAND chip
1248 * @page: page to read
1249 * @offset_in_oob: offset within the OOB area
1250 * @buf: buffer used to store the data
1251 * @len: length of the buffer
1252 *
1253 * This function issues a READ OOB operation.
1254 * This function does not select/unselect the CS line.
1255 *
1256 * Returns 0 on success, a negative error code otherwise.
1257 */
1258int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1259 unsigned int offset_in_oob, void *buf, unsigned int len)
1260{
1261 struct mtd_info *mtd = nand_to_mtd(chip);
1262
1263 if (len && !buf)
1264 return -EINVAL;
1265
1266 if (offset_in_oob + len > mtd->oobsize)
1267 return -EINVAL;
1268
Miquel Raynal8878b122017-11-09 14:16:45 +01001269 if (chip->exec_op)
1270 return nand_read_page_op(chip, page,
1271 mtd->writesize + offset_in_oob,
1272 buf, len);
1273
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001274 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001275 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001276 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001277
1278 return 0;
1279}
1280EXPORT_SYMBOL_GPL(nand_read_oob_op);
1281
Miquel Raynal8878b122017-11-09 14:16:45 +01001282static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1283 unsigned int offset_in_page, const void *buf,
1284 unsigned int len, bool prog)
1285{
1286 struct mtd_info *mtd = nand_to_mtd(chip);
1287 const struct nand_sdr_timings *sdr =
1288 nand_get_sdr_timings(&chip->data_interface);
1289 u8 addrs[5] = {};
1290 struct nand_op_instr instrs[] = {
1291 /*
1292 * The first instruction will be dropped if we're dealing
1293 * with a large page NAND and adjusted if we're dealing
1294 * with a small page NAND and the page offset is > 255.
1295 */
1296 NAND_OP_CMD(NAND_CMD_READ0, 0),
1297 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1298 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1299 NAND_OP_DATA_OUT(len, buf, 0),
1300 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1301 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1302 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001303 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001304 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1305 int ret;
1306 u8 status;
1307
1308 if (naddrs < 0)
1309 return naddrs;
1310
1311 addrs[naddrs++] = page;
1312 addrs[naddrs++] = page >> 8;
1313 if (chip->options & NAND_ROW_ADDR_3)
1314 addrs[naddrs++] = page >> 16;
1315
1316 instrs[2].ctx.addr.naddrs = naddrs;
1317
1318 /* Drop the last two instructions if we're not programming the page. */
1319 if (!prog) {
1320 op.ninstrs -= 2;
1321 /* Also drop the DATA_OUT instruction if empty. */
1322 if (!len)
1323 op.ninstrs--;
1324 }
1325
1326 if (mtd->writesize <= 512) {
1327 /*
1328 * Small pages need some more tweaking: we have to adjust the
1329 * first instruction depending on the page offset we're trying
1330 * to access.
1331 */
1332 if (offset_in_page >= mtd->writesize)
1333 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1334 else if (offset_in_page >= 256 &&
1335 !(chip->options & NAND_BUSWIDTH_16))
1336 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1337 } else {
1338 /*
1339 * Drop the first command if we're dealing with a large page
1340 * NAND.
1341 */
1342 op.instrs++;
1343 op.ninstrs--;
1344 }
1345
1346 ret = nand_exec_op(chip, &op);
1347 if (!prog || ret)
1348 return ret;
1349
1350 ret = nand_status_op(chip, &status);
1351 if (ret)
1352 return ret;
1353
1354 return status;
1355}
1356
Boris Brezillon97d90da2017-11-30 18:01:29 +01001357/**
1358 * nand_prog_page_begin_op - starts a PROG PAGE operation
1359 * @chip: The NAND chip
1360 * @page: page to write
1361 * @offset_in_page: offset within the page
1362 * @buf: buffer containing the data to write to the page
1363 * @len: length of the buffer
1364 *
1365 * This function issues the first half of a PROG PAGE operation.
1366 * This function does not select/unselect the CS line.
1367 *
1368 * Returns 0 on success, a negative error code otherwise.
1369 */
1370int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1371 unsigned int offset_in_page, const void *buf,
1372 unsigned int len)
1373{
1374 struct mtd_info *mtd = nand_to_mtd(chip);
1375
1376 if (len && !buf)
1377 return -EINVAL;
1378
1379 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1380 return -EINVAL;
1381
Miquel Raynal8878b122017-11-09 14:16:45 +01001382 if (chip->exec_op)
1383 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1384 len, false);
1385
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001386 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001387
1388 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001389 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001390
1391 return 0;
1392}
1393EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1394
1395/**
1396 * nand_prog_page_end_op - ends a PROG PAGE operation
1397 * @chip: The NAND chip
1398 *
1399 * This function issues the second half of a PROG PAGE operation.
1400 * This function does not select/unselect the CS line.
1401 *
1402 * Returns 0 on success, a negative error code otherwise.
1403 */
1404int nand_prog_page_end_op(struct nand_chip *chip)
1405{
Miquel Raynal8878b122017-11-09 14:16:45 +01001406 int ret;
1407 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001408
Miquel Raynal8878b122017-11-09 14:16:45 +01001409 if (chip->exec_op) {
1410 const struct nand_sdr_timings *sdr =
1411 nand_get_sdr_timings(&chip->data_interface);
1412 struct nand_op_instr instrs[] = {
1413 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1414 PSEC_TO_NSEC(sdr->tWB_max)),
1415 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1416 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001417 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001418
Miquel Raynal8878b122017-11-09 14:16:45 +01001419 ret = nand_exec_op(chip, &op);
1420 if (ret)
1421 return ret;
1422
1423 ret = nand_status_op(chip, &status);
1424 if (ret)
1425 return ret;
1426 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001427 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001428 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001429 if (ret < 0)
1430 return ret;
1431
1432 status = ret;
1433 }
1434
Boris Brezillon97d90da2017-11-30 18:01:29 +01001435 if (status & NAND_STATUS_FAIL)
1436 return -EIO;
1437
1438 return 0;
1439}
1440EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1441
1442/**
1443 * nand_prog_page_op - Do a full PROG PAGE operation
1444 * @chip: The NAND chip
1445 * @page: page to write
1446 * @offset_in_page: offset within the page
1447 * @buf: buffer containing the data to write to the page
1448 * @len: length of the buffer
1449 *
1450 * This function issues a full PROG PAGE operation.
1451 * This function does not select/unselect the CS line.
1452 *
1453 * Returns 0 on success, a negative error code otherwise.
1454 */
1455int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1456 unsigned int offset_in_page, const void *buf,
1457 unsigned int len)
1458{
1459 struct mtd_info *mtd = nand_to_mtd(chip);
1460 int status;
1461
1462 if (!len || !buf)
1463 return -EINVAL;
1464
1465 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1466 return -EINVAL;
1467
Miquel Raynal8878b122017-11-09 14:16:45 +01001468 if (chip->exec_op) {
1469 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1470 len, true);
1471 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001472 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1473 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001474 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001475 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001476 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001477 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001478
Boris Brezillon97d90da2017-11-30 18:01:29 +01001479 if (status & NAND_STATUS_FAIL)
1480 return -EIO;
1481
1482 return 0;
1483}
1484EXPORT_SYMBOL_GPL(nand_prog_page_op);
1485
1486/**
1487 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1488 * @chip: The NAND chip
1489 * @offset_in_page: offset within the page
1490 * @buf: buffer containing the data to send to the NAND
1491 * @len: length of the buffer
1492 * @force_8bit: force 8-bit bus access
1493 *
1494 * This function issues a CHANGE WRITE COLUMN operation.
1495 * This function does not select/unselect the CS line.
1496 *
1497 * Returns 0 on success, a negative error code otherwise.
1498 */
1499int nand_change_write_column_op(struct nand_chip *chip,
1500 unsigned int offset_in_page,
1501 const void *buf, unsigned int len,
1502 bool force_8bit)
1503{
1504 struct mtd_info *mtd = nand_to_mtd(chip);
1505
1506 if (len && !buf)
1507 return -EINVAL;
1508
1509 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1510 return -EINVAL;
1511
Miquel Raynal8878b122017-11-09 14:16:45 +01001512 /* Small page NANDs do not support column change. */
1513 if (mtd->writesize <= 512)
1514 return -ENOTSUPP;
1515
1516 if (chip->exec_op) {
1517 const struct nand_sdr_timings *sdr =
1518 nand_get_sdr_timings(&chip->data_interface);
1519 u8 addrs[2];
1520 struct nand_op_instr instrs[] = {
1521 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1522 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1523 NAND_OP_DATA_OUT(len, buf, 0),
1524 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001525 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001526 int ret;
1527
1528 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1529 if (ret < 0)
1530 return ret;
1531
1532 instrs[2].ctx.data.force_8bit = force_8bit;
1533
1534 /* Drop the DATA_OUT instruction if len is set to 0. */
1535 if (!len)
1536 op.ninstrs--;
1537
1538 return nand_exec_op(chip, &op);
1539 }
1540
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001541 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001542 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001543 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001544
1545 return 0;
1546}
1547EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1548
1549/**
1550 * nand_readid_op - Do a READID operation
1551 * @chip: The NAND chip
1552 * @addr: address cycle to pass after the READID command
1553 * @buf: buffer used to store the ID
1554 * @len: length of the buffer
1555 *
1556 * This function sends a READID command and reads back the ID returned by the
1557 * NAND.
1558 * This function does not select/unselect the CS line.
1559 *
1560 * Returns 0 on success, a negative error code otherwise.
1561 */
1562int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1563 unsigned int len)
1564{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001565 unsigned int i;
1566 u8 *id = buf;
1567
1568 if (len && !buf)
1569 return -EINVAL;
1570
Miquel Raynal8878b122017-11-09 14:16:45 +01001571 if (chip->exec_op) {
1572 const struct nand_sdr_timings *sdr =
1573 nand_get_sdr_timings(&chip->data_interface);
1574 struct nand_op_instr instrs[] = {
1575 NAND_OP_CMD(NAND_CMD_READID, 0),
1576 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1577 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1578 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001579 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001580
1581 /* Drop the DATA_IN instruction if len is set to 0. */
1582 if (!len)
1583 op.ninstrs--;
1584
1585 return nand_exec_op(chip, &op);
1586 }
1587
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001588 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001589
1590 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001591 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001592
1593 return 0;
1594}
1595EXPORT_SYMBOL_GPL(nand_readid_op);
1596
1597/**
1598 * nand_status_op - Do a STATUS operation
1599 * @chip: The NAND chip
1600 * @status: out variable to store the NAND status
1601 *
1602 * This function sends a STATUS command and reads back the status returned by
1603 * the NAND.
1604 * This function does not select/unselect the CS line.
1605 *
1606 * Returns 0 on success, a negative error code otherwise.
1607 */
1608int nand_status_op(struct nand_chip *chip, u8 *status)
1609{
Miquel Raynal8878b122017-11-09 14:16:45 +01001610 if (chip->exec_op) {
1611 const struct nand_sdr_timings *sdr =
1612 nand_get_sdr_timings(&chip->data_interface);
1613 struct nand_op_instr instrs[] = {
1614 NAND_OP_CMD(NAND_CMD_STATUS,
1615 PSEC_TO_NSEC(sdr->tADL_min)),
1616 NAND_OP_8BIT_DATA_IN(1, status, 0),
1617 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001618 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001619
1620 if (!status)
1621 op.ninstrs--;
1622
1623 return nand_exec_op(chip, &op);
1624 }
1625
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001626 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001627 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001628 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001629
1630 return 0;
1631}
1632EXPORT_SYMBOL_GPL(nand_status_op);
1633
1634/**
1635 * nand_exit_status_op - Exit a STATUS operation
1636 * @chip: The NAND chip
1637 *
1638 * This function sends a READ0 command to cancel the effect of the STATUS
1639 * command to avoid reading only the status until a new read command is sent.
1640 *
1641 * This function does not select/unselect the CS line.
1642 *
1643 * Returns 0 on success, a negative error code otherwise.
1644 */
1645int nand_exit_status_op(struct nand_chip *chip)
1646{
Miquel Raynal8878b122017-11-09 14:16:45 +01001647 if (chip->exec_op) {
1648 struct nand_op_instr instrs[] = {
1649 NAND_OP_CMD(NAND_CMD_READ0, 0),
1650 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001651 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001652
1653 return nand_exec_op(chip, &op);
1654 }
1655
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001656 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001657
1658 return 0;
1659}
Boris Brezillon97d90da2017-11-30 18:01:29 +01001660
1661/**
1662 * nand_erase_op - Do an erase operation
1663 * @chip: The NAND chip
1664 * @eraseblock: block to erase
1665 *
1666 * This function sends an ERASE command and waits for the NAND to be ready
1667 * before returning.
1668 * This function does not select/unselect the CS line.
1669 *
1670 * Returns 0 on success, a negative error code otherwise.
1671 */
1672int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1673{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001674 unsigned int page = eraseblock <<
1675 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01001676 int ret;
1677 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001678
Miquel Raynal8878b122017-11-09 14:16:45 +01001679 if (chip->exec_op) {
1680 const struct nand_sdr_timings *sdr =
1681 nand_get_sdr_timings(&chip->data_interface);
1682 u8 addrs[3] = { page, page >> 8, page >> 16 };
1683 struct nand_op_instr instrs[] = {
1684 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
1685 NAND_OP_ADDR(2, addrs, 0),
1686 NAND_OP_CMD(NAND_CMD_ERASE2,
1687 PSEC_TO_MSEC(sdr->tWB_max)),
1688 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
1689 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001690 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001691
Miquel Raynal8878b122017-11-09 14:16:45 +01001692 if (chip->options & NAND_ROW_ADDR_3)
1693 instrs[1].ctx.addr.naddrs++;
1694
1695 ret = nand_exec_op(chip, &op);
1696 if (ret)
1697 return ret;
1698
1699 ret = nand_status_op(chip, &status);
1700 if (ret)
1701 return ret;
1702 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001703 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
1704 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01001705
Boris Brezillon8395b752018-09-07 00:38:37 +02001706 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001707 if (ret < 0)
1708 return ret;
1709
1710 status = ret;
1711 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001712
1713 if (status & NAND_STATUS_FAIL)
1714 return -EIO;
1715
1716 return 0;
1717}
1718EXPORT_SYMBOL_GPL(nand_erase_op);
1719
1720/**
1721 * nand_set_features_op - Do a SET FEATURES operation
1722 * @chip: The NAND chip
1723 * @feature: feature id
1724 * @data: 4 bytes of data
1725 *
1726 * This function sends a SET FEATURES command and waits for the NAND to be
1727 * ready before returning.
1728 * This function does not select/unselect the CS line.
1729 *
1730 * Returns 0 on success, a negative error code otherwise.
1731 */
1732static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1733 const void *data)
1734{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001735 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01001736 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001737
Miquel Raynal8878b122017-11-09 14:16:45 +01001738 if (chip->exec_op) {
1739 const struct nand_sdr_timings *sdr =
1740 nand_get_sdr_timings(&chip->data_interface);
1741 struct nand_op_instr instrs[] = {
1742 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
1743 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
1744 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
1745 PSEC_TO_NSEC(sdr->tWB_max)),
1746 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
1747 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001748 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001749
Boris Brezillon782d1962018-05-11 14:44:07 +02001750 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01001751 }
1752
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001753 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02001754 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001755 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02001756
Boris Brezillon8395b752018-09-07 00:38:37 +02001757 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02001758 if (ret < 0)
1759 return ret;
1760
1761 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001762 return -EIO;
1763
1764 return 0;
1765}
1766
1767/**
1768 * nand_get_features_op - Do a GET FEATURES operation
1769 * @chip: The NAND chip
1770 * @feature: feature id
1771 * @data: 4 bytes of data
1772 *
1773 * This function sends a GET FEATURES command and waits for the NAND to be
1774 * ready before returning.
1775 * This function does not select/unselect the CS line.
1776 *
1777 * Returns 0 on success, a negative error code otherwise.
1778 */
1779static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1780 void *data)
1781{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001782 u8 *params = data;
1783 int i;
1784
Miquel Raynal8878b122017-11-09 14:16:45 +01001785 if (chip->exec_op) {
1786 const struct nand_sdr_timings *sdr =
1787 nand_get_sdr_timings(&chip->data_interface);
1788 struct nand_op_instr instrs[] = {
1789 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
1790 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
1791 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
1792 PSEC_TO_NSEC(sdr->tRR_min)),
1793 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
1794 data, 0),
1795 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001796 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001797
1798 return nand_exec_op(chip, &op);
1799 }
1800
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001801 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001802 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001803 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001804
1805 return 0;
1806}
1807
Boris Brezillon52f05b62018-07-27 09:44:18 +02001808static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
1809 unsigned int delay_ns)
1810{
1811 if (chip->exec_op) {
1812 struct nand_op_instr instrs[] = {
1813 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
1814 PSEC_TO_NSEC(delay_ns)),
1815 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001816 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001817
1818 return nand_exec_op(chip, &op);
1819 }
1820
1821 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02001822 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001823 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001824 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001825 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001826
1827 return 0;
1828}
1829
Boris Brezillon97d90da2017-11-30 18:01:29 +01001830/**
1831 * nand_reset_op - Do a reset operation
1832 * @chip: The NAND chip
1833 *
1834 * This function sends a RESET command and waits for the NAND to be ready
1835 * before returning.
1836 * This function does not select/unselect the CS line.
1837 *
1838 * Returns 0 on success, a negative error code otherwise.
1839 */
1840int nand_reset_op(struct nand_chip *chip)
1841{
Miquel Raynal8878b122017-11-09 14:16:45 +01001842 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_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
1847 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
1848 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001849 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001850
1851 return nand_exec_op(chip, &op);
1852 }
1853
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001854 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001855
1856 return 0;
1857}
1858EXPORT_SYMBOL_GPL(nand_reset_op);
1859
1860/**
1861 * nand_read_data_op - Read data from the NAND
1862 * @chip: The NAND chip
1863 * @buf: buffer used to store the data
1864 * @len: length of the buffer
1865 * @force_8bit: force 8-bit bus access
1866 *
1867 * This function does a raw data read on the bus. Usually used after launching
1868 * another NAND operation like nand_read_page_op().
1869 * This function does not select/unselect the CS line.
1870 *
1871 * Returns 0 on success, a negative error code otherwise.
1872 */
1873int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1874 bool force_8bit)
1875{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001876 if (!len || !buf)
1877 return -EINVAL;
1878
Miquel Raynal8878b122017-11-09 14:16:45 +01001879 if (chip->exec_op) {
1880 struct nand_op_instr instrs[] = {
1881 NAND_OP_DATA_IN(len, buf, 0),
1882 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001883 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001884
1885 instrs[0].ctx.data.force_8bit = force_8bit;
1886
1887 return nand_exec_op(chip, &op);
1888 }
1889
Boris Brezillon97d90da2017-11-30 18:01:29 +01001890 if (force_8bit) {
1891 u8 *p = buf;
1892 unsigned int i;
1893
1894 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001895 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001896 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001897 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001898 }
1899
1900 return 0;
1901}
1902EXPORT_SYMBOL_GPL(nand_read_data_op);
1903
1904/**
1905 * nand_write_data_op - Write data from the NAND
1906 * @chip: The NAND chip
1907 * @buf: buffer containing the data to send on the bus
1908 * @len: length of the buffer
1909 * @force_8bit: force 8-bit bus access
1910 *
1911 * This function does a raw data write on the bus. Usually used after launching
1912 * another NAND operation like nand_write_page_begin_op().
1913 * This function does not select/unselect the CS line.
1914 *
1915 * Returns 0 on success, a negative error code otherwise.
1916 */
1917int nand_write_data_op(struct nand_chip *chip, const void *buf,
1918 unsigned int len, bool force_8bit)
1919{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001920 if (!len || !buf)
1921 return -EINVAL;
1922
Miquel Raynal8878b122017-11-09 14:16:45 +01001923 if (chip->exec_op) {
1924 struct nand_op_instr instrs[] = {
1925 NAND_OP_DATA_OUT(len, buf, 0),
1926 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001927 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001928
1929 instrs[0].ctx.data.force_8bit = force_8bit;
1930
1931 return nand_exec_op(chip, &op);
1932 }
1933
Boris Brezillon97d90da2017-11-30 18:01:29 +01001934 if (force_8bit) {
1935 const u8 *p = buf;
1936 unsigned int i;
1937
1938 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001939 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001940 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001941 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001942 }
1943
1944 return 0;
1945}
1946EXPORT_SYMBOL_GPL(nand_write_data_op);
1947
1948/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001949 * struct nand_op_parser_ctx - Context used by the parser
1950 * @instrs: array of all the instructions that must be addressed
1951 * @ninstrs: length of the @instrs array
1952 * @subop: Sub-operation to be passed to the NAND controller
1953 *
1954 * This structure is used by the core to split NAND operations into
1955 * sub-operations that can be handled by the NAND controller.
1956 */
1957struct nand_op_parser_ctx {
1958 const struct nand_op_instr *instrs;
1959 unsigned int ninstrs;
1960 struct nand_subop subop;
1961};
1962
1963/**
1964 * nand_op_parser_must_split_instr - Checks if an instruction must be split
1965 * @pat: the parser pattern element that matches @instr
1966 * @instr: pointer to the instruction to check
1967 * @start_offset: this is an in/out parameter. If @instr has already been
1968 * split, then @start_offset is the offset from which to start
1969 * (either an address cycle or an offset in the data buffer).
1970 * Conversely, if the function returns true (ie. instr must be
1971 * split), this parameter is updated to point to the first
1972 * data/address cycle that has not been taken care of.
1973 *
1974 * Some NAND controllers are limited and cannot send X address cycles with a
1975 * unique operation, or cannot read/write more than Y bytes at the same time.
1976 * In this case, split the instruction that does not fit in a single
1977 * controller-operation into two or more chunks.
1978 *
1979 * Returns true if the instruction must be split, false otherwise.
1980 * The @start_offset parameter is also updated to the offset at which the next
1981 * bundle of instruction must start (if an address or a data instruction).
1982 */
1983static bool
1984nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
1985 const struct nand_op_instr *instr,
1986 unsigned int *start_offset)
1987{
1988 switch (pat->type) {
1989 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001990 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01001991 break;
1992
1993 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001994 pat->ctx.addr.maxcycles) {
1995 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01001996 return true;
1997 }
1998 break;
1999
2000 case NAND_OP_DATA_IN_INSTR:
2001 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002002 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002003 break;
2004
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002005 if (instr->ctx.data.len - *start_offset >
2006 pat->ctx.data.maxlen) {
2007 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002008 return true;
2009 }
2010 break;
2011
2012 default:
2013 break;
2014 }
2015
2016 return false;
2017}
2018
2019/**
2020 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2021 * remaining in the parser context
2022 * @pat: the pattern to test
2023 * @ctx: the parser context structure to match with the pattern @pat
2024 *
2025 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2026 * Returns true if this is the case, false ortherwise. When true is returned,
2027 * @ctx->subop is updated with the set of instructions to be passed to the
2028 * controller driver.
2029 */
2030static bool
2031nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2032 struct nand_op_parser_ctx *ctx)
2033{
2034 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2035 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2036 const struct nand_op_instr *instr = ctx->subop.instrs;
2037 unsigned int i, ninstrs;
2038
2039 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2040 /*
2041 * The pattern instruction does not match the operation
2042 * instruction. If the instruction is marked optional in the
2043 * pattern definition, we skip the pattern element and continue
2044 * to the next one. If the element is mandatory, there's no
2045 * match and we can return false directly.
2046 */
2047 if (instr->type != pat->elems[i].type) {
2048 if (!pat->elems[i].optional)
2049 return false;
2050
2051 continue;
2052 }
2053
2054 /*
2055 * Now check the pattern element constraints. If the pattern is
2056 * not able to handle the whole instruction in a single step,
2057 * we have to split it.
2058 * The last_instr_end_off value comes back updated to point to
2059 * the position where we have to split the instruction (the
2060 * start of the next subop chunk).
2061 */
2062 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2063 &instr_offset)) {
2064 ninstrs++;
2065 i++;
2066 break;
2067 }
2068
2069 instr++;
2070 ninstrs++;
2071 instr_offset = 0;
2072 }
2073
2074 /*
2075 * This can happen if all instructions of a pattern are optional.
2076 * Still, if there's not at least one instruction handled by this
2077 * pattern, this is not a match, and we should try the next one (if
2078 * any).
2079 */
2080 if (!ninstrs)
2081 return false;
2082
2083 /*
2084 * We had a match on the pattern head, but the pattern may be longer
2085 * than the instructions we're asked to execute. We need to make sure
2086 * there's no mandatory elements in the pattern tail.
2087 */
2088 for (; i < pat->nelems; i++) {
2089 if (!pat->elems[i].optional)
2090 return false;
2091 }
2092
2093 /*
2094 * We have a match: update the subop structure accordingly and return
2095 * true.
2096 */
2097 ctx->subop.ninstrs = ninstrs;
2098 ctx->subop.last_instr_end_off = instr_offset;
2099
2100 return true;
2101}
2102
2103#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2104static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2105{
2106 const struct nand_op_instr *instr;
2107 char *prefix = " ";
2108 unsigned int i;
2109
2110 pr_debug("executing subop:\n");
2111
2112 for (i = 0; i < ctx->ninstrs; i++) {
2113 instr = &ctx->instrs[i];
2114
2115 if (instr == &ctx->subop.instrs[0])
2116 prefix = " ->";
2117
2118 switch (instr->type) {
2119 case NAND_OP_CMD_INSTR:
2120 pr_debug("%sCMD [0x%02x]\n", prefix,
2121 instr->ctx.cmd.opcode);
2122 break;
2123 case NAND_OP_ADDR_INSTR:
2124 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2125 instr->ctx.addr.naddrs,
2126 instr->ctx.addr.naddrs < 64 ?
2127 instr->ctx.addr.naddrs : 64,
2128 instr->ctx.addr.addrs);
2129 break;
2130 case NAND_OP_DATA_IN_INSTR:
2131 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2132 instr->ctx.data.len,
2133 instr->ctx.data.force_8bit ?
2134 ", force 8-bit" : "");
2135 break;
2136 case NAND_OP_DATA_OUT_INSTR:
2137 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2138 instr->ctx.data.len,
2139 instr->ctx.data.force_8bit ?
2140 ", force 8-bit" : "");
2141 break;
2142 case NAND_OP_WAITRDY_INSTR:
2143 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2144 instr->ctx.waitrdy.timeout_ms);
2145 break;
2146 }
2147
2148 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2149 prefix = " ";
2150 }
2151}
2152#else
2153static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2154{
2155 /* NOP */
2156}
2157#endif
2158
2159/**
2160 * nand_op_parser_exec_op - exec_op parser
2161 * @chip: the NAND chip
2162 * @parser: patterns description provided by the controller driver
2163 * @op: the NAND operation to address
2164 * @check_only: when true, the function only checks if @op can be handled but
2165 * does not execute the operation
2166 *
2167 * Helper function designed to ease integration of NAND controller drivers that
2168 * only support a limited set of instruction sequences. The supported sequences
2169 * are described in @parser, and the framework takes care of splitting @op into
2170 * multiple sub-operations (if required) and pass them back to the ->exec()
2171 * callback of the matching pattern if @check_only is set to false.
2172 *
2173 * NAND controller drivers should call this function from their own ->exec_op()
2174 * implementation.
2175 *
2176 * Returns 0 on success, a negative error code otherwise. A failure can be
2177 * caused by an unsupported operation (none of the supported patterns is able
2178 * to handle the requested operation), or an error returned by one of the
2179 * matching pattern->exec() hook.
2180 */
2181int nand_op_parser_exec_op(struct nand_chip *chip,
2182 const struct nand_op_parser *parser,
2183 const struct nand_operation *op, bool check_only)
2184{
2185 struct nand_op_parser_ctx ctx = {
2186 .subop.instrs = op->instrs,
2187 .instrs = op->instrs,
2188 .ninstrs = op->ninstrs,
2189 };
2190 unsigned int i;
2191
2192 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2193 int ret;
2194
2195 for (i = 0; i < parser->npatterns; i++) {
2196 const struct nand_op_parser_pattern *pattern;
2197
2198 pattern = &parser->patterns[i];
2199 if (!nand_op_parser_match_pat(pattern, &ctx))
2200 continue;
2201
2202 nand_op_parser_trace(&ctx);
2203
2204 if (check_only)
2205 break;
2206
2207 ret = pattern->exec(chip, &ctx.subop);
2208 if (ret)
2209 return ret;
2210
2211 break;
2212 }
2213
2214 if (i == parser->npatterns) {
2215 pr_debug("->exec_op() parser: pattern not found!\n");
2216 return -ENOTSUPP;
2217 }
2218
2219 /*
2220 * Update the context structure by pointing to the start of the
2221 * next subop.
2222 */
2223 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2224 if (ctx.subop.last_instr_end_off)
2225 ctx.subop.instrs -= 1;
2226
2227 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2228 }
2229
2230 return 0;
2231}
2232EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2233
2234static bool nand_instr_is_data(const struct nand_op_instr *instr)
2235{
2236 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2237 instr->type == NAND_OP_DATA_OUT_INSTR);
2238}
2239
2240static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2241 unsigned int instr_idx)
2242{
2243 return subop && instr_idx < subop->ninstrs;
2244}
2245
Miquel Raynal760c4352018-07-19 00:09:12 +02002246static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2247 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002248{
2249 if (instr_idx)
2250 return 0;
2251
2252 return subop->first_instr_start_off;
2253}
2254
2255/**
2256 * nand_subop_get_addr_start_off - Get the start offset in an address array
2257 * @subop: The entire sub-operation
2258 * @instr_idx: Index of the instruction inside the sub-operation
2259 *
2260 * During driver development, one could be tempted to directly use the
2261 * ->addr.addrs field of address instructions. This is wrong as address
2262 * instructions might be split.
2263 *
2264 * Given an address instruction, returns the offset of the first cycle to issue.
2265 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002266unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2267 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002268{
Miquel Raynal760c4352018-07-19 00:09:12 +02002269 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2270 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2271 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002272
2273 return nand_subop_get_start_off(subop, instr_idx);
2274}
2275EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2276
2277/**
2278 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2279 * @subop: The entire sub-operation
2280 * @instr_idx: Index of the instruction inside the sub-operation
2281 *
2282 * During driver development, one could be tempted to directly use the
2283 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2284 * might be split.
2285 *
2286 * Given an address instruction, returns the number of address cycle to issue.
2287 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002288unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2289 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002290{
2291 int start_off, end_off;
2292
Miquel Raynal760c4352018-07-19 00:09:12 +02002293 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2294 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2295 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002296
2297 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2298
2299 if (instr_idx == subop->ninstrs - 1 &&
2300 subop->last_instr_end_off)
2301 end_off = subop->last_instr_end_off;
2302 else
2303 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2304
2305 return end_off - start_off;
2306}
2307EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2308
2309/**
2310 * nand_subop_get_data_start_off - Get the start offset in a data array
2311 * @subop: The entire sub-operation
2312 * @instr_idx: Index of the instruction inside the sub-operation
2313 *
2314 * During driver development, one could be tempted to directly use the
2315 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2316 * instructions might be split.
2317 *
2318 * Given a data instruction, returns the offset to start from.
2319 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002320unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2321 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002322{
Miquel Raynal760c4352018-07-19 00:09:12 +02002323 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2324 !nand_instr_is_data(&subop->instrs[instr_idx])))
2325 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002326
2327 return nand_subop_get_start_off(subop, instr_idx);
2328}
2329EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2330
2331/**
2332 * nand_subop_get_data_len - Get the number of bytes to retrieve
2333 * @subop: The entire sub-operation
2334 * @instr_idx: Index of the instruction inside the sub-operation
2335 *
2336 * During driver development, one could be tempted to directly use the
2337 * ->data->len field of a data instruction. This is wrong as data instructions
2338 * might be split.
2339 *
2340 * Returns the length of the chunk of data to send/receive.
2341 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002342unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2343 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002344{
2345 int start_off = 0, end_off;
2346
Miquel Raynal760c4352018-07-19 00:09:12 +02002347 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2348 !nand_instr_is_data(&subop->instrs[instr_idx])))
2349 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002350
2351 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2352
2353 if (instr_idx == subop->ninstrs - 1 &&
2354 subop->last_instr_end_off)
2355 end_off = subop->last_instr_end_off;
2356 else
2357 end_off = subop->instrs[instr_idx].ctx.data.len;
2358
2359 return end_off - start_off;
2360}
2361EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002364 * nand_reset - Reset and initialize a NAND device
2365 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002366 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002367 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002368 * Save the timings data structure, then apply SDR timings mode 0 (see
2369 * nand_reset_data_interface for details), do the reset operation, and
2370 * apply back the previous timings.
2371 *
2372 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002373 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002374int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002375{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002376 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002377 int ret;
2378
Boris Brezillon104e4422017-03-16 09:35:58 +01002379 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002380 if (ret)
2381 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002382
Boris Brezillon73f907f2016-10-24 16:46:20 +02002383 /*
2384 * The CS line has to be released before we can apply the new NAND
Boris Brezillon1d017852018-11-11 08:55:14 +01002385 * interface settings, hence this weird nand_select_target()
2386 * nand_deselect_target() dance.
Boris Brezillon73f907f2016-10-24 16:46:20 +02002387 */
Boris Brezillon1d017852018-11-11 08:55:14 +01002388 nand_select_target(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002389 ret = nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01002390 nand_deselect_target(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002391 if (ret)
2392 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002393
Miquel Raynal107b7d62018-03-19 14:47:25 +01002394 /*
2395 * A nand_reset_data_interface() put both the NAND chip and the NAND
2396 * controller in timings mode 0. If the default mode for this chip is
2397 * also 0, no need to proceed to the change again. Plus, at probe time,
2398 * nand_setup_data_interface() uses ->set/get_features() which would
2399 * fail anyway as the parameter page is not available yet.
2400 */
2401 if (!chip->onfi_timing_mode_default)
2402 return 0;
2403
Miquel Raynal17fa8042017-11-30 18:01:31 +01002404 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002405 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002406 if (ret)
2407 return ret;
2408
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002409 return 0;
2410}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002411EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002412
2413/**
Boris Brezillon45240362018-09-07 00:38:40 +02002414 * nand_get_features - wrapper to perform a GET_FEATURE
2415 * @chip: NAND chip info structure
2416 * @addr: feature address
2417 * @subfeature_param: the subfeature parameters, a four bytes array
2418 *
2419 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2420 * operation cannot be handled.
2421 */
2422int nand_get_features(struct nand_chip *chip, int addr,
2423 u8 *subfeature_param)
2424{
2425 if (!nand_supports_get_features(chip, addr))
2426 return -ENOTSUPP;
2427
2428 if (chip->legacy.get_features)
2429 return chip->legacy.get_features(chip, addr, subfeature_param);
2430
2431 return nand_get_features_op(chip, addr, subfeature_param);
2432}
Boris Brezillon45240362018-09-07 00:38:40 +02002433
2434/**
2435 * nand_set_features - wrapper to perform a SET_FEATURE
2436 * @chip: NAND chip info structure
2437 * @addr: feature address
2438 * @subfeature_param: the subfeature parameters, a four bytes array
2439 *
2440 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2441 * operation cannot be handled.
2442 */
2443int nand_set_features(struct nand_chip *chip, int addr,
2444 u8 *subfeature_param)
2445{
2446 if (!nand_supports_set_features(chip, addr))
2447 return -ENOTSUPP;
2448
2449 if (chip->legacy.set_features)
2450 return chip->legacy.set_features(chip, addr, subfeature_param);
2451
2452 return nand_set_features_op(chip, addr, subfeature_param);
2453}
Boris Brezillon45240362018-09-07 00:38:40 +02002454
2455/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002456 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2457 * @buf: buffer to test
2458 * @len: buffer length
2459 * @bitflips_threshold: maximum number of bitflips
2460 *
2461 * Check if a buffer contains only 0xff, which means the underlying region
2462 * has been erased and is ready to be programmed.
2463 * The bitflips_threshold specify the maximum number of bitflips before
2464 * considering the region is not erased.
2465 * Note: The logic of this function has been extracted from the memweight
2466 * implementation, except that nand_check_erased_buf function exit before
2467 * testing the whole buffer if the number of bitflips exceed the
2468 * bitflips_threshold value.
2469 *
2470 * Returns a positive number of bitflips less than or equal to
2471 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2472 * threshold.
2473 */
2474static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2475{
2476 const unsigned char *bitmap = buf;
2477 int bitflips = 0;
2478 int weight;
2479
2480 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2481 len--, bitmap++) {
2482 weight = hweight8(*bitmap);
2483 bitflips += BITS_PER_BYTE - weight;
2484 if (unlikely(bitflips > bitflips_threshold))
2485 return -EBADMSG;
2486 }
2487
2488 for (; len >= sizeof(long);
2489 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002490 unsigned long d = *((unsigned long *)bitmap);
2491 if (d == ~0UL)
2492 continue;
2493 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002494 bitflips += BITS_PER_LONG - weight;
2495 if (unlikely(bitflips > bitflips_threshold))
2496 return -EBADMSG;
2497 }
2498
2499 for (; len > 0; len--, bitmap++) {
2500 weight = hweight8(*bitmap);
2501 bitflips += BITS_PER_BYTE - weight;
2502 if (unlikely(bitflips > bitflips_threshold))
2503 return -EBADMSG;
2504 }
2505
2506 return bitflips;
2507}
2508
2509/**
2510 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2511 * 0xff data
2512 * @data: data buffer to test
2513 * @datalen: data length
2514 * @ecc: ECC buffer
2515 * @ecclen: ECC length
2516 * @extraoob: extra OOB buffer
2517 * @extraooblen: extra OOB length
2518 * @bitflips_threshold: maximum number of bitflips
2519 *
2520 * Check if a data buffer and its associated ECC and OOB data contains only
2521 * 0xff pattern, which means the underlying region has been erased and is
2522 * ready to be programmed.
2523 * The bitflips_threshold specify the maximum number of bitflips before
2524 * considering the region as not erased.
2525 *
2526 * Note:
2527 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2528 * different from the NAND page size. When fixing bitflips, ECC engines will
2529 * report the number of errors per chunk, and the NAND core infrastructure
2530 * expect you to return the maximum number of bitflips for the whole page.
2531 * This is why you should always use this function on a single chunk and
2532 * not on the whole page. After checking each chunk you should update your
2533 * max_bitflips value accordingly.
2534 * 2/ When checking for bitflips in erased pages you should not only check
2535 * the payload data but also their associated ECC data, because a user might
2536 * have programmed almost all bits to 1 but a few. In this case, we
2537 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2538 * this case.
2539 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2540 * data are protected by the ECC engine.
2541 * It could also be used if you support subpages and want to attach some
2542 * extra OOB data to an ECC chunk.
2543 *
2544 * Returns a positive number of bitflips less than or equal to
2545 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2546 * threshold. In case of success, the passed buffers are filled with 0xff.
2547 */
2548int nand_check_erased_ecc_chunk(void *data, int datalen,
2549 void *ecc, int ecclen,
2550 void *extraoob, int extraooblen,
2551 int bitflips_threshold)
2552{
2553 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2554
2555 data_bitflips = nand_check_erased_buf(data, datalen,
2556 bitflips_threshold);
2557 if (data_bitflips < 0)
2558 return data_bitflips;
2559
2560 bitflips_threshold -= data_bitflips;
2561
2562 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2563 if (ecc_bitflips < 0)
2564 return ecc_bitflips;
2565
2566 bitflips_threshold -= ecc_bitflips;
2567
2568 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2569 bitflips_threshold);
2570 if (extraoob_bitflips < 0)
2571 return extraoob_bitflips;
2572
2573 if (data_bitflips)
2574 memset(data, 0xff, datalen);
2575
2576 if (ecc_bitflips)
2577 memset(ecc, 0xff, ecclen);
2578
2579 if (extraoob_bitflips)
2580 memset(extraoob, 0xff, extraooblen);
2581
2582 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2583}
2584EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2585
2586/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002587 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002588 * @chip: nand chip info structure
2589 * @buf: buffer to store read data
2590 * @oob_required: caller requires OOB data read to chip->oob_poi
2591 * @page: page number to read
2592 *
2593 * Returns -ENOTSUPP unconditionally.
2594 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002595int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2596 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002597{
2598 return -ENOTSUPP;
2599}
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002600
2601/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002602 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002603 * @chip: nand chip info structure
2604 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002605 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002606 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002607 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002608 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002609 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002610int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2611 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002612{
Boris Brezillonb9761682018-09-06 14:05:20 +02002613 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002614 int ret;
2615
Boris Brezillon25f815f2017-11-30 18:01:30 +01002616 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002617 if (ret)
2618 return ret;
2619
2620 if (oob_required) {
2621 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2622 false);
2623 if (ret)
2624 return ret;
2625 }
2626
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002627 return 0;
2628}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002629EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002630
2631/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002632 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002633 * @chip: nand chip info structure
2634 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002635 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002636 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002637 *
2638 * We need a special oob layout and handling even when OOB isn't used.
2639 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002640static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07002641 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002642{
Boris Brezillonb9761682018-09-06 14:05:20 +02002643 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08002644 int eccsize = chip->ecc.size;
2645 int eccbytes = chip->ecc.bytes;
2646 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002647 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002648
Boris Brezillon25f815f2017-11-30 18:01:30 +01002649 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2650 if (ret)
2651 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002652
2653 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002654 ret = nand_read_data_op(chip, buf, eccsize, false);
2655 if (ret)
2656 return ret;
2657
David Brownell52ff49d2009-03-04 12:01:36 -08002658 buf += eccsize;
2659
2660 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002661 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2662 false);
2663 if (ret)
2664 return ret;
2665
David Brownell52ff49d2009-03-04 12:01:36 -08002666 oob += chip->ecc.prepad;
2667 }
2668
Boris Brezillon97d90da2017-11-30 18:01:29 +01002669 ret = nand_read_data_op(chip, oob, eccbytes, false);
2670 if (ret)
2671 return ret;
2672
David Brownell52ff49d2009-03-04 12:01:36 -08002673 oob += eccbytes;
2674
2675 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002676 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2677 false);
2678 if (ret)
2679 return ret;
2680
David Brownell52ff49d2009-03-04 12:01:36 -08002681 oob += chip->ecc.postpad;
2682 }
2683 }
2684
2685 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002686 if (size) {
2687 ret = nand_read_data_op(chip, oob, size, false);
2688 if (ret)
2689 return ret;
2690 }
David Brownell52ff49d2009-03-04 12:01:36 -08002691
2692 return 0;
2693}
2694
2695/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002696 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002697 * @chip: nand chip info structure
2698 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002699 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002700 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002701 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002702static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
2703 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Boris Brezillonb9761682018-09-06 14:05:20 +02002705 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002706 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002707 int eccbytes = chip->ecc.bytes;
2708 int eccsteps = chip->ecc.steps;
2709 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002710 uint8_t *ecc_calc = chip->ecc.calc_buf;
2711 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002712 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002713
Boris Brezillonb9761682018-09-06 14:05:20 +02002714 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002715
2716 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002717 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002718
Boris Brezillon846031d2016-02-03 20:11:00 +01002719 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2720 chip->ecc.total);
2721 if (ret)
2722 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002723
2724 eccsteps = chip->ecc.steps;
2725 p = buf;
2726
2727 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2728 int stat;
2729
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002730 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07002731 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002732 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002733 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002734 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002735 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2736 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002737 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002738 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01002739}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302742 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002743 * @chip: nand chip info structure
2744 * @data_offs: offset of requested data within the page
2745 * @readlen: data length
2746 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08002747 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01002748 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002749static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
2750 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01002751{
Boris Brezillonb9761682018-09-06 14:05:20 +02002752 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002753 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002754 uint8_t *p;
2755 int data_col_addr, i, gaps = 0;
2756 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
2757 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01002758 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07002759 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01002760 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01002761
Brian Norris7854d3f2011-06-23 14:12:08 -07002762 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01002763 start_step = data_offs / chip->ecc.size;
2764 end_step = (data_offs + readlen - 1) / chip->ecc.size;
2765 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10302766 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01002767
Brian Norris8b6e50c2011-05-25 14:59:01 -07002768 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01002769 datafrag_len = num_steps * chip->ecc.size;
2770 eccfrag_len = num_steps * chip->ecc.bytes;
2771
2772 data_col_addr = start_step * chip->ecc.size;
2773 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01002774 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01002775 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002776 if (ret)
2777 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002778
Brian Norris8b6e50c2011-05-25 14:59:01 -07002779 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01002780 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002781 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01002782
Brian Norris8b6e50c2011-05-25 14:59:01 -07002783 /*
2784 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07002785 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07002786 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002787 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
2788 if (ret)
2789 return ret;
2790
2791 if (oobregion.length < eccfrag_len)
2792 gaps = 1;
2793
Alexey Korolev3d459552008-05-15 17:23:18 +01002794 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002795 ret = nand_change_read_column_op(chip, mtd->writesize,
2796 chip->oob_poi, mtd->oobsize,
2797 false);
2798 if (ret)
2799 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002800 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002801 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002802 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07002803 * about buswidth alignment in read_buf.
2804 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002805 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01002806 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01002807 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002808 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01002809 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
2810 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002811 aligned_len++;
2812
Boris Brezillon97d90da2017-11-30 18:01:29 +01002813 ret = nand_change_read_column_op(chip,
2814 mtd->writesize + aligned_pos,
2815 &chip->oob_poi[aligned_pos],
2816 aligned_len, false);
2817 if (ret)
2818 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002819 }
2820
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002821 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01002822 chip->oob_poi, index, eccfrag_len);
2823 if (ret)
2824 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002825
2826 p = bufpoi + data_col_addr;
2827 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
2828 int stat;
2829
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002830 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002831 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002832 if (stat == -EBADMSG &&
2833 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2834 /* check for empty pages with bitflips */
2835 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002836 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002837 chip->ecc.bytes,
2838 NULL, 0,
2839 chip->ecc.strength);
2840 }
2841
Mike Dunn3f91e942012-04-25 12:06:09 -07002842 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002843 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002844 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01002845 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002846 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2847 }
Alexey Korolev3d459552008-05-15 17:23:18 +01002848 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002849 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01002850}
2851
2852/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002853 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002854 * @chip: nand chip info structure
2855 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002856 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002857 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002858 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002859 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002860 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002861static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
2862 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002863{
Boris Brezillonb9761682018-09-06 14:05:20 +02002864 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002865 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002866 int eccbytes = chip->ecc.bytes;
2867 int eccsteps = chip->ecc.steps;
2868 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002869 uint8_t *ecc_calc = chip->ecc.calc_buf;
2870 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002871 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002872
Boris Brezillon25f815f2017-11-30 18:01:30 +01002873 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2874 if (ret)
2875 return ret;
2876
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002877 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02002878 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002879
2880 ret = nand_read_data_op(chip, p, eccsize, false);
2881 if (ret)
2882 return ret;
2883
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002884 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002885 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002886
2887 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2888 if (ret)
2889 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002890
Boris Brezillon846031d2016-02-03 20:11:00 +01002891 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2892 chip->ecc.total);
2893 if (ret)
2894 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002895
2896 eccsteps = chip->ecc.steps;
2897 p = buf;
2898
2899 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2900 int stat;
2901
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002902 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002903 if (stat == -EBADMSG &&
2904 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2905 /* check for empty pages with bitflips */
2906 stat = nand_check_erased_ecc_chunk(p, eccsize,
2907 &ecc_code[i], eccbytes,
2908 NULL, 0,
2909 chip->ecc.strength);
2910 }
2911
Mike Dunn3f91e942012-04-25 12:06:09 -07002912 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002913 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002914 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002915 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002916 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2917 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002918 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002919 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002920}
2921
2922/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002923 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07002924 * @chip: nand chip info structure
2925 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002926 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002927 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002928 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002929 * Hardware ECC for large page chips, require OOB to be read first. For this
2930 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2931 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2932 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2933 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002934 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002935static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
2936 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002937{
Boris Brezillonb9761682018-09-06 14:05:20 +02002938 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002939 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002940 int eccbytes = chip->ecc.bytes;
2941 int eccsteps = chip->ecc.steps;
2942 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002943 uint8_t *ecc_code = chip->ecc.code_buf;
2944 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002945 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002946
2947 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01002948 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2949 if (ret)
2950 return ret;
2951
2952 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2953 if (ret)
2954 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002955
Boris Brezillon846031d2016-02-03 20:11:00 +01002956 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2957 chip->ecc.total);
2958 if (ret)
2959 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002960
2961 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2962 int stat;
2963
Boris Brezillonec476362018-09-06 14:05:17 +02002964 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002965
2966 ret = nand_read_data_op(chip, p, eccsize, false);
2967 if (ret)
2968 return ret;
2969
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002970 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002971
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002972 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002973 if (stat == -EBADMSG &&
2974 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2975 /* check for empty pages with bitflips */
2976 stat = nand_check_erased_ecc_chunk(p, eccsize,
2977 &ecc_code[i], eccbytes,
2978 NULL, 0,
2979 chip->ecc.strength);
2980 }
2981
Mike Dunn3f91e942012-04-25 12:06:09 -07002982 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002983 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002984 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002985 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002986 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2987 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002988 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002989 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002990}
2991
2992/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002993 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07002994 * @chip: nand chip info structure
2995 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002996 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002997 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002998 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002999 * The hw generator calculates the error syndrome automatically. Therefore we
3000 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003001 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003002static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3003 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003004{
Boris Brezillonb9761682018-09-06 14:05:20 +02003005 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003006 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003007 int eccbytes = chip->ecc.bytes;
3008 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003009 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003010 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003011 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003012 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003013
Boris Brezillon25f815f2017-11-30 18:01:30 +01003014 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3015 if (ret)
3016 return ret;
3017
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003018 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3019 int stat;
3020
Boris Brezillonec476362018-09-06 14:05:17 +02003021 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003022
3023 ret = nand_read_data_op(chip, p, eccsize, false);
3024 if (ret)
3025 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003026
3027 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003028 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3029 false);
3030 if (ret)
3031 return ret;
3032
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003033 oob += chip->ecc.prepad;
3034 }
3035
Boris Brezillonec476362018-09-06 14:05:17 +02003036 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003037
3038 ret = nand_read_data_op(chip, oob, eccbytes, false);
3039 if (ret)
3040 return ret;
3041
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003042 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003043
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003044 oob += eccbytes;
3045
3046 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003047 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3048 false);
3049 if (ret)
3050 return ret;
3051
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003052 oob += chip->ecc.postpad;
3053 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003054
3055 if (stat == -EBADMSG &&
3056 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3057 /* check for empty pages with bitflips */
3058 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3059 oob - eccpadbytes,
3060 eccpadbytes,
3061 NULL, 0,
3062 chip->ecc.strength);
3063 }
3064
3065 if (stat < 0) {
3066 mtd->ecc_stats.failed++;
3067 } else {
3068 mtd->ecc_stats.corrected += stat;
3069 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3070 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003071 }
3072
3073 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003074 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003075 if (i) {
3076 ret = nand_read_data_op(chip, oob, i, false);
3077 if (ret)
3078 return ret;
3079 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003080
Mike Dunn3f91e942012-04-25 12:06:09 -07003081 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003082}
3083
3084/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003085 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon08136212018-11-11 08:55:03 +01003086 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003087 * @oob: oob destination address
3088 * @ops: oob ops structure
3089 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003090 */
Boris Brezillon08136212018-11-11 08:55:03 +01003091static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003092 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003093{
Boris Brezillon08136212018-11-11 08:55:03 +01003094 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003095 int ret;
3096
Florian Fainellif8ac0412010-09-07 13:23:43 +02003097 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003098
Brian Norris0612b9d2011-08-30 18:45:40 -07003099 case MTD_OPS_PLACE_OOB:
3100 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003101 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3102 return oob + len;
3103
Boris Brezillon846031d2016-02-03 20:11:00 +01003104 case MTD_OPS_AUTO_OOB:
3105 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3106 ops->ooboffs, len);
3107 BUG_ON(ret);
3108 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003109
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003110 default:
3111 BUG();
3112 }
3113 return NULL;
3114}
3115
3116/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003117 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003118 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003119 * @retry_mode: the retry mode to use
3120 *
3121 * Some vendors supply a special command to shift the Vt threshold, to be used
3122 * when there are too many bitflips in a page (i.e., ECC error). After setting
3123 * a new threshold, the host should retry reading the page.
3124 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003125static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003126{
Brian Norrisba84fb52014-01-03 15:13:33 -08003127 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3128
3129 if (retry_mode >= chip->read_retries)
3130 return -EINVAL;
3131
3132 if (!chip->setup_read_retry)
3133 return -EOPNOTSUPP;
3134
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003135 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003136}
3137
Boris Brezillon85e08e52018-07-27 09:44:17 +02003138static void nand_wait_readrdy(struct nand_chip *chip)
3139{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003140 const struct nand_sdr_timings *sdr;
3141
Boris Brezillon85e08e52018-07-27 09:44:17 +02003142 if (!(chip->options & NAND_NEED_READRDY))
3143 return;
3144
Boris Brezillon52f05b62018-07-27 09:44:18 +02003145 sdr = nand_get_sdr_timings(&chip->data_interface);
3146 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003147}
3148
Brian Norrisba84fb52014-01-03 15:13:33 -08003149/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003150 * nand_do_read_ops - [INTERN] Read data with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003151 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003152 * @from: offset to read from
3153 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003154 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003155 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003156 */
Boris Brezillon08136212018-11-11 08:55:03 +01003157static int nand_do_read_ops(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003158 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003159{
Brian Norrise47f3db2012-05-02 10:14:56 -07003160 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris Brezillon08136212018-11-11 08:55:03 +01003161 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003162 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003163 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003164 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003165 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003166
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003167 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003168 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003169 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003170 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003171 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003173 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003174 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003176 realpage = (int)(from >> chip->page_shift);
3177 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003179 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003181 buf = ops->datbuf;
3182 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003183 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003184
Florian Fainellif8ac0412010-09-07 13:23:43 +02003185 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003186 unsigned int ecc_failures = mtd->ecc_stats.failed;
3187
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003188 bytes = min(mtd->writesize - col, readlen);
3189 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003190
Kamal Dasu66507c72014-05-01 20:51:19 -04003191 if (!aligned)
3192 use_bufpoi = 1;
3193 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003194 use_bufpoi = !virt_addr_valid(buf) ||
3195 !IS_ALIGNED((unsigned long)buf,
3196 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003197 else
3198 use_bufpoi = 0;
3199
Brian Norris8b6e50c2011-05-25 14:59:01 -07003200 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003201 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003202 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003203
3204 if (use_bufpoi && aligned)
3205 pr_debug("%s: using read bounce buffer for buf@%p\n",
3206 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Brian Norrisba84fb52014-01-03 15:13:33 -08003208read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003209 /*
3210 * Now read the page into the buffer. Absent an error,
3211 * the read methods return max bitflips per ecc step.
3212 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003213 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003214 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003215 oob_required,
3216 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003217 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3218 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003219 ret = chip->ecc.read_subpage(chip, col, bytes,
3220 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003221 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003222 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003223 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003224 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003225 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003226 /* Invalidate page cache */
3227 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003228 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003229 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003230
3231 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003232 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003233 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003234 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003235 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003236 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003237 chip->pagebuf_bitflips = ret;
3238 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003239 /* Invalidate page cache */
3240 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003241 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003242 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003244
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003245 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003246 int toread = min(oobreadlen, max_oobsize);
3247
3248 if (toread) {
Boris Brezillon08136212018-11-11 08:55:03 +01003249 oob = nand_transfer_oob(chip, oob, ops,
3250 toread);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003251 oobreadlen -= toread;
3252 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003253 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003254
Boris Brezillon85e08e52018-07-27 09:44:17 +02003255 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003256
Brian Norrisba84fb52014-01-03 15:13:33 -08003257 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003258 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003259 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003260 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003261 retry_mode);
3262 if (ret < 0)
3263 break;
3264
3265 /* Reset failures; retry */
3266 mtd->ecc_stats.failed = ecc_failures;
3267 goto read_retry;
3268 } else {
3269 /* No more retry modes; real failure */
3270 ecc_fail = true;
3271 }
3272 }
3273
3274 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003275 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003276 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003277 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003278 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003279 max_bitflips = max_t(unsigned int, max_bitflips,
3280 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003283 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003284
Brian Norrisba84fb52014-01-03 15:13:33 -08003285 /* Reset to retry mode 0 */
3286 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003287 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003288 if (ret < 0)
3289 break;
3290 retry_mode = 0;
3291 }
3292
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003293 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Brian Norris8b6e50c2011-05-25 14:59:01 -07003296 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 col = 0;
3298 /* Increment page address */
3299 realpage++;
3300
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003301 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 /* Check, if we cross a chip boundary */
3303 if (!page) {
3304 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003305 nand_deselect_target(chip);
3306 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003309 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003311 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003312 if (oob)
3313 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Mike Dunn3f91e942012-04-25 12:06:09 -07003315 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003316 return ret;
3317
Brian Norrisb72f3df2013-12-03 11:04:14 -08003318 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003319 return -EBADMSG;
3320
Mike Dunnedbc45402012-04-25 12:06:11 -07003321 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003322}
3323
3324/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003325 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003326 * @chip: nand chip info structure
3327 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003328 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003329int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003330{
Boris Brezillonb9761682018-09-06 14:05:20 +02003331 struct mtd_info *mtd = nand_to_mtd(chip);
3332
Boris Brezillon97d90da2017-11-30 18:01:29 +01003333 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003334}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003335EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003336
3337/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003338 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003339 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003340 * @chip: nand chip info structure
3341 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003342 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003343static int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003344{
Boris Brezillonb9761682018-09-06 14:05:20 +02003345 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003346 int length = mtd->oobsize;
3347 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3348 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003349 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003350 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003351
Boris Brezillon97d90da2017-11-30 18:01:29 +01003352 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3353 if (ret)
3354 return ret;
3355
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003356 for (i = 0; i < chip->ecc.steps; i++) {
3357 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003358 int ret;
3359
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003360 pos = eccsize + i * (eccsize + chunk);
3361 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003362 ret = nand_change_read_column_op(chip, pos,
3363 NULL, 0,
3364 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003365 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003366 ret = nand_read_page_op(chip, page, pos, NULL,
3367 0);
3368
3369 if (ret)
3370 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003371 } else
3372 sndrnd = 1;
3373 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003374
3375 ret = nand_read_data_op(chip, bufpoi, toread, false);
3376 if (ret)
3377 return ret;
3378
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003379 bufpoi += toread;
3380 length -= toread;
3381 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003382 if (length > 0) {
3383 ret = nand_read_data_op(chip, bufpoi, length, false);
3384 if (ret)
3385 return ret;
3386 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003387
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003388 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003389}
3390
3391/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003392 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003393 * @chip: nand chip info structure
3394 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003395 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003396int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003397{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003398 struct mtd_info *mtd = nand_to_mtd(chip);
3399
Boris Brezillon97d90da2017-11-30 18:01:29 +01003400 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3401 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003402}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003403EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003404
3405/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003406 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003407 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003408 * @chip: nand chip info structure
3409 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003410 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003411static int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003412{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003413 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003414 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3415 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003416 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003417 const uint8_t *bufpoi = chip->oob_poi;
3418
3419 /*
3420 * data-ecc-data-ecc ... ecc-oob
3421 * or
3422 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3423 */
3424 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3425 pos = steps * (eccsize + chunk);
3426 steps = 0;
3427 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003428 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003429
Boris Brezillon97d90da2017-11-30 18:01:29 +01003430 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3431 if (ret)
3432 return ret;
3433
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003434 for (i = 0; i < steps; i++) {
3435 if (sndcmd) {
3436 if (mtd->writesize <= 512) {
3437 uint32_t fill = 0xFFFFFFFF;
3438
3439 len = eccsize;
3440 while (len > 0) {
3441 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003442
3443 ret = nand_write_data_op(chip, &fill,
3444 num, false);
3445 if (ret)
3446 return ret;
3447
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003448 len -= num;
3449 }
3450 } else {
3451 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003452 ret = nand_change_write_column_op(chip, pos,
3453 NULL, 0,
3454 false);
3455 if (ret)
3456 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003457 }
3458 } else
3459 sndcmd = 1;
3460 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003461
3462 ret = nand_write_data_op(chip, bufpoi, len, false);
3463 if (ret)
3464 return ret;
3465
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003466 bufpoi += len;
3467 length -= len;
3468 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003469 if (length > 0) {
3470 ret = nand_write_data_op(chip, bufpoi, length, false);
3471 if (ret)
3472 return ret;
3473 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003474
Boris Brezillon97d90da2017-11-30 18:01:29 +01003475 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003476}
3477
3478/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003479 * nand_do_read_oob - [INTERN] NAND read out-of-band
Boris Brezillon08136212018-11-11 08:55:03 +01003480 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003481 * @from: offset to read from
3482 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003484 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 */
Boris Brezillon08136212018-11-11 08:55:03 +01003486static int nand_do_read_oob(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003487 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488{
Boris Brezillon08136212018-11-11 08:55:03 +01003489 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003490 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003491 int page, realpage, chipnr;
Brian Norris041e4572011-06-23 16:45:24 -07003492 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003493 int readlen = ops->ooblen;
3494 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003495 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003496 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Brian Norris289c0522011-07-19 10:06:09 -07003498 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303499 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Brian Norris041e4572011-06-23 16:45:24 -07003501 stats = mtd->ecc_stats;
3502
Boris BREZILLON29f10582016-03-07 10:46:52 +01003503 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003504
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003505 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003506 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003508 /* Shift to get page */
3509 realpage = (int)(from >> chip->page_shift);
3510 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Florian Fainellif8ac0412010-09-07 13:23:43 +02003512 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003513 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003514 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003515 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003516 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003517
3518 if (ret < 0)
3519 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003520
3521 len = min(len, readlen);
Boris Brezillon08136212018-11-11 08:55:03 +01003522 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003523
Boris Brezillon85e08e52018-07-27 09:44:17 +02003524 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003525
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003526 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3527
Vitaly Wool70145682006-11-03 18:20:38 +03003528 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003529 if (!readlen)
3530 break;
3531
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003532 /* Increment page address */
3533 realpage++;
3534
3535 page = realpage & chip->pagemask;
3536 /* Check, if we cross a chip boundary */
3537 if (!page) {
3538 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003539 nand_deselect_target(chip);
3540 nand_select_target(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003543 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003545 ops->oobretlen = ops->ooblen - readlen;
3546
3547 if (ret < 0)
3548 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003549
3550 if (mtd->ecc_stats.failed - stats.failed)
3551 return -EBADMSG;
3552
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003553 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554}
3555
3556/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003557 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003558 * @mtd: MTD device structure
3559 * @from: offset to read from
3560 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003562 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003564static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3565 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
Boris Brezillon08136212018-11-11 08:55:03 +01003567 struct nand_chip *chip = mtd_to_nand(mtd);
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003568 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003569
3570 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003572 if (ops->mode != MTD_OPS_PLACE_OOB &&
3573 ops->mode != MTD_OPS_AUTO_OOB &&
3574 ops->mode != MTD_OPS_RAW)
3575 return -ENOTSUPP;
3576
Boris Brezillon08136212018-11-11 08:55:03 +01003577 nand_get_device(chip, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003579 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01003580 ret = nand_do_read_oob(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003581 else
Boris Brezillon08136212018-11-11 08:55:03 +01003582 ret = nand_do_read_ops(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003583
Boris Brezillon08136212018-11-11 08:55:03 +01003584 nand_release_device(chip);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003585 return ret;
3586}
3587
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003588/**
3589 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003590 * @chip: nand chip info structure
3591 * @buf: data buffer
3592 * @oob_required: must write chip->oob_poi to OOB
3593 * @page: page number to write
3594 *
3595 * Returns -ENOTSUPP unconditionally.
3596 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003597int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3598 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003599{
3600 return -ENOTSUPP;
3601}
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003602
3603/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003604 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003605 * @chip: nand chip info structure
3606 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003607 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003608 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003609 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003610 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003611 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003612int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3613 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003614{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003615 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003616 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003617
Boris Brezillon25f815f2017-11-30 18:01:30 +01003618 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003619 if (ret)
3620 return ret;
3621
3622 if (oob_required) {
3623 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3624 false);
3625 if (ret)
3626 return ret;
3627 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003628
Boris Brezillon25f815f2017-11-30 18:01:30 +01003629 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003631EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003633/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003634 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003635 * @chip: nand chip info structure
3636 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003637 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003638 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003639 *
3640 * We need a special oob layout and handling even when ECC isn't checked.
3641 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003642static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003643 const uint8_t *buf, int oob_required,
3644 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003645{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003646 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003647 int eccsize = chip->ecc.size;
3648 int eccbytes = chip->ecc.bytes;
3649 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003650 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003651
Boris Brezillon25f815f2017-11-30 18:01:30 +01003652 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3653 if (ret)
3654 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003655
3656 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003657 ret = nand_write_data_op(chip, buf, eccsize, false);
3658 if (ret)
3659 return ret;
3660
David Brownell52ff49d2009-03-04 12:01:36 -08003661 buf += eccsize;
3662
3663 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003664 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3665 false);
3666 if (ret)
3667 return ret;
3668
David Brownell52ff49d2009-03-04 12:01:36 -08003669 oob += chip->ecc.prepad;
3670 }
3671
Boris Brezillon97d90da2017-11-30 18:01:29 +01003672 ret = nand_write_data_op(chip, oob, eccbytes, false);
3673 if (ret)
3674 return ret;
3675
David Brownell52ff49d2009-03-04 12:01:36 -08003676 oob += eccbytes;
3677
3678 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003679 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3680 false);
3681 if (ret)
3682 return ret;
3683
David Brownell52ff49d2009-03-04 12:01:36 -08003684 oob += chip->ecc.postpad;
3685 }
3686 }
3687
3688 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003689 if (size) {
3690 ret = nand_write_data_op(chip, oob, size, false);
3691 if (ret)
3692 return ret;
3693 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003694
Boris Brezillon25f815f2017-11-30 18:01:30 +01003695 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003696}
3697/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003698 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003699 * @chip: nand chip info structure
3700 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003701 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003702 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003703 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003704static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
3705 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003706{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003707 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003708 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003709 int eccbytes = chip->ecc.bytes;
3710 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003711 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003712 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003713
Brian Norris7854d3f2011-06-23 14:12:08 -07003714 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003715 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003716 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003717
Boris Brezillon846031d2016-02-03 20:11:00 +01003718 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3719 chip->ecc.total);
3720 if (ret)
3721 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003722
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003723 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003724}
3725
3726/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003727 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003728 * @chip: nand chip info structure
3729 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003730 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003731 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003732 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003733static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
3734 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003735{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003736 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003737 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003738 int eccbytes = chip->ecc.bytes;
3739 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003740 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003741 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003742
Boris Brezillon25f815f2017-11-30 18:01:30 +01003743 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3744 if (ret)
3745 return ret;
3746
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003747 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003748 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003749
3750 ret = nand_write_data_op(chip, p, eccsize, false);
3751 if (ret)
3752 return ret;
3753
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003754 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003755 }
3756
Boris Brezillon846031d2016-02-03 20:11:00 +01003757 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3758 chip->ecc.total);
3759 if (ret)
3760 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003761
Boris Brezillon97d90da2017-11-30 18:01:29 +01003762 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3763 if (ret)
3764 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003765
Boris Brezillon25f815f2017-11-30 18:01:30 +01003766 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003767}
3768
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303769
3770/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08003771 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303772 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07003773 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303774 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07003775 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303776 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003777 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303778 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003779static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
3780 uint32_t data_len, const uint8_t *buf,
3781 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303782{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003783 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303784 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003785 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303786 int ecc_size = chip->ecc.size;
3787 int ecc_bytes = chip->ecc.bytes;
3788 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303789 uint32_t start_step = offset / ecc_size;
3790 uint32_t end_step = (offset + data_len - 1) / ecc_size;
3791 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01003792 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303793
Boris Brezillon25f815f2017-11-30 18:01:30 +01003794 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3795 if (ret)
3796 return ret;
3797
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303798 for (step = 0; step < ecc_steps; step++) {
3799 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02003800 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303801
3802 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003803 ret = nand_write_data_op(chip, buf, ecc_size, false);
3804 if (ret)
3805 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303806
3807 /* mask ECC of un-touched subpages by padding 0xFF */
3808 if ((step < start_step) || (step > end_step))
3809 memset(ecc_calc, 0xff, ecc_bytes);
3810 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003811 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303812
3813 /* mask OOB of un-touched subpages by padding 0xFF */
3814 /* if oob_required, preserve OOB metadata of written subpage */
3815 if (!oob_required || (step < start_step) || (step > end_step))
3816 memset(oob_buf, 0xff, oob_bytes);
3817
Brian Norrisd6a950802013-08-08 17:16:36 -07003818 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303819 ecc_calc += ecc_bytes;
3820 oob_buf += oob_bytes;
3821 }
3822
3823 /* copy calculated ECC for whole page to chip->buffer->oob */
3824 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003825 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01003826 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3827 chip->ecc.total);
3828 if (ret)
3829 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303830
3831 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003832 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3833 if (ret)
3834 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303835
Boris Brezillon25f815f2017-11-30 18:01:30 +01003836 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303837}
3838
3839
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003840/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003841 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003842 * @chip: nand chip info structure
3843 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003844 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003845 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003846 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003847 * The hw generator calculates the error syndrome automatically. Therefore we
3848 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003849 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003850static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
3851 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003852{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003853 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003854 int i, eccsize = chip->ecc.size;
3855 int eccbytes = chip->ecc.bytes;
3856 int eccsteps = chip->ecc.steps;
3857 const uint8_t *p = buf;
3858 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003859 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003860
Boris Brezillon25f815f2017-11-30 18:01:30 +01003861 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3862 if (ret)
3863 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003864
3865 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003866 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003867
3868 ret = nand_write_data_op(chip, p, eccsize, false);
3869 if (ret)
3870 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003871
3872 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003873 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3874 false);
3875 if (ret)
3876 return ret;
3877
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003878 oob += chip->ecc.prepad;
3879 }
3880
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003881 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003882
3883 ret = nand_write_data_op(chip, oob, eccbytes, false);
3884 if (ret)
3885 return ret;
3886
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003887 oob += eccbytes;
3888
3889 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003890 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3891 false);
3892 if (ret)
3893 return ret;
3894
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003895 oob += chip->ecc.postpad;
3896 }
3897 }
3898
3899 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003900 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003901 if (i) {
3902 ret = nand_write_data_op(chip, oob, i, false);
3903 if (ret)
3904 return ret;
3905 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003906
Boris Brezillon25f815f2017-11-30 18:01:30 +01003907 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003908}
3909
3910/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003911 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07003912 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303913 * @offset: address offset within the page
3914 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07003915 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07003916 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07003917 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003918 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003919 */
Boris Brezillon08136212018-11-11 08:55:03 +01003920static int nand_write_page(struct nand_chip *chip, uint32_t offset,
3921 int data_len, const uint8_t *buf, int oob_required,
3922 int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003923{
Boris Brezillon08136212018-11-11 08:55:03 +01003924 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303925 int status, subpage;
3926
3927 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3928 chip->ecc.write_subpage)
3929 subpage = offset || (data_len < mtd->writesize);
3930 else
3931 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003932
David Woodhouse956e9442006-09-25 17:12:39 +01003933 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003934 status = chip->ecc.write_page_raw(chip, buf, oob_required,
3935 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303936 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003937 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
3938 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003939 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003940 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08003941
3942 if (status < 0)
3943 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003944
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003945 return 0;
3946}
3947
Florian Fainellif8ac0412010-09-07 13:23:43 +02003948#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003949
3950/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003951 * nand_do_write_ops - [INTERN] NAND write with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003952 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003953 * @to: offset to write to
3954 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003955 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003956 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003957 */
Boris Brezillon08136212018-11-11 08:55:03 +01003958static int nand_do_write_ops(struct nand_chip *chip, loff_t to,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003959 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003960{
Boris Brezillon08136212018-11-11 08:55:03 +01003961 struct mtd_info *mtd = nand_to_mtd(chip);
Corentin Labbe73600b62017-09-02 10:49:38 +02003962 int chipnr, realpage, page, column;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003963 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02003964
3965 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003966 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003967
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003968 uint8_t *oob = ops->oobbuf;
3969 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303970 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07003971 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003972
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003973 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003974 if (!writelen)
3975 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003976
Brian Norris8b6e50c2011-05-25 14:59:01 -07003977 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003978 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003979 pr_notice("%s: attempt to write non page aligned data\n",
3980 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003981 return -EINVAL;
3982 }
3983
Thomas Gleixner29072b92006-09-28 15:38:36 +02003984 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003985
Thomas Gleixner6a930962006-06-28 00:11:45 +02003986 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003987 nand_select_target(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02003988
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003989 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01003990 if (nand_check_wp(chip)) {
Huang Shijieb0bb6902012-11-19 14:43:29 +08003991 ret = -EIO;
3992 goto err_out;
3993 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003994
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003995 realpage = (int)(to >> chip->page_shift);
3996 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003997
3998 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07003999 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4000 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004001 chip->pagebuf = -1;
4002
Maxim Levitsky782ce792010-02-22 20:39:36 +02004003 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004004 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4005 ret = -EINVAL;
4006 goto err_out;
4007 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004008
Florian Fainellif8ac0412010-09-07 13:23:43 +02004009 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004010 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004011 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004012 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004013 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004014
Kamal Dasu66507c72014-05-01 20:51:19 -04004015 if (part_pagewr)
4016 use_bufpoi = 1;
4017 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004018 use_bufpoi = !virt_addr_valid(buf) ||
4019 !IS_ALIGNED((unsigned long)buf,
4020 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004021 else
4022 use_bufpoi = 0;
4023
4024 /* Partial page write?, or need to use bounce buffer */
4025 if (use_bufpoi) {
4026 pr_debug("%s: using write bounce buffer for buf@%p\n",
4027 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004028 if (part_pagewr)
4029 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004030 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004031 memset(chip->data_buf, 0xff, mtd->writesize);
4032 memcpy(&chip->data_buf[column], buf, bytes);
4033 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004034 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004035
Maxim Levitsky782ce792010-02-22 20:39:36 +02004036 if (unlikely(oob)) {
4037 size_t len = min(oobwritelen, oobmaxlen);
Boris Brezillon08136212018-11-11 08:55:03 +01004038 oob = nand_fill_oob(chip, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004039 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004040 } else {
4041 /* We still need to erase leftover OOB data */
4042 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004043 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004044
Boris Brezillon08136212018-11-11 08:55:03 +01004045 ret = nand_write_page(chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004046 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004047 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004048 if (ret)
4049 break;
4050
4051 writelen -= bytes;
4052 if (!writelen)
4053 break;
4054
Thomas Gleixner29072b92006-09-28 15:38:36 +02004055 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004056 buf += bytes;
4057 realpage++;
4058
4059 page = realpage & chip->pagemask;
4060 /* Check, if we cross a chip boundary */
4061 if (!page) {
4062 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004063 nand_deselect_target(chip);
4064 nand_select_target(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004065 }
4066 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004067
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004068 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004069 if (unlikely(oob))
4070 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004071
4072err_out:
Boris Brezillon1d017852018-11-11 08:55:14 +01004073 nand_deselect_target(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004074 return ret;
4075}
4076
4077/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004078 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004079 * @mtd: MTD device structure
4080 * @to: offset to write to
4081 * @len: number of bytes to write
4082 * @retlen: pointer to variable to store the number of written bytes
4083 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004084 *
4085 * NAND write with ECC. Used when performing writes in interrupt context, this
4086 * may for example be called by mtdoops when writing an oops while in panic.
4087 */
4088static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4089 size_t *retlen, const uint8_t *buf)
4090{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004091 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004092 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004093 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004094 int ret;
4095
Brian Norris8b6e50c2011-05-25 14:59:01 -07004096 /* Grab the device */
Boris Brezillon08136212018-11-11 08:55:03 +01004097 panic_nand_get_device(chip, FL_WRITING);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004098
Boris Brezillon1d017852018-11-11 08:55:14 +01004099 nand_select_target(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004100
4101 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004102 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004103
Brian Norris0ec56dc2015-02-28 02:02:30 -08004104 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004105 ops.len = len;
4106 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004107 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004108
Boris Brezillon08136212018-11-11 08:55:03 +01004109 ret = nand_do_write_ops(chip, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004110
Brian Norris4a89ff82011-08-30 18:45:45 -07004111 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004112 return ret;
4113}
4114
4115/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004116 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004117 * @mtd: MTD device structure
4118 * @to: offset to write to
4119 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004120 */
4121static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4122 struct mtd_oob_ops *ops)
4123{
Boris Brezillon08136212018-11-11 08:55:03 +01004124 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004125 int ret = -ENOTSUPP;
4126
4127 ops->retlen = 0;
4128
Boris Brezillon08136212018-11-11 08:55:03 +01004129 nand_get_device(chip, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004130
Florian Fainellif8ac0412010-09-07 13:23:43 +02004131 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004132 case MTD_OPS_PLACE_OOB:
4133 case MTD_OPS_AUTO_OOB:
4134 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004135 break;
4136
4137 default:
4138 goto out;
4139 }
4140
4141 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01004142 ret = nand_do_write_oob(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004143 else
Boris Brezillon08136212018-11-11 08:55:03 +01004144 ret = nand_do_write_ops(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004145
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004146out:
Boris Brezillon08136212018-11-11 08:55:03 +01004147 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return ret;
4149}
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151/**
Brian Norris49c50b92014-05-06 16:02:19 -07004152 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004153 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004154 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 *
Brian Norris49c50b92014-05-06 16:02:19 -07004156 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004158static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004160 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004161
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004163 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004164
Boris Brezillon97d90da2017-11-30 18:01:29 +01004165 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166}
4167
4168/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004170 * @mtd: MTD device structure
4171 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004173 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004175static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004177 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004181 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004182 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004183 * @instr: erase instruction
4184 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004186 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004188int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004189 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
Adrian Hunter69423d92008-12-10 13:37:21 +00004191 int page, status, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004192 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
Brian Norris289c0522011-07-19 10:06:09 -07004194 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4195 __func__, (unsigned long long)instr->addr,
4196 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Boris Brezillon08136212018-11-11 08:55:03 +01004198 if (check_offs_len(chip, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 /* Grab the lock and see if the device is available */
Boris Brezillon08136212018-11-11 08:55:03 +01004202 nand_get_device(chip, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
4204 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004205 page = (int)(instr->addr >> chip->page_shift);
4206 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
4208 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004209 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 /* Select the NAND device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004212 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01004215 if (nand_check_wp(chip)) {
Brian Norris289c0522011-07-19 10:06:09 -07004216 pr_debug("%s: device is write protected!\n",
4217 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004218 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 goto erase_exit;
4220 }
4221
4222 /* Loop through the pages */
4223 len = instr->len;
4224
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004226 /* Check if we have a bad block, we do not erase bad blocks! */
Boris Brezillon08136212018-11-11 08:55:03 +01004227 if (nand_block_checkbad(chip, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304228 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004229 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4230 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004231 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 goto erase_exit;
4233 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004234
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004235 /*
4236 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004237 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004238 */
4239 if (page <= chip->pagebuf && chip->pagebuf <
4240 (page + pages_per_block))
4241 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
Boris Brezillonf9ebd1b2018-09-07 00:38:39 +02004243 if (chip->legacy.erase)
4244 status = chip->legacy.erase(chip,
4245 page & chip->pagemask);
4246 else
4247 status = single_erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
4249 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004250 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004251 pr_debug("%s: failed erase, page 0x%08x\n",
4252 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004253 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004254 instr->fail_addr =
4255 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 goto erase_exit;
4257 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004260 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 page += pages_per_block;
4262
4263 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004264 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004266 nand_deselect_target(chip);
4267 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 }
4269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004271 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004272erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004275 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004276 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 /* Return more or less happy */
4279 return ret;
4280}
4281
4282/**
4283 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004284 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004286 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004288static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289{
Boris Brezillon08136212018-11-11 08:55:03 +01004290 struct nand_chip *chip = mtd_to_nand(mtd);
4291
Brian Norris289c0522011-07-19 10:06:09 -07004292 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
4294 /* Grab the lock and see if the device is available */
Boris Brezillon08136212018-11-11 08:55:03 +01004295 nand_get_device(chip, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 /* Release it and go back */
Boris Brezillon08136212018-11-11 08:55:03 +01004297 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298}
4299
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004301 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004302 * @mtd: MTD device structure
4303 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004305static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304307 struct nand_chip *chip = mtd_to_nand(mtd);
4308 int chipnr = (int)(offs >> chip->chip_shift);
4309 int ret;
4310
4311 /* Select the NAND device */
Boris Brezillon08136212018-11-11 08:55:03 +01004312 nand_get_device(chip, FL_READING);
Boris Brezillon1d017852018-11-11 08:55:14 +01004313 nand_select_target(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304314
Boris Brezillon08136212018-11-11 08:55:03 +01004315 ret = nand_block_checkbad(chip, offs, 0);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304316
Boris Brezillon1d017852018-11-11 08:55:14 +01004317 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004318 nand_release_device(chip);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304319
4320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321}
4322
4323/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004324 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004325 * @mtd: MTD device structure
4326 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004328static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 int ret;
4331
Florian Fainellif8ac0412010-09-07 13:23:43 +02004332 ret = nand_block_isbad(mtd, ofs);
4333 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004334 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 if (ret > 0)
4336 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004337 return ret;
4338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Boris Brezillon08136212018-11-11 08:55:03 +01004340 return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341}
4342
4343/**
Zach Brown56718422017-01-10 13:30:20 -06004344 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4345 * @mtd: MTD device structure
4346 * @ofs: offset relative to mtd start
4347 * @len: length of mtd
4348 */
4349static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4350{
4351 struct nand_chip *chip = mtd_to_nand(mtd);
4352 u32 part_start_block;
4353 u32 part_end_block;
4354 u32 part_start_die;
4355 u32 part_end_die;
4356
4357 /*
4358 * max_bb_per_die and blocks_per_die used to determine
4359 * the maximum bad block count.
4360 */
4361 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4362 return -ENOTSUPP;
4363
4364 /* Get the start and end of the partition in erase blocks. */
4365 part_start_block = mtd_div_by_eb(ofs, mtd);
4366 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4367
4368 /* Get the start and end LUNs of the partition. */
4369 part_start_die = part_start_block / chip->blocks_per_die;
4370 part_end_die = part_end_block / chip->blocks_per_die;
4371
4372 /*
4373 * Look up the bad blocks per unit and multiply by the number of units
4374 * that the partition spans.
4375 */
4376 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4377}
4378
4379/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004380 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004381 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004382 */
4383static int nand_suspend(struct mtd_info *mtd)
4384{
Boris Brezillon08136212018-11-11 08:55:03 +01004385 return nand_get_device(mtd_to_nand(mtd), FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004386}
4387
4388/**
4389 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004390 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004391 */
4392static void nand_resume(struct mtd_info *mtd)
4393{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004394 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004395
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004396 if (chip->state == FL_PM_SUSPENDED)
Boris Brezillon08136212018-11-11 08:55:03 +01004397 nand_release_device(chip);
Vitaly Wool962034f2005-09-15 14:58:53 +01004398 else
Brian Norrisd0370212011-07-19 10:06:08 -07004399 pr_err("%s called for a chip which is not in suspended state\n",
4400 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004401}
4402
Scott Branden72ea4032014-11-20 11:18:05 -08004403/**
4404 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4405 * prevent further operations
4406 * @mtd: MTD device structure
4407 */
4408static void nand_shutdown(struct mtd_info *mtd)
4409{
Boris Brezillon08136212018-11-11 08:55:03 +01004410 nand_get_device(mtd_to_nand(mtd), FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004411}
4412
Brian Norris8b6e50c2011-05-25 14:59:01 -07004413/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004414static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004415{
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004416 nand_legacy_set_defaults(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004417
4418 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004419 chip->controller = &chip->dummy_controller;
4420 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004421 }
4422
Masahiro Yamada477544c2017-03-30 17:15:05 +09004423 if (!chip->buf_align)
4424 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004425}
4426
Brian Norris8b6e50c2011-05-25 14:59:01 -07004427/* Sanitize ONFI strings so we can safely print them */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004428void sanitize_string(uint8_t *s, size_t len)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004429{
4430 ssize_t i;
4431
Brian Norris8b6e50c2011-05-25 14:59:01 -07004432 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004433 s[len - 1] = 0;
4434
Brian Norris8b6e50c2011-05-25 14:59:01 -07004435 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004436 for (i = 0; i < len - 1; i++) {
4437 if (s[i] < ' ' || s[i] > 127)
4438 s[i] = '?';
4439 }
4440
Brian Norris8b6e50c2011-05-25 14:59:01 -07004441 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004442 strim(s);
4443}
4444
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004445/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07004446 * nand_id_has_period - Check if an ID string has a given wraparound period
4447 * @id_data: the ID string
4448 * @arrlen: the length of the @id_data array
4449 * @period: the period of repitition
4450 *
4451 * Check if an ID string is repeated within a given sequence of bytes at
4452 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08004453 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07004454 * if the repetition has a period of @period; otherwise, returns zero.
4455 */
4456static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4457{
4458 int i, j;
4459 for (i = 0; i < period; i++)
4460 for (j = i + period; j < arrlen; j += period)
4461 if (id_data[i] != id_data[j])
4462 return 0;
4463 return 1;
4464}
4465
4466/*
4467 * nand_id_len - Get the length of an ID string returned by CMD_READID
4468 * @id_data: the ID string
4469 * @arrlen: the length of the @id_data array
4470
4471 * Returns the length of the ID string, according to known wraparound/trailing
4472 * zero patterns. If no pattern exists, returns the length of the array.
4473 */
4474static int nand_id_len(u8 *id_data, int arrlen)
4475{
4476 int last_nonzero, period;
4477
4478 /* Find last non-zero byte */
4479 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4480 if (id_data[last_nonzero])
4481 break;
4482
4483 /* All zeros */
4484 if (last_nonzero < 0)
4485 return 0;
4486
4487 /* Calculate wraparound period */
4488 for (period = 1; period < arrlen; period++)
4489 if (nand_id_has_period(id_data, arrlen, period))
4490 break;
4491
4492 /* There's a repeated pattern */
4493 if (period < arrlen)
4494 return period;
4495
4496 /* There are trailing zeros */
4497 if (last_nonzero < arrlen - 1)
4498 return last_nonzero + 1;
4499
4500 /* No pattern detected */
4501 return arrlen;
4502}
4503
Huang Shijie7db906b2013-09-25 14:58:11 +08004504/* Extract the bits of per cell from the 3rd byte of the extended ID */
4505static int nand_get_bits_per_cell(u8 cellinfo)
4506{
4507 int bits;
4508
4509 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4510 bits >>= NAND_CI_CELLTYPE_SHIFT;
4511 return bits + 1;
4512}
4513
Brian Norrise3b88bd2012-09-24 20:40:52 -07004514/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004515 * Many new NAND share similar device ID codes, which represent the size of the
4516 * chip. The rest of the parameters must be decoded according to generic or
4517 * manufacturer-specific "extended ID" decoding patterns.
4518 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004519void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004520{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004521 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02004522 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004523 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004524 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08004525 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004526 /* The 4th id byte is the important one */
4527 extid = id_data[3];
4528
Boris Brezillon01389b62016-06-08 10:30:18 +02004529 /* Calc pagesize */
4530 mtd->writesize = 1024 << (extid & 0x03);
4531 extid >>= 2;
4532 /* Calc oobsize */
4533 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4534 extid >>= 2;
4535 /* Calc blocksize. Blocksize is multiples of 64KiB */
4536 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4537 extid >>= 2;
4538 /* Get buswidth information */
4539 if (extid & 0x1)
4540 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004541}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004542EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004543
4544/*
Brian Norrisf23a4812012-09-24 20:40:51 -07004545 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4546 * decodes a matching ID table entry and assigns the MTD size parameters for
4547 * the chip.
4548 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004549static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07004550{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004551 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07004552
4553 mtd->erasesize = type->erasesize;
4554 mtd->writesize = type->pagesize;
4555 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07004556
Huang Shijie1c195e92013-09-25 14:58:12 +08004557 /* All legacy ID NAND are small-page, SLC */
4558 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07004559}
4560
4561/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004562 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4563 * heuristic patterns using various detected parameters (e.g., manufacturer,
4564 * page size, cell-type information).
4565 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004566static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07004567{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004568 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004569
4570 /* Set the bad block position */
4571 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4572 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4573 else
4574 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004575}
4576
Huang Shijieec6e87e2013-03-15 11:01:00 +08004577static inline bool is_full_id_nand(struct nand_flash_dev *type)
4578{
4579 return type->id_len;
4580}
4581
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004582static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02004583 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004584{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004585 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02004586 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004587
Huang Shijieec6e87e2013-03-15 11:01:00 +08004588 if (!strncmp(type->id, id_data, type->id_len)) {
4589 mtd->writesize = type->pagesize;
4590 mtd->erasesize = type->erasesize;
4591 mtd->oobsize = type->oobsize;
4592
Huang Shijie7db906b2013-09-25 14:58:11 +08004593 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004594 chip->chipsize = (uint64_t)type->chipsize << 20;
4595 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004596 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4597 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004598 chip->onfi_timing_mode_default =
4599 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004600
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004601 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4602 if (!chip->parameters.model)
4603 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004604
Huang Shijieec6e87e2013-03-15 11:01:00 +08004605 return true;
4606 }
4607 return false;
4608}
4609
Brian Norris7e74c2d2012-09-24 20:40:49 -07004610/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004611 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4612 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4613 * table.
4614 */
4615static void nand_manufacturer_detect(struct nand_chip *chip)
4616{
4617 /*
4618 * Try manufacturer detection if available and use
4619 * nand_decode_ext_id() otherwise.
4620 */
4621 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004622 chip->manufacturer.desc->ops->detect) {
4623 /* The 3rd id byte holds MLC / multichip data */
4624 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004625 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004626 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004627 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004628 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004629}
4630
4631/*
4632 * Manufacturer initialization. This function is called for all NANDs including
4633 * ONFI and JEDEC compliant ones.
4634 * Manufacturer drivers should put all their specific initialization code in
4635 * their ->init() hook.
4636 */
4637static int nand_manufacturer_init(struct nand_chip *chip)
4638{
4639 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4640 !chip->manufacturer.desc->ops->init)
4641 return 0;
4642
4643 return chip->manufacturer.desc->ops->init(chip);
4644}
4645
4646/*
4647 * Manufacturer cleanup. This function is called for all NANDs including
4648 * ONFI and JEDEC compliant ones.
4649 * Manufacturer drivers should put all their specific cleanup code in their
4650 * ->cleanup() hook.
4651 */
4652static void nand_manufacturer_cleanup(struct nand_chip *chip)
4653{
4654 /* Release manufacturer private data */
4655 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4656 chip->manufacturer.desc->ops->cleanup)
4657 chip->manufacturer.desc->ops->cleanup(chip);
4658}
4659
Boris Brezillon348d56a2018-09-07 00:38:48 +02004660static const char *
4661nand_manufacturer_name(const struct nand_manufacturer *manufacturer)
4662{
4663 return manufacturer ? manufacturer->name : "Unknown";
4664}
4665
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004666/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004667 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004668 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004669static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004670{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004671 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004672 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004673 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004674 u8 *id_data = chip->id.data;
4675 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Karl Beldanef89a882008-09-15 14:37:29 +02004677 /*
4678 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004679 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004680 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004681 ret = nand_reset(chip, 0);
4682 if (ret)
4683 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004684
4685 /* Select the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004686 nand_select_target(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004689 ret = nand_readid_op(chip, 0, id_data, 2);
4690 if (ret)
4691 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
4693 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004694 maf_id = id_data[0];
4695 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Brian Norris8b6e50c2011-05-25 14:59:01 -07004697 /*
4698 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004699 * interface concerns can cause random data which looks like a
4700 * possibly credible NAND flash to appear. If the two results do
4701 * not match, ignore the device completely.
4702 */
4703
Brian Norris4aef9b72012-09-24 20:40:48 -07004704 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004705 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
4706 if (ret)
4707 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01004708
Boris Brezillon7f501f02016-05-24 19:20:05 +02004709 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004710 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004711 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004712 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004713 }
4714
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02004715 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02004716
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004717 /* Try to identify manufacturer */
4718 manufacturer = nand_get_manufacturer(maf_id);
4719 chip->manufacturer.desc = manufacturer;
4720
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004721 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004722 type = nand_flash_ids;
4723
Boris Brezillon29a198a2016-05-24 20:17:48 +02004724 /*
4725 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4726 * override it.
4727 * This is required to make sure initial NAND bus width set by the
4728 * NAND controller driver is coherent with the real NAND bus width
4729 * (extracted by auto-detection code).
4730 */
4731 busw = chip->options & NAND_BUSWIDTH_16;
4732
4733 /*
4734 * The flag is only set (never cleared), reset it to its default value
4735 * before starting auto-detection.
4736 */
4737 chip->options &= ~NAND_BUSWIDTH_16;
4738
Huang Shijieec6e87e2013-03-15 11:01:00 +08004739 for (; type->name != NULL; type++) {
4740 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004741 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004742 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004743 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004744 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004745 }
4746 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004747
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004748 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004749 /* Check if the chip is ONFI compliant */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004750 ret = nand_onfi_detect(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01004751 if (ret < 0)
4752 return ret;
4753 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004754 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004755
4756 /* Check if the chip is JEDEC compliant */
Boris Brezillon8ae3fbf2018-09-07 00:38:51 +02004757 ret = nand_jedec_detect(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01004758 if (ret < 0)
4759 return ret;
4760 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08004761 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004762 }
4763
David Woodhouse5e81e882010-02-26 18:32:56 +00004764 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004765 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004766
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004767 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4768 if (!chip->parameters.model)
4769 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004770
Adrian Hunter69423d92008-12-10 13:37:21 +00004771 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004772
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004773 if (!type->pagesize)
4774 nand_manufacturer_detect(chip);
4775 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004776 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004777
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004778 /* Get chip options */
4779 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004780
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004781ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01004782 if (!mtd->name)
4783 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004784
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004785 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004786 WARN_ON(busw & NAND_BUSWIDTH_16);
4787 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004788 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4789 /*
4790 * Check, if buswidth is correct. Hardware drivers should set
4791 * chip correct!
4792 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004793 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004794 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004795 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4796 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004797 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4798 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004799 ret = -EINVAL;
4800
4801 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004802 }
4803
Boris Brezillon7f501f02016-05-24 19:20:05 +02004804 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004805
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004806 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004807 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004808 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004809 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004810
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004811 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004812 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004813 if (chip->chipsize & 0xffffffff)
4814 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004815 else {
4816 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4817 chip->chip_shift += 32 - 1;
4818 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004819
Masahiro Yamada14157f82017-09-13 11:05:50 +09004820 if (chip->chip_shift - chip->page_shift > 16)
4821 chip->options |= NAND_ROW_ADDR_3;
4822
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004823 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004824
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004825 nand_legacy_adjust_cmdfunc(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004826
Ezequiel Garcia20171642013-11-25 08:30:31 -03004827 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004828 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01004829 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4830 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02004831 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004832 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004833 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004834 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004835
4836free_detect_allocation:
4837 kfree(chip->parameters.model);
4838
4839 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004840}
4841
Boris Brezillond48f62b2016-04-01 14:54:32 +02004842static const char * const nand_ecc_modes[] = {
4843 [NAND_ECC_NONE] = "none",
4844 [NAND_ECC_SOFT] = "soft",
4845 [NAND_ECC_HW] = "hw",
4846 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4847 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004848 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004849};
4850
4851static int of_get_nand_ecc_mode(struct device_node *np)
4852{
4853 const char *pm;
4854 int err, i;
4855
4856 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4857 if (err < 0)
4858 return err;
4859
4860 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4861 if (!strcasecmp(pm, nand_ecc_modes[i]))
4862 return i;
4863
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004864 /*
4865 * For backward compatibility we support few obsoleted values that don't
4866 * have their mappings into nand_ecc_modes_t anymore (they were merged
4867 * with other enums).
4868 */
4869 if (!strcasecmp(pm, "soft_bch"))
4870 return NAND_ECC_SOFT;
4871
Boris Brezillond48f62b2016-04-01 14:54:32 +02004872 return -ENODEV;
4873}
4874
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004875static const char * const nand_ecc_algos[] = {
4876 [NAND_ECC_HAMMING] = "hamming",
4877 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02004878 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004879};
4880
Boris Brezillond48f62b2016-04-01 14:54:32 +02004881static int of_get_nand_ecc_algo(struct device_node *np)
4882{
4883 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004884 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004885
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004886 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4887 if (!err) {
4888 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4889 if (!strcasecmp(pm, nand_ecc_algos[i]))
4890 return i;
4891 return -ENODEV;
4892 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004893
4894 /*
4895 * For backward compatibility we also read "nand-ecc-mode" checking
4896 * for some obsoleted values that were specifying ECC algorithm.
4897 */
4898 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4899 if (err < 0)
4900 return err;
4901
4902 if (!strcasecmp(pm, "soft"))
4903 return NAND_ECC_HAMMING;
4904 else if (!strcasecmp(pm, "soft_bch"))
4905 return NAND_ECC_BCH;
4906
4907 return -ENODEV;
4908}
4909
4910static int of_get_nand_ecc_step_size(struct device_node *np)
4911{
4912 int ret;
4913 u32 val;
4914
4915 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4916 return ret ? ret : val;
4917}
4918
4919static int of_get_nand_ecc_strength(struct device_node *np)
4920{
4921 int ret;
4922 u32 val;
4923
4924 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4925 return ret ? ret : val;
4926}
4927
4928static int of_get_nand_bus_width(struct device_node *np)
4929{
4930 u32 val;
4931
4932 if (of_property_read_u32(np, "nand-bus-width", &val))
4933 return 8;
4934
4935 switch (val) {
4936 case 8:
4937 case 16:
4938 return val;
4939 default:
4940 return -EIO;
4941 }
4942}
4943
4944static bool of_get_nand_on_flash_bbt(struct device_node *np)
4945{
4946 return of_property_read_bool(np, "nand-on-flash-bbt");
4947}
4948
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004949static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004950{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004951 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004952 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004953
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004954 if (!dn)
4955 return 0;
4956
Brian Norris5844fee2015-01-23 00:22:27 -08004957 if (of_get_nand_bus_width(dn) == 16)
4958 chip->options |= NAND_BUSWIDTH_16;
4959
Stefan Agnerf922bd72018-06-24 23:27:23 +02004960 if (of_property_read_bool(dn, "nand-is-boot-medium"))
4961 chip->options |= NAND_IS_BOOT_MEDIUM;
4962
Brian Norris5844fee2015-01-23 00:22:27 -08004963 if (of_get_nand_on_flash_bbt(dn))
4964 chip->bbt_options |= NAND_BBT_USE_FLASH;
4965
4966 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004967 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004968 ecc_strength = of_get_nand_ecc_strength(dn);
4969 ecc_step = of_get_nand_ecc_step_size(dn);
4970
Brian Norris5844fee2015-01-23 00:22:27 -08004971 if (ecc_mode >= 0)
4972 chip->ecc.mode = ecc_mode;
4973
Rafał Miłecki79082452016-03-23 11:19:02 +01004974 if (ecc_algo >= 0)
4975 chip->ecc.algo = ecc_algo;
4976
Brian Norris5844fee2015-01-23 00:22:27 -08004977 if (ecc_strength >= 0)
4978 chip->ecc.strength = ecc_strength;
4979
4980 if (ecc_step > 0)
4981 chip->ecc.size = ecc_step;
4982
Boris Brezillonba78ee02016-06-08 17:04:22 +02004983 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4984 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4985
Brian Norris5844fee2015-01-23 00:22:27 -08004986 return 0;
4987}
4988
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004989/**
Miquel Raynal98732da2018-07-25 15:31:50 +02004990 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02004991 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004992 * @maxchips: number of chips to scan for
4993 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004994 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004995 * This is the first phase of the normal nand_scan() function. It reads the
4996 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004997 *
Miquel Raynal98732da2018-07-25 15:31:50 +02004998 * This helper used to be called directly from controller drivers that needed
4999 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5000 * prevented dynamic allocations during this phase which was unconvenient and
5001 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005002 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005003static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005004 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005005{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005006 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02005007 int nand_maf_id, nand_dev_id;
5008 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08005009 int ret;
5010
Boris Brezillonae2294b2018-11-11 08:55:15 +01005011 /* Assume all dies are deselected when we enter nand_scan_ident(). */
5012 chip->cur_cs = -1;
5013
Miquel Raynal17fa8042017-11-30 18:01:31 +01005014 /* Enforce the right timings for reset/detection */
5015 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5016
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005017 ret = nand_dt_init(chip);
5018 if (ret)
5019 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005020
Brian Norrisf7a8e382016-01-05 10:39:45 -08005021 if (!mtd->name && mtd->dev.parent)
5022 mtd->name = dev_name(mtd->dev.parent);
5023
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02005024 if (chip->exec_op && !chip->select_chip) {
5025 pr_err("->select_chip() is mandatory when implementing ->exec_op()\n");
5026 return -EINVAL;
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005027 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005028
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02005029 ret = nand_legacy_check_hooks(chip);
5030 if (ret)
5031 return ret;
5032
Boris Brezillon1d017852018-11-11 08:55:14 +01005033 /*
5034 * Start with chips->numchips = maxchips to let nand_select_target() do
5035 * its job. chip->numchips will be adjusted after.
5036 */
5037 chip->numchips = maxchips;
5038
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005039 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005040 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005041
5042 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005043 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005044 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005045 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005046 pr_warn("No NAND device found\n");
Boris Brezillon1d017852018-11-11 08:55:14 +01005047 nand_deselect_target(chip);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005048 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 }
5050
Boris Brezillon7f501f02016-05-24 19:20:05 +02005051 nand_maf_id = chip->id.data[0];
5052 nand_dev_id = chip->id.data[1];
5053
Boris Brezillon1d017852018-11-11 08:55:14 +01005054 nand_deselect_target(chip);
Huang Shijie07300162012-11-09 16:23:45 +08005055
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005056 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005057 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005058 u8 id[2];
5059
Karl Beldanef89a882008-09-15 14:37:29 +02005060 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005061 nand_reset(chip, i);
5062
Boris Brezillon1d017852018-11-11 08:55:14 +01005063 nand_select_target(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005065 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005067 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon1d017852018-11-11 08:55:14 +01005068 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 break;
Huang Shijie07300162012-11-09 16:23:45 +08005070 }
Boris Brezillon1d017852018-11-11 08:55:14 +01005071 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 }
5073 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005074 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005077 chip->numchips = i;
5078 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
David Woodhouse3b85c322006-09-25 17:06:53 +01005080 return 0;
5081}
5082
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005083static void nand_scan_ident_cleanup(struct nand_chip *chip)
5084{
5085 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005086 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005087}
5088
Boris Brezillon08136212018-11-11 08:55:03 +01005089static int nand_set_ecc_soft_ops(struct nand_chip *chip)
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005090{
Boris Brezillon08136212018-11-11 08:55:03 +01005091 struct mtd_info *mtd = nand_to_mtd(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005092 struct nand_ecc_ctrl *ecc = &chip->ecc;
5093
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005094 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005095 return -EINVAL;
5096
5097 switch (ecc->algo) {
5098 case NAND_ECC_HAMMING:
5099 ecc->calculate = nand_calculate_ecc;
5100 ecc->correct = nand_correct_data;
5101 ecc->read_page = nand_read_page_swecc;
5102 ecc->read_subpage = nand_read_subpage;
5103 ecc->write_page = nand_write_page_swecc;
5104 ecc->read_page_raw = nand_read_page_raw;
5105 ecc->write_page_raw = nand_write_page_raw;
5106 ecc->read_oob = nand_read_oob_std;
5107 ecc->write_oob = nand_write_oob_std;
5108 if (!ecc->size)
5109 ecc->size = 256;
5110 ecc->bytes = 3;
5111 ecc->strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +02005112
5113 if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC))
5114 ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
5115
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005116 return 0;
5117 case NAND_ECC_BCH:
5118 if (!mtd_nand_has_bch()) {
5119 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5120 return -EINVAL;
5121 }
5122 ecc->calculate = nand_bch_calculate_ecc;
5123 ecc->correct = nand_bch_correct_data;
5124 ecc->read_page = nand_read_page_swecc;
5125 ecc->read_subpage = nand_read_subpage;
5126 ecc->write_page = nand_write_page_swecc;
5127 ecc->read_page_raw = nand_read_page_raw;
5128 ecc->write_page_raw = nand_write_page_raw;
5129 ecc->read_oob = nand_read_oob_std;
5130 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005131
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005132 /*
5133 * Board driver should supply ecc.size and ecc.strength
5134 * values to select how many bits are correctable.
5135 * Otherwise, default to 4 bits for large page devices.
5136 */
5137 if (!ecc->size && (mtd->oobsize >= 64)) {
5138 ecc->size = 512;
5139 ecc->strength = 4;
5140 }
5141
5142 /*
5143 * if no ecc placement scheme was provided pickup the default
5144 * large page one.
5145 */
5146 if (!mtd->ooblayout) {
5147 /* handle large page devices only */
5148 if (mtd->oobsize < 64) {
5149 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5150 return -EINVAL;
5151 }
5152
5153 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005154
5155 }
5156
5157 /*
5158 * We can only maximize ECC config when the default layout is
5159 * used, otherwise we don't know how many bytes can really be
5160 * used.
5161 */
5162 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5163 ecc->options & NAND_ECC_MAXIMIZE) {
5164 int steps, bytes;
5165
5166 /* Always prefer 1k blocks over 512bytes ones */
5167 ecc->size = 1024;
5168 steps = mtd->writesize / ecc->size;
5169
5170 /* Reserve 2 bytes for the BBM */
5171 bytes = (mtd->oobsize - 2) / steps;
5172 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005173 }
5174
5175 /* See nand_bch_init() for details. */
5176 ecc->bytes = 0;
5177 ecc->priv = nand_bch_init(mtd);
5178 if (!ecc->priv) {
5179 WARN(1, "BCH ECC initialization failed!\n");
5180 return -EINVAL;
5181 }
5182 return 0;
5183 default:
5184 WARN(1, "Unsupported ECC algorithm!\n");
5185 return -EINVAL;
5186 }
5187}
5188
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005189/**
5190 * nand_check_ecc_caps - check the sanity of preset ECC settings
5191 * @chip: nand chip info structure
5192 * @caps: ECC caps info structure
5193 * @oobavail: OOB size that the ECC engine can use
5194 *
5195 * When ECC step size and strength are already set, check if they are supported
5196 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5197 * On success, the calculated ECC bytes is set.
5198 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305199static int
5200nand_check_ecc_caps(struct nand_chip *chip,
5201 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005202{
5203 struct mtd_info *mtd = nand_to_mtd(chip);
5204 const struct nand_ecc_step_info *stepinfo;
5205 int preset_step = chip->ecc.size;
5206 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305207 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005208 int i, j;
5209
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005210 for (i = 0; i < caps->nstepinfos; i++) {
5211 stepinfo = &caps->stepinfos[i];
5212
5213 if (stepinfo->stepsize != preset_step)
5214 continue;
5215
5216 for (j = 0; j < stepinfo->nstrengths; j++) {
5217 if (stepinfo->strengths[j] != preset_strength)
5218 continue;
5219
5220 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5221 preset_strength);
5222 if (WARN_ON_ONCE(ecc_bytes < 0))
5223 return ecc_bytes;
5224
5225 if (ecc_bytes * nsteps > oobavail) {
5226 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5227 preset_step, preset_strength);
5228 return -ENOSPC;
5229 }
5230
5231 chip->ecc.bytes = ecc_bytes;
5232
5233 return 0;
5234 }
5235 }
5236
5237 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5238 preset_step, preset_strength);
5239
5240 return -ENOTSUPP;
5241}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005242
5243/**
5244 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5245 * @chip: nand chip info structure
5246 * @caps: ECC engine caps info structure
5247 * @oobavail: OOB size that the ECC engine can use
5248 *
5249 * If a chip's ECC requirement is provided, try to meet it with the least
5250 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5251 * On success, the chosen ECC settings are set.
5252 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305253static int
5254nand_match_ecc_req(struct nand_chip *chip,
5255 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005256{
5257 struct mtd_info *mtd = nand_to_mtd(chip);
5258 const struct nand_ecc_step_info *stepinfo;
5259 int req_step = chip->ecc_step_ds;
5260 int req_strength = chip->ecc_strength_ds;
5261 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5262 int best_step, best_strength, best_ecc_bytes;
5263 int best_ecc_bytes_total = INT_MAX;
5264 int i, j;
5265
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005266 /* No information provided by the NAND chip */
5267 if (!req_step || !req_strength)
5268 return -ENOTSUPP;
5269
5270 /* number of correctable bits the chip requires in a page */
5271 req_corr = mtd->writesize / req_step * req_strength;
5272
5273 for (i = 0; i < caps->nstepinfos; i++) {
5274 stepinfo = &caps->stepinfos[i];
5275 step_size = stepinfo->stepsize;
5276
5277 for (j = 0; j < stepinfo->nstrengths; j++) {
5278 strength = stepinfo->strengths[j];
5279
5280 /*
5281 * If both step size and strength are smaller than the
5282 * chip's requirement, it is not easy to compare the
5283 * resulted reliability.
5284 */
5285 if (step_size < req_step && strength < req_strength)
5286 continue;
5287
5288 if (mtd->writesize % step_size)
5289 continue;
5290
5291 nsteps = mtd->writesize / step_size;
5292
5293 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5294 if (WARN_ON_ONCE(ecc_bytes < 0))
5295 continue;
5296 ecc_bytes_total = ecc_bytes * nsteps;
5297
5298 if (ecc_bytes_total > oobavail ||
5299 strength * nsteps < req_corr)
5300 continue;
5301
5302 /*
5303 * We assume the best is to meet the chip's requrement
5304 * with the least number of ECC bytes.
5305 */
5306 if (ecc_bytes_total < best_ecc_bytes_total) {
5307 best_ecc_bytes_total = ecc_bytes_total;
5308 best_step = step_size;
5309 best_strength = strength;
5310 best_ecc_bytes = ecc_bytes;
5311 }
5312 }
5313 }
5314
5315 if (best_ecc_bytes_total == INT_MAX)
5316 return -ENOTSUPP;
5317
5318 chip->ecc.size = best_step;
5319 chip->ecc.strength = best_strength;
5320 chip->ecc.bytes = best_ecc_bytes;
5321
5322 return 0;
5323}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005324
5325/**
5326 * nand_maximize_ecc - choose the max ECC strength available
5327 * @chip: nand chip info structure
5328 * @caps: ECC engine caps info structure
5329 * @oobavail: OOB size that the ECC engine can use
5330 *
5331 * Choose the max ECC strength that is supported on the controller, and can fit
5332 * within the chip's OOB. On success, the chosen ECC settings are set.
5333 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305334static int
5335nand_maximize_ecc(struct nand_chip *chip,
5336 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005337{
5338 struct mtd_info *mtd = nand_to_mtd(chip);
5339 const struct nand_ecc_step_info *stepinfo;
5340 int step_size, strength, nsteps, ecc_bytes, corr;
5341 int best_corr = 0;
5342 int best_step = 0;
5343 int best_strength, best_ecc_bytes;
5344 int i, j;
5345
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005346 for (i = 0; i < caps->nstepinfos; i++) {
5347 stepinfo = &caps->stepinfos[i];
5348 step_size = stepinfo->stepsize;
5349
5350 /* If chip->ecc.size is already set, respect it */
5351 if (chip->ecc.size && step_size != chip->ecc.size)
5352 continue;
5353
5354 for (j = 0; j < stepinfo->nstrengths; j++) {
5355 strength = stepinfo->strengths[j];
5356
5357 if (mtd->writesize % step_size)
5358 continue;
5359
5360 nsteps = mtd->writesize / step_size;
5361
5362 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5363 if (WARN_ON_ONCE(ecc_bytes < 0))
5364 continue;
5365
5366 if (ecc_bytes * nsteps > oobavail)
5367 continue;
5368
5369 corr = strength * nsteps;
5370
5371 /*
5372 * If the number of correctable bits is the same,
5373 * bigger step_size has more reliability.
5374 */
5375 if (corr > best_corr ||
5376 (corr == best_corr && step_size > best_step)) {
5377 best_corr = corr;
5378 best_step = step_size;
5379 best_strength = strength;
5380 best_ecc_bytes = ecc_bytes;
5381 }
5382 }
5383 }
5384
5385 if (!best_corr)
5386 return -ENOTSUPP;
5387
5388 chip->ecc.size = best_step;
5389 chip->ecc.strength = best_strength;
5390 chip->ecc.bytes = best_ecc_bytes;
5391
5392 return 0;
5393}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005394
Abhishek Sahu181ace92018-06-20 12:57:28 +05305395/**
5396 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
5397 * @chip: nand chip info structure
5398 * @caps: ECC engine caps info structure
5399 * @oobavail: OOB size that the ECC engine can use
5400 *
5401 * Choose the ECC configuration according to following logic
5402 *
5403 * 1. If both ECC step size and ECC strength are already set (usually by DT)
5404 * then check if it is supported by this controller.
5405 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
5406 * 3. Otherwise, try to match the ECC step size and ECC strength closest
5407 * to the chip's requirement. If available OOB size can't fit the chip
5408 * requirement then fallback to the maximum ECC step size and ECC strength.
5409 *
5410 * On success, the chosen ECC settings are set.
5411 */
5412int nand_ecc_choose_conf(struct nand_chip *chip,
5413 const struct nand_ecc_caps *caps, int oobavail)
5414{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305415 struct mtd_info *mtd = nand_to_mtd(chip);
5416
5417 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
5418 return -EINVAL;
5419
Abhishek Sahu181ace92018-06-20 12:57:28 +05305420 if (chip->ecc.size && chip->ecc.strength)
5421 return nand_check_ecc_caps(chip, caps, oobavail);
5422
5423 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
5424 return nand_maximize_ecc(chip, caps, oobavail);
5425
5426 if (!nand_match_ecc_req(chip, caps, oobavail))
5427 return 0;
5428
5429 return nand_maximize_ecc(chip, caps, oobavail);
5430}
5431EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
5432
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005433/*
5434 * Check if the chip configuration meet the datasheet requirements.
5435
5436 * If our configuration corrects A bits per B bytes and the minimum
5437 * required correction level is X bits per Y bytes, then we must ensure
5438 * both of the following are true:
5439 *
5440 * (1) A / B >= X / Y
5441 * (2) A >= X
5442 *
5443 * Requirement (1) ensures we can correct for the required bitflip density.
5444 * Requirement (2) ensures we can correct even when all bitflips are clumped
5445 * in the same sector.
5446 */
Boris Brezillon08136212018-11-11 08:55:03 +01005447static bool nand_ecc_strength_good(struct nand_chip *chip)
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005448{
Boris Brezillon08136212018-11-11 08:55:03 +01005449 struct mtd_info *mtd = nand_to_mtd(chip);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005450 struct nand_ecc_ctrl *ecc = &chip->ecc;
5451 int corr, ds_corr;
5452
5453 if (ecc->size == 0 || chip->ecc_step_ds == 0)
5454 /* Not enough information */
5455 return true;
5456
5457 /*
5458 * We get the number of corrected bits per page to compare
5459 * the correction density.
5460 */
5461 corr = (mtd->writesize * ecc->strength) / ecc->size;
5462 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
5463
5464 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
5465}
David Woodhouse3b85c322006-09-25 17:06:53 +01005466
5467/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005468 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005469 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01005470 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005471 * This is the second phase of the normal nand_scan() function. It fills out
5472 * all the uninitialized function pointers with the defaults and scans for a
5473 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01005474 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005475static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01005476{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005477 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08005478 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005479 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01005480
Brian Norrise2414f42012-02-06 13:44:00 -08005481 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005482 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07005483 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005484 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07005485 }
Brian Norrise2414f42012-02-06 13:44:00 -08005486
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005487 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005488 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02005489 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005490
Boris Brezillonf84674b2017-06-02 12:18:24 +02005491 /*
5492 * FIXME: some NAND manufacturer drivers expect the first die to be
5493 * selected when manufacturer->init() is called. They should be fixed
5494 * to explictly select the relevant die when interacting with the NAND
5495 * chip.
5496 */
Boris Brezillon1d017852018-11-11 08:55:14 +01005497 nand_select_target(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005498 ret = nand_manufacturer_init(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01005499 nand_deselect_target(chip);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005500 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005501 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005502
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01005503 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005504 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005505
5506 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005507 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005508 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005509 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005510 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005511 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01005514 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 break;
5516 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005517 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02005518 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02005521 /*
5522 * Expose the whole OOB area to users if ECC_NONE
5523 * is passed. We could do that for all kind of
5524 * ->oobsize, but we must keep the old large/small
5525 * page with ECC layout when ->oobsize <= 128 for
5526 * compatibility reasons.
5527 */
5528 if (ecc->mode == NAND_ECC_NONE) {
5529 mtd_set_ooblayout(mtd,
5530 &nand_ooblayout_lp_ops);
5531 break;
5532 }
5533
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005534 WARN(1, "No oob scheme defined for oobsize %d\n",
5535 mtd->oobsize);
5536 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005537 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 }
5539 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005540
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005541 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005542 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005543 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01005544 */
David Woodhouse956e9442006-09-25 17:12:39 +01005545
Huang Shijie97de79e02013-10-18 14:20:53 +08005546 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005547 case NAND_ECC_HW_OOB_FIRST:
5548 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08005549 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005550 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5551 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005552 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005553 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005554 if (!ecc->read_page)
5555 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005556
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005557 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07005558 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005559 if (!ecc->read_page)
5560 ecc->read_page = nand_read_page_hwecc;
5561 if (!ecc->write_page)
5562 ecc->write_page = nand_write_page_hwecc;
5563 if (!ecc->read_page_raw)
5564 ecc->read_page_raw = nand_read_page_raw;
5565 if (!ecc->write_page_raw)
5566 ecc->write_page_raw = nand_write_page_raw;
5567 if (!ecc->read_oob)
5568 ecc->read_oob = nand_read_oob_std;
5569 if (!ecc->write_oob)
5570 ecc->write_oob = nand_write_oob_std;
5571 if (!ecc->read_subpage)
5572 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02005573 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08005574 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005575
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005576 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08005577 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5578 (!ecc->read_page ||
5579 ecc->read_page == nand_read_page_hwecc ||
5580 !ecc->write_page ||
5581 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005582 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5583 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005584 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005585 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07005586 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005587 if (!ecc->read_page)
5588 ecc->read_page = nand_read_page_syndrome;
5589 if (!ecc->write_page)
5590 ecc->write_page = nand_write_page_syndrome;
5591 if (!ecc->read_page_raw)
5592 ecc->read_page_raw = nand_read_page_raw_syndrome;
5593 if (!ecc->write_page_raw)
5594 ecc->write_page_raw = nand_write_page_raw_syndrome;
5595 if (!ecc->read_oob)
5596 ecc->read_oob = nand_read_oob_syndrome;
5597 if (!ecc->write_oob)
5598 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005599
Huang Shijie97de79e02013-10-18 14:20:53 +08005600 if (mtd->writesize >= ecc->size) {
5601 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005602 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
5603 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005604 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07005605 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005606 break;
Mike Dunne2788c92012-04-25 12:06:10 -07005607 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005608 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5609 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08005610 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02005611 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005613 case NAND_ECC_SOFT:
Boris Brezillon08136212018-11-11 08:55:03 +01005614 ret = nand_set_ecc_soft_ops(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005615 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005616 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005617 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01005618 }
5619 break;
5620
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005621 case NAND_ECC_ON_DIE:
5622 if (!ecc->read_page || !ecc->write_page) {
5623 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
5624 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005625 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005626 }
5627 if (!ecc->read_oob)
5628 ecc->read_oob = nand_read_oob_std;
5629 if (!ecc->write_oob)
5630 ecc->write_oob = nand_write_oob_std;
5631 break;
5632
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005633 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005634 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08005635 ecc->read_page = nand_read_page_raw;
5636 ecc->write_page = nand_write_page_raw;
5637 ecc->read_oob = nand_read_oob_std;
5638 ecc->read_page_raw = nand_read_page_raw;
5639 ecc->write_page_raw = nand_write_page_raw;
5640 ecc->write_oob = nand_write_oob_std;
5641 ecc->size = mtd->writesize;
5642 ecc->bytes = 0;
5643 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005645
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005647 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5648 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005649 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005652 if (ecc->correct || ecc->calculate) {
5653 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5654 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5655 if (!ecc->calc_buf || !ecc->code_buf) {
5656 ret = -ENOMEM;
5657 goto err_nand_manuf_cleanup;
5658 }
5659 }
5660
Brian Norris9ce244b2011-08-30 18:45:37 -07005661 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005662 if (!ecc->read_oob_raw)
5663 ecc->read_oob_raw = ecc->read_oob;
5664 if (!ecc->write_oob_raw)
5665 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005666
Boris Brezillon846031d2016-02-03 20:11:00 +01005667 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005668 mtd->ecc_strength = ecc->strength;
5669 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005670
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005671 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005672 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005673 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005674 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005675 ecc->steps = mtd->writesize / ecc->size;
5676 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005677 WARN(1, "Invalid ECC parameters\n");
5678 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005679 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005681 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005682 if (ecc->total > mtd->oobsize) {
5683 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5684 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005685 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005686 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005687
Boris Brezillon846031d2016-02-03 20:11:00 +01005688 /*
5689 * The number of bytes available for a client to place data into
5690 * the out of band area.
5691 */
5692 ret = mtd_ooblayout_count_freebytes(mtd);
5693 if (ret < 0)
5694 ret = 0;
5695
5696 mtd->oobavail = ret;
5697
5698 /* ECC sanity check: warn if it's too weak */
Boris Brezillon08136212018-11-11 08:55:03 +01005699 if (!nand_ecc_strength_good(chip))
Boris Brezillon846031d2016-02-03 20:11:00 +01005700 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5701 mtd->name);
5702
Brian Norris8b6e50c2011-05-25 14:59:01 -07005703 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005704 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005705 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005706 case 2:
5707 mtd->subpage_sft = 1;
5708 break;
5709 case 4:
5710 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005711 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005712 mtd->subpage_sft = 2;
5713 break;
5714 }
5715 }
5716 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5717
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02005718 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005719 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005722 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005724 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305725 switch (ecc->mode) {
5726 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305727 if (chip->page_shift > 9)
5728 chip->options |= NAND_SUBPAGE_READ;
5729 break;
5730
5731 default:
5732 break;
5733 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005734
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08005736 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02005737 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5738 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005739 mtd->_erase = nand_erase;
5740 mtd->_point = NULL;
5741 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005742 mtd->_panic_write = panic_nand_write;
5743 mtd->_read_oob = nand_read_oob;
5744 mtd->_write_oob = nand_write_oob;
5745 mtd->_sync = nand_sync;
5746 mtd->_lock = NULL;
5747 mtd->_unlock = NULL;
5748 mtd->_suspend = nand_suspend;
5749 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005750 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005751 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005752 mtd->_block_isbad = nand_block_isbad;
5753 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06005754 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01005755 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005757 /*
5758 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5759 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5760 * properly set.
5761 */
5762 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005763 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
Boris Brezillonf84674b2017-06-02 12:18:24 +02005765 /* Initialize the ->data_interface field. */
5766 ret = nand_init_data_interface(chip);
5767 if (ret)
5768 goto err_nand_manuf_cleanup;
5769
5770 /* Enter fastest possible mode on all dies. */
5771 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005772 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005773 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005774 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005775 }
5776
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005777 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005778 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780
5781 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02005782 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07005783 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005784 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005785
Brian Norris44d41822017-05-01 17:04:50 -07005786 return 0;
5787
Boris Brezillonf84674b2017-06-02 12:18:24 +02005788
5789err_nand_manuf_cleanup:
5790 nand_manufacturer_cleanup(chip);
5791
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005792err_free_buf:
5793 kfree(chip->data_buf);
5794 kfree(ecc->code_buf);
5795 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07005796
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005797 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798}
5799
Miquel Raynal05b54c72018-07-19 01:05:46 +02005800static int nand_attach(struct nand_chip *chip)
5801{
5802 if (chip->controller->ops && chip->controller->ops->attach_chip)
5803 return chip->controller->ops->attach_chip(chip);
5804
5805 return 0;
5806}
5807
5808static void nand_detach(struct nand_chip *chip)
5809{
5810 if (chip->controller->ops && chip->controller->ops->detach_chip)
5811 chip->controller->ops->detach_chip(chip);
5812}
5813
David Woodhouse3b85c322006-09-25 17:06:53 +01005814/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005815 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005816 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02005817 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005818 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01005819 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005820 * This fills out all the uninitialized function pointers with the defaults.
5821 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005822 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005823 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005824int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005825 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01005826{
5827 int ret;
5828
Boris Brezillon800342d2018-08-04 22:59:23 +02005829 if (!maxchips)
5830 return -EINVAL;
5831
5832 ret = nand_scan_ident(chip, maxchips, ids);
5833 if (ret)
5834 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005835
5836 ret = nand_attach(chip);
5837 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005838 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005839
Boris Brezillon00ad3782018-09-06 14:05:14 +02005840 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005841 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005842 goto detach_chip;
5843
5844 return 0;
5845
5846detach_chip:
5847 nand_detach(chip);
5848cleanup_ident:
5849 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005850
David Woodhouse3b85c322006-09-25 17:06:53 +01005851 return ret;
5852}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005853EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01005854
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005856 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5857 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005858 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005859void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005861 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005862 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005863 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5864
Jesper Juhlfa671642005-11-07 01:01:27 -08005865 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005866 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005867 kfree(chip->data_buf);
5868 kfree(chip->ecc.code_buf);
5869 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07005870
5871 /* Free bad block descriptor memory */
5872 if (chip->badblock_pattern && chip->badblock_pattern->options
5873 & NAND_BBT_DYNAMICSTRUCT)
5874 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005875
5876 /* Free manufacturer priv data. */
5877 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005878
5879 /* Free controller specific allocations after chip identification */
5880 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005881
5882 /* Free identification phase allocations */
5883 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884}
Miquel Raynal05b54c72018-07-19 01:05:46 +02005885
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005886EXPORT_SYMBOL_GPL(nand_cleanup);
5887
5888/**
5889 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5890 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02005891 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005892 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02005893void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005894{
Boris Brezillon59ac2762018-09-06 14:05:15 +02005895 mtd_device_unregister(nand_to_mtd(chip));
5896 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005897}
David Woodhousee0c7d762006-05-13 18:07:53 +01005898EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005899
David Woodhousee0c7d762006-05-13 18:07:53 +01005900MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005901MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5902MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005903MODULE_DESCRIPTION("Generic NAND flash driver code");