blob: 839494ac457c15c4697a357f2136f0cf0852acc6 [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 Brezillon02b4a522018-11-11 08:55:16 +0100250
Boris Brezillon7d6c37e2018-11-11 08:55:22 +0100251 if (chip->legacy.select_chip)
252 chip->legacy.select_chip(chip, cs);
Boris Brezillon1d017852018-11-11 08:55:14 +0100253}
254EXPORT_SYMBOL_GPL(nand_select_target);
255
256/**
257 * nand_deselect_target() - Deselect the currently selected target
258 * @chip: NAND chip object
259 *
260 * Deselect the currently selected NAND target. The result of operations
261 * executed on @chip after the target has been deselected is undefined.
262 */
263void nand_deselect_target(struct nand_chip *chip)
264{
Boris Brezillon7d6c37e2018-11-11 08:55:22 +0100265 if (chip->legacy.select_chip)
266 chip->legacy.select_chip(chip, -1);
Boris Brezillon02b4a522018-11-11 08:55:16 +0100267
Boris Brezillonae2294b2018-11-11 08:55:15 +0100268 chip->cur_cs = -1;
Boris Brezillon1d017852018-11-11 08:55:14 +0100269}
270EXPORT_SYMBOL_GPL(nand_deselect_target);
271
272/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * nand_release_device - [GENERIC] release chip
Boris Brezillon08136212018-11-11 08:55:03 +0100274 * @chip: NAND chip object
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000275 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800276 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Boris Brezillon08136212018-11-11 08:55:03 +0100278static void nand_release_device(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200280 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200281 spin_lock(&chip->controller->lock);
282 chip->controller->active = NULL;
283 chip->state = FL_READY;
284 wake_up(&chip->controller->wq);
285 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Boris Brezillonc17556f2018-09-06 14:05:25 +0200290 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700291 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000293 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200295static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200297 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900298 int page, page_end, res;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900299 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Brian Norris5fb15492011-05-31 16:31:21 -0700301 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700302 ofs += mtd->erasesize - mtd->writesize;
303
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100304 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900305 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100306
Masahiro Yamadac120e752017-03-23 05:07:01 +0900307 for (; page < page_end; page++) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200308 res = chip->ecc.read_oob(chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530309 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900310 return res;
311
312 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000313
Brian Norriscdbec052012-01-13 18:11:48 -0800314 if (likely(chip->badblockbits == 8))
315 res = bad != 0xFF;
316 else
317 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900318 if (res)
319 return res;
320 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200321
Masahiro Yamadac120e752017-03-23 05:07:01 +0900322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Boris Brezillon99f33512018-11-11 08:55:04 +0100325static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
326{
327 if (chip->legacy.block_bad)
328 return chip->legacy.block_bad(chip, ofs);
329
330 return nand_block_bad(chip, ofs);
331}
332
333/**
334 * panic_nand_get_device - [GENERIC] Get chip for selected access
335 * @chip: the nand chip descriptor
336 * @new_state: the state which is requested
337 *
338 * Used when in panic, no locks are taken.
339 */
340static void panic_nand_get_device(struct nand_chip *chip, int new_state)
341{
342 /* Hardware controller shared among independent devices */
343 chip->controller->active = chip;
344 chip->state = new_state;
345}
346
347/**
348 * nand_get_device - [GENERIC] Get chip for selected access
349 * @chip: NAND chip structure
350 * @new_state: the state which is requested
351 *
352 * Get the device and lock it for exclusive access
353 */
354static int
355nand_get_device(struct nand_chip *chip, int new_state)
356{
357 spinlock_t *lock = &chip->controller->lock;
358 wait_queue_head_t *wq = &chip->controller->wq;
359 DECLARE_WAITQUEUE(wait, current);
360retry:
361 spin_lock(lock);
362
363 /* Hardware controller shared among independent devices */
364 if (!chip->controller->active)
365 chip->controller->active = chip;
366
367 if (chip->controller->active == chip && chip->state == FL_READY) {
368 chip->state = new_state;
369 spin_unlock(lock);
370 return 0;
371 }
372 if (new_state == FL_PM_SUSPENDED) {
373 if (chip->controller->active->state == FL_PM_SUSPENDED) {
374 chip->state = FL_PM_SUSPENDED;
375 spin_unlock(lock);
376 return 0;
377 }
378 }
379 set_current_state(TASK_UNINTERRUPTIBLE);
380 add_wait_queue(wq, &wait);
381 spin_unlock(lock);
382 schedule();
383 remove_wait_queue(wq, &wait);
384 goto retry;
385}
386
387/**
388 * nand_check_wp - [GENERIC] check if the chip is write protected
389 * @chip: NAND chip object
390 *
391 * Check, if the device is write protected. The function expects, that the
392 * device is already selected.
393 */
394static int nand_check_wp(struct nand_chip *chip)
395{
396 u8 status;
397 int ret;
398
399 /* Broken xD cards report WP despite being writable */
400 if (chip->options & NAND_BROKEN_XD)
401 return 0;
402
403 /* Check the WP bit */
404 ret = nand_status_op(chip, &status);
405 if (ret)
406 return ret;
407
408 return status & NAND_STATUS_WP ? 0 : 1;
409}
410
411/**
412 * nand_fill_oob - [INTERN] Transfer client buffer to oob
Randy Dunlap455e7b32019-01-27 18:21:42 -0800413 * @chip: NAND chip object
Boris Brezillon99f33512018-11-11 08:55:04 +0100414 * @oob: oob data buffer
415 * @len: oob data write length
416 * @ops: oob ops structure
417 */
418static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
419 struct mtd_oob_ops *ops)
420{
421 struct mtd_info *mtd = nand_to_mtd(chip);
422 int ret;
423
424 /*
425 * Initialise to all 0xFF, to avoid the possibility of left over OOB
426 * data from a previous OOB read.
427 */
428 memset(chip->oob_poi, 0xff, mtd->oobsize);
429
430 switch (ops->mode) {
431
432 case MTD_OPS_PLACE_OOB:
433 case MTD_OPS_RAW:
434 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
435 return oob + len;
436
437 case MTD_OPS_AUTO_OOB:
438 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
439 ops->ooboffs, len);
440 BUG_ON(ret);
441 return oob + len;
442
443 default:
444 BUG();
445 }
446 return NULL;
447}
448
449/**
450 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
451 * @chip: NAND chip object
452 * @to: offset to write to
453 * @ops: oob operation description structure
454 *
455 * NAND write out-of-band.
456 */
457static int nand_do_write_oob(struct nand_chip *chip, loff_t to,
458 struct mtd_oob_ops *ops)
459{
460 struct mtd_info *mtd = nand_to_mtd(chip);
461 int chipnr, page, status, len;
462
463 pr_debug("%s: to = 0x%08x, len = %i\n",
464 __func__, (unsigned int)to, (int)ops->ooblen);
465
466 len = mtd_oobavail(mtd, ops);
467
468 /* Do not allow write past end of page */
469 if ((ops->ooboffs + ops->ooblen) > len) {
470 pr_debug("%s: attempt to write past end of page\n",
471 __func__);
472 return -EINVAL;
473 }
474
475 chipnr = (int)(to >> chip->chip_shift);
476
477 /*
478 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
479 * of my DiskOnChip 2000 test units) will clear the whole data page too
480 * if we don't do this. I have no clue why, but I seem to have 'fixed'
481 * it in the doc2000 driver in August 1999. dwmw2.
482 */
483 nand_reset(chip, chipnr);
484
Boris Brezillon1d017852018-11-11 08:55:14 +0100485 nand_select_target(chip, chipnr);
Boris Brezillon99f33512018-11-11 08:55:04 +0100486
487 /* Shift to get page */
488 page = (int)(to >> chip->page_shift);
489
490 /* Check, if it is write protected */
491 if (nand_check_wp(chip)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100492 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100493 return -EROFS;
494 }
495
496 /* Invalidate the page cache, if we write to the cached page */
497 if (page == chip->pagebuf)
498 chip->pagebuf = -1;
499
500 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
501
502 if (ops->mode == MTD_OPS_RAW)
503 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
504 else
505 status = chip->ecc.write_oob(chip, page & chip->pagemask);
506
Boris Brezillon1d017852018-11-11 08:55:14 +0100507 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100508
509 if (status)
510 return status;
511
512 ops->oobretlen = ops->ooblen;
513
514 return 0;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700518 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200519 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700520 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700522 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700523 * specific driver. It provides the details for writing a bad block marker to a
524 * block.
525 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200526static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700527{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200528 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700529 struct mtd_oob_ops ops;
530 uint8_t buf[2] = { 0, 0 };
531 int ret = 0, res, i = 0;
532
Brian Norris0ec56dc2015-02-28 02:02:30 -0800533 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700534 ops.oobbuf = buf;
535 ops.ooboffs = chip->badblockpos;
536 if (chip->options & NAND_BUSWIDTH_16) {
537 ops.ooboffs &= ~0x01;
538 ops.len = ops.ooblen = 2;
539 } else {
540 ops.len = ops.ooblen = 1;
541 }
542 ops.mode = MTD_OPS_PLACE_OOB;
543
544 /* Write to first/last page(s) if necessary */
545 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
546 ofs += mtd->erasesize - mtd->writesize;
547 do {
Boris Brezillon08136212018-11-11 08:55:03 +0100548 res = nand_do_write_oob(chip, ofs, &ops);
Brian Norris5a0edb22013-07-30 17:52:58 -0700549 if (!ret)
550 ret = res;
551
552 i++;
553 ofs += mtd->writesize;
554 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
555
556 return ret;
557}
558
559/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200560 * nand_markbad_bbm - mark a block by updating the BBM
561 * @chip: NAND chip object
562 * @ofs: offset of the block to mark bad
563 */
564int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
565{
566 if (chip->legacy.block_markbad)
567 return chip->legacy.block_markbad(chip, ofs);
568
569 return nand_default_block_markbad(chip, ofs);
570}
571
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200572/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700573 * nand_block_markbad_lowlevel - mark a block bad
Boris Brezillon08136212018-11-11 08:55:03 +0100574 * @chip: NAND chip object
Brian Norris5a0edb22013-07-30 17:52:58 -0700575 * @ofs: offset from device start
576 *
577 * This function performs the generic NAND bad block marking steps (i.e., bad
578 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200579 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700580 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700581 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300582 *
Brian Norrise2414f42012-02-06 13:44:00 -0800583 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700584 * (2) write bad block marker to OOB area of affected block (unless flag
585 * NAND_BBT_NO_OOB_BBM is present)
586 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300587 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700588 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800589 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590*/
Boris Brezillon08136212018-11-11 08:55:03 +0100591static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Boris Brezillon08136212018-11-11 08:55:03 +0100593 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisb32843b2013-07-30 17:52:59 -0700594 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000595
Brian Norrisb32843b2013-07-30 17:52:59 -0700596 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800597 struct erase_info einfo;
598
599 /* Attempt erase before marking OOB */
600 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800601 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300602 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200603 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800604
Brian Norrisb32843b2013-07-30 17:52:59 -0700605 /* Write bad block marker to OOB */
Boris Brezillon08136212018-11-11 08:55:03 +0100606 nand_get_device(chip, FL_WRITING);
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200607 ret = nand_markbad_bbm(chip, ofs);
Boris Brezillon08136212018-11-11 08:55:03 +0100608 nand_release_device(chip);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200609 }
Brian Norrise2414f42012-02-06 13:44:00 -0800610
Brian Norrisb32843b2013-07-30 17:52:59 -0700611 /* Mark block bad in BBT */
612 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200613 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800614 if (!ret)
615 ret = res;
616 }
617
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200618 if (!ret)
619 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300620
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200621 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000624/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800625 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700626 * @mtd: MTD device structure
627 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300628 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800629 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300630 */
631static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
632{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100633 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300634
635 if (!chip->bbt)
636 return 0;
637 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200638 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300639}
640
641/**
642 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Boris Brezillon08136212018-11-11 08:55:03 +0100643 * @chip: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300644 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700645 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 *
647 * Check, if the block is bad. Either by reading the bad block table or
648 * calling of the scan function.
649 */
Boris Brezillon08136212018-11-11 08:55:03 +0100650static int nand_block_checkbad(struct nand_chip *chip, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200653 if (chip->bbt)
654 return nand_isbad_bbt(chip, ofs, allowbbt);
655
656 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200659/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100660 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
661 * @chip: NAND chip structure
662 * @timeout_ms: Timeout in ms
663 *
664 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
665 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
666 * returned.
667 *
668 * This helper is intended to be used when the controller does not have access
669 * to the NAND R/B pin.
670 *
671 * Be aware that calling this helper from an ->exec_op() implementation means
672 * ->exec_op() must be re-entrant.
673 *
674 * Return 0 if the NAND chip is ready, a negative error otherwise.
675 */
676int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
677{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200678 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100679 u8 status = 0;
680 int ret;
681
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100682 if (!nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +0100683 return -ENOTSUPP;
684
Boris Brezillon3057fce2018-05-04 21:24:31 +0200685 /* Wait tWB before polling the STATUS reg. */
686 timings = nand_get_sdr_timings(&chip->data_interface);
687 ndelay(PSEC_TO_NSEC(timings->tWB_max));
688
Miquel Raynal8878b122017-11-09 14:16:45 +0100689 ret = nand_status_op(chip, NULL);
690 if (ret)
691 return ret;
692
693 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
694 do {
695 ret = nand_read_data_op(chip, &status, sizeof(status), true);
696 if (ret)
697 break;
698
699 if (status & NAND_STATUS_READY)
700 break;
701
702 /*
703 * Typical lowest execution time for a tR on most NANDs is 10us,
704 * use this as polling delay before doing something smarter (ie.
705 * deriving a delay from the timeout value, timeout_ms/ratio).
706 */
707 udelay(10);
708 } while (time_before(jiffies, timeout_ms));
709
710 /*
711 * We have to exit READ_STATUS mode in order to read real data on the
712 * bus in case the WAITRDY instruction is preceding a DATA_IN
713 * instruction.
714 */
715 nand_exit_status_op(chip);
716
717 if (ret)
718 return ret;
719
720 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
721};
722EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
723
724/**
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +0200725 * nand_gpio_waitrdy - Poll R/B GPIO pin until ready
726 * @chip: NAND chip structure
727 * @gpiod: GPIO descriptor of R/B pin
728 * @timeout_ms: Timeout in ms
729 *
730 * Poll the R/B GPIO pin until it becomes ready. If that does not happen
731 * whitin the specified timeout, -ETIMEDOUT is returned.
732 *
733 * This helper is intended to be used when the controller has access to the
734 * NAND R/B pin over GPIO.
735 *
736 * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise.
737 */
738int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
739 unsigned long timeout_ms)
740{
741 /* Wait until R/B pin indicates chip is ready or timeout occurs */
742 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
743 do {
744 if (gpiod_get_value_cansleep(gpiod))
745 return 0;
746
747 cond_resched();
748 } while (time_before(jiffies, timeout_ms));
749
750 return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT;
751};
752EXPORT_SYMBOL_GPL(nand_gpio_waitrdy);
753
754/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700755 * panic_nand_wait - [GENERIC] wait until the command is done
Brian Norris8b6e50c2011-05-25 14:59:01 -0700756 * @chip: NAND chip structure
757 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200758 *
759 * Wait for command done. This is a helper function for nand_wait used when
760 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400761 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200762 */
Boris Brezillon3d4af7c2018-09-07 00:38:49 +0200763void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200764{
765 int i;
766 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200767 if (chip->legacy.dev_ready) {
768 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200769 break;
770 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100771 int ret;
772 u8 status;
773
774 ret = nand_read_data_op(chip, &status, sizeof(status),
775 true);
776 if (ret)
777 return;
778
779 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200780 break;
781 }
782 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200783 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200784}
785
Miquel Raynal789157e2018-03-19 14:47:28 +0100786static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +0100787{
Miquel Raynal789157e2018-03-19 14:47:28 +0100788 return (chip->parameters.supports_set_get_features &&
789 test_bit(addr, chip->parameters.get_feature_list));
790}
791
792static bool nand_supports_set_features(struct nand_chip *chip, int addr)
793{
794 return (chip->parameters.supports_set_get_features &&
795 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +0100796}
797
798/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200799 * nand_reset_data_interface - Reset data interface and timings
800 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100801 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200802 *
803 * Reset the Data interface and timings to ONFI mode 0.
804 *
805 * Returns 0 for success or negative error code otherwise.
806 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100807static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200808{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200809 int ret;
810
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100811 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200812 return 0;
813
814 /*
815 * The ONFI specification says:
816 * "
817 * To transition from NV-DDR or NV-DDR2 to the SDR data
818 * interface, the host shall use the Reset (FFh) command
819 * using SDR timing mode 0. A device in any timing mode is
820 * required to recognize Reset (FFh) command issued in SDR
821 * timing mode 0.
822 * "
823 *
824 * Configure the data interface in SDR mode and set the
825 * timings to timing mode 0.
826 */
827
Miquel Raynal17fa8042017-11-30 18:01:31 +0100828 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100829 ret = chip->controller->ops->setup_data_interface(chip, chipnr,
830 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200831 if (ret)
832 pr_err("Failed to configure data interface to SDR timing mode 0\n");
833
834 return ret;
835}
836
837/**
838 * nand_setup_data_interface - Setup the best data interface and timings
839 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100840 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200841 *
842 * Find and configure the best data interface and NAND timings supported by
843 * the chip and the driver.
844 * First tries to retrieve supported timing modes from ONFI information,
845 * and if the NAND chip does not support ONFI, relies on the
846 * ->onfi_timing_mode_default specified in the nand_ids table.
847 *
848 * Returns 0 for success or negative error code otherwise.
849 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100850static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200851{
Miquel Raynal97baea12018-03-19 14:47:20 +0100852 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
853 chip->onfi_timing_mode_default,
854 };
Boris Brezillond8e725d2016-09-15 10:32:50 +0200855 int ret;
856
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100857 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200858 return 0;
859
Miquel Raynal993447b2018-03-19 14:47:21 +0100860 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +0100861 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100862 nand_select_target(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +0100863 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
864 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100865 nand_deselect_target(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200866 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +0100867 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +0200868 }
869
Miquel Raynal97baea12018-03-19 14:47:20 +0100870 /* Change the mode on the controller side */
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100871 ret = chip->controller->ops->setup_data_interface(chip, chipnr,
872 &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +0100873 if (ret)
874 return ret;
875
876 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +0100877 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +0100878 return 0;
879
880 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon1d017852018-11-11 08:55:14 +0100881 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100882 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
883 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100884 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100885 if (ret)
886 goto err_reset_chip;
887
888 if (tmode_param[0] != chip->onfi_timing_mode_default) {
889 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
890 chip->onfi_timing_mode_default);
891 goto err_reset_chip;
892 }
893
894 return 0;
895
896err_reset_chip:
897 /*
898 * Fallback to mode 0 if the chip explicitly did not ack the chosen
899 * timing mode.
900 */
901 nand_reset_data_interface(chip, chipnr);
Boris Brezillon1d017852018-11-11 08:55:14 +0100902 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100903 nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +0100904 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100905
Boris Brezillond8e725d2016-09-15 10:32:50 +0200906 return ret;
907}
908
909/**
910 * nand_init_data_interface - find the best data interface and timings
911 * @chip: The NAND chip
912 *
913 * Find the best data interface and NAND timings supported by the chip
914 * and the driver.
915 * First tries to retrieve supported timing modes from ONFI information,
916 * and if the NAND chip does not support ONFI, relies on the
917 * ->onfi_timing_mode_default specified in the nand_ids table. After this
918 * function nand_chip->data_interface is initialized with the best timing mode
919 * available.
920 *
921 * Returns 0 for success or negative error code otherwise.
922 */
923static int nand_init_data_interface(struct nand_chip *chip)
924{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200925 int modes, mode, ret;
926
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100927 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200928 return 0;
929
930 /*
931 * First try to identify the best timings from ONFI parameters and
932 * if the NAND does not support ONFI, fallback to the default ONFI
933 * timing mode.
934 */
Boris Brezillon462f35d2018-09-07 00:38:47 +0200935 if (chip->parameters.onfi) {
936 modes = chip->parameters.onfi->async_timing_mode;
937 } else {
Boris Brezillond8e725d2016-09-15 10:32:50 +0200938 if (!chip->onfi_timing_mode_default)
939 return 0;
940
941 modes = GENMASK(chip->onfi_timing_mode_default, 0);
942 }
943
Boris Brezillond8e725d2016-09-15 10:32:50 +0200944 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +0100945 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200946 if (ret)
947 continue;
948
Miquel Raynald787b8b2017-12-22 18:12:41 +0100949 /*
950 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
951 * controller supports the requested timings.
952 */
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100953 ret = chip->controller->ops->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +0100954 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +0100955 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200956 if (!ret) {
957 chip->onfi_timing_mode_default = mode;
958 break;
959 }
960 }
961
962 return 0;
963}
964
Boris Brezillond8e725d2016-09-15 10:32:50 +0200965/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100966 * nand_fill_column_cycles - fill the column cycles of an address
967 * @chip: The NAND chip
968 * @addrs: Array of address cycles to fill
969 * @offset_in_page: The offset in the page
970 *
971 * Fills the first or the first two bytes of the @addrs field depending
972 * on the NAND bus width and the page size.
973 *
974 * Returns the number of cycles needed to encode the column, or a negative
975 * error code in case one of the arguments is invalid.
976 */
977static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
978 unsigned int offset_in_page)
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100979{
Miquel Raynal8878b122017-11-09 14:16:45 +0100980 struct mtd_info *mtd = nand_to_mtd(chip);
981
982 /* Make sure the offset is less than the actual page size. */
983 if (offset_in_page > mtd->writesize + mtd->oobsize)
984 return -EINVAL;
985
986 /*
987 * On small page NANDs, there's a dedicated command to access the OOB
988 * area, and the column address is relative to the start of the OOB
989 * area, not the start of the page. Asjust the address accordingly.
990 */
991 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
992 offset_in_page -= mtd->writesize;
993
994 /*
995 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
996 * wide, then it must be divided by 2.
997 */
998 if (chip->options & NAND_BUSWIDTH_16) {
999 if (WARN_ON(offset_in_page % 2))
1000 return -EINVAL;
1001
1002 offset_in_page /= 2;
1003 }
1004
1005 addrs[0] = offset_in_page;
1006
1007 /*
1008 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1009 * need 2
1010 */
1011 if (mtd->writesize <= 512)
1012 return 1;
1013
1014 addrs[1] = offset_in_page >> 8;
1015
1016 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Miquel Raynal8878b122017-11-09 14:16:45 +01001019static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1020 unsigned int offset_in_page, void *buf,
1021 unsigned int len)
1022{
1023 struct mtd_info *mtd = nand_to_mtd(chip);
1024 const struct nand_sdr_timings *sdr =
1025 nand_get_sdr_timings(&chip->data_interface);
1026 u8 addrs[4];
1027 struct nand_op_instr instrs[] = {
1028 NAND_OP_CMD(NAND_CMD_READ0, 0),
1029 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1030 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1031 PSEC_TO_NSEC(sdr->tRR_min)),
1032 NAND_OP_DATA_IN(len, buf, 0),
1033 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001034 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001035 int ret;
1036
1037 /* Drop the DATA_IN instruction if len is set to 0. */
1038 if (!len)
1039 op.ninstrs--;
1040
1041 if (offset_in_page >= mtd->writesize)
1042 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1043 else if (offset_in_page >= 256 &&
1044 !(chip->options & NAND_BUSWIDTH_16))
1045 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1046
1047 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1048 if (ret < 0)
1049 return ret;
1050
1051 addrs[1] = page;
1052 addrs[2] = page >> 8;
1053
1054 if (chip->options & NAND_ROW_ADDR_3) {
1055 addrs[3] = page >> 16;
1056 instrs[1].ctx.addr.naddrs++;
1057 }
1058
1059 return nand_exec_op(chip, &op);
1060}
1061
1062static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1063 unsigned int offset_in_page, void *buf,
1064 unsigned int len)
1065{
1066 const struct nand_sdr_timings *sdr =
1067 nand_get_sdr_timings(&chip->data_interface);
1068 u8 addrs[5];
1069 struct nand_op_instr instrs[] = {
1070 NAND_OP_CMD(NAND_CMD_READ0, 0),
1071 NAND_OP_ADDR(4, addrs, 0),
1072 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1073 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1074 PSEC_TO_NSEC(sdr->tRR_min)),
1075 NAND_OP_DATA_IN(len, buf, 0),
1076 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001077 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001078 int ret;
1079
1080 /* Drop the DATA_IN instruction if len is set to 0. */
1081 if (!len)
1082 op.ninstrs--;
1083
1084 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1085 if (ret < 0)
1086 return ret;
1087
1088 addrs[2] = page;
1089 addrs[3] = page >> 8;
1090
1091 if (chip->options & NAND_ROW_ADDR_3) {
1092 addrs[4] = page >> 16;
1093 instrs[1].ctx.addr.naddrs++;
1094 }
1095
1096 return nand_exec_op(chip, &op);
1097}
1098
1099/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001100 * nand_read_page_op - Do a READ PAGE operation
1101 * @chip: The NAND chip
1102 * @page: page to read
1103 * @offset_in_page: offset within the page
1104 * @buf: buffer used to store the data
1105 * @len: length of the buffer
1106 *
1107 * This function issues a READ PAGE operation.
1108 * This function does not select/unselect the CS line.
1109 *
1110 * Returns 0 on success, a negative error code otherwise.
1111 */
1112int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1113 unsigned int offset_in_page, void *buf, unsigned int len)
1114{
1115 struct mtd_info *mtd = nand_to_mtd(chip);
1116
1117 if (len && !buf)
1118 return -EINVAL;
1119
1120 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1121 return -EINVAL;
1122
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001123 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001124 if (mtd->writesize > 512)
1125 return nand_lp_exec_read_page_op(chip, page,
1126 offset_in_page, buf,
1127 len);
1128
1129 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1130 buf, len);
1131 }
1132
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001133 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001134 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001135 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001136
1137 return 0;
1138}
1139EXPORT_SYMBOL_GPL(nand_read_page_op);
1140
1141/**
1142 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1143 * @chip: The NAND chip
1144 * @page: parameter page to read
1145 * @buf: buffer used to store the data
1146 * @len: length of the buffer
1147 *
1148 * This function issues a READ PARAMETER PAGE operation.
1149 * This function does not select/unselect the CS line.
1150 *
1151 * Returns 0 on success, a negative error code otherwise.
1152 */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02001153int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1154 unsigned int len)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001155{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001156 unsigned int i;
1157 u8 *p = buf;
1158
1159 if (len && !buf)
1160 return -EINVAL;
1161
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001162 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001163 const struct nand_sdr_timings *sdr =
1164 nand_get_sdr_timings(&chip->data_interface);
1165 struct nand_op_instr instrs[] = {
1166 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1167 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1168 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1169 PSEC_TO_NSEC(sdr->tRR_min)),
1170 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1171 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001172 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001173
1174 /* Drop the DATA_IN instruction if len is set to 0. */
1175 if (!len)
1176 op.ninstrs--;
1177
1178 return nand_exec_op(chip, &op);
1179 }
1180
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001181 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001182 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001183 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001184
1185 return 0;
1186}
1187
1188/**
1189 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1190 * @chip: The NAND chip
1191 * @offset_in_page: offset within the page
1192 * @buf: buffer used to store the data
1193 * @len: length of the buffer
1194 * @force_8bit: force 8-bit bus access
1195 *
1196 * This function issues a CHANGE READ COLUMN operation.
1197 * This function does not select/unselect the CS line.
1198 *
1199 * Returns 0 on success, a negative error code otherwise.
1200 */
1201int nand_change_read_column_op(struct nand_chip *chip,
1202 unsigned int offset_in_page, void *buf,
1203 unsigned int len, bool force_8bit)
1204{
1205 struct mtd_info *mtd = nand_to_mtd(chip);
1206
1207 if (len && !buf)
1208 return -EINVAL;
1209
1210 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1211 return -EINVAL;
1212
Miquel Raynal8878b122017-11-09 14:16:45 +01001213 /* Small page NANDs do not support column change. */
1214 if (mtd->writesize <= 512)
1215 return -ENOTSUPP;
1216
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001217 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001218 const struct nand_sdr_timings *sdr =
1219 nand_get_sdr_timings(&chip->data_interface);
1220 u8 addrs[2] = {};
1221 struct nand_op_instr instrs[] = {
1222 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1223 NAND_OP_ADDR(2, addrs, 0),
1224 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1225 PSEC_TO_NSEC(sdr->tCCS_min)),
1226 NAND_OP_DATA_IN(len, buf, 0),
1227 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001228 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001229 int ret;
1230
1231 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1232 if (ret < 0)
1233 return ret;
1234
1235 /* Drop the DATA_IN instruction if len is set to 0. */
1236 if (!len)
1237 op.ninstrs--;
1238
1239 instrs[3].ctx.data.force_8bit = force_8bit;
1240
1241 return nand_exec_op(chip, &op);
1242 }
1243
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001244 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001245 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001246 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001247
1248 return 0;
1249}
1250EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1251
1252/**
1253 * nand_read_oob_op - Do a READ OOB operation
1254 * @chip: The NAND chip
1255 * @page: page to read
1256 * @offset_in_oob: offset within the OOB area
1257 * @buf: buffer used to store the data
1258 * @len: length of the buffer
1259 *
1260 * This function issues a READ OOB operation.
1261 * This function does not select/unselect the CS line.
1262 *
1263 * Returns 0 on success, a negative error code otherwise.
1264 */
1265int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1266 unsigned int offset_in_oob, void *buf, unsigned int len)
1267{
1268 struct mtd_info *mtd = nand_to_mtd(chip);
1269
1270 if (len && !buf)
1271 return -EINVAL;
1272
1273 if (offset_in_oob + len > mtd->oobsize)
1274 return -EINVAL;
1275
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001276 if (nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +01001277 return nand_read_page_op(chip, page,
1278 mtd->writesize + offset_in_oob,
1279 buf, len);
1280
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001281 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001282 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001283 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001284
1285 return 0;
1286}
1287EXPORT_SYMBOL_GPL(nand_read_oob_op);
1288
Miquel Raynal8878b122017-11-09 14:16:45 +01001289static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1290 unsigned int offset_in_page, const void *buf,
1291 unsigned int len, bool prog)
1292{
1293 struct mtd_info *mtd = nand_to_mtd(chip);
1294 const struct nand_sdr_timings *sdr =
1295 nand_get_sdr_timings(&chip->data_interface);
1296 u8 addrs[5] = {};
1297 struct nand_op_instr instrs[] = {
1298 /*
1299 * The first instruction will be dropped if we're dealing
1300 * with a large page NAND and adjusted if we're dealing
1301 * with a small page NAND and the page offset is > 255.
1302 */
1303 NAND_OP_CMD(NAND_CMD_READ0, 0),
1304 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1305 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1306 NAND_OP_DATA_OUT(len, buf, 0),
1307 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1308 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1309 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001310 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001311 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1312 int ret;
1313 u8 status;
1314
1315 if (naddrs < 0)
1316 return naddrs;
1317
1318 addrs[naddrs++] = page;
1319 addrs[naddrs++] = page >> 8;
1320 if (chip->options & NAND_ROW_ADDR_3)
1321 addrs[naddrs++] = page >> 16;
1322
1323 instrs[2].ctx.addr.naddrs = naddrs;
1324
1325 /* Drop the last two instructions if we're not programming the page. */
1326 if (!prog) {
1327 op.ninstrs -= 2;
1328 /* Also drop the DATA_OUT instruction if empty. */
1329 if (!len)
1330 op.ninstrs--;
1331 }
1332
1333 if (mtd->writesize <= 512) {
1334 /*
1335 * Small pages need some more tweaking: we have to adjust the
1336 * first instruction depending on the page offset we're trying
1337 * to access.
1338 */
1339 if (offset_in_page >= mtd->writesize)
1340 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1341 else if (offset_in_page >= 256 &&
1342 !(chip->options & NAND_BUSWIDTH_16))
1343 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1344 } else {
1345 /*
1346 * Drop the first command if we're dealing with a large page
1347 * NAND.
1348 */
1349 op.instrs++;
1350 op.ninstrs--;
1351 }
1352
1353 ret = nand_exec_op(chip, &op);
1354 if (!prog || ret)
1355 return ret;
1356
1357 ret = nand_status_op(chip, &status);
1358 if (ret)
1359 return ret;
1360
1361 return status;
1362}
1363
Boris Brezillon97d90da2017-11-30 18:01:29 +01001364/**
1365 * nand_prog_page_begin_op - starts a PROG PAGE operation
1366 * @chip: The NAND chip
1367 * @page: page to write
1368 * @offset_in_page: offset within the page
1369 * @buf: buffer containing the data to write to the page
1370 * @len: length of the buffer
1371 *
1372 * This function issues the first half of a PROG PAGE operation.
1373 * This function does not select/unselect the CS line.
1374 *
1375 * Returns 0 on success, a negative error code otherwise.
1376 */
1377int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1378 unsigned int offset_in_page, const void *buf,
1379 unsigned int len)
1380{
1381 struct mtd_info *mtd = nand_to_mtd(chip);
1382
1383 if (len && !buf)
1384 return -EINVAL;
1385
1386 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1387 return -EINVAL;
1388
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001389 if (nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +01001390 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1391 len, false);
1392
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001393 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001394
1395 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001396 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001397
1398 return 0;
1399}
1400EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1401
1402/**
1403 * nand_prog_page_end_op - ends a PROG PAGE operation
1404 * @chip: The NAND chip
1405 *
1406 * This function issues the second half of a PROG PAGE operation.
1407 * This function does not select/unselect the CS line.
1408 *
1409 * Returns 0 on success, a negative error code otherwise.
1410 */
1411int nand_prog_page_end_op(struct nand_chip *chip)
1412{
Miquel Raynal8878b122017-11-09 14:16:45 +01001413 int ret;
1414 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001415
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001416 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001417 const struct nand_sdr_timings *sdr =
1418 nand_get_sdr_timings(&chip->data_interface);
1419 struct nand_op_instr instrs[] = {
1420 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1421 PSEC_TO_NSEC(sdr->tWB_max)),
1422 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1423 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001424 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001425
Miquel Raynal8878b122017-11-09 14:16:45 +01001426 ret = nand_exec_op(chip, &op);
1427 if (ret)
1428 return ret;
1429
1430 ret = nand_status_op(chip, &status);
1431 if (ret)
1432 return ret;
1433 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001434 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001435 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001436 if (ret < 0)
1437 return ret;
1438
1439 status = ret;
1440 }
1441
Boris Brezillon97d90da2017-11-30 18:01:29 +01001442 if (status & NAND_STATUS_FAIL)
1443 return -EIO;
1444
1445 return 0;
1446}
1447EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1448
1449/**
1450 * nand_prog_page_op - Do a full PROG PAGE operation
1451 * @chip: The NAND chip
1452 * @page: page to write
1453 * @offset_in_page: offset within the page
1454 * @buf: buffer containing the data to write to the page
1455 * @len: length of the buffer
1456 *
1457 * This function issues a full PROG PAGE operation.
1458 * This function does not select/unselect the CS line.
1459 *
1460 * Returns 0 on success, a negative error code otherwise.
1461 */
1462int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1463 unsigned int offset_in_page, const void *buf,
1464 unsigned int len)
1465{
1466 struct mtd_info *mtd = nand_to_mtd(chip);
1467 int status;
1468
1469 if (!len || !buf)
1470 return -EINVAL;
1471
1472 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1473 return -EINVAL;
1474
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001475 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001476 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1477 len, true);
1478 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001479 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1480 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001481 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001482 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001483 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001484 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001485
Boris Brezillon97d90da2017-11-30 18:01:29 +01001486 if (status & NAND_STATUS_FAIL)
1487 return -EIO;
1488
1489 return 0;
1490}
1491EXPORT_SYMBOL_GPL(nand_prog_page_op);
1492
1493/**
1494 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1495 * @chip: The NAND chip
1496 * @offset_in_page: offset within the page
1497 * @buf: buffer containing the data to send to the NAND
1498 * @len: length of the buffer
1499 * @force_8bit: force 8-bit bus access
1500 *
1501 * This function issues a CHANGE WRITE COLUMN operation.
1502 * This function does not select/unselect the CS line.
1503 *
1504 * Returns 0 on success, a negative error code otherwise.
1505 */
1506int nand_change_write_column_op(struct nand_chip *chip,
1507 unsigned int offset_in_page,
1508 const void *buf, unsigned int len,
1509 bool force_8bit)
1510{
1511 struct mtd_info *mtd = nand_to_mtd(chip);
1512
1513 if (len && !buf)
1514 return -EINVAL;
1515
1516 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1517 return -EINVAL;
1518
Miquel Raynal8878b122017-11-09 14:16:45 +01001519 /* Small page NANDs do not support column change. */
1520 if (mtd->writesize <= 512)
1521 return -ENOTSUPP;
1522
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001523 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001524 const struct nand_sdr_timings *sdr =
1525 nand_get_sdr_timings(&chip->data_interface);
1526 u8 addrs[2];
1527 struct nand_op_instr instrs[] = {
1528 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1529 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1530 NAND_OP_DATA_OUT(len, buf, 0),
1531 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001532 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001533 int ret;
1534
1535 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1536 if (ret < 0)
1537 return ret;
1538
1539 instrs[2].ctx.data.force_8bit = force_8bit;
1540
1541 /* Drop the DATA_OUT instruction if len is set to 0. */
1542 if (!len)
1543 op.ninstrs--;
1544
1545 return nand_exec_op(chip, &op);
1546 }
1547
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001548 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001549 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001550 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001551
1552 return 0;
1553}
1554EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1555
1556/**
1557 * nand_readid_op - Do a READID operation
1558 * @chip: The NAND chip
1559 * @addr: address cycle to pass after the READID command
1560 * @buf: buffer used to store the ID
1561 * @len: length of the buffer
1562 *
1563 * This function sends a READID command and reads back the ID returned by the
1564 * NAND.
1565 * This function does not select/unselect the CS line.
1566 *
1567 * Returns 0 on success, a negative error code otherwise.
1568 */
1569int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1570 unsigned int len)
1571{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001572 unsigned int i;
1573 u8 *id = buf;
1574
1575 if (len && !buf)
1576 return -EINVAL;
1577
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001578 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001579 const struct nand_sdr_timings *sdr =
1580 nand_get_sdr_timings(&chip->data_interface);
1581 struct nand_op_instr instrs[] = {
1582 NAND_OP_CMD(NAND_CMD_READID, 0),
1583 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1584 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1585 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001586 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001587
1588 /* Drop the DATA_IN instruction if len is set to 0. */
1589 if (!len)
1590 op.ninstrs--;
1591
1592 return nand_exec_op(chip, &op);
1593 }
1594
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001595 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001596
1597 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001598 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001599
1600 return 0;
1601}
1602EXPORT_SYMBOL_GPL(nand_readid_op);
1603
1604/**
1605 * nand_status_op - Do a STATUS operation
1606 * @chip: The NAND chip
1607 * @status: out variable to store the NAND status
1608 *
1609 * This function sends a STATUS command and reads back the status returned by
1610 * the NAND.
1611 * This function does not select/unselect the CS line.
1612 *
1613 * Returns 0 on success, a negative error code otherwise.
1614 */
1615int nand_status_op(struct nand_chip *chip, u8 *status)
1616{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001617 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001618 const struct nand_sdr_timings *sdr =
1619 nand_get_sdr_timings(&chip->data_interface);
1620 struct nand_op_instr instrs[] = {
1621 NAND_OP_CMD(NAND_CMD_STATUS,
1622 PSEC_TO_NSEC(sdr->tADL_min)),
1623 NAND_OP_8BIT_DATA_IN(1, status, 0),
1624 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001625 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001626
1627 if (!status)
1628 op.ninstrs--;
1629
1630 return nand_exec_op(chip, &op);
1631 }
1632
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001633 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001634 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001635 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001636
1637 return 0;
1638}
1639EXPORT_SYMBOL_GPL(nand_status_op);
1640
1641/**
1642 * nand_exit_status_op - Exit a STATUS operation
1643 * @chip: The NAND chip
1644 *
1645 * This function sends a READ0 command to cancel the effect of the STATUS
1646 * command to avoid reading only the status until a new read command is sent.
1647 *
1648 * This function does not select/unselect the CS line.
1649 *
1650 * Returns 0 on success, a negative error code otherwise.
1651 */
1652int nand_exit_status_op(struct nand_chip *chip)
1653{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001654 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001655 struct nand_op_instr instrs[] = {
1656 NAND_OP_CMD(NAND_CMD_READ0, 0),
1657 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001658 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001659
1660 return nand_exec_op(chip, &op);
1661 }
1662
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001663 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001664
1665 return 0;
1666}
Boris Brezillon97d90da2017-11-30 18:01:29 +01001667
1668/**
1669 * nand_erase_op - Do an erase operation
1670 * @chip: The NAND chip
1671 * @eraseblock: block to erase
1672 *
1673 * This function sends an ERASE command and waits for the NAND to be ready
1674 * before returning.
1675 * This function does not select/unselect the CS line.
1676 *
1677 * Returns 0 on success, a negative error code otherwise.
1678 */
1679int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1680{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001681 unsigned int page = eraseblock <<
1682 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01001683 int ret;
1684 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001685
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001686 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001687 const struct nand_sdr_timings *sdr =
1688 nand_get_sdr_timings(&chip->data_interface);
1689 u8 addrs[3] = { page, page >> 8, page >> 16 };
1690 struct nand_op_instr instrs[] = {
1691 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
1692 NAND_OP_ADDR(2, addrs, 0),
1693 NAND_OP_CMD(NAND_CMD_ERASE2,
1694 PSEC_TO_MSEC(sdr->tWB_max)),
1695 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
1696 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001697 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001698
Miquel Raynal8878b122017-11-09 14:16:45 +01001699 if (chip->options & NAND_ROW_ADDR_3)
1700 instrs[1].ctx.addr.naddrs++;
1701
1702 ret = nand_exec_op(chip, &op);
1703 if (ret)
1704 return ret;
1705
1706 ret = nand_status_op(chip, &status);
1707 if (ret)
1708 return ret;
1709 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001710 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
1711 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01001712
Boris Brezillon8395b752018-09-07 00:38:37 +02001713 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001714 if (ret < 0)
1715 return ret;
1716
1717 status = ret;
1718 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001719
1720 if (status & NAND_STATUS_FAIL)
1721 return -EIO;
1722
1723 return 0;
1724}
1725EXPORT_SYMBOL_GPL(nand_erase_op);
1726
1727/**
1728 * nand_set_features_op - Do a SET FEATURES operation
1729 * @chip: The NAND chip
1730 * @feature: feature id
1731 * @data: 4 bytes of data
1732 *
1733 * This function sends a SET FEATURES command and waits for the NAND to be
1734 * ready before returning.
1735 * This function does not select/unselect the CS line.
1736 *
1737 * Returns 0 on success, a negative error code otherwise.
1738 */
1739static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1740 const void *data)
1741{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001742 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01001743 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001744
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001745 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001746 const struct nand_sdr_timings *sdr =
1747 nand_get_sdr_timings(&chip->data_interface);
1748 struct nand_op_instr instrs[] = {
1749 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
1750 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
1751 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
1752 PSEC_TO_NSEC(sdr->tWB_max)),
1753 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
1754 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001755 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001756
Boris Brezillon782d1962018-05-11 14:44:07 +02001757 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01001758 }
1759
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001760 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02001761 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001762 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02001763
Boris Brezillon8395b752018-09-07 00:38:37 +02001764 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02001765 if (ret < 0)
1766 return ret;
1767
1768 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001769 return -EIO;
1770
1771 return 0;
1772}
1773
1774/**
1775 * nand_get_features_op - Do a GET FEATURES operation
1776 * @chip: The NAND chip
1777 * @feature: feature id
1778 * @data: 4 bytes of data
1779 *
1780 * This function sends a GET FEATURES command and waits for the NAND to be
1781 * ready before returning.
1782 * This function does not select/unselect the CS line.
1783 *
1784 * Returns 0 on success, a negative error code otherwise.
1785 */
1786static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1787 void *data)
1788{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001789 u8 *params = data;
1790 int i;
1791
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001792 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001793 const struct nand_sdr_timings *sdr =
1794 nand_get_sdr_timings(&chip->data_interface);
1795 struct nand_op_instr instrs[] = {
1796 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
1797 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
1798 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
1799 PSEC_TO_NSEC(sdr->tRR_min)),
1800 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
1801 data, 0),
1802 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001803 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001804
1805 return nand_exec_op(chip, &op);
1806 }
1807
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001808 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001809 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001810 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001811
1812 return 0;
1813}
1814
Boris Brezillon52f05b62018-07-27 09:44:18 +02001815static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
1816 unsigned int delay_ns)
1817{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001818 if (nand_has_exec_op(chip)) {
Boris Brezillon52f05b62018-07-27 09:44:18 +02001819 struct nand_op_instr instrs[] = {
1820 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
1821 PSEC_TO_NSEC(delay_ns)),
1822 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001823 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001824
1825 return nand_exec_op(chip, &op);
1826 }
1827
1828 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02001829 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001830 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001831 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001832 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001833
1834 return 0;
1835}
1836
Boris Brezillon97d90da2017-11-30 18:01:29 +01001837/**
1838 * nand_reset_op - Do a reset operation
1839 * @chip: The NAND chip
1840 *
1841 * This function sends a RESET command and waits for the NAND to be ready
1842 * before returning.
1843 * This function does not select/unselect the CS line.
1844 *
1845 * Returns 0 on success, a negative error code otherwise.
1846 */
1847int nand_reset_op(struct nand_chip *chip)
1848{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001849 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001850 const struct nand_sdr_timings *sdr =
1851 nand_get_sdr_timings(&chip->data_interface);
1852 struct nand_op_instr instrs[] = {
1853 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
1854 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
1855 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001856 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001857
1858 return nand_exec_op(chip, &op);
1859 }
1860
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001861 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001862
1863 return 0;
1864}
1865EXPORT_SYMBOL_GPL(nand_reset_op);
1866
1867/**
1868 * nand_read_data_op - Read data from the NAND
1869 * @chip: The NAND chip
1870 * @buf: buffer used to store the data
1871 * @len: length of the buffer
1872 * @force_8bit: force 8-bit bus access
1873 *
1874 * This function does a raw data read on the bus. Usually used after launching
1875 * another NAND operation like nand_read_page_op().
1876 * This function does not select/unselect the CS line.
1877 *
1878 * Returns 0 on success, a negative error code otherwise.
1879 */
1880int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1881 bool force_8bit)
1882{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001883 if (!len || !buf)
1884 return -EINVAL;
1885
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001886 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001887 struct nand_op_instr instrs[] = {
1888 NAND_OP_DATA_IN(len, buf, 0),
1889 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001890 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001891
1892 instrs[0].ctx.data.force_8bit = force_8bit;
1893
1894 return nand_exec_op(chip, &op);
1895 }
1896
Boris Brezillon97d90da2017-11-30 18:01:29 +01001897 if (force_8bit) {
1898 u8 *p = buf;
1899 unsigned int i;
1900
1901 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001902 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001903 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001904 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001905 }
1906
1907 return 0;
1908}
1909EXPORT_SYMBOL_GPL(nand_read_data_op);
1910
1911/**
1912 * nand_write_data_op - Write data from the NAND
1913 * @chip: The NAND chip
1914 * @buf: buffer containing the data to send on the bus
1915 * @len: length of the buffer
1916 * @force_8bit: force 8-bit bus access
1917 *
1918 * This function does a raw data write on the bus. Usually used after launching
1919 * another NAND operation like nand_write_page_begin_op().
1920 * This function does not select/unselect the CS line.
1921 *
1922 * Returns 0 on success, a negative error code otherwise.
1923 */
1924int nand_write_data_op(struct nand_chip *chip, const void *buf,
1925 unsigned int len, bool force_8bit)
1926{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001927 if (!len || !buf)
1928 return -EINVAL;
1929
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001930 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001931 struct nand_op_instr instrs[] = {
1932 NAND_OP_DATA_OUT(len, buf, 0),
1933 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001934 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001935
1936 instrs[0].ctx.data.force_8bit = force_8bit;
1937
1938 return nand_exec_op(chip, &op);
1939 }
1940
Boris Brezillon97d90da2017-11-30 18:01:29 +01001941 if (force_8bit) {
1942 const u8 *p = buf;
1943 unsigned int i;
1944
1945 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001946 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001947 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001948 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001949 }
1950
1951 return 0;
1952}
1953EXPORT_SYMBOL_GPL(nand_write_data_op);
1954
1955/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001956 * struct nand_op_parser_ctx - Context used by the parser
1957 * @instrs: array of all the instructions that must be addressed
1958 * @ninstrs: length of the @instrs array
1959 * @subop: Sub-operation to be passed to the NAND controller
1960 *
1961 * This structure is used by the core to split NAND operations into
1962 * sub-operations that can be handled by the NAND controller.
1963 */
1964struct nand_op_parser_ctx {
1965 const struct nand_op_instr *instrs;
1966 unsigned int ninstrs;
1967 struct nand_subop subop;
1968};
1969
1970/**
1971 * nand_op_parser_must_split_instr - Checks if an instruction must be split
1972 * @pat: the parser pattern element that matches @instr
1973 * @instr: pointer to the instruction to check
1974 * @start_offset: this is an in/out parameter. If @instr has already been
1975 * split, then @start_offset is the offset from which to start
1976 * (either an address cycle or an offset in the data buffer).
1977 * Conversely, if the function returns true (ie. instr must be
1978 * split), this parameter is updated to point to the first
1979 * data/address cycle that has not been taken care of.
1980 *
1981 * Some NAND controllers are limited and cannot send X address cycles with a
1982 * unique operation, or cannot read/write more than Y bytes at the same time.
1983 * In this case, split the instruction that does not fit in a single
1984 * controller-operation into two or more chunks.
1985 *
1986 * Returns true if the instruction must be split, false otherwise.
1987 * The @start_offset parameter is also updated to the offset at which the next
1988 * bundle of instruction must start (if an address or a data instruction).
1989 */
1990static bool
1991nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
1992 const struct nand_op_instr *instr,
1993 unsigned int *start_offset)
1994{
1995 switch (pat->type) {
1996 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001997 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01001998 break;
1999
2000 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002001 pat->ctx.addr.maxcycles) {
2002 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002003 return true;
2004 }
2005 break;
2006
2007 case NAND_OP_DATA_IN_INSTR:
2008 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002009 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002010 break;
2011
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002012 if (instr->ctx.data.len - *start_offset >
2013 pat->ctx.data.maxlen) {
2014 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002015 return true;
2016 }
2017 break;
2018
2019 default:
2020 break;
2021 }
2022
2023 return false;
2024}
2025
2026/**
2027 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2028 * remaining in the parser context
2029 * @pat: the pattern to test
2030 * @ctx: the parser context structure to match with the pattern @pat
2031 *
2032 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2033 * Returns true if this is the case, false ortherwise. When true is returned,
2034 * @ctx->subop is updated with the set of instructions to be passed to the
2035 * controller driver.
2036 */
2037static bool
2038nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2039 struct nand_op_parser_ctx *ctx)
2040{
2041 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2042 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2043 const struct nand_op_instr *instr = ctx->subop.instrs;
2044 unsigned int i, ninstrs;
2045
2046 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2047 /*
2048 * The pattern instruction does not match the operation
2049 * instruction. If the instruction is marked optional in the
2050 * pattern definition, we skip the pattern element and continue
2051 * to the next one. If the element is mandatory, there's no
2052 * match and we can return false directly.
2053 */
2054 if (instr->type != pat->elems[i].type) {
2055 if (!pat->elems[i].optional)
2056 return false;
2057
2058 continue;
2059 }
2060
2061 /*
2062 * Now check the pattern element constraints. If the pattern is
2063 * not able to handle the whole instruction in a single step,
2064 * we have to split it.
2065 * The last_instr_end_off value comes back updated to point to
2066 * the position where we have to split the instruction (the
2067 * start of the next subop chunk).
2068 */
2069 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2070 &instr_offset)) {
2071 ninstrs++;
2072 i++;
2073 break;
2074 }
2075
2076 instr++;
2077 ninstrs++;
2078 instr_offset = 0;
2079 }
2080
2081 /*
2082 * This can happen if all instructions of a pattern are optional.
2083 * Still, if there's not at least one instruction handled by this
2084 * pattern, this is not a match, and we should try the next one (if
2085 * any).
2086 */
2087 if (!ninstrs)
2088 return false;
2089
2090 /*
2091 * We had a match on the pattern head, but the pattern may be longer
2092 * than the instructions we're asked to execute. We need to make sure
2093 * there's no mandatory elements in the pattern tail.
2094 */
2095 for (; i < pat->nelems; i++) {
2096 if (!pat->elems[i].optional)
2097 return false;
2098 }
2099
2100 /*
2101 * We have a match: update the subop structure accordingly and return
2102 * true.
2103 */
2104 ctx->subop.ninstrs = ninstrs;
2105 ctx->subop.last_instr_end_off = instr_offset;
2106
2107 return true;
2108}
2109
2110#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2111static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2112{
2113 const struct nand_op_instr *instr;
2114 char *prefix = " ";
2115 unsigned int i;
2116
2117 pr_debug("executing subop:\n");
2118
2119 for (i = 0; i < ctx->ninstrs; i++) {
2120 instr = &ctx->instrs[i];
2121
2122 if (instr == &ctx->subop.instrs[0])
2123 prefix = " ->";
2124
2125 switch (instr->type) {
2126 case NAND_OP_CMD_INSTR:
2127 pr_debug("%sCMD [0x%02x]\n", prefix,
2128 instr->ctx.cmd.opcode);
2129 break;
2130 case NAND_OP_ADDR_INSTR:
2131 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2132 instr->ctx.addr.naddrs,
2133 instr->ctx.addr.naddrs < 64 ?
2134 instr->ctx.addr.naddrs : 64,
2135 instr->ctx.addr.addrs);
2136 break;
2137 case NAND_OP_DATA_IN_INSTR:
2138 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2139 instr->ctx.data.len,
2140 instr->ctx.data.force_8bit ?
2141 ", force 8-bit" : "");
2142 break;
2143 case NAND_OP_DATA_OUT_INSTR:
2144 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2145 instr->ctx.data.len,
2146 instr->ctx.data.force_8bit ?
2147 ", force 8-bit" : "");
2148 break;
2149 case NAND_OP_WAITRDY_INSTR:
2150 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2151 instr->ctx.waitrdy.timeout_ms);
2152 break;
2153 }
2154
2155 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2156 prefix = " ";
2157 }
2158}
2159#else
2160static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2161{
2162 /* NOP */
2163}
2164#endif
2165
2166/**
2167 * nand_op_parser_exec_op - exec_op parser
2168 * @chip: the NAND chip
2169 * @parser: patterns description provided by the controller driver
2170 * @op: the NAND operation to address
2171 * @check_only: when true, the function only checks if @op can be handled but
2172 * does not execute the operation
2173 *
2174 * Helper function designed to ease integration of NAND controller drivers that
2175 * only support a limited set of instruction sequences. The supported sequences
2176 * are described in @parser, and the framework takes care of splitting @op into
2177 * multiple sub-operations (if required) and pass them back to the ->exec()
2178 * callback of the matching pattern if @check_only is set to false.
2179 *
2180 * NAND controller drivers should call this function from their own ->exec_op()
2181 * implementation.
2182 *
2183 * Returns 0 on success, a negative error code otherwise. A failure can be
2184 * caused by an unsupported operation (none of the supported patterns is able
2185 * to handle the requested operation), or an error returned by one of the
2186 * matching pattern->exec() hook.
2187 */
2188int nand_op_parser_exec_op(struct nand_chip *chip,
2189 const struct nand_op_parser *parser,
2190 const struct nand_operation *op, bool check_only)
2191{
2192 struct nand_op_parser_ctx ctx = {
2193 .subop.instrs = op->instrs,
2194 .instrs = op->instrs,
2195 .ninstrs = op->ninstrs,
2196 };
2197 unsigned int i;
2198
2199 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2200 int ret;
2201
2202 for (i = 0; i < parser->npatterns; i++) {
2203 const struct nand_op_parser_pattern *pattern;
2204
2205 pattern = &parser->patterns[i];
2206 if (!nand_op_parser_match_pat(pattern, &ctx))
2207 continue;
2208
2209 nand_op_parser_trace(&ctx);
2210
2211 if (check_only)
2212 break;
2213
2214 ret = pattern->exec(chip, &ctx.subop);
2215 if (ret)
2216 return ret;
2217
2218 break;
2219 }
2220
2221 if (i == parser->npatterns) {
2222 pr_debug("->exec_op() parser: pattern not found!\n");
2223 return -ENOTSUPP;
2224 }
2225
2226 /*
2227 * Update the context structure by pointing to the start of the
2228 * next subop.
2229 */
2230 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2231 if (ctx.subop.last_instr_end_off)
2232 ctx.subop.instrs -= 1;
2233
2234 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2235 }
2236
2237 return 0;
2238}
2239EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2240
2241static bool nand_instr_is_data(const struct nand_op_instr *instr)
2242{
2243 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2244 instr->type == NAND_OP_DATA_OUT_INSTR);
2245}
2246
2247static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2248 unsigned int instr_idx)
2249{
2250 return subop && instr_idx < subop->ninstrs;
2251}
2252
Miquel Raynal760c4352018-07-19 00:09:12 +02002253static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2254 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002255{
2256 if (instr_idx)
2257 return 0;
2258
2259 return subop->first_instr_start_off;
2260}
2261
2262/**
2263 * nand_subop_get_addr_start_off - Get the start offset in an address array
2264 * @subop: The entire sub-operation
2265 * @instr_idx: Index of the instruction inside the sub-operation
2266 *
2267 * During driver development, one could be tempted to directly use the
2268 * ->addr.addrs field of address instructions. This is wrong as address
2269 * instructions might be split.
2270 *
2271 * Given an address instruction, returns the offset of the first cycle to issue.
2272 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002273unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2274 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002275{
Miquel Raynal760c4352018-07-19 00:09:12 +02002276 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2277 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2278 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002279
2280 return nand_subop_get_start_off(subop, instr_idx);
2281}
2282EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2283
2284/**
2285 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2286 * @subop: The entire sub-operation
2287 * @instr_idx: Index of the instruction inside the sub-operation
2288 *
2289 * During driver development, one could be tempted to directly use the
2290 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2291 * might be split.
2292 *
2293 * Given an address instruction, returns the number of address cycle to issue.
2294 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002295unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2296 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002297{
2298 int start_off, end_off;
2299
Miquel Raynal760c4352018-07-19 00:09:12 +02002300 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2301 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2302 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002303
2304 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2305
2306 if (instr_idx == subop->ninstrs - 1 &&
2307 subop->last_instr_end_off)
2308 end_off = subop->last_instr_end_off;
2309 else
2310 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2311
2312 return end_off - start_off;
2313}
2314EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2315
2316/**
2317 * nand_subop_get_data_start_off - Get the start offset in a data array
2318 * @subop: The entire sub-operation
2319 * @instr_idx: Index of the instruction inside the sub-operation
2320 *
2321 * During driver development, one could be tempted to directly use the
2322 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2323 * instructions might be split.
2324 *
2325 * Given a data instruction, returns the offset to start from.
2326 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002327unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2328 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002329{
Miquel Raynal760c4352018-07-19 00:09:12 +02002330 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2331 !nand_instr_is_data(&subop->instrs[instr_idx])))
2332 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002333
2334 return nand_subop_get_start_off(subop, instr_idx);
2335}
2336EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2337
2338/**
2339 * nand_subop_get_data_len - Get the number of bytes to retrieve
2340 * @subop: The entire sub-operation
2341 * @instr_idx: Index of the instruction inside the sub-operation
2342 *
2343 * During driver development, one could be tempted to directly use the
2344 * ->data->len field of a data instruction. This is wrong as data instructions
2345 * might be split.
2346 *
2347 * Returns the length of the chunk of data to send/receive.
2348 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002349unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2350 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002351{
2352 int start_off = 0, end_off;
2353
Miquel Raynal760c4352018-07-19 00:09:12 +02002354 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2355 !nand_instr_is_data(&subop->instrs[instr_idx])))
2356 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002357
2358 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2359
2360 if (instr_idx == subop->ninstrs - 1 &&
2361 subop->last_instr_end_off)
2362 end_off = subop->last_instr_end_off;
2363 else
2364 end_off = subop->instrs[instr_idx].ctx.data.len;
2365
2366 return end_off - start_off;
2367}
2368EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002371 * nand_reset - Reset and initialize a NAND device
2372 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002373 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002374 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002375 * Save the timings data structure, then apply SDR timings mode 0 (see
2376 * nand_reset_data_interface for details), do the reset operation, and
2377 * apply back the previous timings.
2378 *
2379 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002380 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002381int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002382{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002383 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002384 int ret;
2385
Boris Brezillon104e4422017-03-16 09:35:58 +01002386 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002387 if (ret)
2388 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002389
Boris Brezillon73f907f2016-10-24 16:46:20 +02002390 /*
2391 * The CS line has to be released before we can apply the new NAND
Boris Brezillon1d017852018-11-11 08:55:14 +01002392 * interface settings, hence this weird nand_select_target()
2393 * nand_deselect_target() dance.
Boris Brezillon73f907f2016-10-24 16:46:20 +02002394 */
Boris Brezillon1d017852018-11-11 08:55:14 +01002395 nand_select_target(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002396 ret = nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01002397 nand_deselect_target(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002398 if (ret)
2399 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002400
Miquel Raynal107b7d62018-03-19 14:47:25 +01002401 /*
2402 * A nand_reset_data_interface() put both the NAND chip and the NAND
2403 * controller in timings mode 0. If the default mode for this chip is
2404 * also 0, no need to proceed to the change again. Plus, at probe time,
2405 * nand_setup_data_interface() uses ->set/get_features() which would
2406 * fail anyway as the parameter page is not available yet.
2407 */
2408 if (!chip->onfi_timing_mode_default)
2409 return 0;
2410
Miquel Raynal17fa8042017-11-30 18:01:31 +01002411 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002412 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002413 if (ret)
2414 return ret;
2415
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002416 return 0;
2417}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002418EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002419
2420/**
Boris Brezillon45240362018-09-07 00:38:40 +02002421 * nand_get_features - wrapper to perform a GET_FEATURE
2422 * @chip: NAND chip info structure
2423 * @addr: feature address
2424 * @subfeature_param: the subfeature parameters, a four bytes array
2425 *
2426 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2427 * operation cannot be handled.
2428 */
2429int nand_get_features(struct nand_chip *chip, int addr,
2430 u8 *subfeature_param)
2431{
2432 if (!nand_supports_get_features(chip, addr))
2433 return -ENOTSUPP;
2434
2435 if (chip->legacy.get_features)
2436 return chip->legacy.get_features(chip, addr, subfeature_param);
2437
2438 return nand_get_features_op(chip, addr, subfeature_param);
2439}
Boris Brezillon45240362018-09-07 00:38:40 +02002440
2441/**
2442 * nand_set_features - wrapper to perform a SET_FEATURE
2443 * @chip: NAND chip info structure
2444 * @addr: feature address
2445 * @subfeature_param: the subfeature parameters, a four bytes array
2446 *
2447 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2448 * operation cannot be handled.
2449 */
2450int nand_set_features(struct nand_chip *chip, int addr,
2451 u8 *subfeature_param)
2452{
2453 if (!nand_supports_set_features(chip, addr))
2454 return -ENOTSUPP;
2455
2456 if (chip->legacy.set_features)
2457 return chip->legacy.set_features(chip, addr, subfeature_param);
2458
2459 return nand_set_features_op(chip, addr, subfeature_param);
2460}
Boris Brezillon45240362018-09-07 00:38:40 +02002461
2462/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002463 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2464 * @buf: buffer to test
2465 * @len: buffer length
2466 * @bitflips_threshold: maximum number of bitflips
2467 *
2468 * Check if a buffer contains only 0xff, which means the underlying region
2469 * has been erased and is ready to be programmed.
2470 * The bitflips_threshold specify the maximum number of bitflips before
2471 * considering the region is not erased.
2472 * Note: The logic of this function has been extracted from the memweight
2473 * implementation, except that nand_check_erased_buf function exit before
2474 * testing the whole buffer if the number of bitflips exceed the
2475 * bitflips_threshold value.
2476 *
2477 * Returns a positive number of bitflips less than or equal to
2478 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2479 * threshold.
2480 */
2481static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2482{
2483 const unsigned char *bitmap = buf;
2484 int bitflips = 0;
2485 int weight;
2486
2487 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2488 len--, bitmap++) {
2489 weight = hweight8(*bitmap);
2490 bitflips += BITS_PER_BYTE - weight;
2491 if (unlikely(bitflips > bitflips_threshold))
2492 return -EBADMSG;
2493 }
2494
2495 for (; len >= sizeof(long);
2496 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002497 unsigned long d = *((unsigned long *)bitmap);
2498 if (d == ~0UL)
2499 continue;
2500 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002501 bitflips += BITS_PER_LONG - weight;
2502 if (unlikely(bitflips > bitflips_threshold))
2503 return -EBADMSG;
2504 }
2505
2506 for (; len > 0; len--, bitmap++) {
2507 weight = hweight8(*bitmap);
2508 bitflips += BITS_PER_BYTE - weight;
2509 if (unlikely(bitflips > bitflips_threshold))
2510 return -EBADMSG;
2511 }
2512
2513 return bitflips;
2514}
2515
2516/**
2517 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2518 * 0xff data
2519 * @data: data buffer to test
2520 * @datalen: data length
2521 * @ecc: ECC buffer
2522 * @ecclen: ECC length
2523 * @extraoob: extra OOB buffer
2524 * @extraooblen: extra OOB length
2525 * @bitflips_threshold: maximum number of bitflips
2526 *
2527 * Check if a data buffer and its associated ECC and OOB data contains only
2528 * 0xff pattern, which means the underlying region has been erased and is
2529 * ready to be programmed.
2530 * The bitflips_threshold specify the maximum number of bitflips before
2531 * considering the region as not erased.
2532 *
2533 * Note:
2534 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2535 * different from the NAND page size. When fixing bitflips, ECC engines will
2536 * report the number of errors per chunk, and the NAND core infrastructure
2537 * expect you to return the maximum number of bitflips for the whole page.
2538 * This is why you should always use this function on a single chunk and
2539 * not on the whole page. After checking each chunk you should update your
2540 * max_bitflips value accordingly.
2541 * 2/ When checking for bitflips in erased pages you should not only check
2542 * the payload data but also their associated ECC data, because a user might
2543 * have programmed almost all bits to 1 but a few. In this case, we
2544 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2545 * this case.
2546 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2547 * data are protected by the ECC engine.
2548 * It could also be used if you support subpages and want to attach some
2549 * extra OOB data to an ECC chunk.
2550 *
2551 * Returns a positive number of bitflips less than or equal to
2552 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2553 * threshold. In case of success, the passed buffers are filled with 0xff.
2554 */
2555int nand_check_erased_ecc_chunk(void *data, int datalen,
2556 void *ecc, int ecclen,
2557 void *extraoob, int extraooblen,
2558 int bitflips_threshold)
2559{
2560 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2561
2562 data_bitflips = nand_check_erased_buf(data, datalen,
2563 bitflips_threshold);
2564 if (data_bitflips < 0)
2565 return data_bitflips;
2566
2567 bitflips_threshold -= data_bitflips;
2568
2569 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2570 if (ecc_bitflips < 0)
2571 return ecc_bitflips;
2572
2573 bitflips_threshold -= ecc_bitflips;
2574
2575 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2576 bitflips_threshold);
2577 if (extraoob_bitflips < 0)
2578 return extraoob_bitflips;
2579
2580 if (data_bitflips)
2581 memset(data, 0xff, datalen);
2582
2583 if (ecc_bitflips)
2584 memset(ecc, 0xff, ecclen);
2585
2586 if (extraoob_bitflips)
2587 memset(extraoob, 0xff, extraooblen);
2588
2589 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2590}
2591EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2592
2593/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002594 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002595 * @chip: nand chip info structure
2596 * @buf: buffer to store read data
2597 * @oob_required: caller requires OOB data read to chip->oob_poi
2598 * @page: page number to read
2599 *
2600 * Returns -ENOTSUPP unconditionally.
2601 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002602int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2603 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002604{
2605 return -ENOTSUPP;
2606}
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002607
2608/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002609 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002610 * @chip: nand chip info structure
2611 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002612 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002613 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002614 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002615 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002616 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002617int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2618 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002619{
Boris Brezillonb9761682018-09-06 14:05:20 +02002620 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002621 int ret;
2622
Boris Brezillon25f815f2017-11-30 18:01:30 +01002623 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002624 if (ret)
2625 return ret;
2626
2627 if (oob_required) {
2628 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2629 false);
2630 if (ret)
2631 return ret;
2632 }
2633
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002634 return 0;
2635}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002636EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002637
2638/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002639 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002640 * @chip: nand chip info structure
2641 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002642 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002643 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002644 *
2645 * We need a special oob layout and handling even when OOB isn't used.
2646 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002647static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07002648 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002649{
Boris Brezillonb9761682018-09-06 14:05:20 +02002650 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08002651 int eccsize = chip->ecc.size;
2652 int eccbytes = chip->ecc.bytes;
2653 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002654 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002655
Boris Brezillon25f815f2017-11-30 18:01:30 +01002656 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2657 if (ret)
2658 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002659
2660 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002661 ret = nand_read_data_op(chip, buf, eccsize, false);
2662 if (ret)
2663 return ret;
2664
David Brownell52ff49d2009-03-04 12:01:36 -08002665 buf += eccsize;
2666
2667 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002668 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2669 false);
2670 if (ret)
2671 return ret;
2672
David Brownell52ff49d2009-03-04 12:01:36 -08002673 oob += chip->ecc.prepad;
2674 }
2675
Boris Brezillon97d90da2017-11-30 18:01:29 +01002676 ret = nand_read_data_op(chip, oob, eccbytes, false);
2677 if (ret)
2678 return ret;
2679
David Brownell52ff49d2009-03-04 12:01:36 -08002680 oob += eccbytes;
2681
2682 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002683 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2684 false);
2685 if (ret)
2686 return ret;
2687
David Brownell52ff49d2009-03-04 12:01:36 -08002688 oob += chip->ecc.postpad;
2689 }
2690 }
2691
2692 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002693 if (size) {
2694 ret = nand_read_data_op(chip, oob, size, false);
2695 if (ret)
2696 return ret;
2697 }
David Brownell52ff49d2009-03-04 12:01:36 -08002698
2699 return 0;
2700}
2701
2702/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002703 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002704 * @chip: nand chip info structure
2705 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002706 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002707 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002708 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002709static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
2710 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Boris Brezillonb9761682018-09-06 14:05:20 +02002712 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002713 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002714 int eccbytes = chip->ecc.bytes;
2715 int eccsteps = chip->ecc.steps;
2716 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002717 uint8_t *ecc_calc = chip->ecc.calc_buf;
2718 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002719 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002720
Boris Brezillonb9761682018-09-06 14:05:20 +02002721 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002722
2723 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002724 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002725
Boris Brezillon846031d2016-02-03 20:11:00 +01002726 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2727 chip->ecc.total);
2728 if (ret)
2729 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002730
2731 eccsteps = chip->ecc.steps;
2732 p = buf;
2733
2734 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2735 int stat;
2736
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002737 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07002738 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002739 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002740 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002741 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002742 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2743 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002744 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002745 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01002746}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302749 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002750 * @chip: nand chip info structure
2751 * @data_offs: offset of requested data within the page
2752 * @readlen: data length
2753 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08002754 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01002755 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002756static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
2757 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01002758{
Boris Brezillonb9761682018-09-06 14:05:20 +02002759 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002760 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002761 uint8_t *p;
2762 int data_col_addr, i, gaps = 0;
2763 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
2764 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01002765 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07002766 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01002767 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01002768
Brian Norris7854d3f2011-06-23 14:12:08 -07002769 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01002770 start_step = data_offs / chip->ecc.size;
2771 end_step = (data_offs + readlen - 1) / chip->ecc.size;
2772 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10302773 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01002774
Brian Norris8b6e50c2011-05-25 14:59:01 -07002775 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01002776 datafrag_len = num_steps * chip->ecc.size;
2777 eccfrag_len = num_steps * chip->ecc.bytes;
2778
2779 data_col_addr = start_step * chip->ecc.size;
2780 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01002781 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01002782 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002783 if (ret)
2784 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002785
Brian Norris8b6e50c2011-05-25 14:59:01 -07002786 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01002787 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002788 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01002789
Brian Norris8b6e50c2011-05-25 14:59:01 -07002790 /*
2791 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07002792 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07002793 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002794 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
2795 if (ret)
2796 return ret;
2797
2798 if (oobregion.length < eccfrag_len)
2799 gaps = 1;
2800
Alexey Korolev3d459552008-05-15 17:23:18 +01002801 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002802 ret = nand_change_read_column_op(chip, mtd->writesize,
2803 chip->oob_poi, mtd->oobsize,
2804 false);
2805 if (ret)
2806 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002807 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002808 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002809 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07002810 * about buswidth alignment in read_buf.
2811 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002812 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01002813 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01002814 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002815 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01002816 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
2817 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002818 aligned_len++;
2819
Boris Brezillon97d90da2017-11-30 18:01:29 +01002820 ret = nand_change_read_column_op(chip,
2821 mtd->writesize + aligned_pos,
2822 &chip->oob_poi[aligned_pos],
2823 aligned_len, false);
2824 if (ret)
2825 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002826 }
2827
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002828 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01002829 chip->oob_poi, index, eccfrag_len);
2830 if (ret)
2831 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002832
2833 p = bufpoi + data_col_addr;
2834 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
2835 int stat;
2836
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002837 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002838 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002839 if (stat == -EBADMSG &&
2840 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2841 /* check for empty pages with bitflips */
2842 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002843 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002844 chip->ecc.bytes,
2845 NULL, 0,
2846 chip->ecc.strength);
2847 }
2848
Mike Dunn3f91e942012-04-25 12:06:09 -07002849 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002850 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002851 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01002852 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002853 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2854 }
Alexey Korolev3d459552008-05-15 17:23:18 +01002855 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002856 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01002857}
2858
2859/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002860 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002861 * @chip: nand chip info structure
2862 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002863 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002864 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002865 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002866 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002867 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002868static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
2869 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002870{
Boris Brezillonb9761682018-09-06 14:05:20 +02002871 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002872 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002873 int eccbytes = chip->ecc.bytes;
2874 int eccsteps = chip->ecc.steps;
2875 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002876 uint8_t *ecc_calc = chip->ecc.calc_buf;
2877 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002878 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002879
Boris Brezillon25f815f2017-11-30 18:01:30 +01002880 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2881 if (ret)
2882 return ret;
2883
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002884 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02002885 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002886
2887 ret = nand_read_data_op(chip, p, eccsize, false);
2888 if (ret)
2889 return ret;
2890
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002891 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002892 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002893
2894 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2895 if (ret)
2896 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002897
Boris Brezillon846031d2016-02-03 20:11:00 +01002898 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2899 chip->ecc.total);
2900 if (ret)
2901 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002902
2903 eccsteps = chip->ecc.steps;
2904 p = buf;
2905
2906 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2907 int stat;
2908
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002909 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002910 if (stat == -EBADMSG &&
2911 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2912 /* check for empty pages with bitflips */
2913 stat = nand_check_erased_ecc_chunk(p, eccsize,
2914 &ecc_code[i], eccbytes,
2915 NULL, 0,
2916 chip->ecc.strength);
2917 }
2918
Mike Dunn3f91e942012-04-25 12:06:09 -07002919 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002920 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002921 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002922 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002923 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2924 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002925 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002926 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002927}
2928
2929/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002930 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07002931 * @chip: nand chip info structure
2932 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002933 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002934 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002935 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002936 * Hardware ECC for large page chips, require OOB to be read first. For this
2937 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2938 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2939 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2940 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002941 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002942static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
2943 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002944{
Boris Brezillonb9761682018-09-06 14:05:20 +02002945 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002946 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002947 int eccbytes = chip->ecc.bytes;
2948 int eccsteps = chip->ecc.steps;
2949 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002950 uint8_t *ecc_code = chip->ecc.code_buf;
2951 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002952 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002953
2954 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01002955 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2956 if (ret)
2957 return ret;
2958
2959 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2960 if (ret)
2961 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002962
Boris Brezillon846031d2016-02-03 20:11:00 +01002963 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2964 chip->ecc.total);
2965 if (ret)
2966 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002967
2968 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2969 int stat;
2970
Boris Brezillonec476362018-09-06 14:05:17 +02002971 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002972
2973 ret = nand_read_data_op(chip, p, eccsize, false);
2974 if (ret)
2975 return ret;
2976
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002977 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002978
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002979 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002980 if (stat == -EBADMSG &&
2981 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2982 /* check for empty pages with bitflips */
2983 stat = nand_check_erased_ecc_chunk(p, eccsize,
2984 &ecc_code[i], eccbytes,
2985 NULL, 0,
2986 chip->ecc.strength);
2987 }
2988
Mike Dunn3f91e942012-04-25 12:06:09 -07002989 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002990 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002991 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002992 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002993 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2994 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002995 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002996 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002997}
2998
2999/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003000 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003001 * @chip: nand chip info structure
3002 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003003 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003004 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003005 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003006 * The hw generator calculates the error syndrome automatically. Therefore we
3007 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003008 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003009static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3010 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003011{
Boris Brezillonb9761682018-09-06 14:05:20 +02003012 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003013 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003014 int eccbytes = chip->ecc.bytes;
3015 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003016 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003017 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003018 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003019 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003020
Boris Brezillon25f815f2017-11-30 18:01:30 +01003021 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3022 if (ret)
3023 return ret;
3024
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003025 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3026 int stat;
3027
Boris Brezillonec476362018-09-06 14:05:17 +02003028 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003029
3030 ret = nand_read_data_op(chip, p, eccsize, false);
3031 if (ret)
3032 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003033
3034 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003035 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3036 false);
3037 if (ret)
3038 return ret;
3039
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003040 oob += chip->ecc.prepad;
3041 }
3042
Boris Brezillonec476362018-09-06 14:05:17 +02003043 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003044
3045 ret = nand_read_data_op(chip, oob, eccbytes, false);
3046 if (ret)
3047 return ret;
3048
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003049 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003050
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003051 oob += eccbytes;
3052
3053 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003054 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3055 false);
3056 if (ret)
3057 return ret;
3058
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003059 oob += chip->ecc.postpad;
3060 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003061
3062 if (stat == -EBADMSG &&
3063 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3064 /* check for empty pages with bitflips */
3065 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3066 oob - eccpadbytes,
3067 eccpadbytes,
3068 NULL, 0,
3069 chip->ecc.strength);
3070 }
3071
3072 if (stat < 0) {
3073 mtd->ecc_stats.failed++;
3074 } else {
3075 mtd->ecc_stats.corrected += stat;
3076 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3077 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003078 }
3079
3080 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003081 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003082 if (i) {
3083 ret = nand_read_data_op(chip, oob, i, false);
3084 if (ret)
3085 return ret;
3086 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003087
Mike Dunn3f91e942012-04-25 12:06:09 -07003088 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003089}
3090
3091/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003092 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon08136212018-11-11 08:55:03 +01003093 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003094 * @oob: oob destination address
3095 * @ops: oob ops structure
3096 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003097 */
Boris Brezillon08136212018-11-11 08:55:03 +01003098static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003099 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003100{
Boris Brezillon08136212018-11-11 08:55:03 +01003101 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003102 int ret;
3103
Florian Fainellif8ac0412010-09-07 13:23:43 +02003104 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003105
Brian Norris0612b9d2011-08-30 18:45:40 -07003106 case MTD_OPS_PLACE_OOB:
3107 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003108 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3109 return oob + len;
3110
Boris Brezillon846031d2016-02-03 20:11:00 +01003111 case MTD_OPS_AUTO_OOB:
3112 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3113 ops->ooboffs, len);
3114 BUG_ON(ret);
3115 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003116
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003117 default:
3118 BUG();
3119 }
3120 return NULL;
3121}
3122
3123/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003124 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003125 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003126 * @retry_mode: the retry mode to use
3127 *
3128 * Some vendors supply a special command to shift the Vt threshold, to be used
3129 * when there are too many bitflips in a page (i.e., ECC error). After setting
3130 * a new threshold, the host should retry reading the page.
3131 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003132static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003133{
Brian Norrisba84fb52014-01-03 15:13:33 -08003134 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3135
3136 if (retry_mode >= chip->read_retries)
3137 return -EINVAL;
3138
3139 if (!chip->setup_read_retry)
3140 return -EOPNOTSUPP;
3141
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003142 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003143}
3144
Boris Brezillon85e08e52018-07-27 09:44:17 +02003145static void nand_wait_readrdy(struct nand_chip *chip)
3146{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003147 const struct nand_sdr_timings *sdr;
3148
Boris Brezillon85e08e52018-07-27 09:44:17 +02003149 if (!(chip->options & NAND_NEED_READRDY))
3150 return;
3151
Boris Brezillon52f05b62018-07-27 09:44:18 +02003152 sdr = nand_get_sdr_timings(&chip->data_interface);
3153 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003154}
3155
Brian Norrisba84fb52014-01-03 15:13:33 -08003156/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003157 * nand_do_read_ops - [INTERN] Read data with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003158 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003159 * @from: offset to read from
3160 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003161 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003162 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003163 */
Boris Brezillon08136212018-11-11 08:55:03 +01003164static int nand_do_read_ops(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003165 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003166{
Brian Norrise47f3db2012-05-02 10:14:56 -07003167 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris Brezillon08136212018-11-11 08:55:03 +01003168 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003169 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003170 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003171 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003172 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003173
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003174 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003175 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003176 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003177 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003178 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003180 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003181 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003183 realpage = (int)(from >> chip->page_shift);
3184 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003186 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003188 buf = ops->datbuf;
3189 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003190 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003191
Florian Fainellif8ac0412010-09-07 13:23:43 +02003192 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003193 unsigned int ecc_failures = mtd->ecc_stats.failed;
3194
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003195 bytes = min(mtd->writesize - col, readlen);
3196 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003197
Kamal Dasu66507c72014-05-01 20:51:19 -04003198 if (!aligned)
3199 use_bufpoi = 1;
3200 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003201 use_bufpoi = !virt_addr_valid(buf) ||
3202 !IS_ALIGNED((unsigned long)buf,
3203 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003204 else
3205 use_bufpoi = 0;
3206
Brian Norris8b6e50c2011-05-25 14:59:01 -07003207 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003208 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003209 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003210
3211 if (use_bufpoi && aligned)
3212 pr_debug("%s: using read bounce buffer for buf@%p\n",
3213 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Brian Norrisba84fb52014-01-03 15:13:33 -08003215read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003216 /*
3217 * Now read the page into the buffer. Absent an error,
3218 * the read methods return max bitflips per ecc step.
3219 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003220 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003221 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003222 oob_required,
3223 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003224 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3225 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003226 ret = chip->ecc.read_subpage(chip, col, bytes,
3227 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003228 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003229 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003230 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003231 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003232 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003233 /* Invalidate page cache */
3234 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003235 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003236 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003237
3238 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003239 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003240 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003241 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003242 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003243 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003244 chip->pagebuf_bitflips = ret;
3245 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003246 /* Invalidate page cache */
3247 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003248 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003249 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003251
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003252 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003253 int toread = min(oobreadlen, max_oobsize);
3254
3255 if (toread) {
Boris Brezillon08136212018-11-11 08:55:03 +01003256 oob = nand_transfer_oob(chip, oob, ops,
3257 toread);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003258 oobreadlen -= toread;
3259 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003260 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003261
Boris Brezillon85e08e52018-07-27 09:44:17 +02003262 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003263
Brian Norrisba84fb52014-01-03 15:13:33 -08003264 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003265 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003266 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003267 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003268 retry_mode);
3269 if (ret < 0)
3270 break;
3271
3272 /* Reset failures; retry */
3273 mtd->ecc_stats.failed = ecc_failures;
3274 goto read_retry;
3275 } else {
3276 /* No more retry modes; real failure */
3277 ecc_fail = true;
3278 }
3279 }
3280
3281 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003282 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003283 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003284 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003285 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003286 max_bitflips = max_t(unsigned int, max_bitflips,
3287 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003290 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003291
Brian Norrisba84fb52014-01-03 15:13:33 -08003292 /* Reset to retry mode 0 */
3293 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003294 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003295 if (ret < 0)
3296 break;
3297 retry_mode = 0;
3298 }
3299
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003300 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003301 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Brian Norris8b6e50c2011-05-25 14:59:01 -07003303 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 col = 0;
3305 /* Increment page address */
3306 realpage++;
3307
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003308 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 /* Check, if we cross a chip boundary */
3310 if (!page) {
3311 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003312 nand_deselect_target(chip);
3313 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003316 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003318 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003319 if (oob)
3320 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
Mike Dunn3f91e942012-04-25 12:06:09 -07003322 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003323 return ret;
3324
Brian Norrisb72f3df2013-12-03 11:04:14 -08003325 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003326 return -EBADMSG;
3327
Mike Dunnedbc45402012-04-25 12:06:11 -07003328 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003329}
3330
3331/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003332 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003333 * @chip: nand chip info structure
3334 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003335 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003336int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003337{
Boris Brezillonb9761682018-09-06 14:05:20 +02003338 struct mtd_info *mtd = nand_to_mtd(chip);
3339
Boris Brezillon97d90da2017-11-30 18:01:29 +01003340 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003341}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003342EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003343
3344/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003345 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003346 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003347 * @chip: nand chip info structure
3348 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003349 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003350static int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003351{
Boris Brezillonb9761682018-09-06 14:05:20 +02003352 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003353 int length = mtd->oobsize;
3354 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3355 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003356 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003357 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003358
Boris Brezillon97d90da2017-11-30 18:01:29 +01003359 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3360 if (ret)
3361 return ret;
3362
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003363 for (i = 0; i < chip->ecc.steps; i++) {
3364 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003365 int ret;
3366
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003367 pos = eccsize + i * (eccsize + chunk);
3368 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003369 ret = nand_change_read_column_op(chip, pos,
3370 NULL, 0,
3371 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003372 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003373 ret = nand_read_page_op(chip, page, pos, NULL,
3374 0);
3375
3376 if (ret)
3377 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003378 } else
3379 sndrnd = 1;
3380 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003381
3382 ret = nand_read_data_op(chip, bufpoi, toread, false);
3383 if (ret)
3384 return ret;
3385
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003386 bufpoi += toread;
3387 length -= toread;
3388 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003389 if (length > 0) {
3390 ret = nand_read_data_op(chip, bufpoi, length, false);
3391 if (ret)
3392 return ret;
3393 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003394
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003395 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003396}
3397
3398/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003399 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003400 * @chip: nand chip info structure
3401 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003402 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003403int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003404{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003405 struct mtd_info *mtd = nand_to_mtd(chip);
3406
Boris Brezillon97d90da2017-11-30 18:01:29 +01003407 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3408 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003409}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003410EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003411
3412/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003413 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003414 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003415 * @chip: nand chip info structure
3416 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003417 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003418static int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003419{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003420 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003421 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3422 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003423 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003424 const uint8_t *bufpoi = chip->oob_poi;
3425
3426 /*
3427 * data-ecc-data-ecc ... ecc-oob
3428 * or
3429 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3430 */
3431 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3432 pos = steps * (eccsize + chunk);
3433 steps = 0;
3434 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003435 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003436
Boris Brezillon97d90da2017-11-30 18:01:29 +01003437 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3438 if (ret)
3439 return ret;
3440
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003441 for (i = 0; i < steps; i++) {
3442 if (sndcmd) {
3443 if (mtd->writesize <= 512) {
3444 uint32_t fill = 0xFFFFFFFF;
3445
3446 len = eccsize;
3447 while (len > 0) {
3448 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003449
3450 ret = nand_write_data_op(chip, &fill,
3451 num, false);
3452 if (ret)
3453 return ret;
3454
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003455 len -= num;
3456 }
3457 } else {
3458 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003459 ret = nand_change_write_column_op(chip, pos,
3460 NULL, 0,
3461 false);
3462 if (ret)
3463 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003464 }
3465 } else
3466 sndcmd = 1;
3467 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003468
3469 ret = nand_write_data_op(chip, bufpoi, len, false);
3470 if (ret)
3471 return ret;
3472
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003473 bufpoi += len;
3474 length -= len;
3475 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003476 if (length > 0) {
3477 ret = nand_write_data_op(chip, bufpoi, length, false);
3478 if (ret)
3479 return ret;
3480 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003481
Boris Brezillon97d90da2017-11-30 18:01:29 +01003482 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003483}
3484
3485/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003486 * nand_do_read_oob - [INTERN] NAND read out-of-band
Boris Brezillon08136212018-11-11 08:55:03 +01003487 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003488 * @from: offset to read from
3489 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003491 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 */
Boris Brezillon08136212018-11-11 08:55:03 +01003493static int nand_do_read_oob(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003494 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Boris Brezillon08136212018-11-11 08:55:03 +01003496 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003497 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003498 int page, realpage, chipnr;
Brian Norris041e4572011-06-23 16:45:24 -07003499 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003500 int readlen = ops->ooblen;
3501 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003502 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003503 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Brian Norris289c0522011-07-19 10:06:09 -07003505 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303506 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Brian Norris041e4572011-06-23 16:45:24 -07003508 stats = mtd->ecc_stats;
3509
Boris BREZILLON29f10582016-03-07 10:46:52 +01003510 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003511
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003512 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003513 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003515 /* Shift to get page */
3516 realpage = (int)(from >> chip->page_shift);
3517 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Florian Fainellif8ac0412010-09-07 13:23:43 +02003519 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003520 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003521 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003522 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003523 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003524
3525 if (ret < 0)
3526 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003527
3528 len = min(len, readlen);
Boris Brezillon08136212018-11-11 08:55:03 +01003529 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003530
Boris Brezillon85e08e52018-07-27 09:44:17 +02003531 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003532
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003533 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3534
Vitaly Wool70145682006-11-03 18:20:38 +03003535 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003536 if (!readlen)
3537 break;
3538
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003539 /* Increment page address */
3540 realpage++;
3541
3542 page = realpage & chip->pagemask;
3543 /* Check, if we cross a chip boundary */
3544 if (!page) {
3545 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003546 nand_deselect_target(chip);
3547 nand_select_target(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003550 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003552 ops->oobretlen = ops->ooblen - readlen;
3553
3554 if (ret < 0)
3555 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003556
3557 if (mtd->ecc_stats.failed - stats.failed)
3558 return -EBADMSG;
3559
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003560 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
3563/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003564 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003565 * @mtd: MTD device structure
3566 * @from: offset to read from
3567 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003569 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003571static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3572 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573{
Boris Brezillon08136212018-11-11 08:55:03 +01003574 struct nand_chip *chip = mtd_to_nand(mtd);
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003575 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003576
3577 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003579 if (ops->mode != MTD_OPS_PLACE_OOB &&
3580 ops->mode != MTD_OPS_AUTO_OOB &&
3581 ops->mode != MTD_OPS_RAW)
3582 return -ENOTSUPP;
3583
Boris Brezillon08136212018-11-11 08:55:03 +01003584 nand_get_device(chip, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003586 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01003587 ret = nand_do_read_oob(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003588 else
Boris Brezillon08136212018-11-11 08:55:03 +01003589 ret = nand_do_read_ops(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003590
Boris Brezillon08136212018-11-11 08:55:03 +01003591 nand_release_device(chip);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003592 return ret;
3593}
3594
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003595/**
3596 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003597 * @chip: nand chip info structure
3598 * @buf: data buffer
3599 * @oob_required: must write chip->oob_poi to OOB
3600 * @page: page number to write
3601 *
3602 * Returns -ENOTSUPP unconditionally.
3603 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003604int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3605 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003606{
3607 return -ENOTSUPP;
3608}
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003609
3610/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003611 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003612 * @chip: nand chip info structure
3613 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003614 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003615 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003616 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003617 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003618 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003619int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3620 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003621{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003622 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003623 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003624
Boris Brezillon25f815f2017-11-30 18:01:30 +01003625 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003626 if (ret)
3627 return ret;
3628
3629 if (oob_required) {
3630 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3631 false);
3632 if (ret)
3633 return ret;
3634 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003635
Boris Brezillon25f815f2017-11-30 18:01:30 +01003636 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003638EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003640/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003641 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003642 * @chip: nand chip info structure
3643 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003644 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003645 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003646 *
3647 * We need a special oob layout and handling even when ECC isn't checked.
3648 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003649static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003650 const uint8_t *buf, int oob_required,
3651 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003652{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003653 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003654 int eccsize = chip->ecc.size;
3655 int eccbytes = chip->ecc.bytes;
3656 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003657 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003658
Boris Brezillon25f815f2017-11-30 18:01:30 +01003659 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3660 if (ret)
3661 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003662
3663 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003664 ret = nand_write_data_op(chip, buf, eccsize, false);
3665 if (ret)
3666 return ret;
3667
David Brownell52ff49d2009-03-04 12:01:36 -08003668 buf += eccsize;
3669
3670 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003671 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3672 false);
3673 if (ret)
3674 return ret;
3675
David Brownell52ff49d2009-03-04 12:01:36 -08003676 oob += chip->ecc.prepad;
3677 }
3678
Boris Brezillon97d90da2017-11-30 18:01:29 +01003679 ret = nand_write_data_op(chip, oob, eccbytes, false);
3680 if (ret)
3681 return ret;
3682
David Brownell52ff49d2009-03-04 12:01:36 -08003683 oob += eccbytes;
3684
3685 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003686 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3687 false);
3688 if (ret)
3689 return ret;
3690
David Brownell52ff49d2009-03-04 12:01:36 -08003691 oob += chip->ecc.postpad;
3692 }
3693 }
3694
3695 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003696 if (size) {
3697 ret = nand_write_data_op(chip, oob, size, false);
3698 if (ret)
3699 return ret;
3700 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003701
Boris Brezillon25f815f2017-11-30 18:01:30 +01003702 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003703}
3704/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003705 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003706 * @chip: nand chip info structure
3707 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003708 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003709 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003710 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003711static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
3712 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003713{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003714 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003715 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003716 int eccbytes = chip->ecc.bytes;
3717 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003718 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003719 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003720
Brian Norris7854d3f2011-06-23 14:12:08 -07003721 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003722 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003723 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003724
Boris Brezillon846031d2016-02-03 20:11:00 +01003725 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3726 chip->ecc.total);
3727 if (ret)
3728 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003729
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003730 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003731}
3732
3733/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003734 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003735 * @chip: nand chip info structure
3736 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003737 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003738 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003739 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003740static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
3741 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003742{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003743 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003744 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003745 int eccbytes = chip->ecc.bytes;
3746 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003747 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003748 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003749
Boris Brezillon25f815f2017-11-30 18:01:30 +01003750 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3751 if (ret)
3752 return ret;
3753
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003754 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003755 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003756
3757 ret = nand_write_data_op(chip, p, eccsize, false);
3758 if (ret)
3759 return ret;
3760
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003761 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003762 }
3763
Boris Brezillon846031d2016-02-03 20:11:00 +01003764 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3765 chip->ecc.total);
3766 if (ret)
3767 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003768
Boris Brezillon97d90da2017-11-30 18:01:29 +01003769 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3770 if (ret)
3771 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003772
Boris Brezillon25f815f2017-11-30 18:01:30 +01003773 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003774}
3775
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303776
3777/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08003778 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303779 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07003780 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303781 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07003782 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303783 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003784 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303785 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003786static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
3787 uint32_t data_len, const uint8_t *buf,
3788 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303789{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003790 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303791 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003792 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303793 int ecc_size = chip->ecc.size;
3794 int ecc_bytes = chip->ecc.bytes;
3795 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303796 uint32_t start_step = offset / ecc_size;
3797 uint32_t end_step = (offset + data_len - 1) / ecc_size;
3798 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01003799 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303800
Boris Brezillon25f815f2017-11-30 18:01:30 +01003801 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3802 if (ret)
3803 return ret;
3804
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303805 for (step = 0; step < ecc_steps; step++) {
3806 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02003807 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303808
3809 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003810 ret = nand_write_data_op(chip, buf, ecc_size, false);
3811 if (ret)
3812 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303813
3814 /* mask ECC of un-touched subpages by padding 0xFF */
3815 if ((step < start_step) || (step > end_step))
3816 memset(ecc_calc, 0xff, ecc_bytes);
3817 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003818 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303819
3820 /* mask OOB of un-touched subpages by padding 0xFF */
3821 /* if oob_required, preserve OOB metadata of written subpage */
3822 if (!oob_required || (step < start_step) || (step > end_step))
3823 memset(oob_buf, 0xff, oob_bytes);
3824
Brian Norrisd6a950802013-08-08 17:16:36 -07003825 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303826 ecc_calc += ecc_bytes;
3827 oob_buf += oob_bytes;
3828 }
3829
3830 /* copy calculated ECC for whole page to chip->buffer->oob */
3831 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003832 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01003833 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3834 chip->ecc.total);
3835 if (ret)
3836 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303837
3838 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003839 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3840 if (ret)
3841 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303842
Boris Brezillon25f815f2017-11-30 18:01:30 +01003843 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303844}
3845
3846
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003847/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003848 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003849 * @chip: nand chip info structure
3850 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003851 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003852 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003853 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003854 * The hw generator calculates the error syndrome automatically. Therefore we
3855 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003856 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003857static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
3858 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003859{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003860 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003861 int i, eccsize = chip->ecc.size;
3862 int eccbytes = chip->ecc.bytes;
3863 int eccsteps = chip->ecc.steps;
3864 const uint8_t *p = buf;
3865 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003866 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003867
Boris Brezillon25f815f2017-11-30 18:01:30 +01003868 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3869 if (ret)
3870 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003871
3872 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003873 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003874
3875 ret = nand_write_data_op(chip, p, eccsize, false);
3876 if (ret)
3877 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003878
3879 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003880 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3881 false);
3882 if (ret)
3883 return ret;
3884
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003885 oob += chip->ecc.prepad;
3886 }
3887
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003888 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003889
3890 ret = nand_write_data_op(chip, oob, eccbytes, false);
3891 if (ret)
3892 return ret;
3893
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003894 oob += eccbytes;
3895
3896 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003897 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3898 false);
3899 if (ret)
3900 return ret;
3901
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003902 oob += chip->ecc.postpad;
3903 }
3904 }
3905
3906 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003907 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003908 if (i) {
3909 ret = nand_write_data_op(chip, oob, i, false);
3910 if (ret)
3911 return ret;
3912 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003913
Boris Brezillon25f815f2017-11-30 18:01:30 +01003914 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003915}
3916
3917/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003918 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07003919 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303920 * @offset: address offset within the page
3921 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07003922 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07003923 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07003924 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003925 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003926 */
Boris Brezillon08136212018-11-11 08:55:03 +01003927static int nand_write_page(struct nand_chip *chip, uint32_t offset,
3928 int data_len, const uint8_t *buf, int oob_required,
3929 int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003930{
Boris Brezillon08136212018-11-11 08:55:03 +01003931 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303932 int status, subpage;
3933
3934 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3935 chip->ecc.write_subpage)
3936 subpage = offset || (data_len < mtd->writesize);
3937 else
3938 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003939
David Woodhouse956e9442006-09-25 17:12:39 +01003940 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003941 status = chip->ecc.write_page_raw(chip, buf, oob_required,
3942 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303943 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003944 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
3945 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003946 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003947 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08003948
3949 if (status < 0)
3950 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003951
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003952 return 0;
3953}
3954
Florian Fainellif8ac0412010-09-07 13:23:43 +02003955#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003956
3957/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003958 * nand_do_write_ops - [INTERN] NAND write with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003959 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003960 * @to: offset to write to
3961 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003962 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003963 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003964 */
Boris Brezillon08136212018-11-11 08:55:03 +01003965static int nand_do_write_ops(struct nand_chip *chip, loff_t to,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003966 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003967{
Boris Brezillon08136212018-11-11 08:55:03 +01003968 struct mtd_info *mtd = nand_to_mtd(chip);
Corentin Labbe73600b62017-09-02 10:49:38 +02003969 int chipnr, realpage, page, column;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003970 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02003971
3972 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003973 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003974
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003975 uint8_t *oob = ops->oobbuf;
3976 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303977 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07003978 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003979
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003980 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003981 if (!writelen)
3982 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003983
Brian Norris8b6e50c2011-05-25 14:59:01 -07003984 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003985 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003986 pr_notice("%s: attempt to write non page aligned data\n",
3987 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003988 return -EINVAL;
3989 }
3990
Thomas Gleixner29072b92006-09-28 15:38:36 +02003991 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003992
Thomas Gleixner6a930962006-06-28 00:11:45 +02003993 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003994 nand_select_target(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02003995
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003996 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01003997 if (nand_check_wp(chip)) {
Huang Shijieb0bb6902012-11-19 14:43:29 +08003998 ret = -EIO;
3999 goto err_out;
4000 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004001
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004002 realpage = (int)(to >> chip->page_shift);
4003 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004004
4005 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004006 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4007 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004008 chip->pagebuf = -1;
4009
Maxim Levitsky782ce792010-02-22 20:39:36 +02004010 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004011 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4012 ret = -EINVAL;
4013 goto err_out;
4014 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004015
Florian Fainellif8ac0412010-09-07 13:23:43 +02004016 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004017 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004018 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004019 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004020 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004021
Kamal Dasu66507c72014-05-01 20:51:19 -04004022 if (part_pagewr)
4023 use_bufpoi = 1;
4024 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004025 use_bufpoi = !virt_addr_valid(buf) ||
4026 !IS_ALIGNED((unsigned long)buf,
4027 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004028 else
4029 use_bufpoi = 0;
4030
4031 /* Partial page write?, or need to use bounce buffer */
4032 if (use_bufpoi) {
4033 pr_debug("%s: using write bounce buffer for buf@%p\n",
4034 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004035 if (part_pagewr)
4036 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004037 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004038 memset(chip->data_buf, 0xff, mtd->writesize);
4039 memcpy(&chip->data_buf[column], buf, bytes);
4040 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004041 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004042
Maxim Levitsky782ce792010-02-22 20:39:36 +02004043 if (unlikely(oob)) {
4044 size_t len = min(oobwritelen, oobmaxlen);
Boris Brezillon08136212018-11-11 08:55:03 +01004045 oob = nand_fill_oob(chip, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004046 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004047 } else {
4048 /* We still need to erase leftover OOB data */
4049 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004050 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004051
Boris Brezillon08136212018-11-11 08:55:03 +01004052 ret = nand_write_page(chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004053 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004054 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004055 if (ret)
4056 break;
4057
4058 writelen -= bytes;
4059 if (!writelen)
4060 break;
4061
Thomas Gleixner29072b92006-09-28 15:38:36 +02004062 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004063 buf += bytes;
4064 realpage++;
4065
4066 page = realpage & chip->pagemask;
4067 /* Check, if we cross a chip boundary */
4068 if (!page) {
4069 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004070 nand_deselect_target(chip);
4071 nand_select_target(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004072 }
4073 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004074
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004075 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004076 if (unlikely(oob))
4077 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004078
4079err_out:
Boris Brezillon1d017852018-11-11 08:55:14 +01004080 nand_deselect_target(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004081 return ret;
4082}
4083
4084/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004085 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004086 * @mtd: MTD device structure
4087 * @to: offset to write to
4088 * @len: number of bytes to write
4089 * @retlen: pointer to variable to store the number of written bytes
4090 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004091 *
4092 * NAND write with ECC. Used when performing writes in interrupt context, this
4093 * may for example be called by mtdoops when writing an oops while in panic.
4094 */
4095static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4096 size_t *retlen, const uint8_t *buf)
4097{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004098 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004099 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004100 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004101 int ret;
4102
Brian Norris8b6e50c2011-05-25 14:59:01 -07004103 /* Grab the device */
Boris Brezillon08136212018-11-11 08:55:03 +01004104 panic_nand_get_device(chip, FL_WRITING);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004105
Boris Brezillon1d017852018-11-11 08:55:14 +01004106 nand_select_target(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004107
4108 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004109 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004110
Brian Norris0ec56dc2015-02-28 02:02:30 -08004111 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004112 ops.len = len;
4113 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004114 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004115
Boris Brezillon08136212018-11-11 08:55:03 +01004116 ret = nand_do_write_ops(chip, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004117
Brian Norris4a89ff82011-08-30 18:45:45 -07004118 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004119 return ret;
4120}
4121
4122/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004123 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004124 * @mtd: MTD device structure
4125 * @to: offset to write to
4126 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004127 */
4128static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4129 struct mtd_oob_ops *ops)
4130{
Boris Brezillon08136212018-11-11 08:55:03 +01004131 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004132 int ret = -ENOTSUPP;
4133
4134 ops->retlen = 0;
4135
Boris Brezillon08136212018-11-11 08:55:03 +01004136 nand_get_device(chip, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004137
Florian Fainellif8ac0412010-09-07 13:23:43 +02004138 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004139 case MTD_OPS_PLACE_OOB:
4140 case MTD_OPS_AUTO_OOB:
4141 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004142 break;
4143
4144 default:
4145 goto out;
4146 }
4147
4148 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01004149 ret = nand_do_write_oob(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004150 else
Boris Brezillon08136212018-11-11 08:55:03 +01004151 ret = nand_do_write_ops(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004152
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004153out:
Boris Brezillon08136212018-11-11 08:55:03 +01004154 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 return ret;
4156}
4157
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158/**
Brian Norris49c50b92014-05-06 16:02:19 -07004159 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004160 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004161 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 *
Brian Norris49c50b92014-05-06 16:02:19 -07004163 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004165static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004167 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004168
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004170 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004171
Boris Brezillon97d90da2017-11-30 18:01:29 +01004172 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173}
4174
4175/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004177 * @mtd: MTD device structure
4178 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004180 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004182static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004184 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004188 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004189 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004190 * @instr: erase instruction
4191 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004193 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004195int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004196 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
Adrian Hunter69423d92008-12-10 13:37:21 +00004198 int page, status, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004199 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Brian Norris289c0522011-07-19 10:06:09 -07004201 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4202 __func__, (unsigned long long)instr->addr,
4203 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
Boris Brezillon08136212018-11-11 08:55:03 +01004205 if (check_offs_len(chip, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 /* Grab the lock and see if the device is available */
Boris Brezillon08136212018-11-11 08:55:03 +01004209 nand_get_device(chip, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004212 page = (int)(instr->addr >> chip->page_shift);
4213 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
4215 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004216 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
4218 /* Select the NAND device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004219 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01004222 if (nand_check_wp(chip)) {
Brian Norris289c0522011-07-19 10:06:09 -07004223 pr_debug("%s: device is write protected!\n",
4224 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004225 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 goto erase_exit;
4227 }
4228
4229 /* Loop through the pages */
4230 len = instr->len;
4231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004233 /* Check if we have a bad block, we do not erase bad blocks! */
Boris Brezillon08136212018-11-11 08:55:03 +01004234 if (nand_block_checkbad(chip, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304235 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004236 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4237 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004238 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 goto erase_exit;
4240 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004241
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004242 /*
4243 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004244 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004245 */
4246 if (page <= chip->pagebuf && chip->pagebuf <
4247 (page + pages_per_block))
4248 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Boris Brezillonf9ebd1b2018-09-07 00:38:39 +02004250 if (chip->legacy.erase)
4251 status = chip->legacy.erase(chip,
4252 page & chip->pagemask);
4253 else
4254 status = single_erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
4256 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004257 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004258 pr_debug("%s: failed erase, page 0x%08x\n",
4259 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004260 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004261 instr->fail_addr =
4262 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 goto erase_exit;
4264 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004267 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 page += pages_per_block;
4269
4270 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004271 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004273 nand_deselect_target(chip);
4274 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 }
4276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004278 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004279erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004282 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004283 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 /* Return more or less happy */
4286 return ret;
4287}
4288
4289/**
4290 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004291 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004293 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004295static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
Boris Brezillon08136212018-11-11 08:55:03 +01004297 struct nand_chip *chip = mtd_to_nand(mtd);
4298
Brian Norris289c0522011-07-19 10:06:09 -07004299 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
4301 /* Grab the lock and see if the device is available */
Boris Brezillon08136212018-11-11 08:55:03 +01004302 nand_get_device(chip, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 /* Release it and go back */
Boris Brezillon08136212018-11-11 08:55:03 +01004304 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004308 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004309 * @mtd: MTD device structure
4310 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004312static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304314 struct nand_chip *chip = mtd_to_nand(mtd);
4315 int chipnr = (int)(offs >> chip->chip_shift);
4316 int ret;
4317
4318 /* Select the NAND device */
Boris Brezillon08136212018-11-11 08:55:03 +01004319 nand_get_device(chip, FL_READING);
Boris Brezillon1d017852018-11-11 08:55:14 +01004320 nand_select_target(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304321
Boris Brezillon08136212018-11-11 08:55:03 +01004322 ret = nand_block_checkbad(chip, offs, 0);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304323
Boris Brezillon1d017852018-11-11 08:55:14 +01004324 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004325 nand_release_device(chip);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304326
4327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328}
4329
4330/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004331 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004332 * @mtd: MTD device structure
4333 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004335static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 int ret;
4338
Florian Fainellif8ac0412010-09-07 13:23:43 +02004339 ret = nand_block_isbad(mtd, ofs);
4340 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004341 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 if (ret > 0)
4343 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004344 return ret;
4345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Boris Brezillon08136212018-11-11 08:55:03 +01004347 return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348}
4349
4350/**
Zach Brown56718422017-01-10 13:30:20 -06004351 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4352 * @mtd: MTD device structure
4353 * @ofs: offset relative to mtd start
4354 * @len: length of mtd
4355 */
4356static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4357{
4358 struct nand_chip *chip = mtd_to_nand(mtd);
4359 u32 part_start_block;
4360 u32 part_end_block;
4361 u32 part_start_die;
4362 u32 part_end_die;
4363
4364 /*
4365 * max_bb_per_die and blocks_per_die used to determine
4366 * the maximum bad block count.
4367 */
4368 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4369 return -ENOTSUPP;
4370
4371 /* Get the start and end of the partition in erase blocks. */
4372 part_start_block = mtd_div_by_eb(ofs, mtd);
4373 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4374
4375 /* Get the start and end LUNs of the partition. */
4376 part_start_die = part_start_block / chip->blocks_per_die;
4377 part_end_die = part_end_block / chip->blocks_per_die;
4378
4379 /*
4380 * Look up the bad blocks per unit and multiply by the number of units
4381 * that the partition spans.
4382 */
4383 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4384}
4385
4386/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004387 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004388 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004389 */
4390static int nand_suspend(struct mtd_info *mtd)
4391{
Boris Brezillon08136212018-11-11 08:55:03 +01004392 return nand_get_device(mtd_to_nand(mtd), FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004393}
4394
4395/**
4396 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004397 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004398 */
4399static void nand_resume(struct mtd_info *mtd)
4400{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004401 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004402
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004403 if (chip->state == FL_PM_SUSPENDED)
Boris Brezillon08136212018-11-11 08:55:03 +01004404 nand_release_device(chip);
Vitaly Wool962034f2005-09-15 14:58:53 +01004405 else
Brian Norrisd0370212011-07-19 10:06:08 -07004406 pr_err("%s called for a chip which is not in suspended state\n",
4407 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004408}
4409
Scott Branden72ea4032014-11-20 11:18:05 -08004410/**
4411 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4412 * prevent further operations
4413 * @mtd: MTD device structure
4414 */
4415static void nand_shutdown(struct mtd_info *mtd)
4416{
Boris Brezillon08136212018-11-11 08:55:03 +01004417 nand_get_device(mtd_to_nand(mtd), FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004418}
4419
Brian Norris8b6e50c2011-05-25 14:59:01 -07004420/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004421static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004422{
Boris Brezillon7b6a9b22018-11-20 10:02:39 +01004423 /* If no controller is provided, use the dummy, legacy one. */
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004424 if (!chip->controller) {
Boris Brezillon7b6a9b22018-11-20 10:02:39 +01004425 chip->controller = &chip->legacy.dummy_controller;
Miquel Raynal7da45132018-07-17 09:08:02 +02004426 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004427 }
4428
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01004429 nand_legacy_set_defaults(chip);
4430
Masahiro Yamada477544c2017-03-30 17:15:05 +09004431 if (!chip->buf_align)
4432 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004433}
4434
Brian Norris8b6e50c2011-05-25 14:59:01 -07004435/* Sanitize ONFI strings so we can safely print them */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004436void sanitize_string(uint8_t *s, size_t len)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004437{
4438 ssize_t i;
4439
Brian Norris8b6e50c2011-05-25 14:59:01 -07004440 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004441 s[len - 1] = 0;
4442
Brian Norris8b6e50c2011-05-25 14:59:01 -07004443 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004444 for (i = 0; i < len - 1; i++) {
4445 if (s[i] < ' ' || s[i] > 127)
4446 s[i] = '?';
4447 }
4448
Brian Norris8b6e50c2011-05-25 14:59:01 -07004449 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004450 strim(s);
4451}
4452
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004453/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07004454 * nand_id_has_period - Check if an ID string has a given wraparound period
4455 * @id_data: the ID string
4456 * @arrlen: the length of the @id_data array
4457 * @period: the period of repitition
4458 *
4459 * Check if an ID string is repeated within a given sequence of bytes at
4460 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08004461 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07004462 * if the repetition has a period of @period; otherwise, returns zero.
4463 */
4464static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4465{
4466 int i, j;
4467 for (i = 0; i < period; i++)
4468 for (j = i + period; j < arrlen; j += period)
4469 if (id_data[i] != id_data[j])
4470 return 0;
4471 return 1;
4472}
4473
4474/*
4475 * nand_id_len - Get the length of an ID string returned by CMD_READID
4476 * @id_data: the ID string
4477 * @arrlen: the length of the @id_data array
4478
4479 * Returns the length of the ID string, according to known wraparound/trailing
4480 * zero patterns. If no pattern exists, returns the length of the array.
4481 */
4482static int nand_id_len(u8 *id_data, int arrlen)
4483{
4484 int last_nonzero, period;
4485
4486 /* Find last non-zero byte */
4487 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4488 if (id_data[last_nonzero])
4489 break;
4490
4491 /* All zeros */
4492 if (last_nonzero < 0)
4493 return 0;
4494
4495 /* Calculate wraparound period */
4496 for (period = 1; period < arrlen; period++)
4497 if (nand_id_has_period(id_data, arrlen, period))
4498 break;
4499
4500 /* There's a repeated pattern */
4501 if (period < arrlen)
4502 return period;
4503
4504 /* There are trailing zeros */
4505 if (last_nonzero < arrlen - 1)
4506 return last_nonzero + 1;
4507
4508 /* No pattern detected */
4509 return arrlen;
4510}
4511
Huang Shijie7db906b2013-09-25 14:58:11 +08004512/* Extract the bits of per cell from the 3rd byte of the extended ID */
4513static int nand_get_bits_per_cell(u8 cellinfo)
4514{
4515 int bits;
4516
4517 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4518 bits >>= NAND_CI_CELLTYPE_SHIFT;
4519 return bits + 1;
4520}
4521
Brian Norrise3b88bd2012-09-24 20:40:52 -07004522/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004523 * Many new NAND share similar device ID codes, which represent the size of the
4524 * chip. The rest of the parameters must be decoded according to generic or
4525 * manufacturer-specific "extended ID" decoding patterns.
4526 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004527void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004528{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004529 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02004530 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004531 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004532 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08004533 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004534 /* The 4th id byte is the important one */
4535 extid = id_data[3];
4536
Boris Brezillon01389b62016-06-08 10:30:18 +02004537 /* Calc pagesize */
4538 mtd->writesize = 1024 << (extid & 0x03);
4539 extid >>= 2;
4540 /* Calc oobsize */
4541 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4542 extid >>= 2;
4543 /* Calc blocksize. Blocksize is multiples of 64KiB */
4544 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4545 extid >>= 2;
4546 /* Get buswidth information */
4547 if (extid & 0x1)
4548 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004549}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004550EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004551
4552/*
Brian Norrisf23a4812012-09-24 20:40:51 -07004553 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4554 * decodes a matching ID table entry and assigns the MTD size parameters for
4555 * the chip.
4556 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004557static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07004558{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004559 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07004560
4561 mtd->erasesize = type->erasesize;
4562 mtd->writesize = type->pagesize;
4563 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07004564
Huang Shijie1c195e92013-09-25 14:58:12 +08004565 /* All legacy ID NAND are small-page, SLC */
4566 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07004567}
4568
4569/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004570 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4571 * heuristic patterns using various detected parameters (e.g., manufacturer,
4572 * page size, cell-type information).
4573 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004574static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07004575{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004576 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004577
4578 /* Set the bad block position */
4579 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4580 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4581 else
4582 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004583}
4584
Huang Shijieec6e87e2013-03-15 11:01:00 +08004585static inline bool is_full_id_nand(struct nand_flash_dev *type)
4586{
4587 return type->id_len;
4588}
4589
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004590static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02004591 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004592{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004593 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02004594 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004595
Huang Shijieec6e87e2013-03-15 11:01:00 +08004596 if (!strncmp(type->id, id_data, type->id_len)) {
4597 mtd->writesize = type->pagesize;
4598 mtd->erasesize = type->erasesize;
4599 mtd->oobsize = type->oobsize;
4600
Huang Shijie7db906b2013-09-25 14:58:11 +08004601 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004602 chip->chipsize = (uint64_t)type->chipsize << 20;
4603 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004604 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4605 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004606 chip->onfi_timing_mode_default =
4607 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004608
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004609 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4610 if (!chip->parameters.model)
4611 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004612
Huang Shijieec6e87e2013-03-15 11:01:00 +08004613 return true;
4614 }
4615 return false;
4616}
4617
Brian Norris7e74c2d2012-09-24 20:40:49 -07004618/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004619 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4620 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4621 * table.
4622 */
4623static void nand_manufacturer_detect(struct nand_chip *chip)
4624{
4625 /*
4626 * Try manufacturer detection if available and use
4627 * nand_decode_ext_id() otherwise.
4628 */
4629 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004630 chip->manufacturer.desc->ops->detect) {
4631 /* The 3rd id byte holds MLC / multichip data */
4632 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004633 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004634 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004635 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004636 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004637}
4638
4639/*
4640 * Manufacturer initialization. This function is called for all NANDs including
4641 * ONFI and JEDEC compliant ones.
4642 * Manufacturer drivers should put all their specific initialization code in
4643 * their ->init() hook.
4644 */
4645static int nand_manufacturer_init(struct nand_chip *chip)
4646{
4647 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4648 !chip->manufacturer.desc->ops->init)
4649 return 0;
4650
4651 return chip->manufacturer.desc->ops->init(chip);
4652}
4653
4654/*
4655 * Manufacturer cleanup. This function is called for all NANDs including
4656 * ONFI and JEDEC compliant ones.
4657 * Manufacturer drivers should put all their specific cleanup code in their
4658 * ->cleanup() hook.
4659 */
4660static void nand_manufacturer_cleanup(struct nand_chip *chip)
4661{
4662 /* Release manufacturer private data */
4663 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4664 chip->manufacturer.desc->ops->cleanup)
4665 chip->manufacturer.desc->ops->cleanup(chip);
4666}
4667
Boris Brezillon348d56a2018-09-07 00:38:48 +02004668static const char *
4669nand_manufacturer_name(const struct nand_manufacturer *manufacturer)
4670{
4671 return manufacturer ? manufacturer->name : "Unknown";
4672}
4673
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004674/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004675 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004676 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004677static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004678{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004679 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004680 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004681 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004682 u8 *id_data = chip->id.data;
4683 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
Karl Beldanef89a882008-09-15 14:37:29 +02004685 /*
4686 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004687 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004688 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004689 ret = nand_reset(chip, 0);
4690 if (ret)
4691 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004692
4693 /* Select the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004694 nand_select_target(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004695
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004697 ret = nand_readid_op(chip, 0, id_data, 2);
4698 if (ret)
4699 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004702 maf_id = id_data[0];
4703 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Brian Norris8b6e50c2011-05-25 14:59:01 -07004705 /*
4706 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004707 * interface concerns can cause random data which looks like a
4708 * possibly credible NAND flash to appear. If the two results do
4709 * not match, ignore the device completely.
4710 */
4711
Brian Norris4aef9b72012-09-24 20:40:48 -07004712 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004713 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
4714 if (ret)
4715 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01004716
Boris Brezillon7f501f02016-05-24 19:20:05 +02004717 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004718 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004719 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004720 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004721 }
4722
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02004723 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02004724
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004725 /* Try to identify manufacturer */
4726 manufacturer = nand_get_manufacturer(maf_id);
4727 chip->manufacturer.desc = manufacturer;
4728
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004729 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004730 type = nand_flash_ids;
4731
Boris Brezillon29a198a2016-05-24 20:17:48 +02004732 /*
4733 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4734 * override it.
4735 * This is required to make sure initial NAND bus width set by the
4736 * NAND controller driver is coherent with the real NAND bus width
4737 * (extracted by auto-detection code).
4738 */
4739 busw = chip->options & NAND_BUSWIDTH_16;
4740
4741 /*
4742 * The flag is only set (never cleared), reset it to its default value
4743 * before starting auto-detection.
4744 */
4745 chip->options &= ~NAND_BUSWIDTH_16;
4746
Huang Shijieec6e87e2013-03-15 11:01:00 +08004747 for (; type->name != NULL; type++) {
4748 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004749 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004750 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004751 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004752 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004753 }
4754 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004755
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004756 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004757 /* Check if the chip is ONFI compliant */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004758 ret = nand_onfi_detect(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01004759 if (ret < 0)
4760 return ret;
4761 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004762 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004763
4764 /* Check if the chip is JEDEC compliant */
Boris Brezillon8ae3fbf2018-09-07 00:38:51 +02004765 ret = nand_jedec_detect(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01004766 if (ret < 0)
4767 return ret;
4768 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08004769 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004770 }
4771
David Woodhouse5e81e882010-02-26 18:32:56 +00004772 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004773 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004774
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004775 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4776 if (!chip->parameters.model)
4777 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004778
Adrian Hunter69423d92008-12-10 13:37:21 +00004779 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004780
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004781 if (!type->pagesize)
4782 nand_manufacturer_detect(chip);
4783 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004784 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004785
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004786 /* Get chip options */
4787 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004788
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004789ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01004790 if (!mtd->name)
4791 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004792
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004793 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004794 WARN_ON(busw & NAND_BUSWIDTH_16);
4795 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004796 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4797 /*
4798 * Check, if buswidth is correct. Hardware drivers should set
4799 * chip correct!
4800 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004801 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004802 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004803 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4804 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004805 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4806 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004807 ret = -EINVAL;
4808
4809 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004810 }
4811
Boris Brezillon7f501f02016-05-24 19:20:05 +02004812 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004813
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004814 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004815 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004816 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004817 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004818
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004819 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004820 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004821 if (chip->chipsize & 0xffffffff)
4822 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004823 else {
4824 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4825 chip->chip_shift += 32 - 1;
4826 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004827
Masahiro Yamada14157f82017-09-13 11:05:50 +09004828 if (chip->chip_shift - chip->page_shift > 16)
4829 chip->options |= NAND_ROW_ADDR_3;
4830
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004831 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004832
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004833 nand_legacy_adjust_cmdfunc(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004834
Ezequiel Garcia20171642013-11-25 08:30:31 -03004835 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004836 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01004837 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4838 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02004839 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004840 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004841 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004842 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004843
4844free_detect_allocation:
4845 kfree(chip->parameters.model);
4846
4847 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004848}
4849
Boris Brezillond48f62b2016-04-01 14:54:32 +02004850static const char * const nand_ecc_modes[] = {
4851 [NAND_ECC_NONE] = "none",
4852 [NAND_ECC_SOFT] = "soft",
4853 [NAND_ECC_HW] = "hw",
4854 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4855 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004856 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004857};
4858
4859static int of_get_nand_ecc_mode(struct device_node *np)
4860{
4861 const char *pm;
4862 int err, i;
4863
4864 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4865 if (err < 0)
4866 return err;
4867
4868 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4869 if (!strcasecmp(pm, nand_ecc_modes[i]))
4870 return i;
4871
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004872 /*
4873 * For backward compatibility we support few obsoleted values that don't
4874 * have their mappings into nand_ecc_modes_t anymore (they were merged
4875 * with other enums).
4876 */
4877 if (!strcasecmp(pm, "soft_bch"))
4878 return NAND_ECC_SOFT;
4879
Boris Brezillond48f62b2016-04-01 14:54:32 +02004880 return -ENODEV;
4881}
4882
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004883static const char * const nand_ecc_algos[] = {
4884 [NAND_ECC_HAMMING] = "hamming",
4885 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02004886 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004887};
4888
Boris Brezillond48f62b2016-04-01 14:54:32 +02004889static int of_get_nand_ecc_algo(struct device_node *np)
4890{
4891 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004892 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004893
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004894 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4895 if (!err) {
4896 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4897 if (!strcasecmp(pm, nand_ecc_algos[i]))
4898 return i;
4899 return -ENODEV;
4900 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004901
4902 /*
4903 * For backward compatibility we also read "nand-ecc-mode" checking
4904 * for some obsoleted values that were specifying ECC algorithm.
4905 */
4906 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4907 if (err < 0)
4908 return err;
4909
4910 if (!strcasecmp(pm, "soft"))
4911 return NAND_ECC_HAMMING;
4912 else if (!strcasecmp(pm, "soft_bch"))
4913 return NAND_ECC_BCH;
4914
4915 return -ENODEV;
4916}
4917
4918static int of_get_nand_ecc_step_size(struct device_node *np)
4919{
4920 int ret;
4921 u32 val;
4922
4923 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4924 return ret ? ret : val;
4925}
4926
4927static int of_get_nand_ecc_strength(struct device_node *np)
4928{
4929 int ret;
4930 u32 val;
4931
4932 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4933 return ret ? ret : val;
4934}
4935
4936static int of_get_nand_bus_width(struct device_node *np)
4937{
4938 u32 val;
4939
4940 if (of_property_read_u32(np, "nand-bus-width", &val))
4941 return 8;
4942
4943 switch (val) {
4944 case 8:
4945 case 16:
4946 return val;
4947 default:
4948 return -EIO;
4949 }
4950}
4951
4952static bool of_get_nand_on_flash_bbt(struct device_node *np)
4953{
4954 return of_property_read_bool(np, "nand-on-flash-bbt");
4955}
4956
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004957static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004958{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004959 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004960 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004961
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004962 if (!dn)
4963 return 0;
4964
Brian Norris5844fee2015-01-23 00:22:27 -08004965 if (of_get_nand_bus_width(dn) == 16)
4966 chip->options |= NAND_BUSWIDTH_16;
4967
Stefan Agnerf922bd72018-06-24 23:27:23 +02004968 if (of_property_read_bool(dn, "nand-is-boot-medium"))
4969 chip->options |= NAND_IS_BOOT_MEDIUM;
4970
Brian Norris5844fee2015-01-23 00:22:27 -08004971 if (of_get_nand_on_flash_bbt(dn))
4972 chip->bbt_options |= NAND_BBT_USE_FLASH;
4973
4974 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004975 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004976 ecc_strength = of_get_nand_ecc_strength(dn);
4977 ecc_step = of_get_nand_ecc_step_size(dn);
4978
Brian Norris5844fee2015-01-23 00:22:27 -08004979 if (ecc_mode >= 0)
4980 chip->ecc.mode = ecc_mode;
4981
Rafał Miłecki79082452016-03-23 11:19:02 +01004982 if (ecc_algo >= 0)
4983 chip->ecc.algo = ecc_algo;
4984
Brian Norris5844fee2015-01-23 00:22:27 -08004985 if (ecc_strength >= 0)
4986 chip->ecc.strength = ecc_strength;
4987
4988 if (ecc_step > 0)
4989 chip->ecc.size = ecc_step;
4990
Boris Brezillonba78ee02016-06-08 17:04:22 +02004991 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4992 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4993
Brian Norris5844fee2015-01-23 00:22:27 -08004994 return 0;
4995}
4996
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004997/**
Miquel Raynal98732da2018-07-25 15:31:50 +02004998 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02004999 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005000 * @maxchips: number of chips to scan for
5001 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005002 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005003 * This is the first phase of the normal nand_scan() function. It reads the
5004 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005005 *
Miquel Raynal98732da2018-07-25 15:31:50 +02005006 * This helper used to be called directly from controller drivers that needed
5007 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5008 * prevented dynamic allocations during this phase which was unconvenient and
5009 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005010 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005011static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005012 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005013{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005014 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02005015 int nand_maf_id, nand_dev_id;
5016 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08005017 int ret;
5018
Boris Brezillonae2294b2018-11-11 08:55:15 +01005019 /* Assume all dies are deselected when we enter nand_scan_ident(). */
5020 chip->cur_cs = -1;
5021
Miquel Raynal17fa8042017-11-30 18:01:31 +01005022 /* Enforce the right timings for reset/detection */
5023 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5024
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005025 ret = nand_dt_init(chip);
5026 if (ret)
5027 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005028
Brian Norrisf7a8e382016-01-05 10:39:45 -08005029 if (!mtd->name && mtd->dev.parent)
5030 mtd->name = dev_name(mtd->dev.parent);
5031
Boris Brezillon1d017852018-11-11 08:55:14 +01005032 /*
5033 * Start with chips->numchips = maxchips to let nand_select_target() do
5034 * its job. chip->numchips will be adjusted after.
5035 */
5036 chip->numchips = maxchips;
5037
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005038 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005039 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005040
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01005041 ret = nand_legacy_check_hooks(chip);
5042 if (ret)
5043 return ret;
5044
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005045 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005046 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005047 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005048 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005049 pr_warn("No NAND device found\n");
Boris Brezillon1d017852018-11-11 08:55:14 +01005050 nand_deselect_target(chip);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005051 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 }
5053
Boris Brezillon7f501f02016-05-24 19:20:05 +02005054 nand_maf_id = chip->id.data[0];
5055 nand_dev_id = chip->id.data[1];
5056
Boris Brezillon1d017852018-11-11 08:55:14 +01005057 nand_deselect_target(chip);
Huang Shijie07300162012-11-09 16:23:45 +08005058
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005059 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005060 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005061 u8 id[2];
5062
Karl Beldanef89a882008-09-15 14:37:29 +02005063 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005064 nand_reset(chip, i);
5065
Boris Brezillon1d017852018-11-11 08:55:14 +01005066 nand_select_target(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005068 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005070 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon1d017852018-11-11 08:55:14 +01005071 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 break;
Huang Shijie07300162012-11-09 16:23:45 +08005073 }
Boris Brezillon1d017852018-11-11 08:55:14 +01005074 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 }
5076 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005077 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005078
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005080 chip->numchips = i;
5081 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
David Woodhouse3b85c322006-09-25 17:06:53 +01005083 return 0;
5084}
5085
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005086static void nand_scan_ident_cleanup(struct nand_chip *chip)
5087{
5088 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005089 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005090}
5091
Boris Brezillon08136212018-11-11 08:55:03 +01005092static int nand_set_ecc_soft_ops(struct nand_chip *chip)
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005093{
Boris Brezillon08136212018-11-11 08:55:03 +01005094 struct mtd_info *mtd = nand_to_mtd(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005095 struct nand_ecc_ctrl *ecc = &chip->ecc;
5096
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005097 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005098 return -EINVAL;
5099
5100 switch (ecc->algo) {
5101 case NAND_ECC_HAMMING:
5102 ecc->calculate = nand_calculate_ecc;
5103 ecc->correct = nand_correct_data;
5104 ecc->read_page = nand_read_page_swecc;
5105 ecc->read_subpage = nand_read_subpage;
5106 ecc->write_page = nand_write_page_swecc;
5107 ecc->read_page_raw = nand_read_page_raw;
5108 ecc->write_page_raw = nand_write_page_raw;
5109 ecc->read_oob = nand_read_oob_std;
5110 ecc->write_oob = nand_write_oob_std;
5111 if (!ecc->size)
5112 ecc->size = 256;
5113 ecc->bytes = 3;
5114 ecc->strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +02005115
5116 if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC))
5117 ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
5118
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005119 return 0;
5120 case NAND_ECC_BCH:
5121 if (!mtd_nand_has_bch()) {
5122 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5123 return -EINVAL;
5124 }
5125 ecc->calculate = nand_bch_calculate_ecc;
5126 ecc->correct = nand_bch_correct_data;
5127 ecc->read_page = nand_read_page_swecc;
5128 ecc->read_subpage = nand_read_subpage;
5129 ecc->write_page = nand_write_page_swecc;
5130 ecc->read_page_raw = nand_read_page_raw;
5131 ecc->write_page_raw = nand_write_page_raw;
5132 ecc->read_oob = nand_read_oob_std;
5133 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005134
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005135 /*
5136 * Board driver should supply ecc.size and ecc.strength
5137 * values to select how many bits are correctable.
5138 * Otherwise, default to 4 bits for large page devices.
5139 */
5140 if (!ecc->size && (mtd->oobsize >= 64)) {
5141 ecc->size = 512;
5142 ecc->strength = 4;
5143 }
5144
5145 /*
5146 * if no ecc placement scheme was provided pickup the default
5147 * large page one.
5148 */
5149 if (!mtd->ooblayout) {
5150 /* handle large page devices only */
5151 if (mtd->oobsize < 64) {
5152 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5153 return -EINVAL;
5154 }
5155
5156 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005157
5158 }
5159
5160 /*
5161 * We can only maximize ECC config when the default layout is
5162 * used, otherwise we don't know how many bytes can really be
5163 * used.
5164 */
5165 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5166 ecc->options & NAND_ECC_MAXIMIZE) {
5167 int steps, bytes;
5168
5169 /* Always prefer 1k blocks over 512bytes ones */
5170 ecc->size = 1024;
5171 steps = mtd->writesize / ecc->size;
5172
5173 /* Reserve 2 bytes for the BBM */
5174 bytes = (mtd->oobsize - 2) / steps;
5175 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005176 }
5177
5178 /* See nand_bch_init() for details. */
5179 ecc->bytes = 0;
5180 ecc->priv = nand_bch_init(mtd);
5181 if (!ecc->priv) {
5182 WARN(1, "BCH ECC initialization failed!\n");
5183 return -EINVAL;
5184 }
5185 return 0;
5186 default:
5187 WARN(1, "Unsupported ECC algorithm!\n");
5188 return -EINVAL;
5189 }
5190}
5191
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005192/**
5193 * nand_check_ecc_caps - check the sanity of preset ECC settings
5194 * @chip: nand chip info structure
5195 * @caps: ECC caps info structure
5196 * @oobavail: OOB size that the ECC engine can use
5197 *
5198 * When ECC step size and strength are already set, check if they are supported
5199 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5200 * On success, the calculated ECC bytes is set.
5201 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305202static int
5203nand_check_ecc_caps(struct nand_chip *chip,
5204 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005205{
5206 struct mtd_info *mtd = nand_to_mtd(chip);
5207 const struct nand_ecc_step_info *stepinfo;
5208 int preset_step = chip->ecc.size;
5209 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305210 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005211 int i, j;
5212
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005213 for (i = 0; i < caps->nstepinfos; i++) {
5214 stepinfo = &caps->stepinfos[i];
5215
5216 if (stepinfo->stepsize != preset_step)
5217 continue;
5218
5219 for (j = 0; j < stepinfo->nstrengths; j++) {
5220 if (stepinfo->strengths[j] != preset_strength)
5221 continue;
5222
5223 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5224 preset_strength);
5225 if (WARN_ON_ONCE(ecc_bytes < 0))
5226 return ecc_bytes;
5227
5228 if (ecc_bytes * nsteps > oobavail) {
5229 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5230 preset_step, preset_strength);
5231 return -ENOSPC;
5232 }
5233
5234 chip->ecc.bytes = ecc_bytes;
5235
5236 return 0;
5237 }
5238 }
5239
5240 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5241 preset_step, preset_strength);
5242
5243 return -ENOTSUPP;
5244}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005245
5246/**
5247 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5248 * @chip: nand chip info structure
5249 * @caps: ECC engine caps info structure
5250 * @oobavail: OOB size that the ECC engine can use
5251 *
5252 * If a chip's ECC requirement is provided, try to meet it with the least
5253 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5254 * On success, the chosen ECC settings are set.
5255 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305256static int
5257nand_match_ecc_req(struct nand_chip *chip,
5258 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005259{
5260 struct mtd_info *mtd = nand_to_mtd(chip);
5261 const struct nand_ecc_step_info *stepinfo;
5262 int req_step = chip->ecc_step_ds;
5263 int req_strength = chip->ecc_strength_ds;
5264 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5265 int best_step, best_strength, best_ecc_bytes;
5266 int best_ecc_bytes_total = INT_MAX;
5267 int i, j;
5268
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005269 /* No information provided by the NAND chip */
5270 if (!req_step || !req_strength)
5271 return -ENOTSUPP;
5272
5273 /* number of correctable bits the chip requires in a page */
5274 req_corr = mtd->writesize / req_step * req_strength;
5275
5276 for (i = 0; i < caps->nstepinfos; i++) {
5277 stepinfo = &caps->stepinfos[i];
5278 step_size = stepinfo->stepsize;
5279
5280 for (j = 0; j < stepinfo->nstrengths; j++) {
5281 strength = stepinfo->strengths[j];
5282
5283 /*
5284 * If both step size and strength are smaller than the
5285 * chip's requirement, it is not easy to compare the
5286 * resulted reliability.
5287 */
5288 if (step_size < req_step && strength < req_strength)
5289 continue;
5290
5291 if (mtd->writesize % step_size)
5292 continue;
5293
5294 nsteps = mtd->writesize / step_size;
5295
5296 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5297 if (WARN_ON_ONCE(ecc_bytes < 0))
5298 continue;
5299 ecc_bytes_total = ecc_bytes * nsteps;
5300
5301 if (ecc_bytes_total > oobavail ||
5302 strength * nsteps < req_corr)
5303 continue;
5304
5305 /*
5306 * We assume the best is to meet the chip's requrement
5307 * with the least number of ECC bytes.
5308 */
5309 if (ecc_bytes_total < best_ecc_bytes_total) {
5310 best_ecc_bytes_total = ecc_bytes_total;
5311 best_step = step_size;
5312 best_strength = strength;
5313 best_ecc_bytes = ecc_bytes;
5314 }
5315 }
5316 }
5317
5318 if (best_ecc_bytes_total == INT_MAX)
5319 return -ENOTSUPP;
5320
5321 chip->ecc.size = best_step;
5322 chip->ecc.strength = best_strength;
5323 chip->ecc.bytes = best_ecc_bytes;
5324
5325 return 0;
5326}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005327
5328/**
5329 * nand_maximize_ecc - choose the max ECC strength available
5330 * @chip: nand chip info structure
5331 * @caps: ECC engine caps info structure
5332 * @oobavail: OOB size that the ECC engine can use
5333 *
5334 * Choose the max ECC strength that is supported on the controller, and can fit
5335 * within the chip's OOB. On success, the chosen ECC settings are set.
5336 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305337static int
5338nand_maximize_ecc(struct nand_chip *chip,
5339 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005340{
5341 struct mtd_info *mtd = nand_to_mtd(chip);
5342 const struct nand_ecc_step_info *stepinfo;
5343 int step_size, strength, nsteps, ecc_bytes, corr;
5344 int best_corr = 0;
5345 int best_step = 0;
5346 int best_strength, best_ecc_bytes;
5347 int i, j;
5348
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005349 for (i = 0; i < caps->nstepinfos; i++) {
5350 stepinfo = &caps->stepinfos[i];
5351 step_size = stepinfo->stepsize;
5352
5353 /* If chip->ecc.size is already set, respect it */
5354 if (chip->ecc.size && step_size != chip->ecc.size)
5355 continue;
5356
5357 for (j = 0; j < stepinfo->nstrengths; j++) {
5358 strength = stepinfo->strengths[j];
5359
5360 if (mtd->writesize % step_size)
5361 continue;
5362
5363 nsteps = mtd->writesize / step_size;
5364
5365 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5366 if (WARN_ON_ONCE(ecc_bytes < 0))
5367 continue;
5368
5369 if (ecc_bytes * nsteps > oobavail)
5370 continue;
5371
5372 corr = strength * nsteps;
5373
5374 /*
5375 * If the number of correctable bits is the same,
5376 * bigger step_size has more reliability.
5377 */
5378 if (corr > best_corr ||
5379 (corr == best_corr && step_size > best_step)) {
5380 best_corr = corr;
5381 best_step = step_size;
5382 best_strength = strength;
5383 best_ecc_bytes = ecc_bytes;
5384 }
5385 }
5386 }
5387
5388 if (!best_corr)
5389 return -ENOTSUPP;
5390
5391 chip->ecc.size = best_step;
5392 chip->ecc.strength = best_strength;
5393 chip->ecc.bytes = best_ecc_bytes;
5394
5395 return 0;
5396}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005397
Abhishek Sahu181ace92018-06-20 12:57:28 +05305398/**
5399 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
5400 * @chip: nand chip info structure
5401 * @caps: ECC engine caps info structure
5402 * @oobavail: OOB size that the ECC engine can use
5403 *
5404 * Choose the ECC configuration according to following logic
5405 *
5406 * 1. If both ECC step size and ECC strength are already set (usually by DT)
5407 * then check if it is supported by this controller.
5408 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
5409 * 3. Otherwise, try to match the ECC step size and ECC strength closest
5410 * to the chip's requirement. If available OOB size can't fit the chip
5411 * requirement then fallback to the maximum ECC step size and ECC strength.
5412 *
5413 * On success, the chosen ECC settings are set.
5414 */
5415int nand_ecc_choose_conf(struct nand_chip *chip,
5416 const struct nand_ecc_caps *caps, int oobavail)
5417{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305418 struct mtd_info *mtd = nand_to_mtd(chip);
5419
5420 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
5421 return -EINVAL;
5422
Abhishek Sahu181ace92018-06-20 12:57:28 +05305423 if (chip->ecc.size && chip->ecc.strength)
5424 return nand_check_ecc_caps(chip, caps, oobavail);
5425
5426 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
5427 return nand_maximize_ecc(chip, caps, oobavail);
5428
5429 if (!nand_match_ecc_req(chip, caps, oobavail))
5430 return 0;
5431
5432 return nand_maximize_ecc(chip, caps, oobavail);
5433}
5434EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
5435
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005436/*
5437 * Check if the chip configuration meet the datasheet requirements.
5438
5439 * If our configuration corrects A bits per B bytes and the minimum
5440 * required correction level is X bits per Y bytes, then we must ensure
5441 * both of the following are true:
5442 *
5443 * (1) A / B >= X / Y
5444 * (2) A >= X
5445 *
5446 * Requirement (1) ensures we can correct for the required bitflip density.
5447 * Requirement (2) ensures we can correct even when all bitflips are clumped
5448 * in the same sector.
5449 */
Boris Brezillon08136212018-11-11 08:55:03 +01005450static bool nand_ecc_strength_good(struct nand_chip *chip)
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005451{
Boris Brezillon08136212018-11-11 08:55:03 +01005452 struct mtd_info *mtd = nand_to_mtd(chip);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005453 struct nand_ecc_ctrl *ecc = &chip->ecc;
5454 int corr, ds_corr;
5455
5456 if (ecc->size == 0 || chip->ecc_step_ds == 0)
5457 /* Not enough information */
5458 return true;
5459
5460 /*
5461 * We get the number of corrected bits per page to compare
5462 * the correction density.
5463 */
5464 corr = (mtd->writesize * ecc->strength) / ecc->size;
5465 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
5466
5467 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
5468}
David Woodhouse3b85c322006-09-25 17:06:53 +01005469
5470/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005471 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005472 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01005473 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005474 * This is the second phase of the normal nand_scan() function. It fills out
5475 * all the uninitialized function pointers with the defaults and scans for a
5476 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01005477 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005478static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01005479{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005480 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08005481 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005482 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01005483
Brian Norrise2414f42012-02-06 13:44:00 -08005484 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005485 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07005486 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005487 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07005488 }
Brian Norrise2414f42012-02-06 13:44:00 -08005489
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005490 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005491 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02005492 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005493
Boris Brezillonf84674b2017-06-02 12:18:24 +02005494 /*
5495 * FIXME: some NAND manufacturer drivers expect the first die to be
5496 * selected when manufacturer->init() is called. They should be fixed
5497 * to explictly select the relevant die when interacting with the NAND
5498 * chip.
5499 */
Boris Brezillon1d017852018-11-11 08:55:14 +01005500 nand_select_target(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005501 ret = nand_manufacturer_init(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01005502 nand_deselect_target(chip);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005503 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005504 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005505
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01005506 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005507 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005508
5509 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005510 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005511 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005512 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005513 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005514 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01005517 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 break;
5519 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005520 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02005521 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02005524 /*
5525 * Expose the whole OOB area to users if ECC_NONE
5526 * is passed. We could do that for all kind of
5527 * ->oobsize, but we must keep the old large/small
5528 * page with ECC layout when ->oobsize <= 128 for
5529 * compatibility reasons.
5530 */
5531 if (ecc->mode == NAND_ECC_NONE) {
5532 mtd_set_ooblayout(mtd,
5533 &nand_ooblayout_lp_ops);
5534 break;
5535 }
5536
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005537 WARN(1, "No oob scheme defined for oobsize %d\n",
5538 mtd->oobsize);
5539 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005540 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 }
5542 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005543
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005544 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005545 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005546 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01005547 */
David Woodhouse956e9442006-09-25 17:12:39 +01005548
Huang Shijie97de79e02013-10-18 14:20:53 +08005549 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005550 case NAND_ECC_HW_OOB_FIRST:
5551 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08005552 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005553 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5554 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005555 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005556 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005557 if (!ecc->read_page)
5558 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005559
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005560 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07005561 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005562 if (!ecc->read_page)
5563 ecc->read_page = nand_read_page_hwecc;
5564 if (!ecc->write_page)
5565 ecc->write_page = nand_write_page_hwecc;
5566 if (!ecc->read_page_raw)
5567 ecc->read_page_raw = nand_read_page_raw;
5568 if (!ecc->write_page_raw)
5569 ecc->write_page_raw = nand_write_page_raw;
5570 if (!ecc->read_oob)
5571 ecc->read_oob = nand_read_oob_std;
5572 if (!ecc->write_oob)
5573 ecc->write_oob = nand_write_oob_std;
5574 if (!ecc->read_subpage)
5575 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02005576 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08005577 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005578
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005579 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08005580 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5581 (!ecc->read_page ||
5582 ecc->read_page == nand_read_page_hwecc ||
5583 !ecc->write_page ||
5584 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005585 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5586 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005587 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005588 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07005589 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005590 if (!ecc->read_page)
5591 ecc->read_page = nand_read_page_syndrome;
5592 if (!ecc->write_page)
5593 ecc->write_page = nand_write_page_syndrome;
5594 if (!ecc->read_page_raw)
5595 ecc->read_page_raw = nand_read_page_raw_syndrome;
5596 if (!ecc->write_page_raw)
5597 ecc->write_page_raw = nand_write_page_raw_syndrome;
5598 if (!ecc->read_oob)
5599 ecc->read_oob = nand_read_oob_syndrome;
5600 if (!ecc->write_oob)
5601 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005602
Huang Shijie97de79e02013-10-18 14:20:53 +08005603 if (mtd->writesize >= ecc->size) {
5604 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005605 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
5606 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005607 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07005608 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005609 break;
Mike Dunne2788c92012-04-25 12:06:10 -07005610 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005611 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5612 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08005613 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02005614 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005616 case NAND_ECC_SOFT:
Boris Brezillon08136212018-11-11 08:55:03 +01005617 ret = nand_set_ecc_soft_ops(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005618 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005619 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005620 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01005621 }
5622 break;
5623
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005624 case NAND_ECC_ON_DIE:
5625 if (!ecc->read_page || !ecc->write_page) {
5626 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
5627 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005628 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005629 }
5630 if (!ecc->read_oob)
5631 ecc->read_oob = nand_read_oob_std;
5632 if (!ecc->write_oob)
5633 ecc->write_oob = nand_write_oob_std;
5634 break;
5635
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005636 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005637 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08005638 ecc->read_page = nand_read_page_raw;
5639 ecc->write_page = nand_write_page_raw;
5640 ecc->read_oob = nand_read_oob_std;
5641 ecc->read_page_raw = nand_read_page_raw;
5642 ecc->write_page_raw = nand_write_page_raw;
5643 ecc->write_oob = nand_write_oob_std;
5644 ecc->size = mtd->writesize;
5645 ecc->bytes = 0;
5646 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005648
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005650 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5651 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005652 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005655 if (ecc->correct || ecc->calculate) {
5656 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5657 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5658 if (!ecc->calc_buf || !ecc->code_buf) {
5659 ret = -ENOMEM;
5660 goto err_nand_manuf_cleanup;
5661 }
5662 }
5663
Brian Norris9ce244b2011-08-30 18:45:37 -07005664 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005665 if (!ecc->read_oob_raw)
5666 ecc->read_oob_raw = ecc->read_oob;
5667 if (!ecc->write_oob_raw)
5668 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005669
Boris Brezillon846031d2016-02-03 20:11:00 +01005670 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005671 mtd->ecc_strength = ecc->strength;
5672 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005673
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005674 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005675 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005676 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005677 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005678 ecc->steps = mtd->writesize / ecc->size;
5679 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005680 WARN(1, "Invalid ECC parameters\n");
5681 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005682 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005684 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005685 if (ecc->total > mtd->oobsize) {
5686 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5687 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005688 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005689 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005690
Boris Brezillon846031d2016-02-03 20:11:00 +01005691 /*
5692 * The number of bytes available for a client to place data into
5693 * the out of band area.
5694 */
5695 ret = mtd_ooblayout_count_freebytes(mtd);
5696 if (ret < 0)
5697 ret = 0;
5698
5699 mtd->oobavail = ret;
5700
5701 /* ECC sanity check: warn if it's too weak */
Boris Brezillon08136212018-11-11 08:55:03 +01005702 if (!nand_ecc_strength_good(chip))
Boris Brezillon846031d2016-02-03 20:11:00 +01005703 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5704 mtd->name);
5705
Brian Norris8b6e50c2011-05-25 14:59:01 -07005706 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005707 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005708 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005709 case 2:
5710 mtd->subpage_sft = 1;
5711 break;
5712 case 4:
5713 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005714 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005715 mtd->subpage_sft = 2;
5716 break;
5717 }
5718 }
5719 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5720
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02005721 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005722 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005725 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005727 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305728 switch (ecc->mode) {
5729 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305730 if (chip->page_shift > 9)
5731 chip->options |= NAND_SUBPAGE_READ;
5732 break;
5733
5734 default:
5735 break;
5736 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08005739 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02005740 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5741 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005742 mtd->_erase = nand_erase;
5743 mtd->_point = NULL;
5744 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005745 mtd->_panic_write = panic_nand_write;
5746 mtd->_read_oob = nand_read_oob;
5747 mtd->_write_oob = nand_write_oob;
5748 mtd->_sync = nand_sync;
5749 mtd->_lock = NULL;
5750 mtd->_unlock = NULL;
5751 mtd->_suspend = nand_suspend;
5752 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005753 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005754 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005755 mtd->_block_isbad = nand_block_isbad;
5756 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06005757 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01005758 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005760 /*
5761 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5762 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5763 * properly set.
5764 */
5765 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005766 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
Boris Brezillonf84674b2017-06-02 12:18:24 +02005768 /* Initialize the ->data_interface field. */
5769 ret = nand_init_data_interface(chip);
5770 if (ret)
5771 goto err_nand_manuf_cleanup;
5772
5773 /* Enter fastest possible mode on all dies. */
5774 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005775 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005776 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005777 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005778 }
5779
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005780 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005781 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
5784 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02005785 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07005786 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005787 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005788
Brian Norris44d41822017-05-01 17:04:50 -07005789 return 0;
5790
Boris Brezillonf84674b2017-06-02 12:18:24 +02005791
5792err_nand_manuf_cleanup:
5793 nand_manufacturer_cleanup(chip);
5794
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005795err_free_buf:
5796 kfree(chip->data_buf);
5797 kfree(ecc->code_buf);
5798 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07005799
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005800 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801}
5802
Miquel Raynal05b54c72018-07-19 01:05:46 +02005803static int nand_attach(struct nand_chip *chip)
5804{
5805 if (chip->controller->ops && chip->controller->ops->attach_chip)
5806 return chip->controller->ops->attach_chip(chip);
5807
5808 return 0;
5809}
5810
5811static void nand_detach(struct nand_chip *chip)
5812{
5813 if (chip->controller->ops && chip->controller->ops->detach_chip)
5814 chip->controller->ops->detach_chip(chip);
5815}
5816
David Woodhouse3b85c322006-09-25 17:06:53 +01005817/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005818 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005819 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02005820 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005821 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01005822 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005823 * This fills out all the uninitialized function pointers with the defaults.
5824 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005825 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005826 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005827int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005828 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01005829{
5830 int ret;
5831
Boris Brezillon800342d2018-08-04 22:59:23 +02005832 if (!maxchips)
5833 return -EINVAL;
5834
5835 ret = nand_scan_ident(chip, maxchips, ids);
5836 if (ret)
5837 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005838
5839 ret = nand_attach(chip);
5840 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005841 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005842
Boris Brezillon00ad3782018-09-06 14:05:14 +02005843 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005844 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005845 goto detach_chip;
5846
5847 return 0;
5848
5849detach_chip:
5850 nand_detach(chip);
5851cleanup_ident:
5852 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005853
David Woodhouse3b85c322006-09-25 17:06:53 +01005854 return ret;
5855}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005856EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01005857
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005859 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5860 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005861 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005862void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005864 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005865 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005866 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5867
Jesper Juhlfa671642005-11-07 01:01:27 -08005868 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005869 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005870 kfree(chip->data_buf);
5871 kfree(chip->ecc.code_buf);
5872 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07005873
5874 /* Free bad block descriptor memory */
5875 if (chip->badblock_pattern && chip->badblock_pattern->options
5876 & NAND_BBT_DYNAMICSTRUCT)
5877 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005878
5879 /* Free manufacturer priv data. */
5880 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005881
5882 /* Free controller specific allocations after chip identification */
5883 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005884
5885 /* Free identification phase allocations */
5886 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887}
Miquel Raynal05b54c72018-07-19 01:05:46 +02005888
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005889EXPORT_SYMBOL_GPL(nand_cleanup);
5890
5891/**
5892 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5893 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02005894 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005895 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02005896void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005897{
Boris Brezillon59ac2762018-09-06 14:05:15 +02005898 mtd_device_unregister(nand_to_mtd(chip));
5899 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005900}
David Woodhousee0c7d762006-05-13 18:07:53 +01005901EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005902
David Woodhousee0c7d762006-05-13 18:07:53 +01005903MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005904MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5905MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005906MODULE_DESCRIPTION("Generic NAND flash driver code");