blob: f85e6f3b1b2f48ceacdabb0c9b830ab975cc77f2 [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
249 chip->select_chip(chip, cs);
250}
251EXPORT_SYMBOL_GPL(nand_select_target);
252
253/**
254 * nand_deselect_target() - Deselect the currently selected target
255 * @chip: NAND chip object
256 *
257 * Deselect the currently selected NAND target. The result of operations
258 * executed on @chip after the target has been deselected is undefined.
259 */
260void nand_deselect_target(struct nand_chip *chip)
261{
262 chip->select_chip(chip, -1);
263}
264EXPORT_SYMBOL_GPL(nand_deselect_target);
265
266/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * nand_release_device - [GENERIC] release chip
Boris Brezillon08136212018-11-11 08:55:03 +0100268 * @chip: NAND chip object
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000269 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800270 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
Boris Brezillon08136212018-11-11 08:55:03 +0100272static void nand_release_device(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200274 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200275 spin_lock(&chip->controller->lock);
276 chip->controller->active = NULL;
277 chip->state = FL_READY;
278 wake_up(&chip->controller->wq);
279 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Boris Brezillonc17556f2018-09-06 14:05:25 +0200284 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700285 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000287 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200289static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200291 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900292 int page, page_end, res;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900293 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Brian Norris5fb15492011-05-31 16:31:21 -0700295 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700296 ofs += mtd->erasesize - mtd->writesize;
297
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100298 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900299 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100300
Masahiro Yamadac120e752017-03-23 05:07:01 +0900301 for (; page < page_end; page++) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200302 res = chip->ecc.read_oob(chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530303 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900304 return res;
305
306 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000307
Brian Norriscdbec052012-01-13 18:11:48 -0800308 if (likely(chip->badblockbits == 8))
309 res = bad != 0xFF;
310 else
311 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900312 if (res)
313 return res;
314 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200315
Masahiro Yamadac120e752017-03-23 05:07:01 +0900316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Boris Brezillon99f33512018-11-11 08:55:04 +0100319static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
320{
321 if (chip->legacy.block_bad)
322 return chip->legacy.block_bad(chip, ofs);
323
324 return nand_block_bad(chip, ofs);
325}
326
327/**
328 * panic_nand_get_device - [GENERIC] Get chip for selected access
329 * @chip: the nand chip descriptor
330 * @new_state: the state which is requested
331 *
332 * Used when in panic, no locks are taken.
333 */
334static void panic_nand_get_device(struct nand_chip *chip, int new_state)
335{
336 /* Hardware controller shared among independent devices */
337 chip->controller->active = chip;
338 chip->state = new_state;
339}
340
341/**
342 * nand_get_device - [GENERIC] Get chip for selected access
343 * @chip: NAND chip structure
344 * @new_state: the state which is requested
345 *
346 * Get the device and lock it for exclusive access
347 */
348static int
349nand_get_device(struct nand_chip *chip, int new_state)
350{
351 spinlock_t *lock = &chip->controller->lock;
352 wait_queue_head_t *wq = &chip->controller->wq;
353 DECLARE_WAITQUEUE(wait, current);
354retry:
355 spin_lock(lock);
356
357 /* Hardware controller shared among independent devices */
358 if (!chip->controller->active)
359 chip->controller->active = chip;
360
361 if (chip->controller->active == chip && chip->state == FL_READY) {
362 chip->state = new_state;
363 spin_unlock(lock);
364 return 0;
365 }
366 if (new_state == FL_PM_SUSPENDED) {
367 if (chip->controller->active->state == FL_PM_SUSPENDED) {
368 chip->state = FL_PM_SUSPENDED;
369 spin_unlock(lock);
370 return 0;
371 }
372 }
373 set_current_state(TASK_UNINTERRUPTIBLE);
374 add_wait_queue(wq, &wait);
375 spin_unlock(lock);
376 schedule();
377 remove_wait_queue(wq, &wait);
378 goto retry;
379}
380
381/**
382 * nand_check_wp - [GENERIC] check if the chip is write protected
383 * @chip: NAND chip object
384 *
385 * Check, if the device is write protected. The function expects, that the
386 * device is already selected.
387 */
388static int nand_check_wp(struct nand_chip *chip)
389{
390 u8 status;
391 int ret;
392
393 /* Broken xD cards report WP despite being writable */
394 if (chip->options & NAND_BROKEN_XD)
395 return 0;
396
397 /* Check the WP bit */
398 ret = nand_status_op(chip, &status);
399 if (ret)
400 return ret;
401
402 return status & NAND_STATUS_WP ? 0 : 1;
403}
404
405/**
406 * nand_fill_oob - [INTERN] Transfer client buffer to oob
407 * @oob: oob data buffer
408 * @len: oob data write length
409 * @ops: oob ops structure
410 */
411static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
412 struct mtd_oob_ops *ops)
413{
414 struct mtd_info *mtd = nand_to_mtd(chip);
415 int ret;
416
417 /*
418 * Initialise to all 0xFF, to avoid the possibility of left over OOB
419 * data from a previous OOB read.
420 */
421 memset(chip->oob_poi, 0xff, mtd->oobsize);
422
423 switch (ops->mode) {
424
425 case MTD_OPS_PLACE_OOB:
426 case MTD_OPS_RAW:
427 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
428 return oob + len;
429
430 case MTD_OPS_AUTO_OOB:
431 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
432 ops->ooboffs, len);
433 BUG_ON(ret);
434 return oob + len;
435
436 default:
437 BUG();
438 }
439 return NULL;
440}
441
442/**
443 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
444 * @chip: NAND chip object
445 * @to: offset to write to
446 * @ops: oob operation description structure
447 *
448 * NAND write out-of-band.
449 */
450static int nand_do_write_oob(struct nand_chip *chip, loff_t to,
451 struct mtd_oob_ops *ops)
452{
453 struct mtd_info *mtd = nand_to_mtd(chip);
454 int chipnr, page, status, len;
455
456 pr_debug("%s: to = 0x%08x, len = %i\n",
457 __func__, (unsigned int)to, (int)ops->ooblen);
458
459 len = mtd_oobavail(mtd, ops);
460
461 /* Do not allow write past end of page */
462 if ((ops->ooboffs + ops->ooblen) > len) {
463 pr_debug("%s: attempt to write past end of page\n",
464 __func__);
465 return -EINVAL;
466 }
467
468 chipnr = (int)(to >> chip->chip_shift);
469
470 /*
471 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
472 * of my DiskOnChip 2000 test units) will clear the whole data page too
473 * if we don't do this. I have no clue why, but I seem to have 'fixed'
474 * it in the doc2000 driver in August 1999. dwmw2.
475 */
476 nand_reset(chip, chipnr);
477
Boris Brezillon1d017852018-11-11 08:55:14 +0100478 nand_select_target(chip, chipnr);
Boris Brezillon99f33512018-11-11 08:55:04 +0100479
480 /* Shift to get page */
481 page = (int)(to >> chip->page_shift);
482
483 /* Check, if it is write protected */
484 if (nand_check_wp(chip)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100485 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100486 return -EROFS;
487 }
488
489 /* Invalidate the page cache, if we write to the cached page */
490 if (page == chip->pagebuf)
491 chip->pagebuf = -1;
492
493 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
494
495 if (ops->mode == MTD_OPS_RAW)
496 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
497 else
498 status = chip->ecc.write_oob(chip, page & chip->pagemask);
499
Boris Brezillon1d017852018-11-11 08:55:14 +0100500 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100501
502 if (status)
503 return status;
504
505 ops->oobretlen = ops->ooblen;
506
507 return 0;
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700511 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200512 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700513 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700515 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700516 * specific driver. It provides the details for writing a bad block marker to a
517 * block.
518 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200519static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700520{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200521 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700522 struct mtd_oob_ops ops;
523 uint8_t buf[2] = { 0, 0 };
524 int ret = 0, res, i = 0;
525
Brian Norris0ec56dc2015-02-28 02:02:30 -0800526 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700527 ops.oobbuf = buf;
528 ops.ooboffs = chip->badblockpos;
529 if (chip->options & NAND_BUSWIDTH_16) {
530 ops.ooboffs &= ~0x01;
531 ops.len = ops.ooblen = 2;
532 } else {
533 ops.len = ops.ooblen = 1;
534 }
535 ops.mode = MTD_OPS_PLACE_OOB;
536
537 /* Write to first/last page(s) if necessary */
538 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
539 ofs += mtd->erasesize - mtd->writesize;
540 do {
Boris Brezillon08136212018-11-11 08:55:03 +0100541 res = nand_do_write_oob(chip, ofs, &ops);
Brian Norris5a0edb22013-07-30 17:52:58 -0700542 if (!ret)
543 ret = res;
544
545 i++;
546 ofs += mtd->writesize;
547 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
548
549 return ret;
550}
551
552/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200553 * nand_markbad_bbm - mark a block by updating the BBM
554 * @chip: NAND chip object
555 * @ofs: offset of the block to mark bad
556 */
557int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
558{
559 if (chip->legacy.block_markbad)
560 return chip->legacy.block_markbad(chip, ofs);
561
562 return nand_default_block_markbad(chip, ofs);
563}
564
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200565/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700566 * nand_block_markbad_lowlevel - mark a block bad
Boris Brezillon08136212018-11-11 08:55:03 +0100567 * @chip: NAND chip object
Brian Norris5a0edb22013-07-30 17:52:58 -0700568 * @ofs: offset from device start
569 *
570 * This function performs the generic NAND bad block marking steps (i.e., bad
571 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200572 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700573 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700574 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300575 *
Brian Norrise2414f42012-02-06 13:44:00 -0800576 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700577 * (2) write bad block marker to OOB area of affected block (unless flag
578 * NAND_BBT_NO_OOB_BBM is present)
579 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300580 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700581 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800582 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583*/
Boris Brezillon08136212018-11-11 08:55:03 +0100584static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Boris Brezillon08136212018-11-11 08:55:03 +0100586 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisb32843b2013-07-30 17:52:59 -0700587 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000588
Brian Norrisb32843b2013-07-30 17:52:59 -0700589 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800590 struct erase_info einfo;
591
592 /* Attempt erase before marking OOB */
593 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800594 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300595 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200596 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800597
Brian Norrisb32843b2013-07-30 17:52:59 -0700598 /* Write bad block marker to OOB */
Boris Brezillon08136212018-11-11 08:55:03 +0100599 nand_get_device(chip, FL_WRITING);
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200600 ret = nand_markbad_bbm(chip, ofs);
Boris Brezillon08136212018-11-11 08:55:03 +0100601 nand_release_device(chip);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200602 }
Brian Norrise2414f42012-02-06 13:44:00 -0800603
Brian Norrisb32843b2013-07-30 17:52:59 -0700604 /* Mark block bad in BBT */
605 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200606 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800607 if (!ret)
608 ret = res;
609 }
610
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200611 if (!ret)
612 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300613
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200614 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000617/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800618 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700619 * @mtd: MTD device structure
620 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300621 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800622 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300623 */
624static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
625{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100626 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300627
628 if (!chip->bbt)
629 return 0;
630 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200631 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300632}
633
634/**
635 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Boris Brezillon08136212018-11-11 08:55:03 +0100636 * @chip: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300637 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700638 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 *
640 * Check, if the block is bad. Either by reading the bad block table or
641 * calling of the scan function.
642 */
Boris Brezillon08136212018-11-11 08:55:03 +0100643static int nand_block_checkbad(struct nand_chip *chip, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200646 if (chip->bbt)
647 return nand_isbad_bbt(chip, ofs, allowbbt);
648
649 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200652/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100653 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
654 * @chip: NAND chip structure
655 * @timeout_ms: Timeout in ms
656 *
657 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
658 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
659 * returned.
660 *
661 * This helper is intended to be used when the controller does not have access
662 * to the NAND R/B pin.
663 *
664 * Be aware that calling this helper from an ->exec_op() implementation means
665 * ->exec_op() must be re-entrant.
666 *
667 * Return 0 if the NAND chip is ready, a negative error otherwise.
668 */
669int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
670{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200671 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100672 u8 status = 0;
673 int ret;
674
675 if (!chip->exec_op)
676 return -ENOTSUPP;
677
Boris Brezillon3057fce2018-05-04 21:24:31 +0200678 /* Wait tWB before polling the STATUS reg. */
679 timings = nand_get_sdr_timings(&chip->data_interface);
680 ndelay(PSEC_TO_NSEC(timings->tWB_max));
681
Miquel Raynal8878b122017-11-09 14:16:45 +0100682 ret = nand_status_op(chip, NULL);
683 if (ret)
684 return ret;
685
686 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
687 do {
688 ret = nand_read_data_op(chip, &status, sizeof(status), true);
689 if (ret)
690 break;
691
692 if (status & NAND_STATUS_READY)
693 break;
694
695 /*
696 * Typical lowest execution time for a tR on most NANDs is 10us,
697 * use this as polling delay before doing something smarter (ie.
698 * deriving a delay from the timeout value, timeout_ms/ratio).
699 */
700 udelay(10);
701 } while (time_before(jiffies, timeout_ms));
702
703 /*
704 * We have to exit READ_STATUS mode in order to read real data on the
705 * bus in case the WAITRDY instruction is preceding a DATA_IN
706 * instruction.
707 */
708 nand_exit_status_op(chip);
709
710 if (ret)
711 return ret;
712
713 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
714};
715EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
716
717/**
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +0200718 * nand_gpio_waitrdy - Poll R/B GPIO pin until ready
719 * @chip: NAND chip structure
720 * @gpiod: GPIO descriptor of R/B pin
721 * @timeout_ms: Timeout in ms
722 *
723 * Poll the R/B GPIO pin until it becomes ready. If that does not happen
724 * whitin the specified timeout, -ETIMEDOUT is returned.
725 *
726 * This helper is intended to be used when the controller has access to the
727 * NAND R/B pin over GPIO.
728 *
729 * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise.
730 */
731int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
732 unsigned long timeout_ms)
733{
734 /* Wait until R/B pin indicates chip is ready or timeout occurs */
735 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
736 do {
737 if (gpiod_get_value_cansleep(gpiod))
738 return 0;
739
740 cond_resched();
741 } while (time_before(jiffies, timeout_ms));
742
743 return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT;
744};
745EXPORT_SYMBOL_GPL(nand_gpio_waitrdy);
746
747/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700748 * panic_nand_wait - [GENERIC] wait until the command is done
Brian Norris8b6e50c2011-05-25 14:59:01 -0700749 * @chip: NAND chip structure
750 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200751 *
752 * Wait for command done. This is a helper function for nand_wait used when
753 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400754 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200755 */
Boris Brezillon3d4af7c2018-09-07 00:38:49 +0200756void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200757{
758 int i;
759 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200760 if (chip->legacy.dev_ready) {
761 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200762 break;
763 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100764 int ret;
765 u8 status;
766
767 ret = nand_read_data_op(chip, &status, sizeof(status),
768 true);
769 if (ret)
770 return;
771
772 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200773 break;
774 }
775 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200776 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200777}
778
Miquel Raynal789157e2018-03-19 14:47:28 +0100779static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +0100780{
Miquel Raynal789157e2018-03-19 14:47:28 +0100781 return (chip->parameters.supports_set_get_features &&
782 test_bit(addr, chip->parameters.get_feature_list));
783}
784
785static bool nand_supports_set_features(struct nand_chip *chip, int addr)
786{
787 return (chip->parameters.supports_set_get_features &&
788 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +0100789}
790
791/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200792 * nand_reset_data_interface - Reset data interface and timings
793 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100794 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200795 *
796 * Reset the Data interface and timings to ONFI mode 0.
797 *
798 * Returns 0 for success or negative error code otherwise.
799 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100800static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200801{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200802 int ret;
803
804 if (!chip->setup_data_interface)
805 return 0;
806
807 /*
808 * The ONFI specification says:
809 * "
810 * To transition from NV-DDR or NV-DDR2 to the SDR data
811 * interface, the host shall use the Reset (FFh) command
812 * using SDR timing mode 0. A device in any timing mode is
813 * required to recognize Reset (FFh) command issued in SDR
814 * timing mode 0.
815 * "
816 *
817 * Configure the data interface in SDR mode and set the
818 * timings to timing mode 0.
819 */
820
Miquel Raynal17fa8042017-11-30 18:01:31 +0100821 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon858838b2018-09-06 14:05:33 +0200822 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200823 if (ret)
824 pr_err("Failed to configure data interface to SDR timing mode 0\n");
825
826 return ret;
827}
828
829/**
830 * nand_setup_data_interface - Setup the best data interface and timings
831 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100832 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200833 *
834 * Find and configure the best data interface and NAND timings supported by
835 * the chip and the driver.
836 * First tries to retrieve supported timing modes from ONFI information,
837 * and if the NAND chip does not support ONFI, relies on the
838 * ->onfi_timing_mode_default specified in the nand_ids table.
839 *
840 * Returns 0 for success or negative error code otherwise.
841 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100842static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200843{
Miquel Raynal97baea12018-03-19 14:47:20 +0100844 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
845 chip->onfi_timing_mode_default,
846 };
Boris Brezillond8e725d2016-09-15 10:32:50 +0200847 int ret;
848
Miquel Raynal17fa8042017-11-30 18:01:31 +0100849 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200850 return 0;
851
Miquel Raynal993447b2018-03-19 14:47:21 +0100852 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +0100853 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100854 nand_select_target(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +0100855 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
856 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100857 nand_deselect_target(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200858 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +0100859 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +0200860 }
861
Miquel Raynal97baea12018-03-19 14:47:20 +0100862 /* Change the mode on the controller side */
Boris Brezillon858838b2018-09-06 14:05:33 +0200863 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +0100864 if (ret)
865 return ret;
866
867 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +0100868 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +0100869 return 0;
870
871 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon1d017852018-11-11 08:55:14 +0100872 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100873 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
874 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100875 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100876 if (ret)
877 goto err_reset_chip;
878
879 if (tmode_param[0] != chip->onfi_timing_mode_default) {
880 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
881 chip->onfi_timing_mode_default);
882 goto err_reset_chip;
883 }
884
885 return 0;
886
887err_reset_chip:
888 /*
889 * Fallback to mode 0 if the chip explicitly did not ack the chosen
890 * timing mode.
891 */
892 nand_reset_data_interface(chip, chipnr);
Boris Brezillon1d017852018-11-11 08:55:14 +0100893 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100894 nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +0100895 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100896
Boris Brezillond8e725d2016-09-15 10:32:50 +0200897 return ret;
898}
899
900/**
901 * nand_init_data_interface - find the best data interface and timings
902 * @chip: The NAND chip
903 *
904 * Find the best data interface and NAND timings supported by the chip
905 * and the driver.
906 * First tries to retrieve supported timing modes from ONFI information,
907 * and if the NAND chip does not support ONFI, relies on the
908 * ->onfi_timing_mode_default specified in the nand_ids table. After this
909 * function nand_chip->data_interface is initialized with the best timing mode
910 * available.
911 *
912 * Returns 0 for success or negative error code otherwise.
913 */
914static int nand_init_data_interface(struct nand_chip *chip)
915{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200916 int modes, mode, ret;
917
918 if (!chip->setup_data_interface)
919 return 0;
920
921 /*
922 * First try to identify the best timings from ONFI parameters and
923 * if the NAND does not support ONFI, fallback to the default ONFI
924 * timing mode.
925 */
Boris Brezillon462f35d2018-09-07 00:38:47 +0200926 if (chip->parameters.onfi) {
927 modes = chip->parameters.onfi->async_timing_mode;
928 } else {
Boris Brezillond8e725d2016-09-15 10:32:50 +0200929 if (!chip->onfi_timing_mode_default)
930 return 0;
931
932 modes = GENMASK(chip->onfi_timing_mode_default, 0);
933 }
934
Boris Brezillond8e725d2016-09-15 10:32:50 +0200935 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +0100936 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200937 if (ret)
938 continue;
939
Miquel Raynald787b8b2017-12-22 18:12:41 +0100940 /*
941 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
942 * controller supports the requested timings.
943 */
Boris Brezillon858838b2018-09-06 14:05:33 +0200944 ret = chip->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +0100945 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +0100946 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200947 if (!ret) {
948 chip->onfi_timing_mode_default = mode;
949 break;
950 }
951 }
952
953 return 0;
954}
955
Boris Brezillond8e725d2016-09-15 10:32:50 +0200956/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100957 * nand_fill_column_cycles - fill the column cycles of an address
958 * @chip: The NAND chip
959 * @addrs: Array of address cycles to fill
960 * @offset_in_page: The offset in the page
961 *
962 * Fills the first or the first two bytes of the @addrs field depending
963 * on the NAND bus width and the page size.
964 *
965 * Returns the number of cycles needed to encode the column, or a negative
966 * error code in case one of the arguments is invalid.
967 */
968static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
969 unsigned int offset_in_page)
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100970{
Miquel Raynal8878b122017-11-09 14:16:45 +0100971 struct mtd_info *mtd = nand_to_mtd(chip);
972
973 /* Make sure the offset is less than the actual page size. */
974 if (offset_in_page > mtd->writesize + mtd->oobsize)
975 return -EINVAL;
976
977 /*
978 * On small page NANDs, there's a dedicated command to access the OOB
979 * area, and the column address is relative to the start of the OOB
980 * area, not the start of the page. Asjust the address accordingly.
981 */
982 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
983 offset_in_page -= mtd->writesize;
984
985 /*
986 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
987 * wide, then it must be divided by 2.
988 */
989 if (chip->options & NAND_BUSWIDTH_16) {
990 if (WARN_ON(offset_in_page % 2))
991 return -EINVAL;
992
993 offset_in_page /= 2;
994 }
995
996 addrs[0] = offset_in_page;
997
998 /*
999 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1000 * need 2
1001 */
1002 if (mtd->writesize <= 512)
1003 return 1;
1004
1005 addrs[1] = offset_in_page >> 8;
1006
1007 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Miquel Raynal8878b122017-11-09 14:16:45 +01001010static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1011 unsigned int offset_in_page, void *buf,
1012 unsigned int len)
1013{
1014 struct mtd_info *mtd = nand_to_mtd(chip);
1015 const struct nand_sdr_timings *sdr =
1016 nand_get_sdr_timings(&chip->data_interface);
1017 u8 addrs[4];
1018 struct nand_op_instr instrs[] = {
1019 NAND_OP_CMD(NAND_CMD_READ0, 0),
1020 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1021 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1022 PSEC_TO_NSEC(sdr->tRR_min)),
1023 NAND_OP_DATA_IN(len, buf, 0),
1024 };
1025 struct nand_operation op = NAND_OPERATION(instrs);
1026 int ret;
1027
1028 /* Drop the DATA_IN instruction if len is set to 0. */
1029 if (!len)
1030 op.ninstrs--;
1031
1032 if (offset_in_page >= mtd->writesize)
1033 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1034 else if (offset_in_page >= 256 &&
1035 !(chip->options & NAND_BUSWIDTH_16))
1036 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1037
1038 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1039 if (ret < 0)
1040 return ret;
1041
1042 addrs[1] = page;
1043 addrs[2] = page >> 8;
1044
1045 if (chip->options & NAND_ROW_ADDR_3) {
1046 addrs[3] = page >> 16;
1047 instrs[1].ctx.addr.naddrs++;
1048 }
1049
1050 return nand_exec_op(chip, &op);
1051}
1052
1053static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1054 unsigned int offset_in_page, void *buf,
1055 unsigned int len)
1056{
1057 const struct nand_sdr_timings *sdr =
1058 nand_get_sdr_timings(&chip->data_interface);
1059 u8 addrs[5];
1060 struct nand_op_instr instrs[] = {
1061 NAND_OP_CMD(NAND_CMD_READ0, 0),
1062 NAND_OP_ADDR(4, addrs, 0),
1063 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1064 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1065 PSEC_TO_NSEC(sdr->tRR_min)),
1066 NAND_OP_DATA_IN(len, buf, 0),
1067 };
1068 struct nand_operation op = NAND_OPERATION(instrs);
1069 int ret;
1070
1071 /* Drop the DATA_IN instruction if len is set to 0. */
1072 if (!len)
1073 op.ninstrs--;
1074
1075 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1076 if (ret < 0)
1077 return ret;
1078
1079 addrs[2] = page;
1080 addrs[3] = page >> 8;
1081
1082 if (chip->options & NAND_ROW_ADDR_3) {
1083 addrs[4] = page >> 16;
1084 instrs[1].ctx.addr.naddrs++;
1085 }
1086
1087 return nand_exec_op(chip, &op);
1088}
1089
1090/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001091 * nand_read_page_op - Do a READ PAGE operation
1092 * @chip: The NAND chip
1093 * @page: page to read
1094 * @offset_in_page: offset within the page
1095 * @buf: buffer used to store the data
1096 * @len: length of the buffer
1097 *
1098 * This function issues a READ PAGE operation.
1099 * This function does not select/unselect the CS line.
1100 *
1101 * Returns 0 on success, a negative error code otherwise.
1102 */
1103int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1104 unsigned int offset_in_page, void *buf, unsigned int len)
1105{
1106 struct mtd_info *mtd = nand_to_mtd(chip);
1107
1108 if (len && !buf)
1109 return -EINVAL;
1110
1111 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1112 return -EINVAL;
1113
Miquel Raynal8878b122017-11-09 14:16:45 +01001114 if (chip->exec_op) {
1115 if (mtd->writesize > 512)
1116 return nand_lp_exec_read_page_op(chip, page,
1117 offset_in_page, buf,
1118 len);
1119
1120 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1121 buf, len);
1122 }
1123
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001124 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001125 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001126 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001127
1128 return 0;
1129}
1130EXPORT_SYMBOL_GPL(nand_read_page_op);
1131
1132/**
1133 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1134 * @chip: The NAND chip
1135 * @page: parameter page to read
1136 * @buf: buffer used to store the data
1137 * @len: length of the buffer
1138 *
1139 * This function issues a READ PARAMETER PAGE operation.
1140 * This function does not select/unselect the CS line.
1141 *
1142 * Returns 0 on success, a negative error code otherwise.
1143 */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02001144int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1145 unsigned int len)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001146{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001147 unsigned int i;
1148 u8 *p = buf;
1149
1150 if (len && !buf)
1151 return -EINVAL;
1152
Miquel Raynal8878b122017-11-09 14:16:45 +01001153 if (chip->exec_op) {
1154 const struct nand_sdr_timings *sdr =
1155 nand_get_sdr_timings(&chip->data_interface);
1156 struct nand_op_instr instrs[] = {
1157 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1158 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1159 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1160 PSEC_TO_NSEC(sdr->tRR_min)),
1161 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1162 };
1163 struct nand_operation op = NAND_OPERATION(instrs);
1164
1165 /* Drop the DATA_IN instruction if len is set to 0. */
1166 if (!len)
1167 op.ninstrs--;
1168
1169 return nand_exec_op(chip, &op);
1170 }
1171
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001172 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001173 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001174 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001175
1176 return 0;
1177}
1178
1179/**
1180 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1181 * @chip: The NAND chip
1182 * @offset_in_page: offset within the page
1183 * @buf: buffer used to store the data
1184 * @len: length of the buffer
1185 * @force_8bit: force 8-bit bus access
1186 *
1187 * This function issues a CHANGE READ COLUMN operation.
1188 * This function does not select/unselect the CS line.
1189 *
1190 * Returns 0 on success, a negative error code otherwise.
1191 */
1192int nand_change_read_column_op(struct nand_chip *chip,
1193 unsigned int offset_in_page, void *buf,
1194 unsigned int len, bool force_8bit)
1195{
1196 struct mtd_info *mtd = nand_to_mtd(chip);
1197
1198 if (len && !buf)
1199 return -EINVAL;
1200
1201 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1202 return -EINVAL;
1203
Miquel Raynal8878b122017-11-09 14:16:45 +01001204 /* Small page NANDs do not support column change. */
1205 if (mtd->writesize <= 512)
1206 return -ENOTSUPP;
1207
1208 if (chip->exec_op) {
1209 const struct nand_sdr_timings *sdr =
1210 nand_get_sdr_timings(&chip->data_interface);
1211 u8 addrs[2] = {};
1212 struct nand_op_instr instrs[] = {
1213 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1214 NAND_OP_ADDR(2, addrs, 0),
1215 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1216 PSEC_TO_NSEC(sdr->tCCS_min)),
1217 NAND_OP_DATA_IN(len, buf, 0),
1218 };
1219 struct nand_operation op = NAND_OPERATION(instrs);
1220 int ret;
1221
1222 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1223 if (ret < 0)
1224 return ret;
1225
1226 /* Drop the DATA_IN instruction if len is set to 0. */
1227 if (!len)
1228 op.ninstrs--;
1229
1230 instrs[3].ctx.data.force_8bit = force_8bit;
1231
1232 return nand_exec_op(chip, &op);
1233 }
1234
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001235 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001236 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001237 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001238
1239 return 0;
1240}
1241EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1242
1243/**
1244 * nand_read_oob_op - Do a READ OOB operation
1245 * @chip: The NAND chip
1246 * @page: page to read
1247 * @offset_in_oob: offset within the OOB area
1248 * @buf: buffer used to store the data
1249 * @len: length of the buffer
1250 *
1251 * This function issues a READ OOB operation.
1252 * This function does not select/unselect the CS line.
1253 *
1254 * Returns 0 on success, a negative error code otherwise.
1255 */
1256int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1257 unsigned int offset_in_oob, void *buf, unsigned int len)
1258{
1259 struct mtd_info *mtd = nand_to_mtd(chip);
1260
1261 if (len && !buf)
1262 return -EINVAL;
1263
1264 if (offset_in_oob + len > mtd->oobsize)
1265 return -EINVAL;
1266
Miquel Raynal8878b122017-11-09 14:16:45 +01001267 if (chip->exec_op)
1268 return nand_read_page_op(chip, page,
1269 mtd->writesize + offset_in_oob,
1270 buf, len);
1271
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001272 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001273 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001274 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001275
1276 return 0;
1277}
1278EXPORT_SYMBOL_GPL(nand_read_oob_op);
1279
Miquel Raynal8878b122017-11-09 14:16:45 +01001280static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1281 unsigned int offset_in_page, const void *buf,
1282 unsigned int len, bool prog)
1283{
1284 struct mtd_info *mtd = nand_to_mtd(chip);
1285 const struct nand_sdr_timings *sdr =
1286 nand_get_sdr_timings(&chip->data_interface);
1287 u8 addrs[5] = {};
1288 struct nand_op_instr instrs[] = {
1289 /*
1290 * The first instruction will be dropped if we're dealing
1291 * with a large page NAND and adjusted if we're dealing
1292 * with a small page NAND and the page offset is > 255.
1293 */
1294 NAND_OP_CMD(NAND_CMD_READ0, 0),
1295 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1296 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1297 NAND_OP_DATA_OUT(len, buf, 0),
1298 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1299 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1300 };
1301 struct nand_operation op = NAND_OPERATION(instrs);
1302 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1303 int ret;
1304 u8 status;
1305
1306 if (naddrs < 0)
1307 return naddrs;
1308
1309 addrs[naddrs++] = page;
1310 addrs[naddrs++] = page >> 8;
1311 if (chip->options & NAND_ROW_ADDR_3)
1312 addrs[naddrs++] = page >> 16;
1313
1314 instrs[2].ctx.addr.naddrs = naddrs;
1315
1316 /* Drop the last two instructions if we're not programming the page. */
1317 if (!prog) {
1318 op.ninstrs -= 2;
1319 /* Also drop the DATA_OUT instruction if empty. */
1320 if (!len)
1321 op.ninstrs--;
1322 }
1323
1324 if (mtd->writesize <= 512) {
1325 /*
1326 * Small pages need some more tweaking: we have to adjust the
1327 * first instruction depending on the page offset we're trying
1328 * to access.
1329 */
1330 if (offset_in_page >= mtd->writesize)
1331 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1332 else if (offset_in_page >= 256 &&
1333 !(chip->options & NAND_BUSWIDTH_16))
1334 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1335 } else {
1336 /*
1337 * Drop the first command if we're dealing with a large page
1338 * NAND.
1339 */
1340 op.instrs++;
1341 op.ninstrs--;
1342 }
1343
1344 ret = nand_exec_op(chip, &op);
1345 if (!prog || ret)
1346 return ret;
1347
1348 ret = nand_status_op(chip, &status);
1349 if (ret)
1350 return ret;
1351
1352 return status;
1353}
1354
Boris Brezillon97d90da2017-11-30 18:01:29 +01001355/**
1356 * nand_prog_page_begin_op - starts a PROG PAGE operation
1357 * @chip: The NAND chip
1358 * @page: page to write
1359 * @offset_in_page: offset within the page
1360 * @buf: buffer containing the data to write to the page
1361 * @len: length of the buffer
1362 *
1363 * This function issues the first half of a PROG PAGE operation.
1364 * This function does not select/unselect the CS line.
1365 *
1366 * Returns 0 on success, a negative error code otherwise.
1367 */
1368int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1369 unsigned int offset_in_page, const void *buf,
1370 unsigned int len)
1371{
1372 struct mtd_info *mtd = nand_to_mtd(chip);
1373
1374 if (len && !buf)
1375 return -EINVAL;
1376
1377 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1378 return -EINVAL;
1379
Miquel Raynal8878b122017-11-09 14:16:45 +01001380 if (chip->exec_op)
1381 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1382 len, false);
1383
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001384 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001385
1386 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001387 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001388
1389 return 0;
1390}
1391EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1392
1393/**
1394 * nand_prog_page_end_op - ends a PROG PAGE operation
1395 * @chip: The NAND chip
1396 *
1397 * This function issues the second half of a PROG PAGE operation.
1398 * This function does not select/unselect the CS line.
1399 *
1400 * Returns 0 on success, a negative error code otherwise.
1401 */
1402int nand_prog_page_end_op(struct nand_chip *chip)
1403{
Miquel Raynal8878b122017-11-09 14:16:45 +01001404 int ret;
1405 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001406
Miquel Raynal8878b122017-11-09 14:16:45 +01001407 if (chip->exec_op) {
1408 const struct nand_sdr_timings *sdr =
1409 nand_get_sdr_timings(&chip->data_interface);
1410 struct nand_op_instr instrs[] = {
1411 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1412 PSEC_TO_NSEC(sdr->tWB_max)),
1413 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1414 };
1415 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001416
Miquel Raynal8878b122017-11-09 14:16:45 +01001417 ret = nand_exec_op(chip, &op);
1418 if (ret)
1419 return ret;
1420
1421 ret = nand_status_op(chip, &status);
1422 if (ret)
1423 return ret;
1424 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001425 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001426 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001427 if (ret < 0)
1428 return ret;
1429
1430 status = ret;
1431 }
1432
Boris Brezillon97d90da2017-11-30 18:01:29 +01001433 if (status & NAND_STATUS_FAIL)
1434 return -EIO;
1435
1436 return 0;
1437}
1438EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1439
1440/**
1441 * nand_prog_page_op - Do a full PROG PAGE operation
1442 * @chip: The NAND chip
1443 * @page: page to write
1444 * @offset_in_page: offset within the page
1445 * @buf: buffer containing the data to write to the page
1446 * @len: length of the buffer
1447 *
1448 * This function issues a full PROG PAGE operation.
1449 * This function does not select/unselect the CS line.
1450 *
1451 * Returns 0 on success, a negative error code otherwise.
1452 */
1453int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1454 unsigned int offset_in_page, const void *buf,
1455 unsigned int len)
1456{
1457 struct mtd_info *mtd = nand_to_mtd(chip);
1458 int status;
1459
1460 if (!len || !buf)
1461 return -EINVAL;
1462
1463 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1464 return -EINVAL;
1465
Miquel Raynal8878b122017-11-09 14:16:45 +01001466 if (chip->exec_op) {
1467 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1468 len, true);
1469 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001470 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1471 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001472 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001473 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001474 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001475 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001476
Boris Brezillon97d90da2017-11-30 18:01:29 +01001477 if (status & NAND_STATUS_FAIL)
1478 return -EIO;
1479
1480 return 0;
1481}
1482EXPORT_SYMBOL_GPL(nand_prog_page_op);
1483
1484/**
1485 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1486 * @chip: The NAND chip
1487 * @offset_in_page: offset within the page
1488 * @buf: buffer containing the data to send to the NAND
1489 * @len: length of the buffer
1490 * @force_8bit: force 8-bit bus access
1491 *
1492 * This function issues a CHANGE WRITE COLUMN operation.
1493 * This function does not select/unselect the CS line.
1494 *
1495 * Returns 0 on success, a negative error code otherwise.
1496 */
1497int nand_change_write_column_op(struct nand_chip *chip,
1498 unsigned int offset_in_page,
1499 const void *buf, unsigned int len,
1500 bool force_8bit)
1501{
1502 struct mtd_info *mtd = nand_to_mtd(chip);
1503
1504 if (len && !buf)
1505 return -EINVAL;
1506
1507 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1508 return -EINVAL;
1509
Miquel Raynal8878b122017-11-09 14:16:45 +01001510 /* Small page NANDs do not support column change. */
1511 if (mtd->writesize <= 512)
1512 return -ENOTSUPP;
1513
1514 if (chip->exec_op) {
1515 const struct nand_sdr_timings *sdr =
1516 nand_get_sdr_timings(&chip->data_interface);
1517 u8 addrs[2];
1518 struct nand_op_instr instrs[] = {
1519 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1520 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1521 NAND_OP_DATA_OUT(len, buf, 0),
1522 };
1523 struct nand_operation op = NAND_OPERATION(instrs);
1524 int ret;
1525
1526 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1527 if (ret < 0)
1528 return ret;
1529
1530 instrs[2].ctx.data.force_8bit = force_8bit;
1531
1532 /* Drop the DATA_OUT instruction if len is set to 0. */
1533 if (!len)
1534 op.ninstrs--;
1535
1536 return nand_exec_op(chip, &op);
1537 }
1538
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001539 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001540 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001541 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001542
1543 return 0;
1544}
1545EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1546
1547/**
1548 * nand_readid_op - Do a READID operation
1549 * @chip: The NAND chip
1550 * @addr: address cycle to pass after the READID command
1551 * @buf: buffer used to store the ID
1552 * @len: length of the buffer
1553 *
1554 * This function sends a READID command and reads back the ID returned by the
1555 * NAND.
1556 * This function does not select/unselect the CS line.
1557 *
1558 * Returns 0 on success, a negative error code otherwise.
1559 */
1560int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1561 unsigned int len)
1562{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001563 unsigned int i;
1564 u8 *id = buf;
1565
1566 if (len && !buf)
1567 return -EINVAL;
1568
Miquel Raynal8878b122017-11-09 14:16:45 +01001569 if (chip->exec_op) {
1570 const struct nand_sdr_timings *sdr =
1571 nand_get_sdr_timings(&chip->data_interface);
1572 struct nand_op_instr instrs[] = {
1573 NAND_OP_CMD(NAND_CMD_READID, 0),
1574 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1575 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1576 };
1577 struct nand_operation op = NAND_OPERATION(instrs);
1578
1579 /* Drop the DATA_IN instruction if len is set to 0. */
1580 if (!len)
1581 op.ninstrs--;
1582
1583 return nand_exec_op(chip, &op);
1584 }
1585
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001586 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001587
1588 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001589 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001590
1591 return 0;
1592}
1593EXPORT_SYMBOL_GPL(nand_readid_op);
1594
1595/**
1596 * nand_status_op - Do a STATUS operation
1597 * @chip: The NAND chip
1598 * @status: out variable to store the NAND status
1599 *
1600 * This function sends a STATUS command and reads back the status returned by
1601 * the NAND.
1602 * This function does not select/unselect the CS line.
1603 *
1604 * Returns 0 on success, a negative error code otherwise.
1605 */
1606int nand_status_op(struct nand_chip *chip, u8 *status)
1607{
Miquel Raynal8878b122017-11-09 14:16:45 +01001608 if (chip->exec_op) {
1609 const struct nand_sdr_timings *sdr =
1610 nand_get_sdr_timings(&chip->data_interface);
1611 struct nand_op_instr instrs[] = {
1612 NAND_OP_CMD(NAND_CMD_STATUS,
1613 PSEC_TO_NSEC(sdr->tADL_min)),
1614 NAND_OP_8BIT_DATA_IN(1, status, 0),
1615 };
1616 struct nand_operation op = NAND_OPERATION(instrs);
1617
1618 if (!status)
1619 op.ninstrs--;
1620
1621 return nand_exec_op(chip, &op);
1622 }
1623
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001624 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001625 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001626 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001627
1628 return 0;
1629}
1630EXPORT_SYMBOL_GPL(nand_status_op);
1631
1632/**
1633 * nand_exit_status_op - Exit a STATUS operation
1634 * @chip: The NAND chip
1635 *
1636 * This function sends a READ0 command to cancel the effect of the STATUS
1637 * command to avoid reading only the status until a new read command is sent.
1638 *
1639 * This function does not select/unselect the CS line.
1640 *
1641 * Returns 0 on success, a negative error code otherwise.
1642 */
1643int nand_exit_status_op(struct nand_chip *chip)
1644{
Miquel Raynal8878b122017-11-09 14:16:45 +01001645 if (chip->exec_op) {
1646 struct nand_op_instr instrs[] = {
1647 NAND_OP_CMD(NAND_CMD_READ0, 0),
1648 };
1649 struct nand_operation op = NAND_OPERATION(instrs);
1650
1651 return nand_exec_op(chip, &op);
1652 }
1653
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001654 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001655
1656 return 0;
1657}
Boris Brezillon97d90da2017-11-30 18:01:29 +01001658
1659/**
1660 * nand_erase_op - Do an erase operation
1661 * @chip: The NAND chip
1662 * @eraseblock: block to erase
1663 *
1664 * This function sends an ERASE command and waits for the NAND to be ready
1665 * before returning.
1666 * This function does not select/unselect the CS line.
1667 *
1668 * Returns 0 on success, a negative error code otherwise.
1669 */
1670int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1671{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001672 unsigned int page = eraseblock <<
1673 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01001674 int ret;
1675 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001676
Miquel Raynal8878b122017-11-09 14:16:45 +01001677 if (chip->exec_op) {
1678 const struct nand_sdr_timings *sdr =
1679 nand_get_sdr_timings(&chip->data_interface);
1680 u8 addrs[3] = { page, page >> 8, page >> 16 };
1681 struct nand_op_instr instrs[] = {
1682 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
1683 NAND_OP_ADDR(2, addrs, 0),
1684 NAND_OP_CMD(NAND_CMD_ERASE2,
1685 PSEC_TO_MSEC(sdr->tWB_max)),
1686 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
1687 };
1688 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001689
Miquel Raynal8878b122017-11-09 14:16:45 +01001690 if (chip->options & NAND_ROW_ADDR_3)
1691 instrs[1].ctx.addr.naddrs++;
1692
1693 ret = nand_exec_op(chip, &op);
1694 if (ret)
1695 return ret;
1696
1697 ret = nand_status_op(chip, &status);
1698 if (ret)
1699 return ret;
1700 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001701 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
1702 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01001703
Boris Brezillon8395b752018-09-07 00:38:37 +02001704 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001705 if (ret < 0)
1706 return ret;
1707
1708 status = ret;
1709 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001710
1711 if (status & NAND_STATUS_FAIL)
1712 return -EIO;
1713
1714 return 0;
1715}
1716EXPORT_SYMBOL_GPL(nand_erase_op);
1717
1718/**
1719 * nand_set_features_op - Do a SET FEATURES operation
1720 * @chip: The NAND chip
1721 * @feature: feature id
1722 * @data: 4 bytes of data
1723 *
1724 * This function sends a SET FEATURES command and waits for the NAND to be
1725 * ready before returning.
1726 * This function does not select/unselect the CS line.
1727 *
1728 * Returns 0 on success, a negative error code otherwise.
1729 */
1730static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1731 const void *data)
1732{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001733 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01001734 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001735
Miquel Raynal8878b122017-11-09 14:16:45 +01001736 if (chip->exec_op) {
1737 const struct nand_sdr_timings *sdr =
1738 nand_get_sdr_timings(&chip->data_interface);
1739 struct nand_op_instr instrs[] = {
1740 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
1741 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
1742 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
1743 PSEC_TO_NSEC(sdr->tWB_max)),
1744 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
1745 };
1746 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001747
Boris Brezillon782d1962018-05-11 14:44:07 +02001748 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01001749 }
1750
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001751 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02001752 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001753 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02001754
Boris Brezillon8395b752018-09-07 00:38:37 +02001755 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02001756 if (ret < 0)
1757 return ret;
1758
1759 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001760 return -EIO;
1761
1762 return 0;
1763}
1764
1765/**
1766 * nand_get_features_op - Do a GET FEATURES operation
1767 * @chip: The NAND chip
1768 * @feature: feature id
1769 * @data: 4 bytes of data
1770 *
1771 * This function sends a GET FEATURES command and waits for the NAND to be
1772 * ready before returning.
1773 * This function does not select/unselect the CS line.
1774 *
1775 * Returns 0 on success, a negative error code otherwise.
1776 */
1777static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1778 void *data)
1779{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001780 u8 *params = data;
1781 int i;
1782
Miquel Raynal8878b122017-11-09 14:16:45 +01001783 if (chip->exec_op) {
1784 const struct nand_sdr_timings *sdr =
1785 nand_get_sdr_timings(&chip->data_interface);
1786 struct nand_op_instr instrs[] = {
1787 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
1788 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
1789 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
1790 PSEC_TO_NSEC(sdr->tRR_min)),
1791 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
1792 data, 0),
1793 };
1794 struct nand_operation op = NAND_OPERATION(instrs);
1795
1796 return nand_exec_op(chip, &op);
1797 }
1798
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001799 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001800 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001801 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001802
1803 return 0;
1804}
1805
Boris Brezillon52f05b62018-07-27 09:44:18 +02001806static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
1807 unsigned int delay_ns)
1808{
1809 if (chip->exec_op) {
1810 struct nand_op_instr instrs[] = {
1811 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
1812 PSEC_TO_NSEC(delay_ns)),
1813 };
1814 struct nand_operation op = NAND_OPERATION(instrs);
1815
1816 return nand_exec_op(chip, &op);
1817 }
1818
1819 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02001820 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001821 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001822 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001823 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001824
1825 return 0;
1826}
1827
Boris Brezillon97d90da2017-11-30 18:01:29 +01001828/**
1829 * nand_reset_op - Do a reset operation
1830 * @chip: The NAND chip
1831 *
1832 * This function sends a RESET command and waits for the NAND to be ready
1833 * before returning.
1834 * This function does not select/unselect the CS line.
1835 *
1836 * Returns 0 on success, a negative error code otherwise.
1837 */
1838int nand_reset_op(struct nand_chip *chip)
1839{
Miquel Raynal8878b122017-11-09 14:16:45 +01001840 if (chip->exec_op) {
1841 const struct nand_sdr_timings *sdr =
1842 nand_get_sdr_timings(&chip->data_interface);
1843 struct nand_op_instr instrs[] = {
1844 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
1845 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
1846 };
1847 struct nand_operation op = NAND_OPERATION(instrs);
1848
1849 return nand_exec_op(chip, &op);
1850 }
1851
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001852 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001853
1854 return 0;
1855}
1856EXPORT_SYMBOL_GPL(nand_reset_op);
1857
1858/**
1859 * nand_read_data_op - Read data from the NAND
1860 * @chip: The NAND chip
1861 * @buf: buffer used to store the data
1862 * @len: length of the buffer
1863 * @force_8bit: force 8-bit bus access
1864 *
1865 * This function does a raw data read on the bus. Usually used after launching
1866 * another NAND operation like nand_read_page_op().
1867 * This function does not select/unselect the CS line.
1868 *
1869 * Returns 0 on success, a negative error code otherwise.
1870 */
1871int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1872 bool force_8bit)
1873{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001874 if (!len || !buf)
1875 return -EINVAL;
1876
Miquel Raynal8878b122017-11-09 14:16:45 +01001877 if (chip->exec_op) {
1878 struct nand_op_instr instrs[] = {
1879 NAND_OP_DATA_IN(len, buf, 0),
1880 };
1881 struct nand_operation op = NAND_OPERATION(instrs);
1882
1883 instrs[0].ctx.data.force_8bit = force_8bit;
1884
1885 return nand_exec_op(chip, &op);
1886 }
1887
Boris Brezillon97d90da2017-11-30 18:01:29 +01001888 if (force_8bit) {
1889 u8 *p = buf;
1890 unsigned int i;
1891
1892 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001893 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001894 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001895 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001896 }
1897
1898 return 0;
1899}
1900EXPORT_SYMBOL_GPL(nand_read_data_op);
1901
1902/**
1903 * nand_write_data_op - Write data from the NAND
1904 * @chip: The NAND chip
1905 * @buf: buffer containing the data to send on the bus
1906 * @len: length of the buffer
1907 * @force_8bit: force 8-bit bus access
1908 *
1909 * This function does a raw data write on the bus. Usually used after launching
1910 * another NAND operation like nand_write_page_begin_op().
1911 * This function does not select/unselect the CS line.
1912 *
1913 * Returns 0 on success, a negative error code otherwise.
1914 */
1915int nand_write_data_op(struct nand_chip *chip, const void *buf,
1916 unsigned int len, bool force_8bit)
1917{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001918 if (!len || !buf)
1919 return -EINVAL;
1920
Miquel Raynal8878b122017-11-09 14:16:45 +01001921 if (chip->exec_op) {
1922 struct nand_op_instr instrs[] = {
1923 NAND_OP_DATA_OUT(len, buf, 0),
1924 };
1925 struct nand_operation op = NAND_OPERATION(instrs);
1926
1927 instrs[0].ctx.data.force_8bit = force_8bit;
1928
1929 return nand_exec_op(chip, &op);
1930 }
1931
Boris Brezillon97d90da2017-11-30 18:01:29 +01001932 if (force_8bit) {
1933 const u8 *p = buf;
1934 unsigned int i;
1935
1936 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001937 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001938 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001939 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001940 }
1941
1942 return 0;
1943}
1944EXPORT_SYMBOL_GPL(nand_write_data_op);
1945
1946/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001947 * struct nand_op_parser_ctx - Context used by the parser
1948 * @instrs: array of all the instructions that must be addressed
1949 * @ninstrs: length of the @instrs array
1950 * @subop: Sub-operation to be passed to the NAND controller
1951 *
1952 * This structure is used by the core to split NAND operations into
1953 * sub-operations that can be handled by the NAND controller.
1954 */
1955struct nand_op_parser_ctx {
1956 const struct nand_op_instr *instrs;
1957 unsigned int ninstrs;
1958 struct nand_subop subop;
1959};
1960
1961/**
1962 * nand_op_parser_must_split_instr - Checks if an instruction must be split
1963 * @pat: the parser pattern element that matches @instr
1964 * @instr: pointer to the instruction to check
1965 * @start_offset: this is an in/out parameter. If @instr has already been
1966 * split, then @start_offset is the offset from which to start
1967 * (either an address cycle or an offset in the data buffer).
1968 * Conversely, if the function returns true (ie. instr must be
1969 * split), this parameter is updated to point to the first
1970 * data/address cycle that has not been taken care of.
1971 *
1972 * Some NAND controllers are limited and cannot send X address cycles with a
1973 * unique operation, or cannot read/write more than Y bytes at the same time.
1974 * In this case, split the instruction that does not fit in a single
1975 * controller-operation into two or more chunks.
1976 *
1977 * Returns true if the instruction must be split, false otherwise.
1978 * The @start_offset parameter is also updated to the offset at which the next
1979 * bundle of instruction must start (if an address or a data instruction).
1980 */
1981static bool
1982nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
1983 const struct nand_op_instr *instr,
1984 unsigned int *start_offset)
1985{
1986 switch (pat->type) {
1987 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001988 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01001989 break;
1990
1991 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001992 pat->ctx.addr.maxcycles) {
1993 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01001994 return true;
1995 }
1996 break;
1997
1998 case NAND_OP_DATA_IN_INSTR:
1999 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002000 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002001 break;
2002
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002003 if (instr->ctx.data.len - *start_offset >
2004 pat->ctx.data.maxlen) {
2005 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002006 return true;
2007 }
2008 break;
2009
2010 default:
2011 break;
2012 }
2013
2014 return false;
2015}
2016
2017/**
2018 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2019 * remaining in the parser context
2020 * @pat: the pattern to test
2021 * @ctx: the parser context structure to match with the pattern @pat
2022 *
2023 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2024 * Returns true if this is the case, false ortherwise. When true is returned,
2025 * @ctx->subop is updated with the set of instructions to be passed to the
2026 * controller driver.
2027 */
2028static bool
2029nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2030 struct nand_op_parser_ctx *ctx)
2031{
2032 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2033 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2034 const struct nand_op_instr *instr = ctx->subop.instrs;
2035 unsigned int i, ninstrs;
2036
2037 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2038 /*
2039 * The pattern instruction does not match the operation
2040 * instruction. If the instruction is marked optional in the
2041 * pattern definition, we skip the pattern element and continue
2042 * to the next one. If the element is mandatory, there's no
2043 * match and we can return false directly.
2044 */
2045 if (instr->type != pat->elems[i].type) {
2046 if (!pat->elems[i].optional)
2047 return false;
2048
2049 continue;
2050 }
2051
2052 /*
2053 * Now check the pattern element constraints. If the pattern is
2054 * not able to handle the whole instruction in a single step,
2055 * we have to split it.
2056 * The last_instr_end_off value comes back updated to point to
2057 * the position where we have to split the instruction (the
2058 * start of the next subop chunk).
2059 */
2060 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2061 &instr_offset)) {
2062 ninstrs++;
2063 i++;
2064 break;
2065 }
2066
2067 instr++;
2068 ninstrs++;
2069 instr_offset = 0;
2070 }
2071
2072 /*
2073 * This can happen if all instructions of a pattern are optional.
2074 * Still, if there's not at least one instruction handled by this
2075 * pattern, this is not a match, and we should try the next one (if
2076 * any).
2077 */
2078 if (!ninstrs)
2079 return false;
2080
2081 /*
2082 * We had a match on the pattern head, but the pattern may be longer
2083 * than the instructions we're asked to execute. We need to make sure
2084 * there's no mandatory elements in the pattern tail.
2085 */
2086 for (; i < pat->nelems; i++) {
2087 if (!pat->elems[i].optional)
2088 return false;
2089 }
2090
2091 /*
2092 * We have a match: update the subop structure accordingly and return
2093 * true.
2094 */
2095 ctx->subop.ninstrs = ninstrs;
2096 ctx->subop.last_instr_end_off = instr_offset;
2097
2098 return true;
2099}
2100
2101#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2102static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2103{
2104 const struct nand_op_instr *instr;
2105 char *prefix = " ";
2106 unsigned int i;
2107
2108 pr_debug("executing subop:\n");
2109
2110 for (i = 0; i < ctx->ninstrs; i++) {
2111 instr = &ctx->instrs[i];
2112
2113 if (instr == &ctx->subop.instrs[0])
2114 prefix = " ->";
2115
2116 switch (instr->type) {
2117 case NAND_OP_CMD_INSTR:
2118 pr_debug("%sCMD [0x%02x]\n", prefix,
2119 instr->ctx.cmd.opcode);
2120 break;
2121 case NAND_OP_ADDR_INSTR:
2122 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2123 instr->ctx.addr.naddrs,
2124 instr->ctx.addr.naddrs < 64 ?
2125 instr->ctx.addr.naddrs : 64,
2126 instr->ctx.addr.addrs);
2127 break;
2128 case NAND_OP_DATA_IN_INSTR:
2129 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2130 instr->ctx.data.len,
2131 instr->ctx.data.force_8bit ?
2132 ", force 8-bit" : "");
2133 break;
2134 case NAND_OP_DATA_OUT_INSTR:
2135 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2136 instr->ctx.data.len,
2137 instr->ctx.data.force_8bit ?
2138 ", force 8-bit" : "");
2139 break;
2140 case NAND_OP_WAITRDY_INSTR:
2141 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2142 instr->ctx.waitrdy.timeout_ms);
2143 break;
2144 }
2145
2146 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2147 prefix = " ";
2148 }
2149}
2150#else
2151static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2152{
2153 /* NOP */
2154}
2155#endif
2156
2157/**
2158 * nand_op_parser_exec_op - exec_op parser
2159 * @chip: the NAND chip
2160 * @parser: patterns description provided by the controller driver
2161 * @op: the NAND operation to address
2162 * @check_only: when true, the function only checks if @op can be handled but
2163 * does not execute the operation
2164 *
2165 * Helper function designed to ease integration of NAND controller drivers that
2166 * only support a limited set of instruction sequences. The supported sequences
2167 * are described in @parser, and the framework takes care of splitting @op into
2168 * multiple sub-operations (if required) and pass them back to the ->exec()
2169 * callback of the matching pattern if @check_only is set to false.
2170 *
2171 * NAND controller drivers should call this function from their own ->exec_op()
2172 * implementation.
2173 *
2174 * Returns 0 on success, a negative error code otherwise. A failure can be
2175 * caused by an unsupported operation (none of the supported patterns is able
2176 * to handle the requested operation), or an error returned by one of the
2177 * matching pattern->exec() hook.
2178 */
2179int nand_op_parser_exec_op(struct nand_chip *chip,
2180 const struct nand_op_parser *parser,
2181 const struct nand_operation *op, bool check_only)
2182{
2183 struct nand_op_parser_ctx ctx = {
2184 .subop.instrs = op->instrs,
2185 .instrs = op->instrs,
2186 .ninstrs = op->ninstrs,
2187 };
2188 unsigned int i;
2189
2190 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2191 int ret;
2192
2193 for (i = 0; i < parser->npatterns; i++) {
2194 const struct nand_op_parser_pattern *pattern;
2195
2196 pattern = &parser->patterns[i];
2197 if (!nand_op_parser_match_pat(pattern, &ctx))
2198 continue;
2199
2200 nand_op_parser_trace(&ctx);
2201
2202 if (check_only)
2203 break;
2204
2205 ret = pattern->exec(chip, &ctx.subop);
2206 if (ret)
2207 return ret;
2208
2209 break;
2210 }
2211
2212 if (i == parser->npatterns) {
2213 pr_debug("->exec_op() parser: pattern not found!\n");
2214 return -ENOTSUPP;
2215 }
2216
2217 /*
2218 * Update the context structure by pointing to the start of the
2219 * next subop.
2220 */
2221 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2222 if (ctx.subop.last_instr_end_off)
2223 ctx.subop.instrs -= 1;
2224
2225 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2226 }
2227
2228 return 0;
2229}
2230EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2231
2232static bool nand_instr_is_data(const struct nand_op_instr *instr)
2233{
2234 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2235 instr->type == NAND_OP_DATA_OUT_INSTR);
2236}
2237
2238static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2239 unsigned int instr_idx)
2240{
2241 return subop && instr_idx < subop->ninstrs;
2242}
2243
Miquel Raynal760c4352018-07-19 00:09:12 +02002244static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2245 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002246{
2247 if (instr_idx)
2248 return 0;
2249
2250 return subop->first_instr_start_off;
2251}
2252
2253/**
2254 * nand_subop_get_addr_start_off - Get the start offset in an address array
2255 * @subop: The entire sub-operation
2256 * @instr_idx: Index of the instruction inside the sub-operation
2257 *
2258 * During driver development, one could be tempted to directly use the
2259 * ->addr.addrs field of address instructions. This is wrong as address
2260 * instructions might be split.
2261 *
2262 * Given an address instruction, returns the offset of the first cycle to issue.
2263 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002264unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2265 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002266{
Miquel Raynal760c4352018-07-19 00:09:12 +02002267 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2268 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2269 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002270
2271 return nand_subop_get_start_off(subop, instr_idx);
2272}
2273EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2274
2275/**
2276 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2277 * @subop: The entire sub-operation
2278 * @instr_idx: Index of the instruction inside the sub-operation
2279 *
2280 * During driver development, one could be tempted to directly use the
2281 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2282 * might be split.
2283 *
2284 * Given an address instruction, returns the number of address cycle to issue.
2285 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002286unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2287 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002288{
2289 int start_off, end_off;
2290
Miquel Raynal760c4352018-07-19 00:09:12 +02002291 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2292 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2293 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002294
2295 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2296
2297 if (instr_idx == subop->ninstrs - 1 &&
2298 subop->last_instr_end_off)
2299 end_off = subop->last_instr_end_off;
2300 else
2301 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2302
2303 return end_off - start_off;
2304}
2305EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2306
2307/**
2308 * nand_subop_get_data_start_off - Get the start offset in a data array
2309 * @subop: The entire sub-operation
2310 * @instr_idx: Index of the instruction inside the sub-operation
2311 *
2312 * During driver development, one could be tempted to directly use the
2313 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2314 * instructions might be split.
2315 *
2316 * Given a data instruction, returns the offset to start from.
2317 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002318unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2319 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002320{
Miquel Raynal760c4352018-07-19 00:09:12 +02002321 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2322 !nand_instr_is_data(&subop->instrs[instr_idx])))
2323 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002324
2325 return nand_subop_get_start_off(subop, instr_idx);
2326}
2327EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2328
2329/**
2330 * nand_subop_get_data_len - Get the number of bytes to retrieve
2331 * @subop: The entire sub-operation
2332 * @instr_idx: Index of the instruction inside the sub-operation
2333 *
2334 * During driver development, one could be tempted to directly use the
2335 * ->data->len field of a data instruction. This is wrong as data instructions
2336 * might be split.
2337 *
2338 * Returns the length of the chunk of data to send/receive.
2339 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002340unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2341 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002342{
2343 int start_off = 0, end_off;
2344
Miquel Raynal760c4352018-07-19 00:09:12 +02002345 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2346 !nand_instr_is_data(&subop->instrs[instr_idx])))
2347 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002348
2349 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2350
2351 if (instr_idx == subop->ninstrs - 1 &&
2352 subop->last_instr_end_off)
2353 end_off = subop->last_instr_end_off;
2354 else
2355 end_off = subop->instrs[instr_idx].ctx.data.len;
2356
2357 return end_off - start_off;
2358}
2359EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002362 * nand_reset - Reset and initialize a NAND device
2363 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002364 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002365 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002366 * Save the timings data structure, then apply SDR timings mode 0 (see
2367 * nand_reset_data_interface for details), do the reset operation, and
2368 * apply back the previous timings.
2369 *
2370 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002371 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002372int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002373{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002374 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002375 int ret;
2376
Boris Brezillon104e4422017-03-16 09:35:58 +01002377 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002378 if (ret)
2379 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002380
Boris Brezillon73f907f2016-10-24 16:46:20 +02002381 /*
2382 * The CS line has to be released before we can apply the new NAND
Boris Brezillon1d017852018-11-11 08:55:14 +01002383 * interface settings, hence this weird nand_select_target()
2384 * nand_deselect_target() dance.
Boris Brezillon73f907f2016-10-24 16:46:20 +02002385 */
Boris Brezillon1d017852018-11-11 08:55:14 +01002386 nand_select_target(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002387 ret = nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01002388 nand_deselect_target(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002389 if (ret)
2390 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002391
Miquel Raynal107b7d62018-03-19 14:47:25 +01002392 /*
2393 * A nand_reset_data_interface() put both the NAND chip and the NAND
2394 * controller in timings mode 0. If the default mode for this chip is
2395 * also 0, no need to proceed to the change again. Plus, at probe time,
2396 * nand_setup_data_interface() uses ->set/get_features() which would
2397 * fail anyway as the parameter page is not available yet.
2398 */
2399 if (!chip->onfi_timing_mode_default)
2400 return 0;
2401
Miquel Raynal17fa8042017-11-30 18:01:31 +01002402 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002403 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002404 if (ret)
2405 return ret;
2406
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002407 return 0;
2408}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002409EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002410
2411/**
Boris Brezillon45240362018-09-07 00:38:40 +02002412 * nand_get_features - wrapper to perform a GET_FEATURE
2413 * @chip: NAND chip info structure
2414 * @addr: feature address
2415 * @subfeature_param: the subfeature parameters, a four bytes array
2416 *
2417 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2418 * operation cannot be handled.
2419 */
2420int nand_get_features(struct nand_chip *chip, int addr,
2421 u8 *subfeature_param)
2422{
2423 if (!nand_supports_get_features(chip, addr))
2424 return -ENOTSUPP;
2425
2426 if (chip->legacy.get_features)
2427 return chip->legacy.get_features(chip, addr, subfeature_param);
2428
2429 return nand_get_features_op(chip, addr, subfeature_param);
2430}
Boris Brezillon45240362018-09-07 00:38:40 +02002431
2432/**
2433 * nand_set_features - wrapper to perform a SET_FEATURE
2434 * @chip: NAND chip info structure
2435 * @addr: feature address
2436 * @subfeature_param: the subfeature parameters, a four bytes array
2437 *
2438 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2439 * operation cannot be handled.
2440 */
2441int nand_set_features(struct nand_chip *chip, int addr,
2442 u8 *subfeature_param)
2443{
2444 if (!nand_supports_set_features(chip, addr))
2445 return -ENOTSUPP;
2446
2447 if (chip->legacy.set_features)
2448 return chip->legacy.set_features(chip, addr, subfeature_param);
2449
2450 return nand_set_features_op(chip, addr, subfeature_param);
2451}
Boris Brezillon45240362018-09-07 00:38:40 +02002452
2453/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002454 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2455 * @buf: buffer to test
2456 * @len: buffer length
2457 * @bitflips_threshold: maximum number of bitflips
2458 *
2459 * Check if a buffer contains only 0xff, which means the underlying region
2460 * has been erased and is ready to be programmed.
2461 * The bitflips_threshold specify the maximum number of bitflips before
2462 * considering the region is not erased.
2463 * Note: The logic of this function has been extracted from the memweight
2464 * implementation, except that nand_check_erased_buf function exit before
2465 * testing the whole buffer if the number of bitflips exceed the
2466 * bitflips_threshold value.
2467 *
2468 * Returns a positive number of bitflips less than or equal to
2469 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2470 * threshold.
2471 */
2472static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2473{
2474 const unsigned char *bitmap = buf;
2475 int bitflips = 0;
2476 int weight;
2477
2478 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2479 len--, bitmap++) {
2480 weight = hweight8(*bitmap);
2481 bitflips += BITS_PER_BYTE - weight;
2482 if (unlikely(bitflips > bitflips_threshold))
2483 return -EBADMSG;
2484 }
2485
2486 for (; len >= sizeof(long);
2487 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002488 unsigned long d = *((unsigned long *)bitmap);
2489 if (d == ~0UL)
2490 continue;
2491 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002492 bitflips += BITS_PER_LONG - weight;
2493 if (unlikely(bitflips > bitflips_threshold))
2494 return -EBADMSG;
2495 }
2496
2497 for (; len > 0; len--, bitmap++) {
2498 weight = hweight8(*bitmap);
2499 bitflips += BITS_PER_BYTE - weight;
2500 if (unlikely(bitflips > bitflips_threshold))
2501 return -EBADMSG;
2502 }
2503
2504 return bitflips;
2505}
2506
2507/**
2508 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2509 * 0xff data
2510 * @data: data buffer to test
2511 * @datalen: data length
2512 * @ecc: ECC buffer
2513 * @ecclen: ECC length
2514 * @extraoob: extra OOB buffer
2515 * @extraooblen: extra OOB length
2516 * @bitflips_threshold: maximum number of bitflips
2517 *
2518 * Check if a data buffer and its associated ECC and OOB data contains only
2519 * 0xff pattern, which means the underlying region has been erased and is
2520 * ready to be programmed.
2521 * The bitflips_threshold specify the maximum number of bitflips before
2522 * considering the region as not erased.
2523 *
2524 * Note:
2525 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2526 * different from the NAND page size. When fixing bitflips, ECC engines will
2527 * report the number of errors per chunk, and the NAND core infrastructure
2528 * expect you to return the maximum number of bitflips for the whole page.
2529 * This is why you should always use this function on a single chunk and
2530 * not on the whole page. After checking each chunk you should update your
2531 * max_bitflips value accordingly.
2532 * 2/ When checking for bitflips in erased pages you should not only check
2533 * the payload data but also their associated ECC data, because a user might
2534 * have programmed almost all bits to 1 but a few. In this case, we
2535 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2536 * this case.
2537 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2538 * data are protected by the ECC engine.
2539 * It could also be used if you support subpages and want to attach some
2540 * extra OOB data to an ECC chunk.
2541 *
2542 * Returns a positive number of bitflips less than or equal to
2543 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2544 * threshold. In case of success, the passed buffers are filled with 0xff.
2545 */
2546int nand_check_erased_ecc_chunk(void *data, int datalen,
2547 void *ecc, int ecclen,
2548 void *extraoob, int extraooblen,
2549 int bitflips_threshold)
2550{
2551 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2552
2553 data_bitflips = nand_check_erased_buf(data, datalen,
2554 bitflips_threshold);
2555 if (data_bitflips < 0)
2556 return data_bitflips;
2557
2558 bitflips_threshold -= data_bitflips;
2559
2560 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2561 if (ecc_bitflips < 0)
2562 return ecc_bitflips;
2563
2564 bitflips_threshold -= ecc_bitflips;
2565
2566 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2567 bitflips_threshold);
2568 if (extraoob_bitflips < 0)
2569 return extraoob_bitflips;
2570
2571 if (data_bitflips)
2572 memset(data, 0xff, datalen);
2573
2574 if (ecc_bitflips)
2575 memset(ecc, 0xff, ecclen);
2576
2577 if (extraoob_bitflips)
2578 memset(extraoob, 0xff, extraooblen);
2579
2580 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2581}
2582EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2583
2584/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002585 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002586 * @chip: nand chip info structure
2587 * @buf: buffer to store read data
2588 * @oob_required: caller requires OOB data read to chip->oob_poi
2589 * @page: page number to read
2590 *
2591 * Returns -ENOTSUPP unconditionally.
2592 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002593int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2594 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002595{
2596 return -ENOTSUPP;
2597}
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002598
2599/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002600 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002601 * @chip: nand chip info structure
2602 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002603 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002604 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002605 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002606 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002607 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002608int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2609 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002610{
Boris Brezillonb9761682018-09-06 14:05:20 +02002611 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002612 int ret;
2613
Boris Brezillon25f815f2017-11-30 18:01:30 +01002614 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002615 if (ret)
2616 return ret;
2617
2618 if (oob_required) {
2619 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2620 false);
2621 if (ret)
2622 return ret;
2623 }
2624
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002625 return 0;
2626}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002627EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002628
2629/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002630 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002631 * @chip: nand chip info structure
2632 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002633 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002634 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002635 *
2636 * We need a special oob layout and handling even when OOB isn't used.
2637 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002638static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07002639 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002640{
Boris Brezillonb9761682018-09-06 14:05:20 +02002641 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08002642 int eccsize = chip->ecc.size;
2643 int eccbytes = chip->ecc.bytes;
2644 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002645 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002646
Boris Brezillon25f815f2017-11-30 18:01:30 +01002647 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2648 if (ret)
2649 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002650
2651 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002652 ret = nand_read_data_op(chip, buf, eccsize, false);
2653 if (ret)
2654 return ret;
2655
David Brownell52ff49d2009-03-04 12:01:36 -08002656 buf += eccsize;
2657
2658 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002659 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2660 false);
2661 if (ret)
2662 return ret;
2663
David Brownell52ff49d2009-03-04 12:01:36 -08002664 oob += chip->ecc.prepad;
2665 }
2666
Boris Brezillon97d90da2017-11-30 18:01:29 +01002667 ret = nand_read_data_op(chip, oob, eccbytes, false);
2668 if (ret)
2669 return ret;
2670
David Brownell52ff49d2009-03-04 12:01:36 -08002671 oob += eccbytes;
2672
2673 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002674 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2675 false);
2676 if (ret)
2677 return ret;
2678
David Brownell52ff49d2009-03-04 12:01:36 -08002679 oob += chip->ecc.postpad;
2680 }
2681 }
2682
2683 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002684 if (size) {
2685 ret = nand_read_data_op(chip, oob, size, false);
2686 if (ret)
2687 return ret;
2688 }
David Brownell52ff49d2009-03-04 12:01:36 -08002689
2690 return 0;
2691}
2692
2693/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002694 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002695 * @chip: nand chip info structure
2696 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002697 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002698 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002699 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002700static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
2701 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Boris Brezillonb9761682018-09-06 14:05:20 +02002703 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002704 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002705 int eccbytes = chip->ecc.bytes;
2706 int eccsteps = chip->ecc.steps;
2707 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002708 uint8_t *ecc_calc = chip->ecc.calc_buf;
2709 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002710 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002711
Boris Brezillonb9761682018-09-06 14:05:20 +02002712 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002713
2714 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002715 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002716
Boris Brezillon846031d2016-02-03 20:11:00 +01002717 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2718 chip->ecc.total);
2719 if (ret)
2720 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002721
2722 eccsteps = chip->ecc.steps;
2723 p = buf;
2724
2725 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2726 int stat;
2727
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002728 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07002729 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002730 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002731 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002732 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002733 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2734 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002735 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002736 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01002737}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302740 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002741 * @chip: nand chip info structure
2742 * @data_offs: offset of requested data within the page
2743 * @readlen: data length
2744 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08002745 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01002746 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002747static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
2748 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01002749{
Boris Brezillonb9761682018-09-06 14:05:20 +02002750 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002751 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002752 uint8_t *p;
2753 int data_col_addr, i, gaps = 0;
2754 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
2755 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01002756 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07002757 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01002758 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01002759
Brian Norris7854d3f2011-06-23 14:12:08 -07002760 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01002761 start_step = data_offs / chip->ecc.size;
2762 end_step = (data_offs + readlen - 1) / chip->ecc.size;
2763 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10302764 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01002765
Brian Norris8b6e50c2011-05-25 14:59:01 -07002766 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01002767 datafrag_len = num_steps * chip->ecc.size;
2768 eccfrag_len = num_steps * chip->ecc.bytes;
2769
2770 data_col_addr = start_step * chip->ecc.size;
2771 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01002772 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01002773 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002774 if (ret)
2775 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002776
Brian Norris8b6e50c2011-05-25 14:59:01 -07002777 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01002778 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002779 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01002780
Brian Norris8b6e50c2011-05-25 14:59:01 -07002781 /*
2782 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07002783 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07002784 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002785 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
2786 if (ret)
2787 return ret;
2788
2789 if (oobregion.length < eccfrag_len)
2790 gaps = 1;
2791
Alexey Korolev3d459552008-05-15 17:23:18 +01002792 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002793 ret = nand_change_read_column_op(chip, mtd->writesize,
2794 chip->oob_poi, mtd->oobsize,
2795 false);
2796 if (ret)
2797 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002798 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002799 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002800 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07002801 * about buswidth alignment in read_buf.
2802 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002803 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01002804 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01002805 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002806 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01002807 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
2808 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002809 aligned_len++;
2810
Boris Brezillon97d90da2017-11-30 18:01:29 +01002811 ret = nand_change_read_column_op(chip,
2812 mtd->writesize + aligned_pos,
2813 &chip->oob_poi[aligned_pos],
2814 aligned_len, false);
2815 if (ret)
2816 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002817 }
2818
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002819 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01002820 chip->oob_poi, index, eccfrag_len);
2821 if (ret)
2822 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002823
2824 p = bufpoi + data_col_addr;
2825 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
2826 int stat;
2827
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002828 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002829 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002830 if (stat == -EBADMSG &&
2831 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2832 /* check for empty pages with bitflips */
2833 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002834 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002835 chip->ecc.bytes,
2836 NULL, 0,
2837 chip->ecc.strength);
2838 }
2839
Mike Dunn3f91e942012-04-25 12:06:09 -07002840 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002841 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002842 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01002843 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002844 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2845 }
Alexey Korolev3d459552008-05-15 17:23:18 +01002846 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002847 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01002848}
2849
2850/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002851 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002852 * @chip: nand chip info structure
2853 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002854 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002855 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002856 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002857 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002858 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002859static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
2860 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002861{
Boris Brezillonb9761682018-09-06 14:05:20 +02002862 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002863 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002864 int eccbytes = chip->ecc.bytes;
2865 int eccsteps = chip->ecc.steps;
2866 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002867 uint8_t *ecc_calc = chip->ecc.calc_buf;
2868 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002869 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002870
Boris Brezillon25f815f2017-11-30 18:01:30 +01002871 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2872 if (ret)
2873 return ret;
2874
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002875 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02002876 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002877
2878 ret = nand_read_data_op(chip, p, eccsize, false);
2879 if (ret)
2880 return ret;
2881
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002882 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002883 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002884
2885 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2886 if (ret)
2887 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002888
Boris Brezillon846031d2016-02-03 20:11:00 +01002889 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2890 chip->ecc.total);
2891 if (ret)
2892 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002893
2894 eccsteps = chip->ecc.steps;
2895 p = buf;
2896
2897 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2898 int stat;
2899
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002900 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002901 if (stat == -EBADMSG &&
2902 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2903 /* check for empty pages with bitflips */
2904 stat = nand_check_erased_ecc_chunk(p, eccsize,
2905 &ecc_code[i], eccbytes,
2906 NULL, 0,
2907 chip->ecc.strength);
2908 }
2909
Mike Dunn3f91e942012-04-25 12:06:09 -07002910 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002911 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002912 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002913 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002914 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2915 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002916 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002917 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002918}
2919
2920/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002921 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07002922 * @chip: nand chip info structure
2923 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002924 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002925 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002926 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002927 * Hardware ECC for large page chips, require OOB to be read first. For this
2928 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2929 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2930 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2931 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002932 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002933static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
2934 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002935{
Boris Brezillonb9761682018-09-06 14:05:20 +02002936 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002937 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002938 int eccbytes = chip->ecc.bytes;
2939 int eccsteps = chip->ecc.steps;
2940 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002941 uint8_t *ecc_code = chip->ecc.code_buf;
2942 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002943 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002944
2945 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01002946 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2947 if (ret)
2948 return ret;
2949
2950 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2951 if (ret)
2952 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002953
Boris Brezillon846031d2016-02-03 20:11:00 +01002954 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2955 chip->ecc.total);
2956 if (ret)
2957 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002958
2959 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2960 int stat;
2961
Boris Brezillonec476362018-09-06 14:05:17 +02002962 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002963
2964 ret = nand_read_data_op(chip, p, eccsize, false);
2965 if (ret)
2966 return ret;
2967
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002968 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002969
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002970 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002971 if (stat == -EBADMSG &&
2972 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2973 /* check for empty pages with bitflips */
2974 stat = nand_check_erased_ecc_chunk(p, eccsize,
2975 &ecc_code[i], eccbytes,
2976 NULL, 0,
2977 chip->ecc.strength);
2978 }
2979
Mike Dunn3f91e942012-04-25 12:06:09 -07002980 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002981 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002982 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002983 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002984 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2985 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002986 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002987 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002988}
2989
2990/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002991 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07002992 * @chip: nand chip info structure
2993 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002994 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002995 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002996 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002997 * The hw generator calculates the error syndrome automatically. Therefore we
2998 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002999 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003000static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3001 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003002{
Boris Brezillonb9761682018-09-06 14:05:20 +02003003 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003004 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003005 int eccbytes = chip->ecc.bytes;
3006 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003007 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003008 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003009 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003010 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003011
Boris Brezillon25f815f2017-11-30 18:01:30 +01003012 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3013 if (ret)
3014 return ret;
3015
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003016 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3017 int stat;
3018
Boris Brezillonec476362018-09-06 14:05:17 +02003019 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003020
3021 ret = nand_read_data_op(chip, p, eccsize, false);
3022 if (ret)
3023 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003024
3025 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003026 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3027 false);
3028 if (ret)
3029 return ret;
3030
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003031 oob += chip->ecc.prepad;
3032 }
3033
Boris Brezillonec476362018-09-06 14:05:17 +02003034 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003035
3036 ret = nand_read_data_op(chip, oob, eccbytes, false);
3037 if (ret)
3038 return ret;
3039
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003040 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003041
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003042 oob += eccbytes;
3043
3044 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003045 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3046 false);
3047 if (ret)
3048 return ret;
3049
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003050 oob += chip->ecc.postpad;
3051 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003052
3053 if (stat == -EBADMSG &&
3054 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3055 /* check for empty pages with bitflips */
3056 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3057 oob - eccpadbytes,
3058 eccpadbytes,
3059 NULL, 0,
3060 chip->ecc.strength);
3061 }
3062
3063 if (stat < 0) {
3064 mtd->ecc_stats.failed++;
3065 } else {
3066 mtd->ecc_stats.corrected += stat;
3067 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3068 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003069 }
3070
3071 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003072 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003073 if (i) {
3074 ret = nand_read_data_op(chip, oob, i, false);
3075 if (ret)
3076 return ret;
3077 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003078
Mike Dunn3f91e942012-04-25 12:06:09 -07003079 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003080}
3081
3082/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003083 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon08136212018-11-11 08:55:03 +01003084 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003085 * @oob: oob destination address
3086 * @ops: oob ops structure
3087 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003088 */
Boris Brezillon08136212018-11-11 08:55:03 +01003089static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003090 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003091{
Boris Brezillon08136212018-11-11 08:55:03 +01003092 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003093 int ret;
3094
Florian Fainellif8ac0412010-09-07 13:23:43 +02003095 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003096
Brian Norris0612b9d2011-08-30 18:45:40 -07003097 case MTD_OPS_PLACE_OOB:
3098 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003099 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3100 return oob + len;
3101
Boris Brezillon846031d2016-02-03 20:11:00 +01003102 case MTD_OPS_AUTO_OOB:
3103 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3104 ops->ooboffs, len);
3105 BUG_ON(ret);
3106 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003107
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003108 default:
3109 BUG();
3110 }
3111 return NULL;
3112}
3113
3114/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003115 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003116 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003117 * @retry_mode: the retry mode to use
3118 *
3119 * Some vendors supply a special command to shift the Vt threshold, to be used
3120 * when there are too many bitflips in a page (i.e., ECC error). After setting
3121 * a new threshold, the host should retry reading the page.
3122 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003123static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003124{
Brian Norrisba84fb52014-01-03 15:13:33 -08003125 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3126
3127 if (retry_mode >= chip->read_retries)
3128 return -EINVAL;
3129
3130 if (!chip->setup_read_retry)
3131 return -EOPNOTSUPP;
3132
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003133 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003134}
3135
Boris Brezillon85e08e52018-07-27 09:44:17 +02003136static void nand_wait_readrdy(struct nand_chip *chip)
3137{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003138 const struct nand_sdr_timings *sdr;
3139
Boris Brezillon85e08e52018-07-27 09:44:17 +02003140 if (!(chip->options & NAND_NEED_READRDY))
3141 return;
3142
Boris Brezillon52f05b62018-07-27 09:44:18 +02003143 sdr = nand_get_sdr_timings(&chip->data_interface);
3144 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003145}
3146
Brian Norrisba84fb52014-01-03 15:13:33 -08003147/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003148 * nand_do_read_ops - [INTERN] Read data with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003149 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003150 * @from: offset to read from
3151 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003152 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003153 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003154 */
Boris Brezillon08136212018-11-11 08:55:03 +01003155static int nand_do_read_ops(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003156 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003157{
Brian Norrise47f3db2012-05-02 10:14:56 -07003158 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris Brezillon08136212018-11-11 08:55:03 +01003159 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003160 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003161 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003162 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003163 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003164
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003165 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003166 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003167 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003168 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003169 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003171 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003172 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003174 realpage = (int)(from >> chip->page_shift);
3175 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003177 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003179 buf = ops->datbuf;
3180 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003181 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003182
Florian Fainellif8ac0412010-09-07 13:23:43 +02003183 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003184 unsigned int ecc_failures = mtd->ecc_stats.failed;
3185
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003186 bytes = min(mtd->writesize - col, readlen);
3187 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003188
Kamal Dasu66507c72014-05-01 20:51:19 -04003189 if (!aligned)
3190 use_bufpoi = 1;
3191 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003192 use_bufpoi = !virt_addr_valid(buf) ||
3193 !IS_ALIGNED((unsigned long)buf,
3194 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003195 else
3196 use_bufpoi = 0;
3197
Brian Norris8b6e50c2011-05-25 14:59:01 -07003198 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003199 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003200 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003201
3202 if (use_bufpoi && aligned)
3203 pr_debug("%s: using read bounce buffer for buf@%p\n",
3204 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Brian Norrisba84fb52014-01-03 15:13:33 -08003206read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003207 /*
3208 * Now read the page into the buffer. Absent an error,
3209 * the read methods return max bitflips per ecc step.
3210 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003211 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003212 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003213 oob_required,
3214 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003215 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3216 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003217 ret = chip->ecc.read_subpage(chip, col, bytes,
3218 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003219 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003220 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003221 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003222 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003223 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003224 /* Invalidate page cache */
3225 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003226 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003227 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003228
3229 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003230 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003231 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003232 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003233 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003234 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003235 chip->pagebuf_bitflips = ret;
3236 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003237 /* Invalidate page cache */
3238 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003239 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003240 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003242
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003243 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003244 int toread = min(oobreadlen, max_oobsize);
3245
3246 if (toread) {
Boris Brezillon08136212018-11-11 08:55:03 +01003247 oob = nand_transfer_oob(chip, oob, ops,
3248 toread);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003249 oobreadlen -= toread;
3250 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003251 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003252
Boris Brezillon85e08e52018-07-27 09:44:17 +02003253 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003254
Brian Norrisba84fb52014-01-03 15:13:33 -08003255 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003256 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003257 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003258 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003259 retry_mode);
3260 if (ret < 0)
3261 break;
3262
3263 /* Reset failures; retry */
3264 mtd->ecc_stats.failed = ecc_failures;
3265 goto read_retry;
3266 } else {
3267 /* No more retry modes; real failure */
3268 ecc_fail = true;
3269 }
3270 }
3271
3272 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003273 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003274 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003275 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003276 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003277 max_bitflips = max_t(unsigned int, max_bitflips,
3278 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003281 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003282
Brian Norrisba84fb52014-01-03 15:13:33 -08003283 /* Reset to retry mode 0 */
3284 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003285 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003286 if (ret < 0)
3287 break;
3288 retry_mode = 0;
3289 }
3290
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003291 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Brian Norris8b6e50c2011-05-25 14:59:01 -07003294 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 col = 0;
3296 /* Increment page address */
3297 realpage++;
3298
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003299 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 /* Check, if we cross a chip boundary */
3301 if (!page) {
3302 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003303 nand_deselect_target(chip);
3304 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003307 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003309 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003310 if (oob)
3311 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Mike Dunn3f91e942012-04-25 12:06:09 -07003313 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003314 return ret;
3315
Brian Norrisb72f3df2013-12-03 11:04:14 -08003316 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003317 return -EBADMSG;
3318
Mike Dunnedbc45402012-04-25 12:06:11 -07003319 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003320}
3321
3322/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003323 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003324 * @chip: nand chip info structure
3325 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003326 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003327int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003328{
Boris Brezillonb9761682018-09-06 14:05:20 +02003329 struct mtd_info *mtd = nand_to_mtd(chip);
3330
Boris Brezillon97d90da2017-11-30 18:01:29 +01003331 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003332}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003333EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003334
3335/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003336 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003337 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003338 * @chip: nand chip info structure
3339 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003340 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003341static int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003342{
Boris Brezillonb9761682018-09-06 14:05:20 +02003343 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003344 int length = mtd->oobsize;
3345 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3346 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003347 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003348 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003349
Boris Brezillon97d90da2017-11-30 18:01:29 +01003350 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3351 if (ret)
3352 return ret;
3353
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003354 for (i = 0; i < chip->ecc.steps; i++) {
3355 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003356 int ret;
3357
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003358 pos = eccsize + i * (eccsize + chunk);
3359 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003360 ret = nand_change_read_column_op(chip, pos,
3361 NULL, 0,
3362 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003363 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003364 ret = nand_read_page_op(chip, page, pos, NULL,
3365 0);
3366
3367 if (ret)
3368 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003369 } else
3370 sndrnd = 1;
3371 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003372
3373 ret = nand_read_data_op(chip, bufpoi, toread, false);
3374 if (ret)
3375 return ret;
3376
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003377 bufpoi += toread;
3378 length -= toread;
3379 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003380 if (length > 0) {
3381 ret = nand_read_data_op(chip, bufpoi, length, false);
3382 if (ret)
3383 return ret;
3384 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003385
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003386 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003387}
3388
3389/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003390 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003391 * @chip: nand chip info structure
3392 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003393 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003394int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003395{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003396 struct mtd_info *mtd = nand_to_mtd(chip);
3397
Boris Brezillon97d90da2017-11-30 18:01:29 +01003398 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3399 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003400}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003401EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003402
3403/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003404 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003405 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003406 * @chip: nand chip info structure
3407 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003408 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003409static int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003410{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003411 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003412 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3413 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003414 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003415 const uint8_t *bufpoi = chip->oob_poi;
3416
3417 /*
3418 * data-ecc-data-ecc ... ecc-oob
3419 * or
3420 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3421 */
3422 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3423 pos = steps * (eccsize + chunk);
3424 steps = 0;
3425 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003426 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003427
Boris Brezillon97d90da2017-11-30 18:01:29 +01003428 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3429 if (ret)
3430 return ret;
3431
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003432 for (i = 0; i < steps; i++) {
3433 if (sndcmd) {
3434 if (mtd->writesize <= 512) {
3435 uint32_t fill = 0xFFFFFFFF;
3436
3437 len = eccsize;
3438 while (len > 0) {
3439 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003440
3441 ret = nand_write_data_op(chip, &fill,
3442 num, false);
3443 if (ret)
3444 return ret;
3445
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003446 len -= num;
3447 }
3448 } else {
3449 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003450 ret = nand_change_write_column_op(chip, pos,
3451 NULL, 0,
3452 false);
3453 if (ret)
3454 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003455 }
3456 } else
3457 sndcmd = 1;
3458 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003459
3460 ret = nand_write_data_op(chip, bufpoi, len, false);
3461 if (ret)
3462 return ret;
3463
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003464 bufpoi += len;
3465 length -= len;
3466 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003467 if (length > 0) {
3468 ret = nand_write_data_op(chip, bufpoi, length, false);
3469 if (ret)
3470 return ret;
3471 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003472
Boris Brezillon97d90da2017-11-30 18:01:29 +01003473 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003474}
3475
3476/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003477 * nand_do_read_oob - [INTERN] NAND read out-of-band
Boris Brezillon08136212018-11-11 08:55:03 +01003478 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003479 * @from: offset to read from
3480 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003482 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 */
Boris Brezillon08136212018-11-11 08:55:03 +01003484static int nand_do_read_oob(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003485 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Boris Brezillon08136212018-11-11 08:55:03 +01003487 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003488 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003489 int page, realpage, chipnr;
Brian Norris041e4572011-06-23 16:45:24 -07003490 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003491 int readlen = ops->ooblen;
3492 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003493 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003494 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Brian Norris289c0522011-07-19 10:06:09 -07003496 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303497 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Brian Norris041e4572011-06-23 16:45:24 -07003499 stats = mtd->ecc_stats;
3500
Boris BREZILLON29f10582016-03-07 10:46:52 +01003501 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003502
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003503 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003504 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003506 /* Shift to get page */
3507 realpage = (int)(from >> chip->page_shift);
3508 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Florian Fainellif8ac0412010-09-07 13:23:43 +02003510 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003511 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003512 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003513 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003514 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003515
3516 if (ret < 0)
3517 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003518
3519 len = min(len, readlen);
Boris Brezillon08136212018-11-11 08:55:03 +01003520 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003521
Boris Brezillon85e08e52018-07-27 09:44:17 +02003522 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003523
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003524 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3525
Vitaly Wool70145682006-11-03 18:20:38 +03003526 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003527 if (!readlen)
3528 break;
3529
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003530 /* Increment page address */
3531 realpage++;
3532
3533 page = realpage & chip->pagemask;
3534 /* Check, if we cross a chip boundary */
3535 if (!page) {
3536 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003537 nand_deselect_target(chip);
3538 nand_select_target(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003541 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003543 ops->oobretlen = ops->ooblen - readlen;
3544
3545 if (ret < 0)
3546 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003547
3548 if (mtd->ecc_stats.failed - stats.failed)
3549 return -EBADMSG;
3550
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003551 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
3553
3554/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003555 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003556 * @mtd: MTD device structure
3557 * @from: offset to read from
3558 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003560 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003562static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3563 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
Boris Brezillon08136212018-11-11 08:55:03 +01003565 struct nand_chip *chip = mtd_to_nand(mtd);
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003566 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003567
3568 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003570 if (ops->mode != MTD_OPS_PLACE_OOB &&
3571 ops->mode != MTD_OPS_AUTO_OOB &&
3572 ops->mode != MTD_OPS_RAW)
3573 return -ENOTSUPP;
3574
Boris Brezillon08136212018-11-11 08:55:03 +01003575 nand_get_device(chip, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003577 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01003578 ret = nand_do_read_oob(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003579 else
Boris Brezillon08136212018-11-11 08:55:03 +01003580 ret = nand_do_read_ops(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003581
Boris Brezillon08136212018-11-11 08:55:03 +01003582 nand_release_device(chip);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003583 return ret;
3584}
3585
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003586/**
3587 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003588 * @chip: nand chip info structure
3589 * @buf: data buffer
3590 * @oob_required: must write chip->oob_poi to OOB
3591 * @page: page number to write
3592 *
3593 * Returns -ENOTSUPP unconditionally.
3594 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003595int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3596 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003597{
3598 return -ENOTSUPP;
3599}
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003600
3601/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003602 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003603 * @chip: nand chip info structure
3604 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003605 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003606 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003607 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003608 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003609 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003610int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3611 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003612{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003613 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003614 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003615
Boris Brezillon25f815f2017-11-30 18:01:30 +01003616 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003617 if (ret)
3618 return ret;
3619
3620 if (oob_required) {
3621 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3622 false);
3623 if (ret)
3624 return ret;
3625 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003626
Boris Brezillon25f815f2017-11-30 18:01:30 +01003627 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003629EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003631/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003632 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003633 * @chip: nand chip info structure
3634 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003635 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003636 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003637 *
3638 * We need a special oob layout and handling even when ECC isn't checked.
3639 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003640static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003641 const uint8_t *buf, int oob_required,
3642 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003643{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003644 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003645 int eccsize = chip->ecc.size;
3646 int eccbytes = chip->ecc.bytes;
3647 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003648 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003649
Boris Brezillon25f815f2017-11-30 18:01:30 +01003650 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3651 if (ret)
3652 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003653
3654 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003655 ret = nand_write_data_op(chip, buf, eccsize, false);
3656 if (ret)
3657 return ret;
3658
David Brownell52ff49d2009-03-04 12:01:36 -08003659 buf += eccsize;
3660
3661 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003662 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3663 false);
3664 if (ret)
3665 return ret;
3666
David Brownell52ff49d2009-03-04 12:01:36 -08003667 oob += chip->ecc.prepad;
3668 }
3669
Boris Brezillon97d90da2017-11-30 18:01:29 +01003670 ret = nand_write_data_op(chip, oob, eccbytes, false);
3671 if (ret)
3672 return ret;
3673
David Brownell52ff49d2009-03-04 12:01:36 -08003674 oob += eccbytes;
3675
3676 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003677 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3678 false);
3679 if (ret)
3680 return ret;
3681
David Brownell52ff49d2009-03-04 12:01:36 -08003682 oob += chip->ecc.postpad;
3683 }
3684 }
3685
3686 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003687 if (size) {
3688 ret = nand_write_data_op(chip, oob, size, false);
3689 if (ret)
3690 return ret;
3691 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003692
Boris Brezillon25f815f2017-11-30 18:01:30 +01003693 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003694}
3695/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003696 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003697 * @chip: nand chip info structure
3698 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003699 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003700 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003701 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003702static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
3703 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003704{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003705 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003706 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003707 int eccbytes = chip->ecc.bytes;
3708 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003709 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003710 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003711
Brian Norris7854d3f2011-06-23 14:12:08 -07003712 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003713 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003714 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003715
Boris Brezillon846031d2016-02-03 20:11:00 +01003716 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3717 chip->ecc.total);
3718 if (ret)
3719 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003720
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003721 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003722}
3723
3724/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003725 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003726 * @chip: nand chip info structure
3727 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003728 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003729 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003730 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003731static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
3732 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003733{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003734 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003735 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003736 int eccbytes = chip->ecc.bytes;
3737 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003738 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003739 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003740
Boris Brezillon25f815f2017-11-30 18:01:30 +01003741 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3742 if (ret)
3743 return ret;
3744
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003745 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003746 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003747
3748 ret = nand_write_data_op(chip, p, eccsize, false);
3749 if (ret)
3750 return ret;
3751
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003752 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003753 }
3754
Boris Brezillon846031d2016-02-03 20:11:00 +01003755 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3756 chip->ecc.total);
3757 if (ret)
3758 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003759
Boris Brezillon97d90da2017-11-30 18:01:29 +01003760 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3761 if (ret)
3762 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003763
Boris Brezillon25f815f2017-11-30 18:01:30 +01003764 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003765}
3766
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303767
3768/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08003769 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303770 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07003771 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303772 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07003773 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303774 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003775 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303776 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003777static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
3778 uint32_t data_len, const uint8_t *buf,
3779 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303780{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003781 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303782 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003783 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303784 int ecc_size = chip->ecc.size;
3785 int ecc_bytes = chip->ecc.bytes;
3786 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303787 uint32_t start_step = offset / ecc_size;
3788 uint32_t end_step = (offset + data_len - 1) / ecc_size;
3789 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01003790 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303791
Boris Brezillon25f815f2017-11-30 18:01:30 +01003792 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3793 if (ret)
3794 return ret;
3795
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303796 for (step = 0; step < ecc_steps; step++) {
3797 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02003798 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303799
3800 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003801 ret = nand_write_data_op(chip, buf, ecc_size, false);
3802 if (ret)
3803 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303804
3805 /* mask ECC of un-touched subpages by padding 0xFF */
3806 if ((step < start_step) || (step > end_step))
3807 memset(ecc_calc, 0xff, ecc_bytes);
3808 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003809 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303810
3811 /* mask OOB of un-touched subpages by padding 0xFF */
3812 /* if oob_required, preserve OOB metadata of written subpage */
3813 if (!oob_required || (step < start_step) || (step > end_step))
3814 memset(oob_buf, 0xff, oob_bytes);
3815
Brian Norrisd6a950802013-08-08 17:16:36 -07003816 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303817 ecc_calc += ecc_bytes;
3818 oob_buf += oob_bytes;
3819 }
3820
3821 /* copy calculated ECC for whole page to chip->buffer->oob */
3822 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003823 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01003824 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3825 chip->ecc.total);
3826 if (ret)
3827 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303828
3829 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003830 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3831 if (ret)
3832 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303833
Boris Brezillon25f815f2017-11-30 18:01:30 +01003834 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303835}
3836
3837
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003838/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003839 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003840 * @chip: nand chip info structure
3841 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003842 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003843 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003844 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003845 * The hw generator calculates the error syndrome automatically. Therefore we
3846 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003847 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003848static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
3849 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003850{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003851 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003852 int i, eccsize = chip->ecc.size;
3853 int eccbytes = chip->ecc.bytes;
3854 int eccsteps = chip->ecc.steps;
3855 const uint8_t *p = buf;
3856 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003857 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003858
Boris Brezillon25f815f2017-11-30 18:01:30 +01003859 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3860 if (ret)
3861 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003862
3863 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003864 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003865
3866 ret = nand_write_data_op(chip, p, eccsize, false);
3867 if (ret)
3868 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003869
3870 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003871 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3872 false);
3873 if (ret)
3874 return ret;
3875
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003876 oob += chip->ecc.prepad;
3877 }
3878
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003879 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003880
3881 ret = nand_write_data_op(chip, oob, eccbytes, false);
3882 if (ret)
3883 return ret;
3884
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003885 oob += eccbytes;
3886
3887 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003888 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3889 false);
3890 if (ret)
3891 return ret;
3892
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003893 oob += chip->ecc.postpad;
3894 }
3895 }
3896
3897 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003898 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003899 if (i) {
3900 ret = nand_write_data_op(chip, oob, i, false);
3901 if (ret)
3902 return ret;
3903 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003904
Boris Brezillon25f815f2017-11-30 18:01:30 +01003905 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003906}
3907
3908/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003909 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07003910 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303911 * @offset: address offset within the page
3912 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07003913 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07003914 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07003915 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003916 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003917 */
Boris Brezillon08136212018-11-11 08:55:03 +01003918static int nand_write_page(struct nand_chip *chip, uint32_t offset,
3919 int data_len, const uint8_t *buf, int oob_required,
3920 int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003921{
Boris Brezillon08136212018-11-11 08:55:03 +01003922 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303923 int status, subpage;
3924
3925 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3926 chip->ecc.write_subpage)
3927 subpage = offset || (data_len < mtd->writesize);
3928 else
3929 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003930
David Woodhouse956e9442006-09-25 17:12:39 +01003931 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003932 status = chip->ecc.write_page_raw(chip, buf, oob_required,
3933 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303934 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003935 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
3936 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003937 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003938 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08003939
3940 if (status < 0)
3941 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003942
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003943 return 0;
3944}
3945
Florian Fainellif8ac0412010-09-07 13:23:43 +02003946#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003947
3948/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003949 * nand_do_write_ops - [INTERN] NAND write with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003950 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003951 * @to: offset to write to
3952 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003953 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003954 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003955 */
Boris Brezillon08136212018-11-11 08:55:03 +01003956static int nand_do_write_ops(struct nand_chip *chip, loff_t to,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003957 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003958{
Boris Brezillon08136212018-11-11 08:55:03 +01003959 struct mtd_info *mtd = nand_to_mtd(chip);
Corentin Labbe73600b62017-09-02 10:49:38 +02003960 int chipnr, realpage, page, column;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003961 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02003962
3963 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003964 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003965
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003966 uint8_t *oob = ops->oobbuf;
3967 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303968 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07003969 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003970
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003971 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003972 if (!writelen)
3973 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003974
Brian Norris8b6e50c2011-05-25 14:59:01 -07003975 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003976 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003977 pr_notice("%s: attempt to write non page aligned data\n",
3978 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003979 return -EINVAL;
3980 }
3981
Thomas Gleixner29072b92006-09-28 15:38:36 +02003982 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003983
Thomas Gleixner6a930962006-06-28 00:11:45 +02003984 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003985 nand_select_target(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02003986
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003987 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01003988 if (nand_check_wp(chip)) {
Huang Shijieb0bb6902012-11-19 14:43:29 +08003989 ret = -EIO;
3990 goto err_out;
3991 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003992
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003993 realpage = (int)(to >> chip->page_shift);
3994 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003995
3996 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07003997 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3998 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003999 chip->pagebuf = -1;
4000
Maxim Levitsky782ce792010-02-22 20:39:36 +02004001 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004002 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4003 ret = -EINVAL;
4004 goto err_out;
4005 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004006
Florian Fainellif8ac0412010-09-07 13:23:43 +02004007 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004008 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004009 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004010 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004011 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004012
Kamal Dasu66507c72014-05-01 20:51:19 -04004013 if (part_pagewr)
4014 use_bufpoi = 1;
4015 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004016 use_bufpoi = !virt_addr_valid(buf) ||
4017 !IS_ALIGNED((unsigned long)buf,
4018 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004019 else
4020 use_bufpoi = 0;
4021
4022 /* Partial page write?, or need to use bounce buffer */
4023 if (use_bufpoi) {
4024 pr_debug("%s: using write bounce buffer for buf@%p\n",
4025 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004026 if (part_pagewr)
4027 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004028 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004029 memset(chip->data_buf, 0xff, mtd->writesize);
4030 memcpy(&chip->data_buf[column], buf, bytes);
4031 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004032 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004033
Maxim Levitsky782ce792010-02-22 20:39:36 +02004034 if (unlikely(oob)) {
4035 size_t len = min(oobwritelen, oobmaxlen);
Boris Brezillon08136212018-11-11 08:55:03 +01004036 oob = nand_fill_oob(chip, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004037 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004038 } else {
4039 /* We still need to erase leftover OOB data */
4040 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004041 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004042
Boris Brezillon08136212018-11-11 08:55:03 +01004043 ret = nand_write_page(chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004044 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004045 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004046 if (ret)
4047 break;
4048
4049 writelen -= bytes;
4050 if (!writelen)
4051 break;
4052
Thomas Gleixner29072b92006-09-28 15:38:36 +02004053 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004054 buf += bytes;
4055 realpage++;
4056
4057 page = realpage & chip->pagemask;
4058 /* Check, if we cross a chip boundary */
4059 if (!page) {
4060 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004061 nand_deselect_target(chip);
4062 nand_select_target(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004063 }
4064 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004065
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004066 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004067 if (unlikely(oob))
4068 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004069
4070err_out:
Boris Brezillon1d017852018-11-11 08:55:14 +01004071 nand_deselect_target(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004072 return ret;
4073}
4074
4075/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004076 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004077 * @mtd: MTD device structure
4078 * @to: offset to write to
4079 * @len: number of bytes to write
4080 * @retlen: pointer to variable to store the number of written bytes
4081 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004082 *
4083 * NAND write with ECC. Used when performing writes in interrupt context, this
4084 * may for example be called by mtdoops when writing an oops while in panic.
4085 */
4086static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4087 size_t *retlen, const uint8_t *buf)
4088{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004089 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004090 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004091 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004092 int ret;
4093
Brian Norris8b6e50c2011-05-25 14:59:01 -07004094 /* Grab the device */
Boris Brezillon08136212018-11-11 08:55:03 +01004095 panic_nand_get_device(chip, FL_WRITING);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004096
Boris Brezillon1d017852018-11-11 08:55:14 +01004097 nand_select_target(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004098
4099 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004100 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004101
Brian Norris0ec56dc2015-02-28 02:02:30 -08004102 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004103 ops.len = len;
4104 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004105 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004106
Boris Brezillon08136212018-11-11 08:55:03 +01004107 ret = nand_do_write_ops(chip, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004108
Brian Norris4a89ff82011-08-30 18:45:45 -07004109 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004110 return ret;
4111}
4112
4113/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004114 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004115 * @mtd: MTD device structure
4116 * @to: offset to write to
4117 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004118 */
4119static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4120 struct mtd_oob_ops *ops)
4121{
Boris Brezillon08136212018-11-11 08:55:03 +01004122 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004123 int ret = -ENOTSUPP;
4124
4125 ops->retlen = 0;
4126
Boris Brezillon08136212018-11-11 08:55:03 +01004127 nand_get_device(chip, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004128
Florian Fainellif8ac0412010-09-07 13:23:43 +02004129 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004130 case MTD_OPS_PLACE_OOB:
4131 case MTD_OPS_AUTO_OOB:
4132 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004133 break;
4134
4135 default:
4136 goto out;
4137 }
4138
4139 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01004140 ret = nand_do_write_oob(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004141 else
Boris Brezillon08136212018-11-11 08:55:03 +01004142 ret = nand_do_write_ops(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004143
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004144out:
Boris Brezillon08136212018-11-11 08:55:03 +01004145 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 return ret;
4147}
4148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149/**
Brian Norris49c50b92014-05-06 16:02:19 -07004150 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004151 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004152 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 *
Brian Norris49c50b92014-05-06 16:02:19 -07004154 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004156static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004158 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004159
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004161 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004162
Boris Brezillon97d90da2017-11-30 18:01:29 +01004163 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
4166/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004168 * @mtd: MTD device structure
4169 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004171 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004173static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004175 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004179 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004180 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004181 * @instr: erase instruction
4182 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004184 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004186int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004187 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188{
Adrian Hunter69423d92008-12-10 13:37:21 +00004189 int page, status, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004190 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Brian Norris289c0522011-07-19 10:06:09 -07004192 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4193 __func__, (unsigned long long)instr->addr,
4194 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Boris Brezillon08136212018-11-11 08:55:03 +01004196 if (check_offs_len(chip, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 /* Grab the lock and see if the device is available */
Boris Brezillon08136212018-11-11 08:55:03 +01004200 nand_get_device(chip, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
4202 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004203 page = (int)(instr->addr >> chip->page_shift);
4204 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
4206 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004207 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
4209 /* Select the NAND device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004210 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01004213 if (nand_check_wp(chip)) {
Brian Norris289c0522011-07-19 10:06:09 -07004214 pr_debug("%s: device is write protected!\n",
4215 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004216 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 goto erase_exit;
4218 }
4219
4220 /* Loop through the pages */
4221 len = instr->len;
4222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004224 /* Check if we have a bad block, we do not erase bad blocks! */
Boris Brezillon08136212018-11-11 08:55:03 +01004225 if (nand_block_checkbad(chip, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304226 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004227 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4228 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004229 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 goto erase_exit;
4231 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004232
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004233 /*
4234 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004235 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004236 */
4237 if (page <= chip->pagebuf && chip->pagebuf <
4238 (page + pages_per_block))
4239 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
Boris Brezillonf9ebd1b2018-09-07 00:38:39 +02004241 if (chip->legacy.erase)
4242 status = chip->legacy.erase(chip,
4243 page & chip->pagemask);
4244 else
4245 status = single_erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
4247 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004248 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004249 pr_debug("%s: failed erase, page 0x%08x\n",
4250 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004251 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004252 instr->fail_addr =
4253 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 goto erase_exit;
4255 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004258 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 page += pages_per_block;
4260
4261 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004262 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004264 nand_deselect_target(chip);
4265 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 }
4267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004269 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004270erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004273 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004274 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 /* Return more or less happy */
4277 return ret;
4278}
4279
4280/**
4281 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004282 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004284 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004286static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287{
Boris Brezillon08136212018-11-11 08:55:03 +01004288 struct nand_chip *chip = mtd_to_nand(mtd);
4289
Brian Norris289c0522011-07-19 10:06:09 -07004290 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
4292 /* Grab the lock and see if the device is available */
Boris Brezillon08136212018-11-11 08:55:03 +01004293 nand_get_device(chip, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 /* Release it and go back */
Boris Brezillon08136212018-11-11 08:55:03 +01004295 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004299 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004300 * @mtd: MTD device structure
4301 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004303static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304305 struct nand_chip *chip = mtd_to_nand(mtd);
4306 int chipnr = (int)(offs >> chip->chip_shift);
4307 int ret;
4308
4309 /* Select the NAND device */
Boris Brezillon08136212018-11-11 08:55:03 +01004310 nand_get_device(chip, FL_READING);
Boris Brezillon1d017852018-11-11 08:55:14 +01004311 nand_select_target(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304312
Boris Brezillon08136212018-11-11 08:55:03 +01004313 ret = nand_block_checkbad(chip, offs, 0);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304314
Boris Brezillon1d017852018-11-11 08:55:14 +01004315 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004316 nand_release_device(chip);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304317
4318 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319}
4320
4321/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004322 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004323 * @mtd: MTD device structure
4324 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004326static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 int ret;
4329
Florian Fainellif8ac0412010-09-07 13:23:43 +02004330 ret = nand_block_isbad(mtd, ofs);
4331 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004332 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 if (ret > 0)
4334 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004335 return ret;
4336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
Boris Brezillon08136212018-11-11 08:55:03 +01004338 return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
4341/**
Zach Brown56718422017-01-10 13:30:20 -06004342 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4343 * @mtd: MTD device structure
4344 * @ofs: offset relative to mtd start
4345 * @len: length of mtd
4346 */
4347static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4348{
4349 struct nand_chip *chip = mtd_to_nand(mtd);
4350 u32 part_start_block;
4351 u32 part_end_block;
4352 u32 part_start_die;
4353 u32 part_end_die;
4354
4355 /*
4356 * max_bb_per_die and blocks_per_die used to determine
4357 * the maximum bad block count.
4358 */
4359 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4360 return -ENOTSUPP;
4361
4362 /* Get the start and end of the partition in erase blocks. */
4363 part_start_block = mtd_div_by_eb(ofs, mtd);
4364 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4365
4366 /* Get the start and end LUNs of the partition. */
4367 part_start_die = part_start_block / chip->blocks_per_die;
4368 part_end_die = part_end_block / chip->blocks_per_die;
4369
4370 /*
4371 * Look up the bad blocks per unit and multiply by the number of units
4372 * that the partition spans.
4373 */
4374 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4375}
4376
4377/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004378 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004379 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004380 */
4381static int nand_suspend(struct mtd_info *mtd)
4382{
Boris Brezillon08136212018-11-11 08:55:03 +01004383 return nand_get_device(mtd_to_nand(mtd), FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004384}
4385
4386/**
4387 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004388 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004389 */
4390static void nand_resume(struct mtd_info *mtd)
4391{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004392 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004393
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004394 if (chip->state == FL_PM_SUSPENDED)
Boris Brezillon08136212018-11-11 08:55:03 +01004395 nand_release_device(chip);
Vitaly Wool962034f2005-09-15 14:58:53 +01004396 else
Brian Norrisd0370212011-07-19 10:06:08 -07004397 pr_err("%s called for a chip which is not in suspended state\n",
4398 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004399}
4400
Scott Branden72ea4032014-11-20 11:18:05 -08004401/**
4402 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4403 * prevent further operations
4404 * @mtd: MTD device structure
4405 */
4406static void nand_shutdown(struct mtd_info *mtd)
4407{
Boris Brezillon08136212018-11-11 08:55:03 +01004408 nand_get_device(mtd_to_nand(mtd), FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004409}
4410
Brian Norris8b6e50c2011-05-25 14:59:01 -07004411/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004412static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004413{
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004414 nand_legacy_set_defaults(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004415
4416 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004417 chip->controller = &chip->dummy_controller;
4418 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004419 }
4420
Masahiro Yamada477544c2017-03-30 17:15:05 +09004421 if (!chip->buf_align)
4422 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004423}
4424
Brian Norris8b6e50c2011-05-25 14:59:01 -07004425/* Sanitize ONFI strings so we can safely print them */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004426void sanitize_string(uint8_t *s, size_t len)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004427{
4428 ssize_t i;
4429
Brian Norris8b6e50c2011-05-25 14:59:01 -07004430 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004431 s[len - 1] = 0;
4432
Brian Norris8b6e50c2011-05-25 14:59:01 -07004433 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004434 for (i = 0; i < len - 1; i++) {
4435 if (s[i] < ' ' || s[i] > 127)
4436 s[i] = '?';
4437 }
4438
Brian Norris8b6e50c2011-05-25 14:59:01 -07004439 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004440 strim(s);
4441}
4442
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004443/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07004444 * nand_id_has_period - Check if an ID string has a given wraparound period
4445 * @id_data: the ID string
4446 * @arrlen: the length of the @id_data array
4447 * @period: the period of repitition
4448 *
4449 * Check if an ID string is repeated within a given sequence of bytes at
4450 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08004451 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07004452 * if the repetition has a period of @period; otherwise, returns zero.
4453 */
4454static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4455{
4456 int i, j;
4457 for (i = 0; i < period; i++)
4458 for (j = i + period; j < arrlen; j += period)
4459 if (id_data[i] != id_data[j])
4460 return 0;
4461 return 1;
4462}
4463
4464/*
4465 * nand_id_len - Get the length of an ID string returned by CMD_READID
4466 * @id_data: the ID string
4467 * @arrlen: the length of the @id_data array
4468
4469 * Returns the length of the ID string, according to known wraparound/trailing
4470 * zero patterns. If no pattern exists, returns the length of the array.
4471 */
4472static int nand_id_len(u8 *id_data, int arrlen)
4473{
4474 int last_nonzero, period;
4475
4476 /* Find last non-zero byte */
4477 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4478 if (id_data[last_nonzero])
4479 break;
4480
4481 /* All zeros */
4482 if (last_nonzero < 0)
4483 return 0;
4484
4485 /* Calculate wraparound period */
4486 for (period = 1; period < arrlen; period++)
4487 if (nand_id_has_period(id_data, arrlen, period))
4488 break;
4489
4490 /* There's a repeated pattern */
4491 if (period < arrlen)
4492 return period;
4493
4494 /* There are trailing zeros */
4495 if (last_nonzero < arrlen - 1)
4496 return last_nonzero + 1;
4497
4498 /* No pattern detected */
4499 return arrlen;
4500}
4501
Huang Shijie7db906b2013-09-25 14:58:11 +08004502/* Extract the bits of per cell from the 3rd byte of the extended ID */
4503static int nand_get_bits_per_cell(u8 cellinfo)
4504{
4505 int bits;
4506
4507 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4508 bits >>= NAND_CI_CELLTYPE_SHIFT;
4509 return bits + 1;
4510}
4511
Brian Norrise3b88bd2012-09-24 20:40:52 -07004512/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004513 * Many new NAND share similar device ID codes, which represent the size of the
4514 * chip. The rest of the parameters must be decoded according to generic or
4515 * manufacturer-specific "extended ID" decoding patterns.
4516 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004517void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004518{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004519 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02004520 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004521 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004522 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08004523 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004524 /* The 4th id byte is the important one */
4525 extid = id_data[3];
4526
Boris Brezillon01389b62016-06-08 10:30:18 +02004527 /* Calc pagesize */
4528 mtd->writesize = 1024 << (extid & 0x03);
4529 extid >>= 2;
4530 /* Calc oobsize */
4531 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4532 extid >>= 2;
4533 /* Calc blocksize. Blocksize is multiples of 64KiB */
4534 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4535 extid >>= 2;
4536 /* Get buswidth information */
4537 if (extid & 0x1)
4538 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004539}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004540EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004541
4542/*
Brian Norrisf23a4812012-09-24 20:40:51 -07004543 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4544 * decodes a matching ID table entry and assigns the MTD size parameters for
4545 * the chip.
4546 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004547static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07004548{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004549 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07004550
4551 mtd->erasesize = type->erasesize;
4552 mtd->writesize = type->pagesize;
4553 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07004554
Huang Shijie1c195e92013-09-25 14:58:12 +08004555 /* All legacy ID NAND are small-page, SLC */
4556 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07004557}
4558
4559/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004560 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4561 * heuristic patterns using various detected parameters (e.g., manufacturer,
4562 * page size, cell-type information).
4563 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004564static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07004565{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004566 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004567
4568 /* Set the bad block position */
4569 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4570 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4571 else
4572 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004573}
4574
Huang Shijieec6e87e2013-03-15 11:01:00 +08004575static inline bool is_full_id_nand(struct nand_flash_dev *type)
4576{
4577 return type->id_len;
4578}
4579
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004580static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02004581 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004582{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004583 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02004584 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004585
Huang Shijieec6e87e2013-03-15 11:01:00 +08004586 if (!strncmp(type->id, id_data, type->id_len)) {
4587 mtd->writesize = type->pagesize;
4588 mtd->erasesize = type->erasesize;
4589 mtd->oobsize = type->oobsize;
4590
Huang Shijie7db906b2013-09-25 14:58:11 +08004591 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004592 chip->chipsize = (uint64_t)type->chipsize << 20;
4593 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004594 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4595 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004596 chip->onfi_timing_mode_default =
4597 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004598
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004599 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4600 if (!chip->parameters.model)
4601 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004602
Huang Shijieec6e87e2013-03-15 11:01:00 +08004603 return true;
4604 }
4605 return false;
4606}
4607
Brian Norris7e74c2d2012-09-24 20:40:49 -07004608/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004609 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4610 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4611 * table.
4612 */
4613static void nand_manufacturer_detect(struct nand_chip *chip)
4614{
4615 /*
4616 * Try manufacturer detection if available and use
4617 * nand_decode_ext_id() otherwise.
4618 */
4619 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004620 chip->manufacturer.desc->ops->detect) {
4621 /* The 3rd id byte holds MLC / multichip data */
4622 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004623 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004624 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004625 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004626 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004627}
4628
4629/*
4630 * Manufacturer initialization. This function is called for all NANDs including
4631 * ONFI and JEDEC compliant ones.
4632 * Manufacturer drivers should put all their specific initialization code in
4633 * their ->init() hook.
4634 */
4635static int nand_manufacturer_init(struct nand_chip *chip)
4636{
4637 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4638 !chip->manufacturer.desc->ops->init)
4639 return 0;
4640
4641 return chip->manufacturer.desc->ops->init(chip);
4642}
4643
4644/*
4645 * Manufacturer cleanup. This function is called for all NANDs including
4646 * ONFI and JEDEC compliant ones.
4647 * Manufacturer drivers should put all their specific cleanup code in their
4648 * ->cleanup() hook.
4649 */
4650static void nand_manufacturer_cleanup(struct nand_chip *chip)
4651{
4652 /* Release manufacturer private data */
4653 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4654 chip->manufacturer.desc->ops->cleanup)
4655 chip->manufacturer.desc->ops->cleanup(chip);
4656}
4657
Boris Brezillon348d56a2018-09-07 00:38:48 +02004658static const char *
4659nand_manufacturer_name(const struct nand_manufacturer *manufacturer)
4660{
4661 return manufacturer ? manufacturer->name : "Unknown";
4662}
4663
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004664/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004665 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004666 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004667static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004668{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004669 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004670 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004671 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004672 u8 *id_data = chip->id.data;
4673 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
Karl Beldanef89a882008-09-15 14:37:29 +02004675 /*
4676 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004677 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004678 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004679 ret = nand_reset(chip, 0);
4680 if (ret)
4681 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004682
4683 /* Select the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004684 nand_select_target(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004687 ret = nand_readid_op(chip, 0, id_data, 2);
4688 if (ret)
4689 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
4691 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004692 maf_id = id_data[0];
4693 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
Brian Norris8b6e50c2011-05-25 14:59:01 -07004695 /*
4696 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004697 * interface concerns can cause random data which looks like a
4698 * possibly credible NAND flash to appear. If the two results do
4699 * not match, ignore the device completely.
4700 */
4701
Brian Norris4aef9b72012-09-24 20:40:48 -07004702 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004703 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
4704 if (ret)
4705 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01004706
Boris Brezillon7f501f02016-05-24 19:20:05 +02004707 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004708 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004709 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004710 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004711 }
4712
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02004713 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02004714
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004715 /* Try to identify manufacturer */
4716 manufacturer = nand_get_manufacturer(maf_id);
4717 chip->manufacturer.desc = manufacturer;
4718
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004719 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004720 type = nand_flash_ids;
4721
Boris Brezillon29a198a2016-05-24 20:17:48 +02004722 /*
4723 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4724 * override it.
4725 * This is required to make sure initial NAND bus width set by the
4726 * NAND controller driver is coherent with the real NAND bus width
4727 * (extracted by auto-detection code).
4728 */
4729 busw = chip->options & NAND_BUSWIDTH_16;
4730
4731 /*
4732 * The flag is only set (never cleared), reset it to its default value
4733 * before starting auto-detection.
4734 */
4735 chip->options &= ~NAND_BUSWIDTH_16;
4736
Huang Shijieec6e87e2013-03-15 11:01:00 +08004737 for (; type->name != NULL; type++) {
4738 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004739 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004740 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004741 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004742 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004743 }
4744 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004745
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004746 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004747 /* Check if the chip is ONFI compliant */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004748 ret = nand_onfi_detect(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01004749 if (ret < 0)
4750 return ret;
4751 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004752 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004753
4754 /* Check if the chip is JEDEC compliant */
Boris Brezillon8ae3fbf2018-09-07 00:38:51 +02004755 ret = nand_jedec_detect(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01004756 if (ret < 0)
4757 return ret;
4758 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08004759 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004760 }
4761
David Woodhouse5e81e882010-02-26 18:32:56 +00004762 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004763 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004764
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004765 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4766 if (!chip->parameters.model)
4767 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004768
Adrian Hunter69423d92008-12-10 13:37:21 +00004769 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004770
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004771 if (!type->pagesize)
4772 nand_manufacturer_detect(chip);
4773 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004774 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004775
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004776 /* Get chip options */
4777 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004778
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004779ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01004780 if (!mtd->name)
4781 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004782
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004783 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004784 WARN_ON(busw & NAND_BUSWIDTH_16);
4785 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004786 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4787 /*
4788 * Check, if buswidth is correct. Hardware drivers should set
4789 * chip correct!
4790 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004791 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004792 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004793 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4794 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004795 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4796 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004797 ret = -EINVAL;
4798
4799 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004800 }
4801
Boris Brezillon7f501f02016-05-24 19:20:05 +02004802 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004803
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004804 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004805 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004806 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004807 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004808
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004809 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004810 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004811 if (chip->chipsize & 0xffffffff)
4812 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004813 else {
4814 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4815 chip->chip_shift += 32 - 1;
4816 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004817
Masahiro Yamada14157f82017-09-13 11:05:50 +09004818 if (chip->chip_shift - chip->page_shift > 16)
4819 chip->options |= NAND_ROW_ADDR_3;
4820
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004821 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004822
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004823 nand_legacy_adjust_cmdfunc(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004824
Ezequiel Garcia20171642013-11-25 08:30:31 -03004825 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004826 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01004827 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4828 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02004829 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004830 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004831 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004832 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004833
4834free_detect_allocation:
4835 kfree(chip->parameters.model);
4836
4837 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004838}
4839
Boris Brezillond48f62b2016-04-01 14:54:32 +02004840static const char * const nand_ecc_modes[] = {
4841 [NAND_ECC_NONE] = "none",
4842 [NAND_ECC_SOFT] = "soft",
4843 [NAND_ECC_HW] = "hw",
4844 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4845 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004846 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004847};
4848
4849static int of_get_nand_ecc_mode(struct device_node *np)
4850{
4851 const char *pm;
4852 int err, i;
4853
4854 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4855 if (err < 0)
4856 return err;
4857
4858 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4859 if (!strcasecmp(pm, nand_ecc_modes[i]))
4860 return i;
4861
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004862 /*
4863 * For backward compatibility we support few obsoleted values that don't
4864 * have their mappings into nand_ecc_modes_t anymore (they were merged
4865 * with other enums).
4866 */
4867 if (!strcasecmp(pm, "soft_bch"))
4868 return NAND_ECC_SOFT;
4869
Boris Brezillond48f62b2016-04-01 14:54:32 +02004870 return -ENODEV;
4871}
4872
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004873static const char * const nand_ecc_algos[] = {
4874 [NAND_ECC_HAMMING] = "hamming",
4875 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02004876 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004877};
4878
Boris Brezillond48f62b2016-04-01 14:54:32 +02004879static int of_get_nand_ecc_algo(struct device_node *np)
4880{
4881 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004882 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004883
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004884 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4885 if (!err) {
4886 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4887 if (!strcasecmp(pm, nand_ecc_algos[i]))
4888 return i;
4889 return -ENODEV;
4890 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004891
4892 /*
4893 * For backward compatibility we also read "nand-ecc-mode" checking
4894 * for some obsoleted values that were specifying ECC algorithm.
4895 */
4896 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4897 if (err < 0)
4898 return err;
4899
4900 if (!strcasecmp(pm, "soft"))
4901 return NAND_ECC_HAMMING;
4902 else if (!strcasecmp(pm, "soft_bch"))
4903 return NAND_ECC_BCH;
4904
4905 return -ENODEV;
4906}
4907
4908static int of_get_nand_ecc_step_size(struct device_node *np)
4909{
4910 int ret;
4911 u32 val;
4912
4913 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4914 return ret ? ret : val;
4915}
4916
4917static int of_get_nand_ecc_strength(struct device_node *np)
4918{
4919 int ret;
4920 u32 val;
4921
4922 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4923 return ret ? ret : val;
4924}
4925
4926static int of_get_nand_bus_width(struct device_node *np)
4927{
4928 u32 val;
4929
4930 if (of_property_read_u32(np, "nand-bus-width", &val))
4931 return 8;
4932
4933 switch (val) {
4934 case 8:
4935 case 16:
4936 return val;
4937 default:
4938 return -EIO;
4939 }
4940}
4941
4942static bool of_get_nand_on_flash_bbt(struct device_node *np)
4943{
4944 return of_property_read_bool(np, "nand-on-flash-bbt");
4945}
4946
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004947static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004948{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004949 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004950 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004951
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004952 if (!dn)
4953 return 0;
4954
Brian Norris5844fee2015-01-23 00:22:27 -08004955 if (of_get_nand_bus_width(dn) == 16)
4956 chip->options |= NAND_BUSWIDTH_16;
4957
Stefan Agnerf922bd72018-06-24 23:27:23 +02004958 if (of_property_read_bool(dn, "nand-is-boot-medium"))
4959 chip->options |= NAND_IS_BOOT_MEDIUM;
4960
Brian Norris5844fee2015-01-23 00:22:27 -08004961 if (of_get_nand_on_flash_bbt(dn))
4962 chip->bbt_options |= NAND_BBT_USE_FLASH;
4963
4964 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004965 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004966 ecc_strength = of_get_nand_ecc_strength(dn);
4967 ecc_step = of_get_nand_ecc_step_size(dn);
4968
Brian Norris5844fee2015-01-23 00:22:27 -08004969 if (ecc_mode >= 0)
4970 chip->ecc.mode = ecc_mode;
4971
Rafał Miłecki79082452016-03-23 11:19:02 +01004972 if (ecc_algo >= 0)
4973 chip->ecc.algo = ecc_algo;
4974
Brian Norris5844fee2015-01-23 00:22:27 -08004975 if (ecc_strength >= 0)
4976 chip->ecc.strength = ecc_strength;
4977
4978 if (ecc_step > 0)
4979 chip->ecc.size = ecc_step;
4980
Boris Brezillonba78ee02016-06-08 17:04:22 +02004981 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4982 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4983
Brian Norris5844fee2015-01-23 00:22:27 -08004984 return 0;
4985}
4986
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004987/**
Miquel Raynal98732da2018-07-25 15:31:50 +02004988 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02004989 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004990 * @maxchips: number of chips to scan for
4991 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004992 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004993 * This is the first phase of the normal nand_scan() function. It reads the
4994 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004995 *
Miquel Raynal98732da2018-07-25 15:31:50 +02004996 * This helper used to be called directly from controller drivers that needed
4997 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
4998 * prevented dynamic allocations during this phase which was unconvenient and
4999 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005000 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005001static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005002 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005003{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005004 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02005005 int nand_maf_id, nand_dev_id;
5006 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08005007 int ret;
5008
Miquel Raynal17fa8042017-11-30 18:01:31 +01005009 /* Enforce the right timings for reset/detection */
5010 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5011
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005012 ret = nand_dt_init(chip);
5013 if (ret)
5014 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005015
Brian Norrisf7a8e382016-01-05 10:39:45 -08005016 if (!mtd->name && mtd->dev.parent)
5017 mtd->name = dev_name(mtd->dev.parent);
5018
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02005019 if (chip->exec_op && !chip->select_chip) {
5020 pr_err("->select_chip() is mandatory when implementing ->exec_op()\n");
5021 return -EINVAL;
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005022 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005023
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02005024 ret = nand_legacy_check_hooks(chip);
5025 if (ret)
5026 return ret;
5027
Boris Brezillon1d017852018-11-11 08:55:14 +01005028 /*
5029 * Start with chips->numchips = maxchips to let nand_select_target() do
5030 * its job. chip->numchips will be adjusted after.
5031 */
5032 chip->numchips = maxchips;
5033
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005034 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005035 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005036
5037 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005038 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005039 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005040 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005041 pr_warn("No NAND device found\n");
Boris Brezillon1d017852018-11-11 08:55:14 +01005042 nand_deselect_target(chip);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005043 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 }
5045
Boris Brezillon7f501f02016-05-24 19:20:05 +02005046 nand_maf_id = chip->id.data[0];
5047 nand_dev_id = chip->id.data[1];
5048
Boris Brezillon1d017852018-11-11 08:55:14 +01005049 nand_deselect_target(chip);
Huang Shijie07300162012-11-09 16:23:45 +08005050
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005051 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005052 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005053 u8 id[2];
5054
Karl Beldanef89a882008-09-15 14:37:29 +02005055 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005056 nand_reset(chip, i);
5057
Boris Brezillon1d017852018-11-11 08:55:14 +01005058 nand_select_target(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005060 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005062 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon1d017852018-11-11 08:55:14 +01005063 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 break;
Huang Shijie07300162012-11-09 16:23:45 +08005065 }
Boris Brezillon1d017852018-11-11 08:55:14 +01005066 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 }
5068 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005069 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005070
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005072 chip->numchips = i;
5073 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
David Woodhouse3b85c322006-09-25 17:06:53 +01005075 return 0;
5076}
5077
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005078static void nand_scan_ident_cleanup(struct nand_chip *chip)
5079{
5080 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005081 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005082}
5083
Boris Brezillon08136212018-11-11 08:55:03 +01005084static int nand_set_ecc_soft_ops(struct nand_chip *chip)
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005085{
Boris Brezillon08136212018-11-11 08:55:03 +01005086 struct mtd_info *mtd = nand_to_mtd(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005087 struct nand_ecc_ctrl *ecc = &chip->ecc;
5088
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005089 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005090 return -EINVAL;
5091
5092 switch (ecc->algo) {
5093 case NAND_ECC_HAMMING:
5094 ecc->calculate = nand_calculate_ecc;
5095 ecc->correct = nand_correct_data;
5096 ecc->read_page = nand_read_page_swecc;
5097 ecc->read_subpage = nand_read_subpage;
5098 ecc->write_page = nand_write_page_swecc;
5099 ecc->read_page_raw = nand_read_page_raw;
5100 ecc->write_page_raw = nand_write_page_raw;
5101 ecc->read_oob = nand_read_oob_std;
5102 ecc->write_oob = nand_write_oob_std;
5103 if (!ecc->size)
5104 ecc->size = 256;
5105 ecc->bytes = 3;
5106 ecc->strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +02005107
5108 if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC))
5109 ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
5110
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005111 return 0;
5112 case NAND_ECC_BCH:
5113 if (!mtd_nand_has_bch()) {
5114 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5115 return -EINVAL;
5116 }
5117 ecc->calculate = nand_bch_calculate_ecc;
5118 ecc->correct = nand_bch_correct_data;
5119 ecc->read_page = nand_read_page_swecc;
5120 ecc->read_subpage = nand_read_subpage;
5121 ecc->write_page = nand_write_page_swecc;
5122 ecc->read_page_raw = nand_read_page_raw;
5123 ecc->write_page_raw = nand_write_page_raw;
5124 ecc->read_oob = nand_read_oob_std;
5125 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005126
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005127 /*
5128 * Board driver should supply ecc.size and ecc.strength
5129 * values to select how many bits are correctable.
5130 * Otherwise, default to 4 bits for large page devices.
5131 */
5132 if (!ecc->size && (mtd->oobsize >= 64)) {
5133 ecc->size = 512;
5134 ecc->strength = 4;
5135 }
5136
5137 /*
5138 * if no ecc placement scheme was provided pickup the default
5139 * large page one.
5140 */
5141 if (!mtd->ooblayout) {
5142 /* handle large page devices only */
5143 if (mtd->oobsize < 64) {
5144 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5145 return -EINVAL;
5146 }
5147
5148 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005149
5150 }
5151
5152 /*
5153 * We can only maximize ECC config when the default layout is
5154 * used, otherwise we don't know how many bytes can really be
5155 * used.
5156 */
5157 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5158 ecc->options & NAND_ECC_MAXIMIZE) {
5159 int steps, bytes;
5160
5161 /* Always prefer 1k blocks over 512bytes ones */
5162 ecc->size = 1024;
5163 steps = mtd->writesize / ecc->size;
5164
5165 /* Reserve 2 bytes for the BBM */
5166 bytes = (mtd->oobsize - 2) / steps;
5167 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005168 }
5169
5170 /* See nand_bch_init() for details. */
5171 ecc->bytes = 0;
5172 ecc->priv = nand_bch_init(mtd);
5173 if (!ecc->priv) {
5174 WARN(1, "BCH ECC initialization failed!\n");
5175 return -EINVAL;
5176 }
5177 return 0;
5178 default:
5179 WARN(1, "Unsupported ECC algorithm!\n");
5180 return -EINVAL;
5181 }
5182}
5183
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005184/**
5185 * nand_check_ecc_caps - check the sanity of preset ECC settings
5186 * @chip: nand chip info structure
5187 * @caps: ECC caps info structure
5188 * @oobavail: OOB size that the ECC engine can use
5189 *
5190 * When ECC step size and strength are already set, check if they are supported
5191 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5192 * On success, the calculated ECC bytes is set.
5193 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305194static int
5195nand_check_ecc_caps(struct nand_chip *chip,
5196 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005197{
5198 struct mtd_info *mtd = nand_to_mtd(chip);
5199 const struct nand_ecc_step_info *stepinfo;
5200 int preset_step = chip->ecc.size;
5201 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305202 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005203 int i, j;
5204
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005205 for (i = 0; i < caps->nstepinfos; i++) {
5206 stepinfo = &caps->stepinfos[i];
5207
5208 if (stepinfo->stepsize != preset_step)
5209 continue;
5210
5211 for (j = 0; j < stepinfo->nstrengths; j++) {
5212 if (stepinfo->strengths[j] != preset_strength)
5213 continue;
5214
5215 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5216 preset_strength);
5217 if (WARN_ON_ONCE(ecc_bytes < 0))
5218 return ecc_bytes;
5219
5220 if (ecc_bytes * nsteps > oobavail) {
5221 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5222 preset_step, preset_strength);
5223 return -ENOSPC;
5224 }
5225
5226 chip->ecc.bytes = ecc_bytes;
5227
5228 return 0;
5229 }
5230 }
5231
5232 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5233 preset_step, preset_strength);
5234
5235 return -ENOTSUPP;
5236}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005237
5238/**
5239 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5240 * @chip: nand chip info structure
5241 * @caps: ECC engine caps info structure
5242 * @oobavail: OOB size that the ECC engine can use
5243 *
5244 * If a chip's ECC requirement is provided, try to meet it with the least
5245 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5246 * On success, the chosen ECC settings are set.
5247 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305248static int
5249nand_match_ecc_req(struct nand_chip *chip,
5250 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005251{
5252 struct mtd_info *mtd = nand_to_mtd(chip);
5253 const struct nand_ecc_step_info *stepinfo;
5254 int req_step = chip->ecc_step_ds;
5255 int req_strength = chip->ecc_strength_ds;
5256 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5257 int best_step, best_strength, best_ecc_bytes;
5258 int best_ecc_bytes_total = INT_MAX;
5259 int i, j;
5260
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005261 /* No information provided by the NAND chip */
5262 if (!req_step || !req_strength)
5263 return -ENOTSUPP;
5264
5265 /* number of correctable bits the chip requires in a page */
5266 req_corr = mtd->writesize / req_step * req_strength;
5267
5268 for (i = 0; i < caps->nstepinfos; i++) {
5269 stepinfo = &caps->stepinfos[i];
5270 step_size = stepinfo->stepsize;
5271
5272 for (j = 0; j < stepinfo->nstrengths; j++) {
5273 strength = stepinfo->strengths[j];
5274
5275 /*
5276 * If both step size and strength are smaller than the
5277 * chip's requirement, it is not easy to compare the
5278 * resulted reliability.
5279 */
5280 if (step_size < req_step && strength < req_strength)
5281 continue;
5282
5283 if (mtd->writesize % step_size)
5284 continue;
5285
5286 nsteps = mtd->writesize / step_size;
5287
5288 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5289 if (WARN_ON_ONCE(ecc_bytes < 0))
5290 continue;
5291 ecc_bytes_total = ecc_bytes * nsteps;
5292
5293 if (ecc_bytes_total > oobavail ||
5294 strength * nsteps < req_corr)
5295 continue;
5296
5297 /*
5298 * We assume the best is to meet the chip's requrement
5299 * with the least number of ECC bytes.
5300 */
5301 if (ecc_bytes_total < best_ecc_bytes_total) {
5302 best_ecc_bytes_total = ecc_bytes_total;
5303 best_step = step_size;
5304 best_strength = strength;
5305 best_ecc_bytes = ecc_bytes;
5306 }
5307 }
5308 }
5309
5310 if (best_ecc_bytes_total == INT_MAX)
5311 return -ENOTSUPP;
5312
5313 chip->ecc.size = best_step;
5314 chip->ecc.strength = best_strength;
5315 chip->ecc.bytes = best_ecc_bytes;
5316
5317 return 0;
5318}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005319
5320/**
5321 * nand_maximize_ecc - choose the max ECC strength available
5322 * @chip: nand chip info structure
5323 * @caps: ECC engine caps info structure
5324 * @oobavail: OOB size that the ECC engine can use
5325 *
5326 * Choose the max ECC strength that is supported on the controller, and can fit
5327 * within the chip's OOB. On success, the chosen ECC settings are set.
5328 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305329static int
5330nand_maximize_ecc(struct nand_chip *chip,
5331 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005332{
5333 struct mtd_info *mtd = nand_to_mtd(chip);
5334 const struct nand_ecc_step_info *stepinfo;
5335 int step_size, strength, nsteps, ecc_bytes, corr;
5336 int best_corr = 0;
5337 int best_step = 0;
5338 int best_strength, best_ecc_bytes;
5339 int i, j;
5340
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005341 for (i = 0; i < caps->nstepinfos; i++) {
5342 stepinfo = &caps->stepinfos[i];
5343 step_size = stepinfo->stepsize;
5344
5345 /* If chip->ecc.size is already set, respect it */
5346 if (chip->ecc.size && step_size != chip->ecc.size)
5347 continue;
5348
5349 for (j = 0; j < stepinfo->nstrengths; j++) {
5350 strength = stepinfo->strengths[j];
5351
5352 if (mtd->writesize % step_size)
5353 continue;
5354
5355 nsteps = mtd->writesize / step_size;
5356
5357 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5358 if (WARN_ON_ONCE(ecc_bytes < 0))
5359 continue;
5360
5361 if (ecc_bytes * nsteps > oobavail)
5362 continue;
5363
5364 corr = strength * nsteps;
5365
5366 /*
5367 * If the number of correctable bits is the same,
5368 * bigger step_size has more reliability.
5369 */
5370 if (corr > best_corr ||
5371 (corr == best_corr && step_size > best_step)) {
5372 best_corr = corr;
5373 best_step = step_size;
5374 best_strength = strength;
5375 best_ecc_bytes = ecc_bytes;
5376 }
5377 }
5378 }
5379
5380 if (!best_corr)
5381 return -ENOTSUPP;
5382
5383 chip->ecc.size = best_step;
5384 chip->ecc.strength = best_strength;
5385 chip->ecc.bytes = best_ecc_bytes;
5386
5387 return 0;
5388}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005389
Abhishek Sahu181ace92018-06-20 12:57:28 +05305390/**
5391 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
5392 * @chip: nand chip info structure
5393 * @caps: ECC engine caps info structure
5394 * @oobavail: OOB size that the ECC engine can use
5395 *
5396 * Choose the ECC configuration according to following logic
5397 *
5398 * 1. If both ECC step size and ECC strength are already set (usually by DT)
5399 * then check if it is supported by this controller.
5400 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
5401 * 3. Otherwise, try to match the ECC step size and ECC strength closest
5402 * to the chip's requirement. If available OOB size can't fit the chip
5403 * requirement then fallback to the maximum ECC step size and ECC strength.
5404 *
5405 * On success, the chosen ECC settings are set.
5406 */
5407int nand_ecc_choose_conf(struct nand_chip *chip,
5408 const struct nand_ecc_caps *caps, int oobavail)
5409{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305410 struct mtd_info *mtd = nand_to_mtd(chip);
5411
5412 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
5413 return -EINVAL;
5414
Abhishek Sahu181ace92018-06-20 12:57:28 +05305415 if (chip->ecc.size && chip->ecc.strength)
5416 return nand_check_ecc_caps(chip, caps, oobavail);
5417
5418 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
5419 return nand_maximize_ecc(chip, caps, oobavail);
5420
5421 if (!nand_match_ecc_req(chip, caps, oobavail))
5422 return 0;
5423
5424 return nand_maximize_ecc(chip, caps, oobavail);
5425}
5426EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
5427
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005428/*
5429 * Check if the chip configuration meet the datasheet requirements.
5430
5431 * If our configuration corrects A bits per B bytes and the minimum
5432 * required correction level is X bits per Y bytes, then we must ensure
5433 * both of the following are true:
5434 *
5435 * (1) A / B >= X / Y
5436 * (2) A >= X
5437 *
5438 * Requirement (1) ensures we can correct for the required bitflip density.
5439 * Requirement (2) ensures we can correct even when all bitflips are clumped
5440 * in the same sector.
5441 */
Boris Brezillon08136212018-11-11 08:55:03 +01005442static bool nand_ecc_strength_good(struct nand_chip *chip)
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005443{
Boris Brezillon08136212018-11-11 08:55:03 +01005444 struct mtd_info *mtd = nand_to_mtd(chip);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005445 struct nand_ecc_ctrl *ecc = &chip->ecc;
5446 int corr, ds_corr;
5447
5448 if (ecc->size == 0 || chip->ecc_step_ds == 0)
5449 /* Not enough information */
5450 return true;
5451
5452 /*
5453 * We get the number of corrected bits per page to compare
5454 * the correction density.
5455 */
5456 corr = (mtd->writesize * ecc->strength) / ecc->size;
5457 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
5458
5459 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
5460}
David Woodhouse3b85c322006-09-25 17:06:53 +01005461
5462/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005463 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005464 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01005465 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005466 * This is the second phase of the normal nand_scan() function. It fills out
5467 * all the uninitialized function pointers with the defaults and scans for a
5468 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01005469 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005470static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01005471{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005472 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08005473 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005474 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01005475
Brian Norrise2414f42012-02-06 13:44:00 -08005476 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005477 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07005478 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005479 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07005480 }
Brian Norrise2414f42012-02-06 13:44:00 -08005481
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005482 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005483 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02005484 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005485
Boris Brezillonf84674b2017-06-02 12:18:24 +02005486 /*
5487 * FIXME: some NAND manufacturer drivers expect the first die to be
5488 * selected when manufacturer->init() is called. They should be fixed
5489 * to explictly select the relevant die when interacting with the NAND
5490 * chip.
5491 */
Boris Brezillon1d017852018-11-11 08:55:14 +01005492 nand_select_target(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005493 ret = nand_manufacturer_init(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01005494 nand_deselect_target(chip);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005495 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005496 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005497
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01005498 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005499 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005500
5501 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005502 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005503 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005504 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005505 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005506 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01005509 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 break;
5511 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005512 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02005513 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02005516 /*
5517 * Expose the whole OOB area to users if ECC_NONE
5518 * is passed. We could do that for all kind of
5519 * ->oobsize, but we must keep the old large/small
5520 * page with ECC layout when ->oobsize <= 128 for
5521 * compatibility reasons.
5522 */
5523 if (ecc->mode == NAND_ECC_NONE) {
5524 mtd_set_ooblayout(mtd,
5525 &nand_ooblayout_lp_ops);
5526 break;
5527 }
5528
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005529 WARN(1, "No oob scheme defined for oobsize %d\n",
5530 mtd->oobsize);
5531 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005532 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 }
5534 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005535
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005536 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005537 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005538 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01005539 */
David Woodhouse956e9442006-09-25 17:12:39 +01005540
Huang Shijie97de79e02013-10-18 14:20:53 +08005541 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005542 case NAND_ECC_HW_OOB_FIRST:
5543 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08005544 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005545 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5546 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005547 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005548 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005549 if (!ecc->read_page)
5550 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005551
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005552 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07005553 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005554 if (!ecc->read_page)
5555 ecc->read_page = nand_read_page_hwecc;
5556 if (!ecc->write_page)
5557 ecc->write_page = nand_write_page_hwecc;
5558 if (!ecc->read_page_raw)
5559 ecc->read_page_raw = nand_read_page_raw;
5560 if (!ecc->write_page_raw)
5561 ecc->write_page_raw = nand_write_page_raw;
5562 if (!ecc->read_oob)
5563 ecc->read_oob = nand_read_oob_std;
5564 if (!ecc->write_oob)
5565 ecc->write_oob = nand_write_oob_std;
5566 if (!ecc->read_subpage)
5567 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02005568 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08005569 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005570
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005571 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08005572 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5573 (!ecc->read_page ||
5574 ecc->read_page == nand_read_page_hwecc ||
5575 !ecc->write_page ||
5576 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005577 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5578 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005579 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005580 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07005581 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005582 if (!ecc->read_page)
5583 ecc->read_page = nand_read_page_syndrome;
5584 if (!ecc->write_page)
5585 ecc->write_page = nand_write_page_syndrome;
5586 if (!ecc->read_page_raw)
5587 ecc->read_page_raw = nand_read_page_raw_syndrome;
5588 if (!ecc->write_page_raw)
5589 ecc->write_page_raw = nand_write_page_raw_syndrome;
5590 if (!ecc->read_oob)
5591 ecc->read_oob = nand_read_oob_syndrome;
5592 if (!ecc->write_oob)
5593 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005594
Huang Shijie97de79e02013-10-18 14:20:53 +08005595 if (mtd->writesize >= ecc->size) {
5596 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005597 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
5598 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005599 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07005600 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005601 break;
Mike Dunne2788c92012-04-25 12:06:10 -07005602 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005603 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5604 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08005605 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02005606 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005608 case NAND_ECC_SOFT:
Boris Brezillon08136212018-11-11 08:55:03 +01005609 ret = nand_set_ecc_soft_ops(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005610 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005611 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005612 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01005613 }
5614 break;
5615
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005616 case NAND_ECC_ON_DIE:
5617 if (!ecc->read_page || !ecc->write_page) {
5618 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
5619 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005620 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005621 }
5622 if (!ecc->read_oob)
5623 ecc->read_oob = nand_read_oob_std;
5624 if (!ecc->write_oob)
5625 ecc->write_oob = nand_write_oob_std;
5626 break;
5627
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005628 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005629 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08005630 ecc->read_page = nand_read_page_raw;
5631 ecc->write_page = nand_write_page_raw;
5632 ecc->read_oob = nand_read_oob_std;
5633 ecc->read_page_raw = nand_read_page_raw;
5634 ecc->write_page_raw = nand_write_page_raw;
5635 ecc->write_oob = nand_write_oob_std;
5636 ecc->size = mtd->writesize;
5637 ecc->bytes = 0;
5638 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005640
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005642 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5643 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005644 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005647 if (ecc->correct || ecc->calculate) {
5648 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5649 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5650 if (!ecc->calc_buf || !ecc->code_buf) {
5651 ret = -ENOMEM;
5652 goto err_nand_manuf_cleanup;
5653 }
5654 }
5655
Brian Norris9ce244b2011-08-30 18:45:37 -07005656 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005657 if (!ecc->read_oob_raw)
5658 ecc->read_oob_raw = ecc->read_oob;
5659 if (!ecc->write_oob_raw)
5660 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005661
Boris Brezillon846031d2016-02-03 20:11:00 +01005662 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005663 mtd->ecc_strength = ecc->strength;
5664 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005665
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005666 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005667 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005668 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005669 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005670 ecc->steps = mtd->writesize / ecc->size;
5671 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005672 WARN(1, "Invalid ECC parameters\n");
5673 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005674 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005676 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005677 if (ecc->total > mtd->oobsize) {
5678 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5679 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005680 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005681 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005682
Boris Brezillon846031d2016-02-03 20:11:00 +01005683 /*
5684 * The number of bytes available for a client to place data into
5685 * the out of band area.
5686 */
5687 ret = mtd_ooblayout_count_freebytes(mtd);
5688 if (ret < 0)
5689 ret = 0;
5690
5691 mtd->oobavail = ret;
5692
5693 /* ECC sanity check: warn if it's too weak */
Boris Brezillon08136212018-11-11 08:55:03 +01005694 if (!nand_ecc_strength_good(chip))
Boris Brezillon846031d2016-02-03 20:11:00 +01005695 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5696 mtd->name);
5697
Brian Norris8b6e50c2011-05-25 14:59:01 -07005698 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005699 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005700 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005701 case 2:
5702 mtd->subpage_sft = 1;
5703 break;
5704 case 4:
5705 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005706 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005707 mtd->subpage_sft = 2;
5708 break;
5709 }
5710 }
5711 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5712
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02005713 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005714 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005717 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005719 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305720 switch (ecc->mode) {
5721 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305722 if (chip->page_shift > 9)
5723 chip->options |= NAND_SUBPAGE_READ;
5724 break;
5725
5726 default:
5727 break;
5728 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005729
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08005731 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02005732 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5733 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005734 mtd->_erase = nand_erase;
5735 mtd->_point = NULL;
5736 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005737 mtd->_panic_write = panic_nand_write;
5738 mtd->_read_oob = nand_read_oob;
5739 mtd->_write_oob = nand_write_oob;
5740 mtd->_sync = nand_sync;
5741 mtd->_lock = NULL;
5742 mtd->_unlock = NULL;
5743 mtd->_suspend = nand_suspend;
5744 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005745 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005746 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005747 mtd->_block_isbad = nand_block_isbad;
5748 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06005749 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01005750 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005752 /*
5753 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5754 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5755 * properly set.
5756 */
5757 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005758 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
Boris Brezillonf84674b2017-06-02 12:18:24 +02005760 /* Initialize the ->data_interface field. */
5761 ret = nand_init_data_interface(chip);
5762 if (ret)
5763 goto err_nand_manuf_cleanup;
5764
5765 /* Enter fastest possible mode on all dies. */
5766 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005767 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005768 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005769 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005770 }
5771
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005772 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005773 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775
5776 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02005777 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07005778 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005779 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005780
Brian Norris44d41822017-05-01 17:04:50 -07005781 return 0;
5782
Boris Brezillonf84674b2017-06-02 12:18:24 +02005783
5784err_nand_manuf_cleanup:
5785 nand_manufacturer_cleanup(chip);
5786
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005787err_free_buf:
5788 kfree(chip->data_buf);
5789 kfree(ecc->code_buf);
5790 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07005791
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005792 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793}
5794
Miquel Raynal05b54c72018-07-19 01:05:46 +02005795static int nand_attach(struct nand_chip *chip)
5796{
5797 if (chip->controller->ops && chip->controller->ops->attach_chip)
5798 return chip->controller->ops->attach_chip(chip);
5799
5800 return 0;
5801}
5802
5803static void nand_detach(struct nand_chip *chip)
5804{
5805 if (chip->controller->ops && chip->controller->ops->detach_chip)
5806 chip->controller->ops->detach_chip(chip);
5807}
5808
David Woodhouse3b85c322006-09-25 17:06:53 +01005809/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005810 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005811 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02005812 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005813 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01005814 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005815 * This fills out all the uninitialized function pointers with the defaults.
5816 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005817 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005818 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005819int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005820 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01005821{
5822 int ret;
5823
Boris Brezillon800342d2018-08-04 22:59:23 +02005824 if (!maxchips)
5825 return -EINVAL;
5826
5827 ret = nand_scan_ident(chip, maxchips, ids);
5828 if (ret)
5829 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005830
5831 ret = nand_attach(chip);
5832 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005833 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005834
Boris Brezillon00ad3782018-09-06 14:05:14 +02005835 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005836 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005837 goto detach_chip;
5838
5839 return 0;
5840
5841detach_chip:
5842 nand_detach(chip);
5843cleanup_ident:
5844 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005845
David Woodhouse3b85c322006-09-25 17:06:53 +01005846 return ret;
5847}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005848EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01005849
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005851 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5852 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005853 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005854void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005856 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005857 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005858 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5859
Jesper Juhlfa671642005-11-07 01:01:27 -08005860 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005861 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005862 kfree(chip->data_buf);
5863 kfree(chip->ecc.code_buf);
5864 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07005865
5866 /* Free bad block descriptor memory */
5867 if (chip->badblock_pattern && chip->badblock_pattern->options
5868 & NAND_BBT_DYNAMICSTRUCT)
5869 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005870
5871 /* Free manufacturer priv data. */
5872 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005873
5874 /* Free controller specific allocations after chip identification */
5875 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005876
5877 /* Free identification phase allocations */
5878 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879}
Miquel Raynal05b54c72018-07-19 01:05:46 +02005880
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005881EXPORT_SYMBOL_GPL(nand_cleanup);
5882
5883/**
5884 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5885 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02005886 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005887 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02005888void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005889{
Boris Brezillon59ac2762018-09-06 14:05:15 +02005890 mtd_device_unregister(nand_to_mtd(chip));
5891 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005892}
David Woodhousee0c7d762006-05-13 18:07:53 +01005893EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005894
David Woodhousee0c7d762006-05-13 18:07:53 +01005895MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005896MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5897MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005898MODULE_DESCRIPTION("Generic NAND flash driver code");