blob: 833f7061ff336f03ed791447dedb1baa0ee5c7f9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010042#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/interrupt.h>
44#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020045#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020047#include <linux/of.h>
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +020048#include <linux/gpio/consumer.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010049
Boris Brezillon348d56a2018-09-07 00:38:48 +020050#include "internals.h"
51
Boris Brezillon41b207a2016-02-03 19:06:15 +010052/* Define default oob placement schemes for large and small page devices */
53static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
54 struct mtd_oob_region *oobregion)
55{
56 struct nand_chip *chip = mtd_to_nand(mtd);
57 struct nand_ecc_ctrl *ecc = &chip->ecc;
58
59 if (section > 1)
60 return -ERANGE;
61
62 if (!section) {
63 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020064 if (mtd->oobsize == 16)
65 oobregion->length = 4;
66 else
67 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010068 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020069 if (mtd->oobsize == 8)
70 return -ERANGE;
71
Boris Brezillon41b207a2016-02-03 19:06:15 +010072 oobregion->offset = 6;
73 oobregion->length = ecc->total - 4;
74 }
75
76 return 0;
77}
78
79static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
80 struct mtd_oob_region *oobregion)
81{
82 if (section > 1)
83 return -ERANGE;
84
85 if (mtd->oobsize == 16) {
86 if (section)
87 return -ERANGE;
88
89 oobregion->length = 8;
90 oobregion->offset = 8;
91 } else {
92 oobregion->length = 2;
93 if (!section)
94 oobregion->offset = 3;
95 else
96 oobregion->offset = 6;
97 }
98
99 return 0;
100}
101
102const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
103 .ecc = nand_ooblayout_ecc_sp,
104 .free = nand_ooblayout_free_sp,
105};
106EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
107
108static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
109 struct mtd_oob_region *oobregion)
110{
111 struct nand_chip *chip = mtd_to_nand(mtd);
112 struct nand_ecc_ctrl *ecc = &chip->ecc;
113
Miquel Raynal882fd152017-08-26 17:19:15 +0200114 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100115 return -ERANGE;
116
117 oobregion->length = ecc->total;
118 oobregion->offset = mtd->oobsize - oobregion->length;
119
120 return 0;
121}
122
123static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
124 struct mtd_oob_region *oobregion)
125{
126 struct nand_chip *chip = mtd_to_nand(mtd);
127 struct nand_ecc_ctrl *ecc = &chip->ecc;
128
129 if (section)
130 return -ERANGE;
131
132 oobregion->length = mtd->oobsize - ecc->total - 2;
133 oobregion->offset = 2;
134
135 return 0;
136}
137
138const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
139 .ecc = nand_ooblayout_ecc_lp,
140 .free = nand_ooblayout_free_lp,
141};
142EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200143
Alexander Couzens6a623e02017-05-02 12:19:00 +0200144/*
145 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
146 * are placed at a fixed offset.
147 */
148static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
149 struct mtd_oob_region *oobregion)
150{
151 struct nand_chip *chip = mtd_to_nand(mtd);
152 struct nand_ecc_ctrl *ecc = &chip->ecc;
153
154 if (section)
155 return -ERANGE;
156
157 switch (mtd->oobsize) {
158 case 64:
159 oobregion->offset = 40;
160 break;
161 case 128:
162 oobregion->offset = 80;
163 break;
164 default:
165 return -EINVAL;
166 }
167
168 oobregion->length = ecc->total;
169 if (oobregion->offset + oobregion->length > mtd->oobsize)
170 return -ERANGE;
171
172 return 0;
173}
174
175static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
176 struct mtd_oob_region *oobregion)
177{
178 struct nand_chip *chip = mtd_to_nand(mtd);
179 struct nand_ecc_ctrl *ecc = &chip->ecc;
180 int ecc_offset = 0;
181
182 if (section < 0 || section > 1)
183 return -ERANGE;
184
185 switch (mtd->oobsize) {
186 case 64:
187 ecc_offset = 40;
188 break;
189 case 128:
190 ecc_offset = 80;
191 break;
192 default:
193 return -EINVAL;
194 }
195
196 if (section == 0) {
197 oobregion->offset = 2;
198 oobregion->length = ecc_offset - 2;
199 } else {
200 oobregion->offset = ecc_offset + ecc->total;
201 oobregion->length = mtd->oobsize - oobregion->offset;
202 }
203
204 return 0;
205}
206
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100207static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200208 .ecc = nand_ooblayout_ecc_lp_hamming,
209 .free = nand_ooblayout_free_lp_hamming,
210};
211
Boris Brezillon08136212018-11-11 08:55:03 +0100212static int check_offs_len(struct nand_chip *chip, loff_t ofs, uint64_t len)
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530213{
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530214 int ret = 0;
215
216 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300217 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700218 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530219 ret = -EINVAL;
220 }
221
222 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530228 return ret;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/**
Boris Brezillon1d017852018-11-11 08:55:14 +0100232 * nand_select_target() - Select a NAND target (A.K.A. die)
233 * @chip: NAND chip object
234 * @cs: the CS line to select. Note that this CS id is always from the chip
235 * PoV, not the controller one
236 *
237 * Select a NAND target so that further operations executed on @chip go to the
238 * selected NAND target.
239 */
240void nand_select_target(struct nand_chip *chip, unsigned int cs)
241{
242 /*
243 * cs should always lie between 0 and chip->numchips, when that's not
244 * the case it's a bug and the caller should be fixed.
245 */
246 if (WARN_ON(cs > chip->numchips))
247 return;
248
Boris Brezillonae2294b2018-11-11 08:55:15 +0100249 chip->cur_cs = cs;
Boris Brezillon02b4a522018-11-11 08:55:16 +0100250
Boris Brezillon7d6c37e2018-11-11 08:55:22 +0100251 if (chip->legacy.select_chip)
252 chip->legacy.select_chip(chip, cs);
Boris Brezillon1d017852018-11-11 08:55:14 +0100253}
254EXPORT_SYMBOL_GPL(nand_select_target);
255
256/**
257 * nand_deselect_target() - Deselect the currently selected target
258 * @chip: NAND chip object
259 *
260 * Deselect the currently selected NAND target. The result of operations
261 * executed on @chip after the target has been deselected is undefined.
262 */
263void nand_deselect_target(struct nand_chip *chip)
264{
Boris Brezillon7d6c37e2018-11-11 08:55:22 +0100265 if (chip->legacy.select_chip)
266 chip->legacy.select_chip(chip, -1);
Boris Brezillon02b4a522018-11-11 08:55:16 +0100267
Boris Brezillonae2294b2018-11-11 08:55:15 +0100268 chip->cur_cs = -1;
Boris Brezillon1d017852018-11-11 08:55:14 +0100269}
270EXPORT_SYMBOL_GPL(nand_deselect_target);
271
272/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * nand_release_device - [GENERIC] release chip
Boris Brezillon08136212018-11-11 08:55:03 +0100274 * @chip: NAND chip object
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000275 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800276 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Boris Brezillon08136212018-11-11 08:55:03 +0100278static void nand_release_device(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200280 /* Release the controller and the chip */
Boris Brezillon013e6292018-11-20 11:57:20 +0100281 mutex_unlock(&chip->controller->lock);
282 mutex_unlock(&chip->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Boris Brezillonc17556f2018-09-06 14:05:25 +0200287 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700288 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000290 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200292static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200294 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900295 int page, page_end, res;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900296 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Brian Norris5fb15492011-05-31 16:31:21 -0700298 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700299 ofs += mtd->erasesize - mtd->writesize;
300
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100301 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900302 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100303
Masahiro Yamadac120e752017-03-23 05:07:01 +0900304 for (; page < page_end; page++) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200305 res = chip->ecc.read_oob(chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530306 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900307 return res;
308
309 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000310
Brian Norriscdbec052012-01-13 18:11:48 -0800311 if (likely(chip->badblockbits == 8))
312 res = bad != 0xFF;
313 else
314 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900315 if (res)
316 return res;
317 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200318
Masahiro Yamadac120e752017-03-23 05:07:01 +0900319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Boris Brezillon99f33512018-11-11 08:55:04 +0100322static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
323{
324 if (chip->legacy.block_bad)
325 return chip->legacy.block_bad(chip, ofs);
326
327 return nand_block_bad(chip, ofs);
328}
329
330/**
Boris Brezillon99f33512018-11-11 08:55:04 +0100331 * nand_get_device - [GENERIC] Get chip for selected access
332 * @chip: NAND chip structure
Boris Brezillon99f33512018-11-11 08:55:04 +0100333 *
Boris Brezillon013e6292018-11-20 11:57:20 +0100334 * Lock the device and its controller for exclusive access
335 *
336 * Return: -EBUSY if the chip has been suspended, 0 otherwise
Boris Brezillon99f33512018-11-11 08:55:04 +0100337 */
Boris Brezillon013e6292018-11-20 11:57:20 +0100338static int nand_get_device(struct nand_chip *chip)
Boris Brezillon99f33512018-11-11 08:55:04 +0100339{
Boris Brezillon013e6292018-11-20 11:57:20 +0100340 mutex_lock(&chip->lock);
341 if (chip->suspended) {
342 mutex_unlock(&chip->lock);
343 return -EBUSY;
Boris Brezillon99f33512018-11-11 08:55:04 +0100344 }
Boris Brezillon013e6292018-11-20 11:57:20 +0100345 mutex_lock(&chip->controller->lock);
346
347 return 0;
Boris Brezillon99f33512018-11-11 08:55:04 +0100348}
349
350/**
351 * nand_check_wp - [GENERIC] check if the chip is write protected
352 * @chip: NAND chip object
353 *
354 * Check, if the device is write protected. The function expects, that the
355 * device is already selected.
356 */
357static int nand_check_wp(struct nand_chip *chip)
358{
359 u8 status;
360 int ret;
361
362 /* Broken xD cards report WP despite being writable */
363 if (chip->options & NAND_BROKEN_XD)
364 return 0;
365
366 /* Check the WP bit */
367 ret = nand_status_op(chip, &status);
368 if (ret)
369 return ret;
370
371 return status & NAND_STATUS_WP ? 0 : 1;
372}
373
374/**
375 * nand_fill_oob - [INTERN] Transfer client buffer to oob
376 * @oob: oob data buffer
377 * @len: oob data write length
378 * @ops: oob ops structure
379 */
380static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
381 struct mtd_oob_ops *ops)
382{
383 struct mtd_info *mtd = nand_to_mtd(chip);
384 int ret;
385
386 /*
387 * Initialise to all 0xFF, to avoid the possibility of left over OOB
388 * data from a previous OOB read.
389 */
390 memset(chip->oob_poi, 0xff, mtd->oobsize);
391
392 switch (ops->mode) {
393
394 case MTD_OPS_PLACE_OOB:
395 case MTD_OPS_RAW:
396 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
397 return oob + len;
398
399 case MTD_OPS_AUTO_OOB:
400 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
401 ops->ooboffs, len);
402 BUG_ON(ret);
403 return oob + len;
404
405 default:
406 BUG();
407 }
408 return NULL;
409}
410
411/**
412 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
413 * @chip: NAND chip object
414 * @to: offset to write to
415 * @ops: oob operation description structure
416 *
417 * NAND write out-of-band.
418 */
419static int nand_do_write_oob(struct nand_chip *chip, loff_t to,
420 struct mtd_oob_ops *ops)
421{
422 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +0900423 int chipnr, page, status, len, ret;
Boris Brezillon99f33512018-11-11 08:55:04 +0100424
425 pr_debug("%s: to = 0x%08x, len = %i\n",
426 __func__, (unsigned int)to, (int)ops->ooblen);
427
428 len = mtd_oobavail(mtd, ops);
429
430 /* Do not allow write past end of page */
431 if ((ops->ooboffs + ops->ooblen) > len) {
432 pr_debug("%s: attempt to write past end of page\n",
433 __func__);
434 return -EINVAL;
435 }
436
437 chipnr = (int)(to >> chip->chip_shift);
438
439 /*
440 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
441 * of my DiskOnChip 2000 test units) will clear the whole data page too
442 * if we don't do this. I have no clue why, but I seem to have 'fixed'
443 * it in the doc2000 driver in August 1999. dwmw2.
444 */
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +0900445 ret = nand_reset(chip, chipnr);
446 if (ret)
447 return ret;
Boris Brezillon99f33512018-11-11 08:55:04 +0100448
Boris Brezillon1d017852018-11-11 08:55:14 +0100449 nand_select_target(chip, chipnr);
Boris Brezillon99f33512018-11-11 08:55:04 +0100450
451 /* Shift to get page */
452 page = (int)(to >> chip->page_shift);
453
454 /* Check, if it is write protected */
455 if (nand_check_wp(chip)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100456 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100457 return -EROFS;
458 }
459
460 /* Invalidate the page cache, if we write to the cached page */
461 if (page == chip->pagebuf)
462 chip->pagebuf = -1;
463
464 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
465
466 if (ops->mode == MTD_OPS_RAW)
467 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
468 else
469 status = chip->ecc.write_oob(chip, page & chip->pagemask);
470
Boris Brezillon1d017852018-11-11 08:55:14 +0100471 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100472
473 if (status)
474 return status;
475
476 ops->oobretlen = ops->ooblen;
477
478 return 0;
479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700482 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200483 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700484 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700486 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700487 * specific driver. It provides the details for writing a bad block marker to a
488 * block.
489 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200490static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700491{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200492 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700493 struct mtd_oob_ops ops;
494 uint8_t buf[2] = { 0, 0 };
495 int ret = 0, res, i = 0;
496
Brian Norris0ec56dc2015-02-28 02:02:30 -0800497 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700498 ops.oobbuf = buf;
499 ops.ooboffs = chip->badblockpos;
500 if (chip->options & NAND_BUSWIDTH_16) {
501 ops.ooboffs &= ~0x01;
502 ops.len = ops.ooblen = 2;
503 } else {
504 ops.len = ops.ooblen = 1;
505 }
506 ops.mode = MTD_OPS_PLACE_OOB;
507
508 /* Write to first/last page(s) if necessary */
509 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
510 ofs += mtd->erasesize - mtd->writesize;
511 do {
Boris Brezillon08136212018-11-11 08:55:03 +0100512 res = nand_do_write_oob(chip, ofs, &ops);
Brian Norris5a0edb22013-07-30 17:52:58 -0700513 if (!ret)
514 ret = res;
515
516 i++;
517 ofs += mtd->writesize;
518 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
519
520 return ret;
521}
522
523/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200524 * nand_markbad_bbm - mark a block by updating the BBM
525 * @chip: NAND chip object
526 * @ofs: offset of the block to mark bad
527 */
528int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
529{
530 if (chip->legacy.block_markbad)
531 return chip->legacy.block_markbad(chip, ofs);
532
533 return nand_default_block_markbad(chip, ofs);
534}
535
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200536/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700537 * nand_block_markbad_lowlevel - mark a block bad
Boris Brezillon08136212018-11-11 08:55:03 +0100538 * @chip: NAND chip object
Brian Norris5a0edb22013-07-30 17:52:58 -0700539 * @ofs: offset from device start
540 *
541 * This function performs the generic NAND bad block marking steps (i.e., bad
542 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200543 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700544 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700545 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300546 *
Brian Norrise2414f42012-02-06 13:44:00 -0800547 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700548 * (2) write bad block marker to OOB area of affected block (unless flag
549 * NAND_BBT_NO_OOB_BBM is present)
550 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300551 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700552 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800553 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554*/
Boris Brezillon08136212018-11-11 08:55:03 +0100555static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Boris Brezillon08136212018-11-11 08:55:03 +0100557 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisb32843b2013-07-30 17:52:59 -0700558 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000559
Brian Norrisb32843b2013-07-30 17:52:59 -0700560 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800561 struct erase_info einfo;
562
563 /* Attempt erase before marking OOB */
564 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800565 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300566 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200567 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800568
Brian Norrisb32843b2013-07-30 17:52:59 -0700569 /* Write bad block marker to OOB */
Boris Brezillon013e6292018-11-20 11:57:20 +0100570 ret = nand_get_device(chip);
571 if (ret)
572 return ret;
573
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200574 ret = nand_markbad_bbm(chip, ofs);
Boris Brezillon08136212018-11-11 08:55:03 +0100575 nand_release_device(chip);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200576 }
Brian Norrise2414f42012-02-06 13:44:00 -0800577
Brian Norrisb32843b2013-07-30 17:52:59 -0700578 /* Mark block bad in BBT */
579 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200580 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800581 if (!ret)
582 ret = res;
583 }
584
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200585 if (!ret)
586 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300587
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200588 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000591/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800592 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700593 * @mtd: MTD device structure
594 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300595 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800596 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300597 */
598static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
599{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100600 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300601
602 if (!chip->bbt)
603 return 0;
604 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200605 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300606}
607
608/**
609 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Boris Brezillon08136212018-11-11 08:55:03 +0100610 * @chip: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300611 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700612 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *
614 * Check, if the block is bad. Either by reading the bad block table or
615 * calling of the scan function.
616 */
Boris Brezillon08136212018-11-11 08:55:03 +0100617static int nand_block_checkbad(struct nand_chip *chip, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200620 if (chip->bbt)
621 return nand_isbad_bbt(chip, ofs, allowbbt);
622
623 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200626/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100627 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
628 * @chip: NAND chip structure
629 * @timeout_ms: Timeout in ms
630 *
631 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
632 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
633 * returned.
634 *
635 * This helper is intended to be used when the controller does not have access
636 * to the NAND R/B pin.
637 *
638 * Be aware that calling this helper from an ->exec_op() implementation means
639 * ->exec_op() must be re-entrant.
640 *
641 * Return 0 if the NAND chip is ready, a negative error otherwise.
642 */
643int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
644{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200645 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100646 u8 status = 0;
647 int ret;
648
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100649 if (!nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +0100650 return -ENOTSUPP;
651
Boris Brezillon3057fce2018-05-04 21:24:31 +0200652 /* Wait tWB before polling the STATUS reg. */
653 timings = nand_get_sdr_timings(&chip->data_interface);
654 ndelay(PSEC_TO_NSEC(timings->tWB_max));
655
Miquel Raynal8878b122017-11-09 14:16:45 +0100656 ret = nand_status_op(chip, NULL);
657 if (ret)
658 return ret;
659
660 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
661 do {
662 ret = nand_read_data_op(chip, &status, sizeof(status), true);
663 if (ret)
664 break;
665
666 if (status & NAND_STATUS_READY)
667 break;
668
669 /*
670 * Typical lowest execution time for a tR on most NANDs is 10us,
671 * use this as polling delay before doing something smarter (ie.
672 * deriving a delay from the timeout value, timeout_ms/ratio).
673 */
674 udelay(10);
675 } while (time_before(jiffies, timeout_ms));
676
677 /*
678 * We have to exit READ_STATUS mode in order to read real data on the
679 * bus in case the WAITRDY instruction is preceding a DATA_IN
680 * instruction.
681 */
682 nand_exit_status_op(chip);
683
684 if (ret)
685 return ret;
686
687 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
688};
689EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
690
691/**
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +0200692 * nand_gpio_waitrdy - Poll R/B GPIO pin until ready
693 * @chip: NAND chip structure
694 * @gpiod: GPIO descriptor of R/B pin
695 * @timeout_ms: Timeout in ms
696 *
697 * Poll the R/B GPIO pin until it becomes ready. If that does not happen
698 * whitin the specified timeout, -ETIMEDOUT is returned.
699 *
700 * This helper is intended to be used when the controller has access to the
701 * NAND R/B pin over GPIO.
702 *
703 * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise.
704 */
705int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
706 unsigned long timeout_ms)
707{
708 /* Wait until R/B pin indicates chip is ready or timeout occurs */
709 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
710 do {
711 if (gpiod_get_value_cansleep(gpiod))
712 return 0;
713
714 cond_resched();
715 } while (time_before(jiffies, timeout_ms));
716
717 return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT;
718};
719EXPORT_SYMBOL_GPL(nand_gpio_waitrdy);
720
721/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700722 * panic_nand_wait - [GENERIC] wait until the command is done
Brian Norris8b6e50c2011-05-25 14:59:01 -0700723 * @chip: NAND chip structure
724 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200725 *
726 * Wait for command done. This is a helper function for nand_wait used when
727 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400728 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200729 */
Boris Brezillon3d4af7c2018-09-07 00:38:49 +0200730void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200731{
732 int i;
733 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200734 if (chip->legacy.dev_ready) {
735 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200736 break;
737 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100738 int ret;
739 u8 status;
740
741 ret = nand_read_data_op(chip, &status, sizeof(status),
742 true);
743 if (ret)
744 return;
745
746 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200747 break;
748 }
749 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200750 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200751}
752
Miquel Raynal789157e2018-03-19 14:47:28 +0100753static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +0100754{
Miquel Raynal789157e2018-03-19 14:47:28 +0100755 return (chip->parameters.supports_set_get_features &&
756 test_bit(addr, chip->parameters.get_feature_list));
757}
758
759static bool nand_supports_set_features(struct nand_chip *chip, int addr)
760{
761 return (chip->parameters.supports_set_get_features &&
762 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +0100763}
764
765/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200766 * nand_reset_data_interface - Reset data interface and timings
767 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100768 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200769 *
770 * Reset the Data interface and timings to ONFI mode 0.
771 *
772 * Returns 0 for success or negative error code otherwise.
773 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100774static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200775{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200776 int ret;
777
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100778 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200779 return 0;
780
781 /*
782 * The ONFI specification says:
783 * "
784 * To transition from NV-DDR or NV-DDR2 to the SDR data
785 * interface, the host shall use the Reset (FFh) command
786 * using SDR timing mode 0. A device in any timing mode is
787 * required to recognize Reset (FFh) command issued in SDR
788 * timing mode 0.
789 * "
790 *
791 * Configure the data interface in SDR mode and set the
792 * timings to timing mode 0.
793 */
794
Miquel Raynal17fa8042017-11-30 18:01:31 +0100795 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100796 ret = chip->controller->ops->setup_data_interface(chip, chipnr,
797 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200798 if (ret)
799 pr_err("Failed to configure data interface to SDR timing mode 0\n");
800
801 return ret;
802}
803
804/**
805 * nand_setup_data_interface - Setup the best data interface and timings
806 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100807 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200808 *
809 * Find and configure the best data interface and NAND timings supported by
810 * the chip and the driver.
811 * First tries to retrieve supported timing modes from ONFI information,
812 * and if the NAND chip does not support ONFI, relies on the
813 * ->onfi_timing_mode_default specified in the nand_ids table.
814 *
815 * Returns 0 for success or negative error code otherwise.
816 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100817static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200818{
Miquel Raynal97baea12018-03-19 14:47:20 +0100819 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
820 chip->onfi_timing_mode_default,
821 };
Boris Brezillond8e725d2016-09-15 10:32:50 +0200822 int ret;
823
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100824 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200825 return 0;
826
Miquel Raynal993447b2018-03-19 14:47:21 +0100827 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +0100828 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100829 nand_select_target(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +0100830 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
831 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100832 nand_deselect_target(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200833 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +0100834 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +0200835 }
836
Miquel Raynal97baea12018-03-19 14:47:20 +0100837 /* Change the mode on the controller side */
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100838 ret = chip->controller->ops->setup_data_interface(chip, chipnr,
839 &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +0100840 if (ret)
841 return ret;
842
843 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +0100844 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +0100845 return 0;
846
847 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon1d017852018-11-11 08:55:14 +0100848 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100849 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
850 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100851 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100852 if (ret)
853 goto err_reset_chip;
854
855 if (tmode_param[0] != chip->onfi_timing_mode_default) {
856 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
857 chip->onfi_timing_mode_default);
858 goto err_reset_chip;
859 }
860
861 return 0;
862
863err_reset_chip:
864 /*
865 * Fallback to mode 0 if the chip explicitly did not ack the chosen
866 * timing mode.
867 */
868 nand_reset_data_interface(chip, chipnr);
Boris Brezillon1d017852018-11-11 08:55:14 +0100869 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100870 nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +0100871 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100872
Boris Brezillond8e725d2016-09-15 10:32:50 +0200873 return ret;
874}
875
876/**
877 * nand_init_data_interface - find the best data interface and timings
878 * @chip: The NAND chip
879 *
880 * Find the best data interface and NAND timings supported by the chip
881 * and the driver.
882 * First tries to retrieve supported timing modes from ONFI information,
883 * and if the NAND chip does not support ONFI, relies on the
884 * ->onfi_timing_mode_default specified in the nand_ids table. After this
885 * function nand_chip->data_interface is initialized with the best timing mode
886 * available.
887 *
888 * Returns 0 for success or negative error code otherwise.
889 */
890static int nand_init_data_interface(struct nand_chip *chip)
891{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200892 int modes, mode, ret;
893
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100894 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200895 return 0;
896
897 /*
898 * First try to identify the best timings from ONFI parameters and
899 * if the NAND does not support ONFI, fallback to the default ONFI
900 * timing mode.
901 */
Boris Brezillon462f35d2018-09-07 00:38:47 +0200902 if (chip->parameters.onfi) {
903 modes = chip->parameters.onfi->async_timing_mode;
904 } else {
Boris Brezillond8e725d2016-09-15 10:32:50 +0200905 if (!chip->onfi_timing_mode_default)
906 return 0;
907
908 modes = GENMASK(chip->onfi_timing_mode_default, 0);
909 }
910
Boris Brezillond8e725d2016-09-15 10:32:50 +0200911 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +0100912 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200913 if (ret)
914 continue;
915
Miquel Raynald787b8b2017-12-22 18:12:41 +0100916 /*
917 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
918 * controller supports the requested timings.
919 */
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100920 ret = chip->controller->ops->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +0100921 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +0100922 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200923 if (!ret) {
924 chip->onfi_timing_mode_default = mode;
925 break;
926 }
927 }
928
929 return 0;
930}
931
Boris Brezillond8e725d2016-09-15 10:32:50 +0200932/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100933 * nand_fill_column_cycles - fill the column cycles of an address
934 * @chip: The NAND chip
935 * @addrs: Array of address cycles to fill
936 * @offset_in_page: The offset in the page
937 *
938 * Fills the first or the first two bytes of the @addrs field depending
939 * on the NAND bus width and the page size.
940 *
941 * Returns the number of cycles needed to encode the column, or a negative
942 * error code in case one of the arguments is invalid.
943 */
944static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
945 unsigned int offset_in_page)
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100946{
Miquel Raynal8878b122017-11-09 14:16:45 +0100947 struct mtd_info *mtd = nand_to_mtd(chip);
948
949 /* Make sure the offset is less than the actual page size. */
950 if (offset_in_page > mtd->writesize + mtd->oobsize)
951 return -EINVAL;
952
953 /*
954 * On small page NANDs, there's a dedicated command to access the OOB
955 * area, and the column address is relative to the start of the OOB
956 * area, not the start of the page. Asjust the address accordingly.
957 */
958 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
959 offset_in_page -= mtd->writesize;
960
961 /*
962 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
963 * wide, then it must be divided by 2.
964 */
965 if (chip->options & NAND_BUSWIDTH_16) {
966 if (WARN_ON(offset_in_page % 2))
967 return -EINVAL;
968
969 offset_in_page /= 2;
970 }
971
972 addrs[0] = offset_in_page;
973
974 /*
975 * Small page NANDs use 1 cycle for the columns, while large page NANDs
976 * need 2
977 */
978 if (mtd->writesize <= 512)
979 return 1;
980
981 addrs[1] = offset_in_page >> 8;
982
983 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Miquel Raynal8878b122017-11-09 14:16:45 +0100986static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
987 unsigned int offset_in_page, void *buf,
988 unsigned int len)
989{
990 struct mtd_info *mtd = nand_to_mtd(chip);
991 const struct nand_sdr_timings *sdr =
992 nand_get_sdr_timings(&chip->data_interface);
993 u8 addrs[4];
994 struct nand_op_instr instrs[] = {
995 NAND_OP_CMD(NAND_CMD_READ0, 0),
996 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
997 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
998 PSEC_TO_NSEC(sdr->tRR_min)),
999 NAND_OP_DATA_IN(len, buf, 0),
1000 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001001 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001002 int ret;
1003
1004 /* Drop the DATA_IN instruction if len is set to 0. */
1005 if (!len)
1006 op.ninstrs--;
1007
1008 if (offset_in_page >= mtd->writesize)
1009 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1010 else if (offset_in_page >= 256 &&
1011 !(chip->options & NAND_BUSWIDTH_16))
1012 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1013
1014 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1015 if (ret < 0)
1016 return ret;
1017
1018 addrs[1] = page;
1019 addrs[2] = page >> 8;
1020
1021 if (chip->options & NAND_ROW_ADDR_3) {
1022 addrs[3] = page >> 16;
1023 instrs[1].ctx.addr.naddrs++;
1024 }
1025
1026 return nand_exec_op(chip, &op);
1027}
1028
1029static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1030 unsigned int offset_in_page, void *buf,
1031 unsigned int len)
1032{
1033 const struct nand_sdr_timings *sdr =
1034 nand_get_sdr_timings(&chip->data_interface);
1035 u8 addrs[5];
1036 struct nand_op_instr instrs[] = {
1037 NAND_OP_CMD(NAND_CMD_READ0, 0),
1038 NAND_OP_ADDR(4, addrs, 0),
1039 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1040 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1041 PSEC_TO_NSEC(sdr->tRR_min)),
1042 NAND_OP_DATA_IN(len, buf, 0),
1043 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001044 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001045 int ret;
1046
1047 /* Drop the DATA_IN instruction if len is set to 0. */
1048 if (!len)
1049 op.ninstrs--;
1050
1051 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1052 if (ret < 0)
1053 return ret;
1054
1055 addrs[2] = page;
1056 addrs[3] = page >> 8;
1057
1058 if (chip->options & NAND_ROW_ADDR_3) {
1059 addrs[4] = page >> 16;
1060 instrs[1].ctx.addr.naddrs++;
1061 }
1062
1063 return nand_exec_op(chip, &op);
1064}
1065
1066/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001067 * nand_read_page_op - Do a READ PAGE operation
1068 * @chip: The NAND chip
1069 * @page: page to read
1070 * @offset_in_page: offset within the page
1071 * @buf: buffer used to store the data
1072 * @len: length of the buffer
1073 *
1074 * This function issues a READ PAGE operation.
1075 * This function does not select/unselect the CS line.
1076 *
1077 * Returns 0 on success, a negative error code otherwise.
1078 */
1079int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1080 unsigned int offset_in_page, void *buf, unsigned int len)
1081{
1082 struct mtd_info *mtd = nand_to_mtd(chip);
1083
1084 if (len && !buf)
1085 return -EINVAL;
1086
1087 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1088 return -EINVAL;
1089
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001090 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001091 if (mtd->writesize > 512)
1092 return nand_lp_exec_read_page_op(chip, page,
1093 offset_in_page, buf,
1094 len);
1095
1096 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1097 buf, len);
1098 }
1099
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001100 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001101 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001102 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001103
1104 return 0;
1105}
1106EXPORT_SYMBOL_GPL(nand_read_page_op);
1107
1108/**
1109 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1110 * @chip: The NAND chip
1111 * @page: parameter page to read
1112 * @buf: buffer used to store the data
1113 * @len: length of the buffer
1114 *
1115 * This function issues a READ PARAMETER PAGE operation.
1116 * This function does not select/unselect the CS line.
1117 *
1118 * Returns 0 on success, a negative error code otherwise.
1119 */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02001120int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1121 unsigned int len)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001122{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001123 unsigned int i;
1124 u8 *p = buf;
1125
1126 if (len && !buf)
1127 return -EINVAL;
1128
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001129 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001130 const struct nand_sdr_timings *sdr =
1131 nand_get_sdr_timings(&chip->data_interface);
1132 struct nand_op_instr instrs[] = {
1133 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1134 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1135 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1136 PSEC_TO_NSEC(sdr->tRR_min)),
1137 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1138 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001139 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001140
1141 /* Drop the DATA_IN instruction if len is set to 0. */
1142 if (!len)
1143 op.ninstrs--;
1144
1145 return nand_exec_op(chip, &op);
1146 }
1147
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001148 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001149 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001150 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001151
1152 return 0;
1153}
1154
1155/**
1156 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1157 * @chip: The NAND chip
1158 * @offset_in_page: offset within the page
1159 * @buf: buffer used to store the data
1160 * @len: length of the buffer
1161 * @force_8bit: force 8-bit bus access
1162 *
1163 * This function issues a CHANGE READ COLUMN operation.
1164 * This function does not select/unselect the CS line.
1165 *
1166 * Returns 0 on success, a negative error code otherwise.
1167 */
1168int nand_change_read_column_op(struct nand_chip *chip,
1169 unsigned int offset_in_page, void *buf,
1170 unsigned int len, bool force_8bit)
1171{
1172 struct mtd_info *mtd = nand_to_mtd(chip);
1173
1174 if (len && !buf)
1175 return -EINVAL;
1176
1177 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1178 return -EINVAL;
1179
Miquel Raynal8878b122017-11-09 14:16:45 +01001180 /* Small page NANDs do not support column change. */
1181 if (mtd->writesize <= 512)
1182 return -ENOTSUPP;
1183
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001184 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001185 const struct nand_sdr_timings *sdr =
1186 nand_get_sdr_timings(&chip->data_interface);
1187 u8 addrs[2] = {};
1188 struct nand_op_instr instrs[] = {
1189 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1190 NAND_OP_ADDR(2, addrs, 0),
1191 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1192 PSEC_TO_NSEC(sdr->tCCS_min)),
1193 NAND_OP_DATA_IN(len, buf, 0),
1194 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001195 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001196 int ret;
1197
1198 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1199 if (ret < 0)
1200 return ret;
1201
1202 /* Drop the DATA_IN instruction if len is set to 0. */
1203 if (!len)
1204 op.ninstrs--;
1205
1206 instrs[3].ctx.data.force_8bit = force_8bit;
1207
1208 return nand_exec_op(chip, &op);
1209 }
1210
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001211 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001212 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001213 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001214
1215 return 0;
1216}
1217EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1218
1219/**
1220 * nand_read_oob_op - Do a READ OOB operation
1221 * @chip: The NAND chip
1222 * @page: page to read
1223 * @offset_in_oob: offset within the OOB area
1224 * @buf: buffer used to store the data
1225 * @len: length of the buffer
1226 *
1227 * This function issues a READ OOB operation.
1228 * This function does not select/unselect the CS line.
1229 *
1230 * Returns 0 on success, a negative error code otherwise.
1231 */
1232int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1233 unsigned int offset_in_oob, void *buf, unsigned int len)
1234{
1235 struct mtd_info *mtd = nand_to_mtd(chip);
1236
1237 if (len && !buf)
1238 return -EINVAL;
1239
1240 if (offset_in_oob + len > mtd->oobsize)
1241 return -EINVAL;
1242
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001243 if (nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +01001244 return nand_read_page_op(chip, page,
1245 mtd->writesize + offset_in_oob,
1246 buf, len);
1247
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001248 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001249 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001250 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001251
1252 return 0;
1253}
1254EXPORT_SYMBOL_GPL(nand_read_oob_op);
1255
Miquel Raynal8878b122017-11-09 14:16:45 +01001256static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1257 unsigned int offset_in_page, const void *buf,
1258 unsigned int len, bool prog)
1259{
1260 struct mtd_info *mtd = nand_to_mtd(chip);
1261 const struct nand_sdr_timings *sdr =
1262 nand_get_sdr_timings(&chip->data_interface);
1263 u8 addrs[5] = {};
1264 struct nand_op_instr instrs[] = {
1265 /*
1266 * The first instruction will be dropped if we're dealing
1267 * with a large page NAND and adjusted if we're dealing
1268 * with a small page NAND and the page offset is > 255.
1269 */
1270 NAND_OP_CMD(NAND_CMD_READ0, 0),
1271 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1272 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1273 NAND_OP_DATA_OUT(len, buf, 0),
1274 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1275 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1276 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001277 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001278 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1279 int ret;
1280 u8 status;
1281
1282 if (naddrs < 0)
1283 return naddrs;
1284
1285 addrs[naddrs++] = page;
1286 addrs[naddrs++] = page >> 8;
1287 if (chip->options & NAND_ROW_ADDR_3)
1288 addrs[naddrs++] = page >> 16;
1289
1290 instrs[2].ctx.addr.naddrs = naddrs;
1291
1292 /* Drop the last two instructions if we're not programming the page. */
1293 if (!prog) {
1294 op.ninstrs -= 2;
1295 /* Also drop the DATA_OUT instruction if empty. */
1296 if (!len)
1297 op.ninstrs--;
1298 }
1299
1300 if (mtd->writesize <= 512) {
1301 /*
1302 * Small pages need some more tweaking: we have to adjust the
1303 * first instruction depending on the page offset we're trying
1304 * to access.
1305 */
1306 if (offset_in_page >= mtd->writesize)
1307 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1308 else if (offset_in_page >= 256 &&
1309 !(chip->options & NAND_BUSWIDTH_16))
1310 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1311 } else {
1312 /*
1313 * Drop the first command if we're dealing with a large page
1314 * NAND.
1315 */
1316 op.instrs++;
1317 op.ninstrs--;
1318 }
1319
1320 ret = nand_exec_op(chip, &op);
1321 if (!prog || ret)
1322 return ret;
1323
1324 ret = nand_status_op(chip, &status);
1325 if (ret)
1326 return ret;
1327
1328 return status;
1329}
1330
Boris Brezillon97d90da2017-11-30 18:01:29 +01001331/**
1332 * nand_prog_page_begin_op - starts a PROG PAGE operation
1333 * @chip: The NAND chip
1334 * @page: page to write
1335 * @offset_in_page: offset within the page
1336 * @buf: buffer containing the data to write to the page
1337 * @len: length of the buffer
1338 *
1339 * This function issues the first half of a PROG PAGE operation.
1340 * This function does not select/unselect the CS line.
1341 *
1342 * Returns 0 on success, a negative error code otherwise.
1343 */
1344int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1345 unsigned int offset_in_page, const void *buf,
1346 unsigned int len)
1347{
1348 struct mtd_info *mtd = nand_to_mtd(chip);
1349
1350 if (len && !buf)
1351 return -EINVAL;
1352
1353 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1354 return -EINVAL;
1355
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001356 if (nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +01001357 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1358 len, false);
1359
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001360 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001361
1362 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001363 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001364
1365 return 0;
1366}
1367EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1368
1369/**
1370 * nand_prog_page_end_op - ends a PROG PAGE operation
1371 * @chip: The NAND chip
1372 *
1373 * This function issues the second half of a PROG PAGE operation.
1374 * This function does not select/unselect the CS line.
1375 *
1376 * Returns 0 on success, a negative error code otherwise.
1377 */
1378int nand_prog_page_end_op(struct nand_chip *chip)
1379{
Miquel Raynal8878b122017-11-09 14:16:45 +01001380 int ret;
1381 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001382
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001383 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001384 const struct nand_sdr_timings *sdr =
1385 nand_get_sdr_timings(&chip->data_interface);
1386 struct nand_op_instr instrs[] = {
1387 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1388 PSEC_TO_NSEC(sdr->tWB_max)),
1389 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1390 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001391 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001392
Miquel Raynal8878b122017-11-09 14:16:45 +01001393 ret = nand_exec_op(chip, &op);
1394 if (ret)
1395 return ret;
1396
1397 ret = nand_status_op(chip, &status);
1398 if (ret)
1399 return ret;
1400 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001401 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001402 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001403 if (ret < 0)
1404 return ret;
1405
1406 status = ret;
1407 }
1408
Boris Brezillon97d90da2017-11-30 18:01:29 +01001409 if (status & NAND_STATUS_FAIL)
1410 return -EIO;
1411
1412 return 0;
1413}
1414EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1415
1416/**
1417 * nand_prog_page_op - Do a full PROG PAGE operation
1418 * @chip: The NAND chip
1419 * @page: page to write
1420 * @offset_in_page: offset within the page
1421 * @buf: buffer containing the data to write to the page
1422 * @len: length of the buffer
1423 *
1424 * This function issues a full PROG PAGE operation.
1425 * This function does not select/unselect the CS line.
1426 *
1427 * Returns 0 on success, a negative error code otherwise.
1428 */
1429int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1430 unsigned int offset_in_page, const void *buf,
1431 unsigned int len)
1432{
1433 struct mtd_info *mtd = nand_to_mtd(chip);
1434 int status;
1435
1436 if (!len || !buf)
1437 return -EINVAL;
1438
1439 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1440 return -EINVAL;
1441
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001442 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001443 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1444 len, true);
1445 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001446 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1447 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001448 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001449 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001450 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001451 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001452
Boris Brezillon97d90da2017-11-30 18:01:29 +01001453 if (status & NAND_STATUS_FAIL)
1454 return -EIO;
1455
1456 return 0;
1457}
1458EXPORT_SYMBOL_GPL(nand_prog_page_op);
1459
1460/**
1461 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1462 * @chip: The NAND chip
1463 * @offset_in_page: offset within the page
1464 * @buf: buffer containing the data to send to the NAND
1465 * @len: length of the buffer
1466 * @force_8bit: force 8-bit bus access
1467 *
1468 * This function issues a CHANGE WRITE COLUMN operation.
1469 * This function does not select/unselect the CS line.
1470 *
1471 * Returns 0 on success, a negative error code otherwise.
1472 */
1473int nand_change_write_column_op(struct nand_chip *chip,
1474 unsigned int offset_in_page,
1475 const void *buf, unsigned int len,
1476 bool force_8bit)
1477{
1478 struct mtd_info *mtd = nand_to_mtd(chip);
1479
1480 if (len && !buf)
1481 return -EINVAL;
1482
1483 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1484 return -EINVAL;
1485
Miquel Raynal8878b122017-11-09 14:16:45 +01001486 /* Small page NANDs do not support column change. */
1487 if (mtd->writesize <= 512)
1488 return -ENOTSUPP;
1489
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001490 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001491 const struct nand_sdr_timings *sdr =
1492 nand_get_sdr_timings(&chip->data_interface);
1493 u8 addrs[2];
1494 struct nand_op_instr instrs[] = {
1495 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1496 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1497 NAND_OP_DATA_OUT(len, buf, 0),
1498 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001499 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001500 int ret;
1501
1502 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1503 if (ret < 0)
1504 return ret;
1505
1506 instrs[2].ctx.data.force_8bit = force_8bit;
1507
1508 /* Drop the DATA_OUT instruction if len is set to 0. */
1509 if (!len)
1510 op.ninstrs--;
1511
1512 return nand_exec_op(chip, &op);
1513 }
1514
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001515 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001516 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001517 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001518
1519 return 0;
1520}
1521EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1522
1523/**
1524 * nand_readid_op - Do a READID operation
1525 * @chip: The NAND chip
1526 * @addr: address cycle to pass after the READID command
1527 * @buf: buffer used to store the ID
1528 * @len: length of the buffer
1529 *
1530 * This function sends a READID command and reads back the ID returned by the
1531 * NAND.
1532 * This function does not select/unselect the CS line.
1533 *
1534 * Returns 0 on success, a negative error code otherwise.
1535 */
1536int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1537 unsigned int len)
1538{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001539 unsigned int i;
1540 u8 *id = buf;
1541
1542 if (len && !buf)
1543 return -EINVAL;
1544
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001545 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001546 const struct nand_sdr_timings *sdr =
1547 nand_get_sdr_timings(&chip->data_interface);
1548 struct nand_op_instr instrs[] = {
1549 NAND_OP_CMD(NAND_CMD_READID, 0),
1550 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1551 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1552 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001553 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001554
1555 /* Drop the DATA_IN instruction if len is set to 0. */
1556 if (!len)
1557 op.ninstrs--;
1558
1559 return nand_exec_op(chip, &op);
1560 }
1561
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001562 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001563
1564 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001565 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001566
1567 return 0;
1568}
1569EXPORT_SYMBOL_GPL(nand_readid_op);
1570
1571/**
1572 * nand_status_op - Do a STATUS operation
1573 * @chip: The NAND chip
1574 * @status: out variable to store the NAND status
1575 *
1576 * This function sends a STATUS command and reads back the status returned by
1577 * the NAND.
1578 * This function does not select/unselect the CS line.
1579 *
1580 * Returns 0 on success, a negative error code otherwise.
1581 */
1582int nand_status_op(struct nand_chip *chip, u8 *status)
1583{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001584 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001585 const struct nand_sdr_timings *sdr =
1586 nand_get_sdr_timings(&chip->data_interface);
1587 struct nand_op_instr instrs[] = {
1588 NAND_OP_CMD(NAND_CMD_STATUS,
1589 PSEC_TO_NSEC(sdr->tADL_min)),
1590 NAND_OP_8BIT_DATA_IN(1, status, 0),
1591 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001592 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001593
1594 if (!status)
1595 op.ninstrs--;
1596
1597 return nand_exec_op(chip, &op);
1598 }
1599
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001600 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001601 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001602 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001603
1604 return 0;
1605}
1606EXPORT_SYMBOL_GPL(nand_status_op);
1607
1608/**
1609 * nand_exit_status_op - Exit a STATUS operation
1610 * @chip: The NAND chip
1611 *
1612 * This function sends a READ0 command to cancel the effect of the STATUS
1613 * command to avoid reading only the status until a new read command is sent.
1614 *
1615 * This function does not select/unselect the CS line.
1616 *
1617 * Returns 0 on success, a negative error code otherwise.
1618 */
1619int nand_exit_status_op(struct nand_chip *chip)
1620{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001621 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001622 struct nand_op_instr instrs[] = {
1623 NAND_OP_CMD(NAND_CMD_READ0, 0),
1624 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001625 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001626
1627 return nand_exec_op(chip, &op);
1628 }
1629
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001630 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001631
1632 return 0;
1633}
Boris Brezillon97d90da2017-11-30 18:01:29 +01001634
1635/**
1636 * nand_erase_op - Do an erase operation
1637 * @chip: The NAND chip
1638 * @eraseblock: block to erase
1639 *
1640 * This function sends an ERASE command and waits for the NAND to be ready
1641 * before returning.
1642 * This function does not select/unselect the CS line.
1643 *
1644 * Returns 0 on success, a negative error code otherwise.
1645 */
1646int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1647{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001648 unsigned int page = eraseblock <<
1649 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01001650 int ret;
1651 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001652
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001653 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001654 const struct nand_sdr_timings *sdr =
1655 nand_get_sdr_timings(&chip->data_interface);
1656 u8 addrs[3] = { page, page >> 8, page >> 16 };
1657 struct nand_op_instr instrs[] = {
1658 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
1659 NAND_OP_ADDR(2, addrs, 0),
1660 NAND_OP_CMD(NAND_CMD_ERASE2,
1661 PSEC_TO_MSEC(sdr->tWB_max)),
1662 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
1663 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001664 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001665
Miquel Raynal8878b122017-11-09 14:16:45 +01001666 if (chip->options & NAND_ROW_ADDR_3)
1667 instrs[1].ctx.addr.naddrs++;
1668
1669 ret = nand_exec_op(chip, &op);
1670 if (ret)
1671 return ret;
1672
1673 ret = nand_status_op(chip, &status);
1674 if (ret)
1675 return ret;
1676 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001677 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
1678 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01001679
Boris Brezillon8395b752018-09-07 00:38:37 +02001680 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001681 if (ret < 0)
1682 return ret;
1683
1684 status = ret;
1685 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001686
1687 if (status & NAND_STATUS_FAIL)
1688 return -EIO;
1689
1690 return 0;
1691}
1692EXPORT_SYMBOL_GPL(nand_erase_op);
1693
1694/**
1695 * nand_set_features_op - Do a SET FEATURES operation
1696 * @chip: The NAND chip
1697 * @feature: feature id
1698 * @data: 4 bytes of data
1699 *
1700 * This function sends a SET FEATURES command and waits for the NAND to be
1701 * ready before returning.
1702 * This function does not select/unselect the CS line.
1703 *
1704 * Returns 0 on success, a negative error code otherwise.
1705 */
1706static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1707 const void *data)
1708{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001709 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01001710 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001711
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001712 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001713 const struct nand_sdr_timings *sdr =
1714 nand_get_sdr_timings(&chip->data_interface);
1715 struct nand_op_instr instrs[] = {
1716 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
1717 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
1718 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
1719 PSEC_TO_NSEC(sdr->tWB_max)),
1720 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
1721 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001722 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001723
Boris Brezillon782d1962018-05-11 14:44:07 +02001724 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01001725 }
1726
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001727 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02001728 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001729 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02001730
Boris Brezillon8395b752018-09-07 00:38:37 +02001731 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02001732 if (ret < 0)
1733 return ret;
1734
1735 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001736 return -EIO;
1737
1738 return 0;
1739}
1740
1741/**
1742 * nand_get_features_op - Do a GET FEATURES operation
1743 * @chip: The NAND chip
1744 * @feature: feature id
1745 * @data: 4 bytes of data
1746 *
1747 * This function sends a GET FEATURES command and waits for the NAND to be
1748 * ready before returning.
1749 * This function does not select/unselect the CS line.
1750 *
1751 * Returns 0 on success, a negative error code otherwise.
1752 */
1753static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1754 void *data)
1755{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001756 u8 *params = data;
1757 int i;
1758
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001759 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001760 const struct nand_sdr_timings *sdr =
1761 nand_get_sdr_timings(&chip->data_interface);
1762 struct nand_op_instr instrs[] = {
1763 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
1764 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
1765 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
1766 PSEC_TO_NSEC(sdr->tRR_min)),
1767 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
1768 data, 0),
1769 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001770 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001771
1772 return nand_exec_op(chip, &op);
1773 }
1774
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001775 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001776 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001777 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001778
1779 return 0;
1780}
1781
Boris Brezillon52f05b62018-07-27 09:44:18 +02001782static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
1783 unsigned int delay_ns)
1784{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001785 if (nand_has_exec_op(chip)) {
Boris Brezillon52f05b62018-07-27 09:44:18 +02001786 struct nand_op_instr instrs[] = {
1787 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
1788 PSEC_TO_NSEC(delay_ns)),
1789 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001790 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001791
1792 return nand_exec_op(chip, &op);
1793 }
1794
1795 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02001796 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001797 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001798 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001799 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001800
1801 return 0;
1802}
1803
Boris Brezillon97d90da2017-11-30 18:01:29 +01001804/**
1805 * nand_reset_op - Do a reset operation
1806 * @chip: The NAND chip
1807 *
1808 * This function sends a RESET command and waits for the NAND to be ready
1809 * before returning.
1810 * This function does not select/unselect the CS line.
1811 *
1812 * Returns 0 on success, a negative error code otherwise.
1813 */
1814int nand_reset_op(struct nand_chip *chip)
1815{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001816 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001817 const struct nand_sdr_timings *sdr =
1818 nand_get_sdr_timings(&chip->data_interface);
1819 struct nand_op_instr instrs[] = {
1820 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
1821 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
1822 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001823 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001824
1825 return nand_exec_op(chip, &op);
1826 }
1827
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001828 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001829
1830 return 0;
1831}
1832EXPORT_SYMBOL_GPL(nand_reset_op);
1833
1834/**
1835 * nand_read_data_op - Read data from the NAND
1836 * @chip: The NAND chip
1837 * @buf: buffer used to store the data
1838 * @len: length of the buffer
1839 * @force_8bit: force 8-bit bus access
1840 *
1841 * This function does a raw data read on the bus. Usually used after launching
1842 * another NAND operation like nand_read_page_op().
1843 * This function does not select/unselect the CS line.
1844 *
1845 * Returns 0 on success, a negative error code otherwise.
1846 */
1847int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1848 bool force_8bit)
1849{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001850 if (!len || !buf)
1851 return -EINVAL;
1852
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001853 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001854 struct nand_op_instr instrs[] = {
1855 NAND_OP_DATA_IN(len, buf, 0),
1856 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001857 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001858
1859 instrs[0].ctx.data.force_8bit = force_8bit;
1860
1861 return nand_exec_op(chip, &op);
1862 }
1863
Boris Brezillon97d90da2017-11-30 18:01:29 +01001864 if (force_8bit) {
1865 u8 *p = buf;
1866 unsigned int i;
1867
1868 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001869 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001870 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001871 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001872 }
1873
1874 return 0;
1875}
1876EXPORT_SYMBOL_GPL(nand_read_data_op);
1877
1878/**
1879 * nand_write_data_op - Write data from the NAND
1880 * @chip: The NAND chip
1881 * @buf: buffer containing the data to send on the bus
1882 * @len: length of the buffer
1883 * @force_8bit: force 8-bit bus access
1884 *
1885 * This function does a raw data write on the bus. Usually used after launching
1886 * another NAND operation like nand_write_page_begin_op().
1887 * This function does not select/unselect the CS line.
1888 *
1889 * Returns 0 on success, a negative error code otherwise.
1890 */
1891int nand_write_data_op(struct nand_chip *chip, const void *buf,
1892 unsigned int len, bool force_8bit)
1893{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001894 if (!len || !buf)
1895 return -EINVAL;
1896
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001897 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001898 struct nand_op_instr instrs[] = {
1899 NAND_OP_DATA_OUT(len, buf, 0),
1900 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001901 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001902
1903 instrs[0].ctx.data.force_8bit = force_8bit;
1904
1905 return nand_exec_op(chip, &op);
1906 }
1907
Boris Brezillon97d90da2017-11-30 18:01:29 +01001908 if (force_8bit) {
1909 const u8 *p = buf;
1910 unsigned int i;
1911
1912 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001913 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001914 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001915 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001916 }
1917
1918 return 0;
1919}
1920EXPORT_SYMBOL_GPL(nand_write_data_op);
1921
1922/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001923 * struct nand_op_parser_ctx - Context used by the parser
1924 * @instrs: array of all the instructions that must be addressed
1925 * @ninstrs: length of the @instrs array
1926 * @subop: Sub-operation to be passed to the NAND controller
1927 *
1928 * This structure is used by the core to split NAND operations into
1929 * sub-operations that can be handled by the NAND controller.
1930 */
1931struct nand_op_parser_ctx {
1932 const struct nand_op_instr *instrs;
1933 unsigned int ninstrs;
1934 struct nand_subop subop;
1935};
1936
1937/**
1938 * nand_op_parser_must_split_instr - Checks if an instruction must be split
1939 * @pat: the parser pattern element that matches @instr
1940 * @instr: pointer to the instruction to check
1941 * @start_offset: this is an in/out parameter. If @instr has already been
1942 * split, then @start_offset is the offset from which to start
1943 * (either an address cycle or an offset in the data buffer).
1944 * Conversely, if the function returns true (ie. instr must be
1945 * split), this parameter is updated to point to the first
1946 * data/address cycle that has not been taken care of.
1947 *
1948 * Some NAND controllers are limited and cannot send X address cycles with a
1949 * unique operation, or cannot read/write more than Y bytes at the same time.
1950 * In this case, split the instruction that does not fit in a single
1951 * controller-operation into two or more chunks.
1952 *
1953 * Returns true if the instruction must be split, false otherwise.
1954 * The @start_offset parameter is also updated to the offset at which the next
1955 * bundle of instruction must start (if an address or a data instruction).
1956 */
1957static bool
1958nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
1959 const struct nand_op_instr *instr,
1960 unsigned int *start_offset)
1961{
1962 switch (pat->type) {
1963 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001964 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01001965 break;
1966
1967 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001968 pat->ctx.addr.maxcycles) {
1969 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01001970 return true;
1971 }
1972 break;
1973
1974 case NAND_OP_DATA_IN_INSTR:
1975 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001976 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01001977 break;
1978
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001979 if (instr->ctx.data.len - *start_offset >
1980 pat->ctx.data.maxlen) {
1981 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01001982 return true;
1983 }
1984 break;
1985
1986 default:
1987 break;
1988 }
1989
1990 return false;
1991}
1992
1993/**
1994 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
1995 * remaining in the parser context
1996 * @pat: the pattern to test
1997 * @ctx: the parser context structure to match with the pattern @pat
1998 *
1999 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2000 * Returns true if this is the case, false ortherwise. When true is returned,
2001 * @ctx->subop is updated with the set of instructions to be passed to the
2002 * controller driver.
2003 */
2004static bool
2005nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2006 struct nand_op_parser_ctx *ctx)
2007{
2008 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2009 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2010 const struct nand_op_instr *instr = ctx->subop.instrs;
2011 unsigned int i, ninstrs;
2012
2013 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2014 /*
2015 * The pattern instruction does not match the operation
2016 * instruction. If the instruction is marked optional in the
2017 * pattern definition, we skip the pattern element and continue
2018 * to the next one. If the element is mandatory, there's no
2019 * match and we can return false directly.
2020 */
2021 if (instr->type != pat->elems[i].type) {
2022 if (!pat->elems[i].optional)
2023 return false;
2024
2025 continue;
2026 }
2027
2028 /*
2029 * Now check the pattern element constraints. If the pattern is
2030 * not able to handle the whole instruction in a single step,
2031 * we have to split it.
2032 * The last_instr_end_off value comes back updated to point to
2033 * the position where we have to split the instruction (the
2034 * start of the next subop chunk).
2035 */
2036 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2037 &instr_offset)) {
2038 ninstrs++;
2039 i++;
2040 break;
2041 }
2042
2043 instr++;
2044 ninstrs++;
2045 instr_offset = 0;
2046 }
2047
2048 /*
2049 * This can happen if all instructions of a pattern are optional.
2050 * Still, if there's not at least one instruction handled by this
2051 * pattern, this is not a match, and we should try the next one (if
2052 * any).
2053 */
2054 if (!ninstrs)
2055 return false;
2056
2057 /*
2058 * We had a match on the pattern head, but the pattern may be longer
2059 * than the instructions we're asked to execute. We need to make sure
2060 * there's no mandatory elements in the pattern tail.
2061 */
2062 for (; i < pat->nelems; i++) {
2063 if (!pat->elems[i].optional)
2064 return false;
2065 }
2066
2067 /*
2068 * We have a match: update the subop structure accordingly and return
2069 * true.
2070 */
2071 ctx->subop.ninstrs = ninstrs;
2072 ctx->subop.last_instr_end_off = instr_offset;
2073
2074 return true;
2075}
2076
2077#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2078static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2079{
2080 const struct nand_op_instr *instr;
2081 char *prefix = " ";
2082 unsigned int i;
2083
2084 pr_debug("executing subop:\n");
2085
2086 for (i = 0; i < ctx->ninstrs; i++) {
2087 instr = &ctx->instrs[i];
2088
2089 if (instr == &ctx->subop.instrs[0])
2090 prefix = " ->";
2091
2092 switch (instr->type) {
2093 case NAND_OP_CMD_INSTR:
2094 pr_debug("%sCMD [0x%02x]\n", prefix,
2095 instr->ctx.cmd.opcode);
2096 break;
2097 case NAND_OP_ADDR_INSTR:
2098 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2099 instr->ctx.addr.naddrs,
2100 instr->ctx.addr.naddrs < 64 ?
2101 instr->ctx.addr.naddrs : 64,
2102 instr->ctx.addr.addrs);
2103 break;
2104 case NAND_OP_DATA_IN_INSTR:
2105 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2106 instr->ctx.data.len,
2107 instr->ctx.data.force_8bit ?
2108 ", force 8-bit" : "");
2109 break;
2110 case NAND_OP_DATA_OUT_INSTR:
2111 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2112 instr->ctx.data.len,
2113 instr->ctx.data.force_8bit ?
2114 ", force 8-bit" : "");
2115 break;
2116 case NAND_OP_WAITRDY_INSTR:
2117 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2118 instr->ctx.waitrdy.timeout_ms);
2119 break;
2120 }
2121
2122 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2123 prefix = " ";
2124 }
2125}
2126#else
2127static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2128{
2129 /* NOP */
2130}
2131#endif
2132
2133/**
2134 * nand_op_parser_exec_op - exec_op parser
2135 * @chip: the NAND chip
2136 * @parser: patterns description provided by the controller driver
2137 * @op: the NAND operation to address
2138 * @check_only: when true, the function only checks if @op can be handled but
2139 * does not execute the operation
2140 *
2141 * Helper function designed to ease integration of NAND controller drivers that
2142 * only support a limited set of instruction sequences. The supported sequences
2143 * are described in @parser, and the framework takes care of splitting @op into
2144 * multiple sub-operations (if required) and pass them back to the ->exec()
2145 * callback of the matching pattern if @check_only is set to false.
2146 *
2147 * NAND controller drivers should call this function from their own ->exec_op()
2148 * implementation.
2149 *
2150 * Returns 0 on success, a negative error code otherwise. A failure can be
2151 * caused by an unsupported operation (none of the supported patterns is able
2152 * to handle the requested operation), or an error returned by one of the
2153 * matching pattern->exec() hook.
2154 */
2155int nand_op_parser_exec_op(struct nand_chip *chip,
2156 const struct nand_op_parser *parser,
2157 const struct nand_operation *op, bool check_only)
2158{
2159 struct nand_op_parser_ctx ctx = {
2160 .subop.instrs = op->instrs,
2161 .instrs = op->instrs,
2162 .ninstrs = op->ninstrs,
2163 };
2164 unsigned int i;
2165
2166 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2167 int ret;
2168
2169 for (i = 0; i < parser->npatterns; i++) {
2170 const struct nand_op_parser_pattern *pattern;
2171
2172 pattern = &parser->patterns[i];
2173 if (!nand_op_parser_match_pat(pattern, &ctx))
2174 continue;
2175
2176 nand_op_parser_trace(&ctx);
2177
2178 if (check_only)
2179 break;
2180
2181 ret = pattern->exec(chip, &ctx.subop);
2182 if (ret)
2183 return ret;
2184
2185 break;
2186 }
2187
2188 if (i == parser->npatterns) {
2189 pr_debug("->exec_op() parser: pattern not found!\n");
2190 return -ENOTSUPP;
2191 }
2192
2193 /*
2194 * Update the context structure by pointing to the start of the
2195 * next subop.
2196 */
2197 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2198 if (ctx.subop.last_instr_end_off)
2199 ctx.subop.instrs -= 1;
2200
2201 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2202 }
2203
2204 return 0;
2205}
2206EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2207
2208static bool nand_instr_is_data(const struct nand_op_instr *instr)
2209{
2210 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2211 instr->type == NAND_OP_DATA_OUT_INSTR);
2212}
2213
2214static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2215 unsigned int instr_idx)
2216{
2217 return subop && instr_idx < subop->ninstrs;
2218}
2219
Miquel Raynal760c4352018-07-19 00:09:12 +02002220static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2221 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002222{
2223 if (instr_idx)
2224 return 0;
2225
2226 return subop->first_instr_start_off;
2227}
2228
2229/**
2230 * nand_subop_get_addr_start_off - Get the start offset in an address array
2231 * @subop: The entire sub-operation
2232 * @instr_idx: Index of the instruction inside the sub-operation
2233 *
2234 * During driver development, one could be tempted to directly use the
2235 * ->addr.addrs field of address instructions. This is wrong as address
2236 * instructions might be split.
2237 *
2238 * Given an address instruction, returns the offset of the first cycle to issue.
2239 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002240unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2241 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002242{
Miquel Raynal760c4352018-07-19 00:09:12 +02002243 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2244 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2245 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002246
2247 return nand_subop_get_start_off(subop, instr_idx);
2248}
2249EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2250
2251/**
2252 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2253 * @subop: The entire sub-operation
2254 * @instr_idx: Index of the instruction inside the sub-operation
2255 *
2256 * During driver development, one could be tempted to directly use the
2257 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2258 * might be split.
2259 *
2260 * Given an address instruction, returns the number of address cycle to issue.
2261 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002262unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2263 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002264{
2265 int start_off, end_off;
2266
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 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2272
2273 if (instr_idx == subop->ninstrs - 1 &&
2274 subop->last_instr_end_off)
2275 end_off = subop->last_instr_end_off;
2276 else
2277 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2278
2279 return end_off - start_off;
2280}
2281EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2282
2283/**
2284 * nand_subop_get_data_start_off - Get the start offset in a data array
2285 * @subop: The entire sub-operation
2286 * @instr_idx: Index of the instruction inside the sub-operation
2287 *
2288 * During driver development, one could be tempted to directly use the
2289 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2290 * instructions might be split.
2291 *
2292 * Given a data instruction, returns the offset to start from.
2293 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002294unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2295 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002296{
Miquel Raynal760c4352018-07-19 00:09:12 +02002297 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2298 !nand_instr_is_data(&subop->instrs[instr_idx])))
2299 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002300
2301 return nand_subop_get_start_off(subop, instr_idx);
2302}
2303EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2304
2305/**
2306 * nand_subop_get_data_len - Get the number of bytes to retrieve
2307 * @subop: The entire sub-operation
2308 * @instr_idx: Index of the instruction inside the sub-operation
2309 *
2310 * During driver development, one could be tempted to directly use the
2311 * ->data->len field of a data instruction. This is wrong as data instructions
2312 * might be split.
2313 *
2314 * Returns the length of the chunk of data to send/receive.
2315 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002316unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2317 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002318{
2319 int start_off = 0, end_off;
2320
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 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2326
2327 if (instr_idx == subop->ninstrs - 1 &&
2328 subop->last_instr_end_off)
2329 end_off = subop->last_instr_end_off;
2330 else
2331 end_off = subop->instrs[instr_idx].ctx.data.len;
2332
2333 return end_off - start_off;
2334}
2335EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002338 * nand_reset - Reset and initialize a NAND device
2339 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002340 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002341 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002342 * Save the timings data structure, then apply SDR timings mode 0 (see
2343 * nand_reset_data_interface for details), do the reset operation, and
2344 * apply back the previous timings.
2345 *
2346 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002347 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002348int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002349{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002350 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002351 int ret;
2352
Boris Brezillon104e4422017-03-16 09:35:58 +01002353 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002354 if (ret)
2355 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002356
Boris Brezillon73f907f2016-10-24 16:46:20 +02002357 /*
2358 * The CS line has to be released before we can apply the new NAND
Boris Brezillon1d017852018-11-11 08:55:14 +01002359 * interface settings, hence this weird nand_select_target()
2360 * nand_deselect_target() dance.
Boris Brezillon73f907f2016-10-24 16:46:20 +02002361 */
Boris Brezillon1d017852018-11-11 08:55:14 +01002362 nand_select_target(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002363 ret = nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01002364 nand_deselect_target(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002365 if (ret)
2366 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002367
Miquel Raynal107b7d62018-03-19 14:47:25 +01002368 /*
2369 * A nand_reset_data_interface() put both the NAND chip and the NAND
2370 * controller in timings mode 0. If the default mode for this chip is
2371 * also 0, no need to proceed to the change again. Plus, at probe time,
2372 * nand_setup_data_interface() uses ->set/get_features() which would
2373 * fail anyway as the parameter page is not available yet.
2374 */
2375 if (!chip->onfi_timing_mode_default)
2376 return 0;
2377
Miquel Raynal17fa8042017-11-30 18:01:31 +01002378 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002379 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002380 if (ret)
2381 return ret;
2382
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002383 return 0;
2384}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002385EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002386
2387/**
Boris Brezillon45240362018-09-07 00:38:40 +02002388 * nand_get_features - wrapper to perform a GET_FEATURE
2389 * @chip: NAND chip info structure
2390 * @addr: feature address
2391 * @subfeature_param: the subfeature parameters, a four bytes array
2392 *
2393 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2394 * operation cannot be handled.
2395 */
2396int nand_get_features(struct nand_chip *chip, int addr,
2397 u8 *subfeature_param)
2398{
2399 if (!nand_supports_get_features(chip, addr))
2400 return -ENOTSUPP;
2401
2402 if (chip->legacy.get_features)
2403 return chip->legacy.get_features(chip, addr, subfeature_param);
2404
2405 return nand_get_features_op(chip, addr, subfeature_param);
2406}
Boris Brezillon45240362018-09-07 00:38:40 +02002407
2408/**
2409 * nand_set_features - wrapper to perform a SET_FEATURE
2410 * @chip: NAND chip info structure
2411 * @addr: feature address
2412 * @subfeature_param: the subfeature parameters, a four bytes array
2413 *
2414 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2415 * operation cannot be handled.
2416 */
2417int nand_set_features(struct nand_chip *chip, int addr,
2418 u8 *subfeature_param)
2419{
2420 if (!nand_supports_set_features(chip, addr))
2421 return -ENOTSUPP;
2422
2423 if (chip->legacy.set_features)
2424 return chip->legacy.set_features(chip, addr, subfeature_param);
2425
2426 return nand_set_features_op(chip, addr, subfeature_param);
2427}
Boris Brezillon45240362018-09-07 00:38:40 +02002428
2429/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002430 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2431 * @buf: buffer to test
2432 * @len: buffer length
2433 * @bitflips_threshold: maximum number of bitflips
2434 *
2435 * Check if a buffer contains only 0xff, which means the underlying region
2436 * has been erased and is ready to be programmed.
2437 * The bitflips_threshold specify the maximum number of bitflips before
2438 * considering the region is not erased.
2439 * Note: The logic of this function has been extracted from the memweight
2440 * implementation, except that nand_check_erased_buf function exit before
2441 * testing the whole buffer if the number of bitflips exceed the
2442 * bitflips_threshold value.
2443 *
2444 * Returns a positive number of bitflips less than or equal to
2445 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2446 * threshold.
2447 */
2448static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2449{
2450 const unsigned char *bitmap = buf;
2451 int bitflips = 0;
2452 int weight;
2453
2454 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2455 len--, bitmap++) {
2456 weight = hweight8(*bitmap);
2457 bitflips += BITS_PER_BYTE - weight;
2458 if (unlikely(bitflips > bitflips_threshold))
2459 return -EBADMSG;
2460 }
2461
2462 for (; len >= sizeof(long);
2463 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002464 unsigned long d = *((unsigned long *)bitmap);
2465 if (d == ~0UL)
2466 continue;
2467 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002468 bitflips += BITS_PER_LONG - weight;
2469 if (unlikely(bitflips > bitflips_threshold))
2470 return -EBADMSG;
2471 }
2472
2473 for (; len > 0; len--, bitmap++) {
2474 weight = hweight8(*bitmap);
2475 bitflips += BITS_PER_BYTE - weight;
2476 if (unlikely(bitflips > bitflips_threshold))
2477 return -EBADMSG;
2478 }
2479
2480 return bitflips;
2481}
2482
2483/**
2484 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2485 * 0xff data
2486 * @data: data buffer to test
2487 * @datalen: data length
2488 * @ecc: ECC buffer
2489 * @ecclen: ECC length
2490 * @extraoob: extra OOB buffer
2491 * @extraooblen: extra OOB length
2492 * @bitflips_threshold: maximum number of bitflips
2493 *
2494 * Check if a data buffer and its associated ECC and OOB data contains only
2495 * 0xff pattern, which means the underlying region has been erased and is
2496 * ready to be programmed.
2497 * The bitflips_threshold specify the maximum number of bitflips before
2498 * considering the region as not erased.
2499 *
2500 * Note:
2501 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2502 * different from the NAND page size. When fixing bitflips, ECC engines will
2503 * report the number of errors per chunk, and the NAND core infrastructure
2504 * expect you to return the maximum number of bitflips for the whole page.
2505 * This is why you should always use this function on a single chunk and
2506 * not on the whole page. After checking each chunk you should update your
2507 * max_bitflips value accordingly.
2508 * 2/ When checking for bitflips in erased pages you should not only check
2509 * the payload data but also their associated ECC data, because a user might
2510 * have programmed almost all bits to 1 but a few. In this case, we
2511 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2512 * this case.
2513 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2514 * data are protected by the ECC engine.
2515 * It could also be used if you support subpages and want to attach some
2516 * extra OOB data to an ECC chunk.
2517 *
2518 * Returns a positive number of bitflips less than or equal to
2519 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2520 * threshold. In case of success, the passed buffers are filled with 0xff.
2521 */
2522int nand_check_erased_ecc_chunk(void *data, int datalen,
2523 void *ecc, int ecclen,
2524 void *extraoob, int extraooblen,
2525 int bitflips_threshold)
2526{
2527 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2528
2529 data_bitflips = nand_check_erased_buf(data, datalen,
2530 bitflips_threshold);
2531 if (data_bitflips < 0)
2532 return data_bitflips;
2533
2534 bitflips_threshold -= data_bitflips;
2535
2536 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2537 if (ecc_bitflips < 0)
2538 return ecc_bitflips;
2539
2540 bitflips_threshold -= ecc_bitflips;
2541
2542 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2543 bitflips_threshold);
2544 if (extraoob_bitflips < 0)
2545 return extraoob_bitflips;
2546
2547 if (data_bitflips)
2548 memset(data, 0xff, datalen);
2549
2550 if (ecc_bitflips)
2551 memset(ecc, 0xff, ecclen);
2552
2553 if (extraoob_bitflips)
2554 memset(extraoob, 0xff, extraooblen);
2555
2556 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2557}
2558EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2559
2560/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002561 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002562 * @chip: nand chip info structure
2563 * @buf: buffer to store read data
2564 * @oob_required: caller requires OOB data read to chip->oob_poi
2565 * @page: page number to read
2566 *
2567 * Returns -ENOTSUPP unconditionally.
2568 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002569int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2570 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002571{
2572 return -ENOTSUPP;
2573}
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002574
2575/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002576 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002577 * @chip: nand chip info structure
2578 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002579 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002580 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002581 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002582 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002583 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002584int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2585 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002586{
Boris Brezillonb9761682018-09-06 14:05:20 +02002587 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002588 int ret;
2589
Boris Brezillon25f815f2017-11-30 18:01:30 +01002590 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002591 if (ret)
2592 return ret;
2593
2594 if (oob_required) {
2595 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2596 false);
2597 if (ret)
2598 return ret;
2599 }
2600
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002601 return 0;
2602}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002603EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002604
2605/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002606 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002607 * @chip: nand chip info structure
2608 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002609 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002610 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002611 *
2612 * We need a special oob layout and handling even when OOB isn't used.
2613 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002614static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07002615 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002616{
Boris Brezillonb9761682018-09-06 14:05:20 +02002617 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08002618 int eccsize = chip->ecc.size;
2619 int eccbytes = chip->ecc.bytes;
2620 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002621 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002622
Boris Brezillon25f815f2017-11-30 18:01:30 +01002623 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2624 if (ret)
2625 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002626
2627 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002628 ret = nand_read_data_op(chip, buf, eccsize, false);
2629 if (ret)
2630 return ret;
2631
David Brownell52ff49d2009-03-04 12:01:36 -08002632 buf += eccsize;
2633
2634 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002635 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2636 false);
2637 if (ret)
2638 return ret;
2639
David Brownell52ff49d2009-03-04 12:01:36 -08002640 oob += chip->ecc.prepad;
2641 }
2642
Boris Brezillon97d90da2017-11-30 18:01:29 +01002643 ret = nand_read_data_op(chip, oob, eccbytes, false);
2644 if (ret)
2645 return ret;
2646
David Brownell52ff49d2009-03-04 12:01:36 -08002647 oob += eccbytes;
2648
2649 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002650 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2651 false);
2652 if (ret)
2653 return ret;
2654
David Brownell52ff49d2009-03-04 12:01:36 -08002655 oob += chip->ecc.postpad;
2656 }
2657 }
2658
2659 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002660 if (size) {
2661 ret = nand_read_data_op(chip, oob, size, false);
2662 if (ret)
2663 return ret;
2664 }
David Brownell52ff49d2009-03-04 12:01:36 -08002665
2666 return 0;
2667}
2668
2669/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002670 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002671 * @chip: nand chip info structure
2672 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002673 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002674 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002675 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002676static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
2677 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
Boris Brezillonb9761682018-09-06 14:05:20 +02002679 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002680 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002681 int eccbytes = chip->ecc.bytes;
2682 int eccsteps = chip->ecc.steps;
2683 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002684 uint8_t *ecc_calc = chip->ecc.calc_buf;
2685 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002686 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002687
Boris Brezillonb9761682018-09-06 14:05:20 +02002688 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002689
2690 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002691 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002692
Boris Brezillon846031d2016-02-03 20:11:00 +01002693 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2694 chip->ecc.total);
2695 if (ret)
2696 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002697
2698 eccsteps = chip->ecc.steps;
2699 p = buf;
2700
2701 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2702 int stat;
2703
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002704 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07002705 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002706 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002707 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002708 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002709 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2710 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002711 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002712 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01002713}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302716 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002717 * @chip: nand chip info structure
2718 * @data_offs: offset of requested data within the page
2719 * @readlen: data length
2720 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08002721 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01002722 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002723static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
2724 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01002725{
Boris Brezillonb9761682018-09-06 14:05:20 +02002726 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002727 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002728 uint8_t *p;
2729 int data_col_addr, i, gaps = 0;
2730 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
2731 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01002732 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07002733 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01002734 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01002735
Brian Norris7854d3f2011-06-23 14:12:08 -07002736 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01002737 start_step = data_offs / chip->ecc.size;
2738 end_step = (data_offs + readlen - 1) / chip->ecc.size;
2739 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10302740 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01002741
Brian Norris8b6e50c2011-05-25 14:59:01 -07002742 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01002743 datafrag_len = num_steps * chip->ecc.size;
2744 eccfrag_len = num_steps * chip->ecc.bytes;
2745
2746 data_col_addr = start_step * chip->ecc.size;
2747 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01002748 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01002749 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002750 if (ret)
2751 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002752
Brian Norris8b6e50c2011-05-25 14:59:01 -07002753 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01002754 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002755 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01002756
Brian Norris8b6e50c2011-05-25 14:59:01 -07002757 /*
2758 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07002759 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07002760 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002761 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
2762 if (ret)
2763 return ret;
2764
2765 if (oobregion.length < eccfrag_len)
2766 gaps = 1;
2767
Alexey Korolev3d459552008-05-15 17:23:18 +01002768 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002769 ret = nand_change_read_column_op(chip, mtd->writesize,
2770 chip->oob_poi, mtd->oobsize,
2771 false);
2772 if (ret)
2773 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002774 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002775 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002776 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07002777 * about buswidth alignment in read_buf.
2778 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002779 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01002780 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01002781 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002782 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01002783 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
2784 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002785 aligned_len++;
2786
Boris Brezillon97d90da2017-11-30 18:01:29 +01002787 ret = nand_change_read_column_op(chip,
2788 mtd->writesize + aligned_pos,
2789 &chip->oob_poi[aligned_pos],
2790 aligned_len, false);
2791 if (ret)
2792 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002793 }
2794
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002795 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01002796 chip->oob_poi, index, eccfrag_len);
2797 if (ret)
2798 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002799
2800 p = bufpoi + data_col_addr;
2801 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
2802 int stat;
2803
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002804 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002805 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002806 if (stat == -EBADMSG &&
2807 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2808 /* check for empty pages with bitflips */
2809 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002810 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002811 chip->ecc.bytes,
2812 NULL, 0,
2813 chip->ecc.strength);
2814 }
2815
Mike Dunn3f91e942012-04-25 12:06:09 -07002816 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002817 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002818 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01002819 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002820 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2821 }
Alexey Korolev3d459552008-05-15 17:23:18 +01002822 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002823 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01002824}
2825
2826/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002827 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002828 * @chip: nand chip info structure
2829 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002830 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002831 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002832 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002833 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002834 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002835static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
2836 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002837{
Boris Brezillonb9761682018-09-06 14:05:20 +02002838 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002839 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002840 int eccbytes = chip->ecc.bytes;
2841 int eccsteps = chip->ecc.steps;
2842 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002843 uint8_t *ecc_calc = chip->ecc.calc_buf;
2844 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002845 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002846
Boris Brezillon25f815f2017-11-30 18:01:30 +01002847 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2848 if (ret)
2849 return ret;
2850
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002851 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02002852 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002853
2854 ret = nand_read_data_op(chip, p, eccsize, false);
2855 if (ret)
2856 return ret;
2857
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002858 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002859 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002860
2861 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2862 if (ret)
2863 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002864
Boris Brezillon846031d2016-02-03 20:11:00 +01002865 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2866 chip->ecc.total);
2867 if (ret)
2868 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002869
2870 eccsteps = chip->ecc.steps;
2871 p = buf;
2872
2873 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2874 int stat;
2875
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002876 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002877 if (stat == -EBADMSG &&
2878 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2879 /* check for empty pages with bitflips */
2880 stat = nand_check_erased_ecc_chunk(p, eccsize,
2881 &ecc_code[i], eccbytes,
2882 NULL, 0,
2883 chip->ecc.strength);
2884 }
2885
Mike Dunn3f91e942012-04-25 12:06:09 -07002886 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002887 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002888 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002889 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002890 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2891 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002892 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002893 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002894}
2895
2896/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002897 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07002898 * @chip: nand chip info structure
2899 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002900 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002901 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002902 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002903 * Hardware ECC for large page chips, require OOB to be read first. For this
2904 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2905 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2906 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2907 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002908 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002909static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
2910 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002911{
Boris Brezillonb9761682018-09-06 14:05:20 +02002912 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002913 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002914 int eccbytes = chip->ecc.bytes;
2915 int eccsteps = chip->ecc.steps;
2916 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002917 uint8_t *ecc_code = chip->ecc.code_buf;
2918 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002919 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002920
2921 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01002922 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2923 if (ret)
2924 return ret;
2925
2926 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2927 if (ret)
2928 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002929
Boris Brezillon846031d2016-02-03 20:11:00 +01002930 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2931 chip->ecc.total);
2932 if (ret)
2933 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002934
2935 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2936 int stat;
2937
Boris Brezillonec476362018-09-06 14:05:17 +02002938 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002939
2940 ret = nand_read_data_op(chip, p, eccsize, false);
2941 if (ret)
2942 return ret;
2943
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002944 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002945
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002946 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002947 if (stat == -EBADMSG &&
2948 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2949 /* check for empty pages with bitflips */
2950 stat = nand_check_erased_ecc_chunk(p, eccsize,
2951 &ecc_code[i], eccbytes,
2952 NULL, 0,
2953 chip->ecc.strength);
2954 }
2955
Mike Dunn3f91e942012-04-25 12:06:09 -07002956 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002957 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002958 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002959 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002960 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2961 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002962 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002963 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002964}
2965
2966/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002967 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07002968 * @chip: nand chip info structure
2969 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002970 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002971 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002972 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002973 * The hw generator calculates the error syndrome automatically. Therefore we
2974 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002975 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002976static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
2977 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002978{
Boris Brezillonb9761682018-09-06 14:05:20 +02002979 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002980 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002981 int eccbytes = chip->ecc.bytes;
2982 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002983 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002984 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002985 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07002986 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002987
Boris Brezillon25f815f2017-11-30 18:01:30 +01002988 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2989 if (ret)
2990 return ret;
2991
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002992 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2993 int stat;
2994
Boris Brezillonec476362018-09-06 14:05:17 +02002995 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002996
2997 ret = nand_read_data_op(chip, p, eccsize, false);
2998 if (ret)
2999 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003000
3001 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003002 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3003 false);
3004 if (ret)
3005 return ret;
3006
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003007 oob += chip->ecc.prepad;
3008 }
3009
Boris Brezillonec476362018-09-06 14:05:17 +02003010 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003011
3012 ret = nand_read_data_op(chip, oob, eccbytes, false);
3013 if (ret)
3014 return ret;
3015
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003016 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003017
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003018 oob += eccbytes;
3019
3020 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003021 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3022 false);
3023 if (ret)
3024 return ret;
3025
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003026 oob += chip->ecc.postpad;
3027 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003028
3029 if (stat == -EBADMSG &&
3030 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3031 /* check for empty pages with bitflips */
3032 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3033 oob - eccpadbytes,
3034 eccpadbytes,
3035 NULL, 0,
3036 chip->ecc.strength);
3037 }
3038
3039 if (stat < 0) {
3040 mtd->ecc_stats.failed++;
3041 } else {
3042 mtd->ecc_stats.corrected += stat;
3043 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3044 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003045 }
3046
3047 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003048 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003049 if (i) {
3050 ret = nand_read_data_op(chip, oob, i, false);
3051 if (ret)
3052 return ret;
3053 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003054
Mike Dunn3f91e942012-04-25 12:06:09 -07003055 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003056}
3057
3058/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003059 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon08136212018-11-11 08:55:03 +01003060 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003061 * @oob: oob destination address
3062 * @ops: oob ops structure
3063 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003064 */
Boris Brezillon08136212018-11-11 08:55:03 +01003065static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003066 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003067{
Boris Brezillon08136212018-11-11 08:55:03 +01003068 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003069 int ret;
3070
Florian Fainellif8ac0412010-09-07 13:23:43 +02003071 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003072
Brian Norris0612b9d2011-08-30 18:45:40 -07003073 case MTD_OPS_PLACE_OOB:
3074 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003075 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3076 return oob + len;
3077
Boris Brezillon846031d2016-02-03 20:11:00 +01003078 case MTD_OPS_AUTO_OOB:
3079 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3080 ops->ooboffs, len);
3081 BUG_ON(ret);
3082 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003083
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003084 default:
3085 BUG();
3086 }
3087 return NULL;
3088}
3089
3090/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003091 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003092 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003093 * @retry_mode: the retry mode to use
3094 *
3095 * Some vendors supply a special command to shift the Vt threshold, to be used
3096 * when there are too many bitflips in a page (i.e., ECC error). After setting
3097 * a new threshold, the host should retry reading the page.
3098 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003099static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003100{
Brian Norrisba84fb52014-01-03 15:13:33 -08003101 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3102
3103 if (retry_mode >= chip->read_retries)
3104 return -EINVAL;
3105
3106 if (!chip->setup_read_retry)
3107 return -EOPNOTSUPP;
3108
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003109 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003110}
3111
Boris Brezillon85e08e52018-07-27 09:44:17 +02003112static void nand_wait_readrdy(struct nand_chip *chip)
3113{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003114 const struct nand_sdr_timings *sdr;
3115
Boris Brezillon85e08e52018-07-27 09:44:17 +02003116 if (!(chip->options & NAND_NEED_READRDY))
3117 return;
3118
Boris Brezillon52f05b62018-07-27 09:44:18 +02003119 sdr = nand_get_sdr_timings(&chip->data_interface);
3120 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003121}
3122
Brian Norrisba84fb52014-01-03 15:13:33 -08003123/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003124 * nand_do_read_ops - [INTERN] Read data with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003125 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003126 * @from: offset to read from
3127 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003128 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003129 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003130 */
Boris Brezillon08136212018-11-11 08:55:03 +01003131static int nand_do_read_ops(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003132 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003133{
Brian Norrise47f3db2012-05-02 10:14:56 -07003134 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris Brezillon08136212018-11-11 08:55:03 +01003135 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003136 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003137 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003138 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003139 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003140
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003141 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003142 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003143 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003144 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003145 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003147 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003148 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003150 realpage = (int)(from >> chip->page_shift);
3151 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003153 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003155 buf = ops->datbuf;
3156 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003157 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003158
Florian Fainellif8ac0412010-09-07 13:23:43 +02003159 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003160 unsigned int ecc_failures = mtd->ecc_stats.failed;
3161
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003162 bytes = min(mtd->writesize - col, readlen);
3163 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003164
Kamal Dasu66507c72014-05-01 20:51:19 -04003165 if (!aligned)
3166 use_bufpoi = 1;
3167 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003168 use_bufpoi = !virt_addr_valid(buf) ||
3169 !IS_ALIGNED((unsigned long)buf,
3170 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003171 else
3172 use_bufpoi = 0;
3173
Brian Norris8b6e50c2011-05-25 14:59:01 -07003174 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003175 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003176 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003177
3178 if (use_bufpoi && aligned)
3179 pr_debug("%s: using read bounce buffer for buf@%p\n",
3180 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Brian Norrisba84fb52014-01-03 15:13:33 -08003182read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003183 /*
3184 * Now read the page into the buffer. Absent an error,
3185 * the read methods return max bitflips per ecc step.
3186 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003187 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003188 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003189 oob_required,
3190 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003191 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3192 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003193 ret = chip->ecc.read_subpage(chip, col, bytes,
3194 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003195 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003196 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003197 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003198 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003199 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003200 /* Invalidate page cache */
3201 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003202 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003203 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003204
3205 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003206 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003207 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003208 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003209 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003210 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003211 chip->pagebuf_bitflips = ret;
3212 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003213 /* Invalidate page cache */
3214 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003215 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003216 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003218
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003219 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003220 int toread = min(oobreadlen, max_oobsize);
3221
3222 if (toread) {
Boris Brezillon08136212018-11-11 08:55:03 +01003223 oob = nand_transfer_oob(chip, oob, ops,
3224 toread);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003225 oobreadlen -= toread;
3226 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003227 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003228
Boris Brezillon85e08e52018-07-27 09:44:17 +02003229 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003230
Brian Norrisba84fb52014-01-03 15:13:33 -08003231 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003232 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003233 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003234 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003235 retry_mode);
3236 if (ret < 0)
3237 break;
3238
3239 /* Reset failures; retry */
3240 mtd->ecc_stats.failed = ecc_failures;
3241 goto read_retry;
3242 } else {
3243 /* No more retry modes; real failure */
3244 ecc_fail = true;
3245 }
3246 }
3247
3248 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003249 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003250 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003251 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003252 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003253 max_bitflips = max_t(unsigned int, max_bitflips,
3254 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003257 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003258
Brian Norrisba84fb52014-01-03 15:13:33 -08003259 /* Reset to retry mode 0 */
3260 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003261 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003262 if (ret < 0)
3263 break;
3264 retry_mode = 0;
3265 }
3266
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003267 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Brian Norris8b6e50c2011-05-25 14:59:01 -07003270 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 col = 0;
3272 /* Increment page address */
3273 realpage++;
3274
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003275 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 /* Check, if we cross a chip boundary */
3277 if (!page) {
3278 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003279 nand_deselect_target(chip);
3280 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003283 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003285 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003286 if (oob)
3287 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Mike Dunn3f91e942012-04-25 12:06:09 -07003289 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003290 return ret;
3291
Brian Norrisb72f3df2013-12-03 11:04:14 -08003292 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003293 return -EBADMSG;
3294
Mike Dunnedbc45402012-04-25 12:06:11 -07003295 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003296}
3297
3298/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003299 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003300 * @chip: nand chip info structure
3301 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003302 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003303int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003304{
Boris Brezillonb9761682018-09-06 14:05:20 +02003305 struct mtd_info *mtd = nand_to_mtd(chip);
3306
Boris Brezillon97d90da2017-11-30 18:01:29 +01003307 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003308}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003309EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003310
3311/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003312 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003313 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003314 * @chip: nand chip info structure
3315 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003316 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003317static int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003318{
Boris Brezillonb9761682018-09-06 14:05:20 +02003319 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003320 int length = mtd->oobsize;
3321 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3322 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003323 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003324 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003325
Boris Brezillon97d90da2017-11-30 18:01:29 +01003326 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3327 if (ret)
3328 return ret;
3329
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003330 for (i = 0; i < chip->ecc.steps; i++) {
3331 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003332 int ret;
3333
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003334 pos = eccsize + i * (eccsize + chunk);
3335 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003336 ret = nand_change_read_column_op(chip, pos,
3337 NULL, 0,
3338 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003339 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003340 ret = nand_read_page_op(chip, page, pos, NULL,
3341 0);
3342
3343 if (ret)
3344 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003345 } else
3346 sndrnd = 1;
3347 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003348
3349 ret = nand_read_data_op(chip, bufpoi, toread, false);
3350 if (ret)
3351 return ret;
3352
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003353 bufpoi += toread;
3354 length -= toread;
3355 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003356 if (length > 0) {
3357 ret = nand_read_data_op(chip, bufpoi, length, false);
3358 if (ret)
3359 return ret;
3360 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003361
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003362 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003363}
3364
3365/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003366 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003367 * @chip: nand chip info structure
3368 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003369 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003370int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003371{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003372 struct mtd_info *mtd = nand_to_mtd(chip);
3373
Boris Brezillon97d90da2017-11-30 18:01:29 +01003374 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3375 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003376}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003377EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003378
3379/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003380 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003381 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003382 * @chip: nand chip info structure
3383 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003384 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003385static int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003386{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003387 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003388 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3389 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003390 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003391 const uint8_t *bufpoi = chip->oob_poi;
3392
3393 /*
3394 * data-ecc-data-ecc ... ecc-oob
3395 * or
3396 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3397 */
3398 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3399 pos = steps * (eccsize + chunk);
3400 steps = 0;
3401 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003402 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003403
Boris Brezillon97d90da2017-11-30 18:01:29 +01003404 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3405 if (ret)
3406 return ret;
3407
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003408 for (i = 0; i < steps; i++) {
3409 if (sndcmd) {
3410 if (mtd->writesize <= 512) {
3411 uint32_t fill = 0xFFFFFFFF;
3412
3413 len = eccsize;
3414 while (len > 0) {
3415 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003416
3417 ret = nand_write_data_op(chip, &fill,
3418 num, false);
3419 if (ret)
3420 return ret;
3421
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003422 len -= num;
3423 }
3424 } else {
3425 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003426 ret = nand_change_write_column_op(chip, pos,
3427 NULL, 0,
3428 false);
3429 if (ret)
3430 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003431 }
3432 } else
3433 sndcmd = 1;
3434 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003435
3436 ret = nand_write_data_op(chip, bufpoi, len, false);
3437 if (ret)
3438 return ret;
3439
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003440 bufpoi += len;
3441 length -= len;
3442 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003443 if (length > 0) {
3444 ret = nand_write_data_op(chip, bufpoi, length, false);
3445 if (ret)
3446 return ret;
3447 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003448
Boris Brezillon97d90da2017-11-30 18:01:29 +01003449 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003450}
3451
3452/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003453 * nand_do_read_oob - [INTERN] NAND read out-of-band
Boris Brezillon08136212018-11-11 08:55:03 +01003454 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003455 * @from: offset to read from
3456 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003458 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 */
Boris Brezillon08136212018-11-11 08:55:03 +01003460static int nand_do_read_oob(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003461 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
Boris Brezillon08136212018-11-11 08:55:03 +01003463 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003464 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003465 int page, realpage, chipnr;
Brian Norris041e4572011-06-23 16:45:24 -07003466 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003467 int readlen = ops->ooblen;
3468 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003469 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003470 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Brian Norris289c0522011-07-19 10:06:09 -07003472 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303473 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
Brian Norris041e4572011-06-23 16:45:24 -07003475 stats = mtd->ecc_stats;
3476
Boris BREZILLON29f10582016-03-07 10:46:52 +01003477 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003478
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003479 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003480 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003482 /* Shift to get page */
3483 realpage = (int)(from >> chip->page_shift);
3484 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Florian Fainellif8ac0412010-09-07 13:23:43 +02003486 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003487 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003488 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003489 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003490 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003491
3492 if (ret < 0)
3493 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003494
3495 len = min(len, readlen);
Boris Brezillon08136212018-11-11 08:55:03 +01003496 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003497
Boris Brezillon85e08e52018-07-27 09:44:17 +02003498 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003499
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003500 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3501
Vitaly Wool70145682006-11-03 18:20:38 +03003502 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003503 if (!readlen)
3504 break;
3505
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003506 /* Increment page address */
3507 realpage++;
3508
3509 page = realpage & chip->pagemask;
3510 /* Check, if we cross a chip boundary */
3511 if (!page) {
3512 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003513 nand_deselect_target(chip);
3514 nand_select_target(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003517 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003519 ops->oobretlen = ops->ooblen - readlen;
3520
3521 if (ret < 0)
3522 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003523
3524 if (mtd->ecc_stats.failed - stats.failed)
3525 return -EBADMSG;
3526
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003527 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
3529
3530/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003531 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003532 * @mtd: MTD device structure
3533 * @from: offset to read from
3534 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003536 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003538static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3539 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540{
Boris Brezillon08136212018-11-11 08:55:03 +01003541 struct nand_chip *chip = mtd_to_nand(mtd);
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003542 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003543
3544 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003546 if (ops->mode != MTD_OPS_PLACE_OOB &&
3547 ops->mode != MTD_OPS_AUTO_OOB &&
3548 ops->mode != MTD_OPS_RAW)
3549 return -ENOTSUPP;
3550
Boris Brezillon013e6292018-11-20 11:57:20 +01003551 ret = nand_get_device(chip);
3552 if (ret)
3553 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003555 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01003556 ret = nand_do_read_oob(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003557 else
Boris Brezillon08136212018-11-11 08:55:03 +01003558 ret = nand_do_read_ops(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003559
Boris Brezillon08136212018-11-11 08:55:03 +01003560 nand_release_device(chip);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003561 return ret;
3562}
3563
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003564/**
3565 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003566 * @chip: nand chip info structure
3567 * @buf: data buffer
3568 * @oob_required: must write chip->oob_poi to OOB
3569 * @page: page number to write
3570 *
3571 * Returns -ENOTSUPP unconditionally.
3572 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003573int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3574 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003575{
3576 return -ENOTSUPP;
3577}
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003578
3579/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003580 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003581 * @chip: nand chip info structure
3582 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003583 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003584 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003585 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003586 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003587 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003588int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3589 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003590{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003591 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003592 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003593
Boris Brezillon25f815f2017-11-30 18:01:30 +01003594 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003595 if (ret)
3596 return ret;
3597
3598 if (oob_required) {
3599 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3600 false);
3601 if (ret)
3602 return ret;
3603 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003604
Boris Brezillon25f815f2017-11-30 18:01:30 +01003605 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003607EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003609/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003610 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003611 * @chip: nand chip info structure
3612 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003613 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003614 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003615 *
3616 * We need a special oob layout and handling even when ECC isn't checked.
3617 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003618static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003619 const uint8_t *buf, int oob_required,
3620 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003621{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003622 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003623 int eccsize = chip->ecc.size;
3624 int eccbytes = chip->ecc.bytes;
3625 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003626 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003627
Boris Brezillon25f815f2017-11-30 18:01:30 +01003628 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3629 if (ret)
3630 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003631
3632 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003633 ret = nand_write_data_op(chip, buf, eccsize, false);
3634 if (ret)
3635 return ret;
3636
David Brownell52ff49d2009-03-04 12:01:36 -08003637 buf += eccsize;
3638
3639 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003640 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3641 false);
3642 if (ret)
3643 return ret;
3644
David Brownell52ff49d2009-03-04 12:01:36 -08003645 oob += chip->ecc.prepad;
3646 }
3647
Boris Brezillon97d90da2017-11-30 18:01:29 +01003648 ret = nand_write_data_op(chip, oob, eccbytes, false);
3649 if (ret)
3650 return ret;
3651
David Brownell52ff49d2009-03-04 12:01:36 -08003652 oob += eccbytes;
3653
3654 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003655 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3656 false);
3657 if (ret)
3658 return ret;
3659
David Brownell52ff49d2009-03-04 12:01:36 -08003660 oob += chip->ecc.postpad;
3661 }
3662 }
3663
3664 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003665 if (size) {
3666 ret = nand_write_data_op(chip, oob, size, false);
3667 if (ret)
3668 return ret;
3669 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003670
Boris Brezillon25f815f2017-11-30 18:01:30 +01003671 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003672}
3673/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003674 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003675 * @chip: nand chip info structure
3676 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003677 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003678 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003679 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003680static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
3681 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003682{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003683 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003684 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003685 int eccbytes = chip->ecc.bytes;
3686 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003687 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003688 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003689
Brian Norris7854d3f2011-06-23 14:12:08 -07003690 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003691 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003692 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003693
Boris Brezillon846031d2016-02-03 20:11:00 +01003694 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3695 chip->ecc.total);
3696 if (ret)
3697 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003698
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003699 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003700}
3701
3702/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003703 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003704 * @chip: nand chip info structure
3705 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003706 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003707 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003708 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003709static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
3710 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003711{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003712 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003713 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003714 int eccbytes = chip->ecc.bytes;
3715 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003716 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003717 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003718
Boris Brezillon25f815f2017-11-30 18:01:30 +01003719 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3720 if (ret)
3721 return ret;
3722
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003723 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003724 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003725
3726 ret = nand_write_data_op(chip, p, eccsize, false);
3727 if (ret)
3728 return ret;
3729
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003730 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003731 }
3732
Boris Brezillon846031d2016-02-03 20:11:00 +01003733 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3734 chip->ecc.total);
3735 if (ret)
3736 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003737
Boris Brezillon97d90da2017-11-30 18:01:29 +01003738 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3739 if (ret)
3740 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003741
Boris Brezillon25f815f2017-11-30 18:01:30 +01003742 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003743}
3744
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303745
3746/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08003747 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303748 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07003749 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303750 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07003751 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303752 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003753 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303754 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003755static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
3756 uint32_t data_len, const uint8_t *buf,
3757 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303758{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003759 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303760 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003761 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303762 int ecc_size = chip->ecc.size;
3763 int ecc_bytes = chip->ecc.bytes;
3764 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303765 uint32_t start_step = offset / ecc_size;
3766 uint32_t end_step = (offset + data_len - 1) / ecc_size;
3767 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01003768 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303769
Boris Brezillon25f815f2017-11-30 18:01:30 +01003770 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3771 if (ret)
3772 return ret;
3773
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303774 for (step = 0; step < ecc_steps; step++) {
3775 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02003776 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303777
3778 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003779 ret = nand_write_data_op(chip, buf, ecc_size, false);
3780 if (ret)
3781 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303782
3783 /* mask ECC of un-touched subpages by padding 0xFF */
3784 if ((step < start_step) || (step > end_step))
3785 memset(ecc_calc, 0xff, ecc_bytes);
3786 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003787 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303788
3789 /* mask OOB of un-touched subpages by padding 0xFF */
3790 /* if oob_required, preserve OOB metadata of written subpage */
3791 if (!oob_required || (step < start_step) || (step > end_step))
3792 memset(oob_buf, 0xff, oob_bytes);
3793
Brian Norrisd6a950802013-08-08 17:16:36 -07003794 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303795 ecc_calc += ecc_bytes;
3796 oob_buf += oob_bytes;
3797 }
3798
3799 /* copy calculated ECC for whole page to chip->buffer->oob */
3800 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003801 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01003802 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3803 chip->ecc.total);
3804 if (ret)
3805 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303806
3807 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003808 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3809 if (ret)
3810 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303811
Boris Brezillon25f815f2017-11-30 18:01:30 +01003812 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303813}
3814
3815
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003816/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003817 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003818 * @chip: nand chip info structure
3819 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003820 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003821 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003822 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003823 * The hw generator calculates the error syndrome automatically. Therefore we
3824 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003825 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003826static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
3827 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003828{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003829 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003830 int i, eccsize = chip->ecc.size;
3831 int eccbytes = chip->ecc.bytes;
3832 int eccsteps = chip->ecc.steps;
3833 const uint8_t *p = buf;
3834 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003835 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003836
Boris Brezillon25f815f2017-11-30 18:01:30 +01003837 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3838 if (ret)
3839 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003840
3841 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003842 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003843
3844 ret = nand_write_data_op(chip, p, eccsize, false);
3845 if (ret)
3846 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003847
3848 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003849 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3850 false);
3851 if (ret)
3852 return ret;
3853
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003854 oob += chip->ecc.prepad;
3855 }
3856
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003857 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003858
3859 ret = nand_write_data_op(chip, oob, eccbytes, false);
3860 if (ret)
3861 return ret;
3862
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003863 oob += eccbytes;
3864
3865 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003866 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3867 false);
3868 if (ret)
3869 return ret;
3870
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003871 oob += chip->ecc.postpad;
3872 }
3873 }
3874
3875 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003876 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003877 if (i) {
3878 ret = nand_write_data_op(chip, oob, i, false);
3879 if (ret)
3880 return ret;
3881 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003882
Boris Brezillon25f815f2017-11-30 18:01:30 +01003883 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003884}
3885
3886/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003887 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07003888 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303889 * @offset: address offset within the page
3890 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07003891 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07003892 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07003893 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003894 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003895 */
Boris Brezillon08136212018-11-11 08:55:03 +01003896static int nand_write_page(struct nand_chip *chip, uint32_t offset,
3897 int data_len, const uint8_t *buf, int oob_required,
3898 int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003899{
Boris Brezillon08136212018-11-11 08:55:03 +01003900 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303901 int status, subpage;
3902
3903 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3904 chip->ecc.write_subpage)
3905 subpage = offset || (data_len < mtd->writesize);
3906 else
3907 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003908
David Woodhouse956e9442006-09-25 17:12:39 +01003909 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003910 status = chip->ecc.write_page_raw(chip, buf, oob_required,
3911 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303912 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003913 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
3914 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003915 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003916 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08003917
3918 if (status < 0)
3919 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003920
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003921 return 0;
3922}
3923
Florian Fainellif8ac0412010-09-07 13:23:43 +02003924#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003925
3926/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003927 * nand_do_write_ops - [INTERN] NAND write with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003928 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003929 * @to: offset to write to
3930 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003931 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003932 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003933 */
Boris Brezillon08136212018-11-11 08:55:03 +01003934static int nand_do_write_ops(struct nand_chip *chip, loff_t to,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003935 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003936{
Boris Brezillon08136212018-11-11 08:55:03 +01003937 struct mtd_info *mtd = nand_to_mtd(chip);
Corentin Labbe73600b62017-09-02 10:49:38 +02003938 int chipnr, realpage, page, column;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003939 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02003940
3941 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003942 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003943
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003944 uint8_t *oob = ops->oobbuf;
3945 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303946 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07003947 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003948
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003949 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003950 if (!writelen)
3951 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003952
Brian Norris8b6e50c2011-05-25 14:59:01 -07003953 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003954 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003955 pr_notice("%s: attempt to write non page aligned data\n",
3956 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003957 return -EINVAL;
3958 }
3959
Thomas Gleixner29072b92006-09-28 15:38:36 +02003960 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003961
Thomas Gleixner6a930962006-06-28 00:11:45 +02003962 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003963 nand_select_target(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02003964
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003965 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01003966 if (nand_check_wp(chip)) {
Huang Shijieb0bb6902012-11-19 14:43:29 +08003967 ret = -EIO;
3968 goto err_out;
3969 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003970
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003971 realpage = (int)(to >> chip->page_shift);
3972 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003973
3974 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07003975 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3976 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003977 chip->pagebuf = -1;
3978
Maxim Levitsky782ce792010-02-22 20:39:36 +02003979 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003980 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3981 ret = -EINVAL;
3982 goto err_out;
3983 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02003984
Florian Fainellif8ac0412010-09-07 13:23:43 +02003985 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003986 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003987 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003988 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02003989 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02003990
Kamal Dasu66507c72014-05-01 20:51:19 -04003991 if (part_pagewr)
3992 use_bufpoi = 1;
3993 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003994 use_bufpoi = !virt_addr_valid(buf) ||
3995 !IS_ALIGNED((unsigned long)buf,
3996 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003997 else
3998 use_bufpoi = 0;
3999
4000 /* Partial page write?, or need to use bounce buffer */
4001 if (use_bufpoi) {
4002 pr_debug("%s: using write bounce buffer for buf@%p\n",
4003 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004004 if (part_pagewr)
4005 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004006 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004007 memset(chip->data_buf, 0xff, mtd->writesize);
4008 memcpy(&chip->data_buf[column], buf, bytes);
4009 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004010 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004011
Maxim Levitsky782ce792010-02-22 20:39:36 +02004012 if (unlikely(oob)) {
4013 size_t len = min(oobwritelen, oobmaxlen);
Boris Brezillon08136212018-11-11 08:55:03 +01004014 oob = nand_fill_oob(chip, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004015 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004016 } else {
4017 /* We still need to erase leftover OOB data */
4018 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004019 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004020
Boris Brezillon08136212018-11-11 08:55:03 +01004021 ret = nand_write_page(chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004022 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004023 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004024 if (ret)
4025 break;
4026
4027 writelen -= bytes;
4028 if (!writelen)
4029 break;
4030
Thomas Gleixner29072b92006-09-28 15:38:36 +02004031 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004032 buf += bytes;
4033 realpage++;
4034
4035 page = realpage & chip->pagemask;
4036 /* Check, if we cross a chip boundary */
4037 if (!page) {
4038 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004039 nand_deselect_target(chip);
4040 nand_select_target(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004041 }
4042 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004043
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004044 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004045 if (unlikely(oob))
4046 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004047
4048err_out:
Boris Brezillon1d017852018-11-11 08:55:14 +01004049 nand_deselect_target(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004050 return ret;
4051}
4052
4053/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004054 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004055 * @mtd: MTD device structure
4056 * @to: offset to write to
4057 * @len: number of bytes to write
4058 * @retlen: pointer to variable to store the number of written bytes
4059 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004060 *
4061 * NAND write with ECC. Used when performing writes in interrupt context, this
4062 * may for example be called by mtdoops when writing an oops while in panic.
4063 */
4064static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4065 size_t *retlen, const uint8_t *buf)
4066{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004067 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004068 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004069 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004070 int ret;
4071
Boris Brezillon1d017852018-11-11 08:55:14 +01004072 nand_select_target(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004073
4074 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004075 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004076
Brian Norris0ec56dc2015-02-28 02:02:30 -08004077 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004078 ops.len = len;
4079 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004080 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004081
Boris Brezillon08136212018-11-11 08:55:03 +01004082 ret = nand_do_write_ops(chip, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004083
Brian Norris4a89ff82011-08-30 18:45:45 -07004084 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004085 return ret;
4086}
4087
4088/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004089 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004090 * @mtd: MTD device structure
4091 * @to: offset to write to
4092 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004093 */
4094static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4095 struct mtd_oob_ops *ops)
4096{
Boris Brezillon08136212018-11-11 08:55:03 +01004097 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004098 int ret = -ENOTSUPP;
4099
4100 ops->retlen = 0;
4101
Boris Brezillon013e6292018-11-20 11:57:20 +01004102 ret = nand_get_device(chip);
4103 if (ret)
4104 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004105
Florian Fainellif8ac0412010-09-07 13:23:43 +02004106 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004107 case MTD_OPS_PLACE_OOB:
4108 case MTD_OPS_AUTO_OOB:
4109 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004110 break;
4111
4112 default:
4113 goto out;
4114 }
4115
4116 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01004117 ret = nand_do_write_oob(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004118 else
Boris Brezillon08136212018-11-11 08:55:03 +01004119 ret = nand_do_write_ops(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004120
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004121out:
Boris Brezillon08136212018-11-11 08:55:03 +01004122 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 return ret;
4124}
4125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004128 * @mtd: MTD device structure
4129 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004131 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004133static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004135 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004139 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004140 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004141 * @instr: erase instruction
4142 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004144 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004146int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004147 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148{
Masahiro Yamada2d73f3d2019-01-21 15:32:07 +09004149 int page, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004150 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Brian Norris289c0522011-07-19 10:06:09 -07004152 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4153 __func__, (unsigned long long)instr->addr,
4154 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Boris Brezillon08136212018-11-11 08:55:03 +01004156 if (check_offs_len(chip, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 /* Grab the lock and see if the device is available */
Boris Brezillon013e6292018-11-20 11:57:20 +01004160 ret = nand_get_device(chip);
4161 if (ret)
4162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
4164 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004165 page = (int)(instr->addr >> chip->page_shift);
4166 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
4168 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004169 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
4171 /* Select the NAND device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004172 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01004175 if (nand_check_wp(chip)) {
Brian Norris289c0522011-07-19 10:06:09 -07004176 pr_debug("%s: device is write protected!\n",
4177 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004178 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 goto erase_exit;
4180 }
4181
4182 /* Loop through the pages */
4183 len = instr->len;
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004186 /* Check if we have a bad block, we do not erase bad blocks! */
Boris Brezillon08136212018-11-11 08:55:03 +01004187 if (nand_block_checkbad(chip, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304188 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004189 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4190 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004191 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 goto erase_exit;
4193 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004194
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004195 /*
4196 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004197 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004198 */
4199 if (page <= chip->pagebuf && chip->pagebuf <
4200 (page + pages_per_block))
4201 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Masahiro Yamada2d73f3d2019-01-21 15:32:07 +09004203 ret = nand_erase_op(chip, (page & chip->pagemask) >>
4204 (chip->phys_erase_shift - chip->page_shift));
4205 if (ret) {
Brian Norris289c0522011-07-19 10:06:09 -07004206 pr_debug("%s: failed erase, page 0x%08x\n",
4207 __func__, page);
Adrian Hunter69423d92008-12-10 13:37:21 +00004208 instr->fail_addr =
4209 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 goto erase_exit;
4211 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004212
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004214 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 page += pages_per_block;
4216
4217 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004218 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004220 nand_deselect_target(chip);
4221 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 }
4223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004225 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004226erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004229 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004230 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 /* Return more or less happy */
4233 return ret;
4234}
4235
4236/**
4237 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004238 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004240 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004242static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
Boris Brezillon08136212018-11-11 08:55:03 +01004244 struct nand_chip *chip = mtd_to_nand(mtd);
4245
Brian Norris289c0522011-07-19 10:06:09 -07004246 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
4248 /* Grab the lock and see if the device is available */
Boris Brezillon013e6292018-11-20 11:57:20 +01004249 WARN_ON(nand_get_device(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 /* Release it and go back */
Boris Brezillon08136212018-11-11 08:55:03 +01004251 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004255 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004256 * @mtd: MTD device structure
4257 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004259static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304261 struct nand_chip *chip = mtd_to_nand(mtd);
4262 int chipnr = (int)(offs >> chip->chip_shift);
4263 int ret;
4264
4265 /* Select the NAND device */
Boris Brezillon013e6292018-11-20 11:57:20 +01004266 ret = nand_get_device(chip);
4267 if (ret)
4268 return ret;
4269
Boris Brezillon1d017852018-11-11 08:55:14 +01004270 nand_select_target(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304271
Boris Brezillon08136212018-11-11 08:55:03 +01004272 ret = nand_block_checkbad(chip, offs, 0);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304273
Boris Brezillon1d017852018-11-11 08:55:14 +01004274 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004275 nand_release_device(chip);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304276
4277 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278}
4279
4280/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004281 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004282 * @mtd: MTD device structure
4283 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004285static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 int ret;
4288
Florian Fainellif8ac0412010-09-07 13:23:43 +02004289 ret = nand_block_isbad(mtd, ofs);
4290 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004291 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 if (ret > 0)
4293 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004294 return ret;
4295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Boris Brezillon08136212018-11-11 08:55:03 +01004297 return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298}
4299
4300/**
Zach Brown56718422017-01-10 13:30:20 -06004301 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4302 * @mtd: MTD device structure
4303 * @ofs: offset relative to mtd start
4304 * @len: length of mtd
4305 */
4306static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4307{
4308 struct nand_chip *chip = mtd_to_nand(mtd);
4309 u32 part_start_block;
4310 u32 part_end_block;
4311 u32 part_start_die;
4312 u32 part_end_die;
4313
4314 /*
4315 * max_bb_per_die and blocks_per_die used to determine
4316 * the maximum bad block count.
4317 */
4318 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4319 return -ENOTSUPP;
4320
4321 /* Get the start and end of the partition in erase blocks. */
4322 part_start_block = mtd_div_by_eb(ofs, mtd);
4323 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4324
4325 /* Get the start and end LUNs of the partition. */
4326 part_start_die = part_start_block / chip->blocks_per_die;
4327 part_end_die = part_end_block / chip->blocks_per_die;
4328
4329 /*
4330 * Look up the bad blocks per unit and multiply by the number of units
4331 * that the partition spans.
4332 */
4333 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4334}
4335
4336/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004337 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004338 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004339 */
4340static int nand_suspend(struct mtd_info *mtd)
4341{
Boris Brezillon013e6292018-11-20 11:57:20 +01004342 struct nand_chip *chip = mtd_to_nand(mtd);
4343
4344 mutex_lock(&chip->lock);
4345 chip->suspended = 1;
4346 mutex_unlock(&chip->lock);
4347
4348 return 0;
Vitaly Wool962034f2005-09-15 14:58:53 +01004349}
4350
4351/**
4352 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004353 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004354 */
4355static void nand_resume(struct mtd_info *mtd)
4356{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004357 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004358
Boris Brezillon013e6292018-11-20 11:57:20 +01004359 mutex_lock(&chip->lock);
4360 if (chip->suspended)
4361 chip->suspended = 0;
Vitaly Wool962034f2005-09-15 14:58:53 +01004362 else
Brian Norrisd0370212011-07-19 10:06:08 -07004363 pr_err("%s called for a chip which is not in suspended state\n",
4364 __func__);
Boris Brezillon013e6292018-11-20 11:57:20 +01004365 mutex_unlock(&chip->lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01004366}
4367
Scott Branden72ea4032014-11-20 11:18:05 -08004368/**
4369 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4370 * prevent further operations
4371 * @mtd: MTD device structure
4372 */
4373static void nand_shutdown(struct mtd_info *mtd)
4374{
Boris Brezillon013e6292018-11-20 11:57:20 +01004375 nand_suspend(mtd);
Scott Branden72ea4032014-11-20 11:18:05 -08004376}
4377
Brian Norris8b6e50c2011-05-25 14:59:01 -07004378/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004379static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004380{
Boris Brezillon7b6a9b22018-11-20 10:02:39 +01004381 /* If no controller is provided, use the dummy, legacy one. */
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004382 if (!chip->controller) {
Boris Brezillon7b6a9b22018-11-20 10:02:39 +01004383 chip->controller = &chip->legacy.dummy_controller;
Miquel Raynal7da45132018-07-17 09:08:02 +02004384 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004385 }
4386
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01004387 nand_legacy_set_defaults(chip);
4388
Masahiro Yamada477544c2017-03-30 17:15:05 +09004389 if (!chip->buf_align)
4390 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004391}
4392
Brian Norris8b6e50c2011-05-25 14:59:01 -07004393/* Sanitize ONFI strings so we can safely print them */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004394void sanitize_string(uint8_t *s, size_t len)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004395{
4396 ssize_t i;
4397
Brian Norris8b6e50c2011-05-25 14:59:01 -07004398 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004399 s[len - 1] = 0;
4400
Brian Norris8b6e50c2011-05-25 14:59:01 -07004401 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004402 for (i = 0; i < len - 1; i++) {
4403 if (s[i] < ' ' || s[i] > 127)
4404 s[i] = '?';
4405 }
4406
Brian Norris8b6e50c2011-05-25 14:59:01 -07004407 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004408 strim(s);
4409}
4410
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004411/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07004412 * nand_id_has_period - Check if an ID string has a given wraparound period
4413 * @id_data: the ID string
4414 * @arrlen: the length of the @id_data array
4415 * @period: the period of repitition
4416 *
4417 * Check if an ID string is repeated within a given sequence of bytes at
4418 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08004419 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07004420 * if the repetition has a period of @period; otherwise, returns zero.
4421 */
4422static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4423{
4424 int i, j;
4425 for (i = 0; i < period; i++)
4426 for (j = i + period; j < arrlen; j += period)
4427 if (id_data[i] != id_data[j])
4428 return 0;
4429 return 1;
4430}
4431
4432/*
4433 * nand_id_len - Get the length of an ID string returned by CMD_READID
4434 * @id_data: the ID string
4435 * @arrlen: the length of the @id_data array
4436
4437 * Returns the length of the ID string, according to known wraparound/trailing
4438 * zero patterns. If no pattern exists, returns the length of the array.
4439 */
4440static int nand_id_len(u8 *id_data, int arrlen)
4441{
4442 int last_nonzero, period;
4443
4444 /* Find last non-zero byte */
4445 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4446 if (id_data[last_nonzero])
4447 break;
4448
4449 /* All zeros */
4450 if (last_nonzero < 0)
4451 return 0;
4452
4453 /* Calculate wraparound period */
4454 for (period = 1; period < arrlen; period++)
4455 if (nand_id_has_period(id_data, arrlen, period))
4456 break;
4457
4458 /* There's a repeated pattern */
4459 if (period < arrlen)
4460 return period;
4461
4462 /* There are trailing zeros */
4463 if (last_nonzero < arrlen - 1)
4464 return last_nonzero + 1;
4465
4466 /* No pattern detected */
4467 return arrlen;
4468}
4469
Huang Shijie7db906b2013-09-25 14:58:11 +08004470/* Extract the bits of per cell from the 3rd byte of the extended ID */
4471static int nand_get_bits_per_cell(u8 cellinfo)
4472{
4473 int bits;
4474
4475 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4476 bits >>= NAND_CI_CELLTYPE_SHIFT;
4477 return bits + 1;
4478}
4479
Brian Norrise3b88bd2012-09-24 20:40:52 -07004480/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004481 * Many new NAND share similar device ID codes, which represent the size of the
4482 * chip. The rest of the parameters must be decoded according to generic or
4483 * manufacturer-specific "extended ID" decoding patterns.
4484 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004485void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004486{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004487 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02004488 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004489 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004490 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08004491 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004492 /* The 4th id byte is the important one */
4493 extid = id_data[3];
4494
Boris Brezillon01389b62016-06-08 10:30:18 +02004495 /* Calc pagesize */
4496 mtd->writesize = 1024 << (extid & 0x03);
4497 extid >>= 2;
4498 /* Calc oobsize */
4499 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4500 extid >>= 2;
4501 /* Calc blocksize. Blocksize is multiples of 64KiB */
4502 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4503 extid >>= 2;
4504 /* Get buswidth information */
4505 if (extid & 0x1)
4506 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004507}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004508EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004509
4510/*
Brian Norrisf23a4812012-09-24 20:40:51 -07004511 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4512 * decodes a matching ID table entry and assigns the MTD size parameters for
4513 * the chip.
4514 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004515static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07004516{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004517 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07004518
4519 mtd->erasesize = type->erasesize;
4520 mtd->writesize = type->pagesize;
4521 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07004522
Huang Shijie1c195e92013-09-25 14:58:12 +08004523 /* All legacy ID NAND are small-page, SLC */
4524 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07004525}
4526
4527/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004528 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4529 * heuristic patterns using various detected parameters (e.g., manufacturer,
4530 * page size, cell-type information).
4531 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004532static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07004533{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004534 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004535
4536 /* Set the bad block position */
4537 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4538 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4539 else
4540 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004541}
4542
Huang Shijieec6e87e2013-03-15 11:01:00 +08004543static inline bool is_full_id_nand(struct nand_flash_dev *type)
4544{
4545 return type->id_len;
4546}
4547
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004548static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02004549 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004550{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004551 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02004552 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004553
Huang Shijieec6e87e2013-03-15 11:01:00 +08004554 if (!strncmp(type->id, id_data, type->id_len)) {
4555 mtd->writesize = type->pagesize;
4556 mtd->erasesize = type->erasesize;
4557 mtd->oobsize = type->oobsize;
4558
Huang Shijie7db906b2013-09-25 14:58:11 +08004559 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004560 chip->chipsize = (uint64_t)type->chipsize << 20;
4561 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004562 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4563 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004564 chip->onfi_timing_mode_default =
4565 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004566
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004567 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4568 if (!chip->parameters.model)
4569 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004570
Huang Shijieec6e87e2013-03-15 11:01:00 +08004571 return true;
4572 }
4573 return false;
4574}
4575
Brian Norris7e74c2d2012-09-24 20:40:49 -07004576/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004577 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4578 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4579 * table.
4580 */
4581static void nand_manufacturer_detect(struct nand_chip *chip)
4582{
4583 /*
4584 * Try manufacturer detection if available and use
4585 * nand_decode_ext_id() otherwise.
4586 */
4587 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004588 chip->manufacturer.desc->ops->detect) {
4589 /* The 3rd id byte holds MLC / multichip data */
4590 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004591 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004592 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004593 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004594 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004595}
4596
4597/*
4598 * Manufacturer initialization. This function is called for all NANDs including
4599 * ONFI and JEDEC compliant ones.
4600 * Manufacturer drivers should put all their specific initialization code in
4601 * their ->init() hook.
4602 */
4603static int nand_manufacturer_init(struct nand_chip *chip)
4604{
4605 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4606 !chip->manufacturer.desc->ops->init)
4607 return 0;
4608
4609 return chip->manufacturer.desc->ops->init(chip);
4610}
4611
4612/*
4613 * Manufacturer cleanup. This function is called for all NANDs including
4614 * ONFI and JEDEC compliant ones.
4615 * Manufacturer drivers should put all their specific cleanup code in their
4616 * ->cleanup() hook.
4617 */
4618static void nand_manufacturer_cleanup(struct nand_chip *chip)
4619{
4620 /* Release manufacturer private data */
4621 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4622 chip->manufacturer.desc->ops->cleanup)
4623 chip->manufacturer.desc->ops->cleanup(chip);
4624}
4625
Boris Brezillon348d56a2018-09-07 00:38:48 +02004626static const char *
4627nand_manufacturer_name(const struct nand_manufacturer *manufacturer)
4628{
4629 return manufacturer ? manufacturer->name : "Unknown";
4630}
4631
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004632/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004633 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004634 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004635static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004636{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004637 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004638 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004639 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004640 u8 *id_data = chip->id.data;
4641 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Karl Beldanef89a882008-09-15 14:37:29 +02004643 /*
4644 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004645 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004646 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004647 ret = nand_reset(chip, 0);
4648 if (ret)
4649 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004650
4651 /* Select the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004652 nand_select_target(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004655 ret = nand_readid_op(chip, 0, id_data, 2);
4656 if (ret)
4657 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004660 maf_id = id_data[0];
4661 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Brian Norris8b6e50c2011-05-25 14:59:01 -07004663 /*
4664 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004665 * interface concerns can cause random data which looks like a
4666 * possibly credible NAND flash to appear. If the two results do
4667 * not match, ignore the device completely.
4668 */
4669
Brian Norris4aef9b72012-09-24 20:40:48 -07004670 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004671 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
4672 if (ret)
4673 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01004674
Boris Brezillon7f501f02016-05-24 19:20:05 +02004675 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004676 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004677 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004678 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004679 }
4680
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02004681 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02004682
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004683 /* Try to identify manufacturer */
4684 manufacturer = nand_get_manufacturer(maf_id);
4685 chip->manufacturer.desc = manufacturer;
4686
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004687 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004688 type = nand_flash_ids;
4689
Boris Brezillon29a198a2016-05-24 20:17:48 +02004690 /*
4691 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4692 * override it.
4693 * This is required to make sure initial NAND bus width set by the
4694 * NAND controller driver is coherent with the real NAND bus width
4695 * (extracted by auto-detection code).
4696 */
4697 busw = chip->options & NAND_BUSWIDTH_16;
4698
4699 /*
4700 * The flag is only set (never cleared), reset it to its default value
4701 * before starting auto-detection.
4702 */
4703 chip->options &= ~NAND_BUSWIDTH_16;
4704
Huang Shijieec6e87e2013-03-15 11:01:00 +08004705 for (; type->name != NULL; type++) {
4706 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004707 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004708 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004709 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004710 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004711 }
4712 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004713
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004714 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004715 /* Check if the chip is ONFI compliant */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004716 ret = nand_onfi_detect(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01004717 if (ret < 0)
4718 return ret;
4719 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004720 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004721
4722 /* Check if the chip is JEDEC compliant */
Boris Brezillon8ae3fbf2018-09-07 00:38:51 +02004723 ret = nand_jedec_detect(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01004724 if (ret < 0)
4725 return ret;
4726 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08004727 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004728 }
4729
David Woodhouse5e81e882010-02-26 18:32:56 +00004730 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004731 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004732
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004733 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4734 if (!chip->parameters.model)
4735 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004736
Adrian Hunter69423d92008-12-10 13:37:21 +00004737 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004738
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004739 if (!type->pagesize)
4740 nand_manufacturer_detect(chip);
4741 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004742 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004743
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004744 /* Get chip options */
4745 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004746
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004747ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01004748 if (!mtd->name)
4749 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004750
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004751 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004752 WARN_ON(busw & NAND_BUSWIDTH_16);
4753 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004754 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4755 /*
4756 * Check, if buswidth is correct. Hardware drivers should set
4757 * chip correct!
4758 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004759 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004760 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004761 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4762 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004763 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4764 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004765 ret = -EINVAL;
4766
4767 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004768 }
4769
Boris Brezillon7f501f02016-05-24 19:20:05 +02004770 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004771
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004772 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004773 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004774 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004775 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004776
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004777 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004778 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004779 if (chip->chipsize & 0xffffffff)
4780 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004781 else {
4782 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4783 chip->chip_shift += 32 - 1;
4784 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004785
Masahiro Yamada14157f82017-09-13 11:05:50 +09004786 if (chip->chip_shift - chip->page_shift > 16)
4787 chip->options |= NAND_ROW_ADDR_3;
4788
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004789 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004790
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004791 nand_legacy_adjust_cmdfunc(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004792
Ezequiel Garcia20171642013-11-25 08:30:31 -03004793 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004794 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01004795 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4796 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02004797 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004798 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004799 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004800 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004801
4802free_detect_allocation:
4803 kfree(chip->parameters.model);
4804
4805 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004806}
4807
Boris Brezillond48f62b2016-04-01 14:54:32 +02004808static const char * const nand_ecc_modes[] = {
4809 [NAND_ECC_NONE] = "none",
4810 [NAND_ECC_SOFT] = "soft",
4811 [NAND_ECC_HW] = "hw",
4812 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4813 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004814 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004815};
4816
4817static int of_get_nand_ecc_mode(struct device_node *np)
4818{
4819 const char *pm;
4820 int err, i;
4821
4822 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4823 if (err < 0)
4824 return err;
4825
4826 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4827 if (!strcasecmp(pm, nand_ecc_modes[i]))
4828 return i;
4829
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004830 /*
4831 * For backward compatibility we support few obsoleted values that don't
4832 * have their mappings into nand_ecc_modes_t anymore (they were merged
4833 * with other enums).
4834 */
4835 if (!strcasecmp(pm, "soft_bch"))
4836 return NAND_ECC_SOFT;
4837
Boris Brezillond48f62b2016-04-01 14:54:32 +02004838 return -ENODEV;
4839}
4840
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004841static const char * const nand_ecc_algos[] = {
4842 [NAND_ECC_HAMMING] = "hamming",
4843 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02004844 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004845};
4846
Boris Brezillond48f62b2016-04-01 14:54:32 +02004847static int of_get_nand_ecc_algo(struct device_node *np)
4848{
4849 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004850 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004851
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004852 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4853 if (!err) {
4854 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4855 if (!strcasecmp(pm, nand_ecc_algos[i]))
4856 return i;
4857 return -ENODEV;
4858 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004859
4860 /*
4861 * For backward compatibility we also read "nand-ecc-mode" checking
4862 * for some obsoleted values that were specifying ECC algorithm.
4863 */
4864 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4865 if (err < 0)
4866 return err;
4867
4868 if (!strcasecmp(pm, "soft"))
4869 return NAND_ECC_HAMMING;
4870 else if (!strcasecmp(pm, "soft_bch"))
4871 return NAND_ECC_BCH;
4872
4873 return -ENODEV;
4874}
4875
4876static int of_get_nand_ecc_step_size(struct device_node *np)
4877{
4878 int ret;
4879 u32 val;
4880
4881 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4882 return ret ? ret : val;
4883}
4884
4885static int of_get_nand_ecc_strength(struct device_node *np)
4886{
4887 int ret;
4888 u32 val;
4889
4890 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4891 return ret ? ret : val;
4892}
4893
4894static int of_get_nand_bus_width(struct device_node *np)
4895{
4896 u32 val;
4897
4898 if (of_property_read_u32(np, "nand-bus-width", &val))
4899 return 8;
4900
4901 switch (val) {
4902 case 8:
4903 case 16:
4904 return val;
4905 default:
4906 return -EIO;
4907 }
4908}
4909
4910static bool of_get_nand_on_flash_bbt(struct device_node *np)
4911{
4912 return of_property_read_bool(np, "nand-on-flash-bbt");
4913}
4914
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004915static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004916{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004917 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004918 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004919
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004920 if (!dn)
4921 return 0;
4922
Brian Norris5844fee2015-01-23 00:22:27 -08004923 if (of_get_nand_bus_width(dn) == 16)
4924 chip->options |= NAND_BUSWIDTH_16;
4925
Stefan Agnerf922bd72018-06-24 23:27:23 +02004926 if (of_property_read_bool(dn, "nand-is-boot-medium"))
4927 chip->options |= NAND_IS_BOOT_MEDIUM;
4928
Brian Norris5844fee2015-01-23 00:22:27 -08004929 if (of_get_nand_on_flash_bbt(dn))
4930 chip->bbt_options |= NAND_BBT_USE_FLASH;
4931
4932 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004933 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004934 ecc_strength = of_get_nand_ecc_strength(dn);
4935 ecc_step = of_get_nand_ecc_step_size(dn);
4936
Brian Norris5844fee2015-01-23 00:22:27 -08004937 if (ecc_mode >= 0)
4938 chip->ecc.mode = ecc_mode;
4939
Rafał Miłecki79082452016-03-23 11:19:02 +01004940 if (ecc_algo >= 0)
4941 chip->ecc.algo = ecc_algo;
4942
Brian Norris5844fee2015-01-23 00:22:27 -08004943 if (ecc_strength >= 0)
4944 chip->ecc.strength = ecc_strength;
4945
4946 if (ecc_step > 0)
4947 chip->ecc.size = ecc_step;
4948
Boris Brezillonba78ee02016-06-08 17:04:22 +02004949 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4950 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4951
Brian Norris5844fee2015-01-23 00:22:27 -08004952 return 0;
4953}
4954
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004955/**
Miquel Raynal98732da2018-07-25 15:31:50 +02004956 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02004957 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004958 * @maxchips: number of chips to scan for
4959 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004960 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004961 * This is the first phase of the normal nand_scan() function. It reads the
4962 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004963 *
Miquel Raynal98732da2018-07-25 15:31:50 +02004964 * This helper used to be called directly from controller drivers that needed
4965 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
4966 * prevented dynamic allocations during this phase which was unconvenient and
4967 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004968 */
Boris Brezillon871a4072018-08-04 22:59:22 +02004969static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02004970 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004971{
Boris Brezillon00ad3782018-09-06 14:05:14 +02004972 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02004973 int nand_maf_id, nand_dev_id;
4974 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08004975 int ret;
4976
Boris Brezillonae2294b2018-11-11 08:55:15 +01004977 /* Assume all dies are deselected when we enter nand_scan_ident(). */
4978 chip->cur_cs = -1;
4979
Boris Brezillon013e6292018-11-20 11:57:20 +01004980 mutex_init(&chip->lock);
4981
Miquel Raynal17fa8042017-11-30 18:01:31 +01004982 /* Enforce the right timings for reset/detection */
4983 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
4984
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004985 ret = nand_dt_init(chip);
4986 if (ret)
4987 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004988
Brian Norrisf7a8e382016-01-05 10:39:45 -08004989 if (!mtd->name && mtd->dev.parent)
4990 mtd->name = dev_name(mtd->dev.parent);
4991
Boris Brezillon1d017852018-11-11 08:55:14 +01004992 /*
4993 * Start with chips->numchips = maxchips to let nand_select_target() do
4994 * its job. chip->numchips will be adjusted after.
4995 */
4996 chip->numchips = maxchips;
4997
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004998 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004999 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005000
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01005001 ret = nand_legacy_check_hooks(chip);
5002 if (ret)
5003 return ret;
5004
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005005 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005006 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005007 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005008 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005009 pr_warn("No NAND device found\n");
Boris Brezillon1d017852018-11-11 08:55:14 +01005010 nand_deselect_target(chip);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005011 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 }
5013
Boris Brezillon7f501f02016-05-24 19:20:05 +02005014 nand_maf_id = chip->id.data[0];
5015 nand_dev_id = chip->id.data[1];
5016
Boris Brezillon1d017852018-11-11 08:55:14 +01005017 nand_deselect_target(chip);
Huang Shijie07300162012-11-09 16:23:45 +08005018
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005019 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005020 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005021 u8 id[2];
5022
Karl Beldanef89a882008-09-15 14:37:29 +02005023 /* See comment in nand_get_flash_type for reset */
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +09005024 ret = nand_reset(chip, i);
5025 if (ret)
5026 break;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005027
Boris Brezillon1d017852018-11-11 08:55:14 +01005028 nand_select_target(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 /* Send the command for reading device ID */
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +09005030 ret = nand_readid_op(chip, 0, id, sizeof(id));
5031 if (ret)
5032 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005034 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon1d017852018-11-11 08:55:14 +01005035 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 break;
Huang Shijie07300162012-11-09 16:23:45 +08005037 }
Boris Brezillon1d017852018-11-11 08:55:14 +01005038 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 }
5040 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005041 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005042
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005044 chip->numchips = i;
5045 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
David Woodhouse3b85c322006-09-25 17:06:53 +01005047 return 0;
5048}
5049
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005050static void nand_scan_ident_cleanup(struct nand_chip *chip)
5051{
5052 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005053 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005054}
5055
Boris Brezillon08136212018-11-11 08:55:03 +01005056static int nand_set_ecc_soft_ops(struct nand_chip *chip)
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005057{
Boris Brezillon08136212018-11-11 08:55:03 +01005058 struct mtd_info *mtd = nand_to_mtd(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005059 struct nand_ecc_ctrl *ecc = &chip->ecc;
5060
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005061 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005062 return -EINVAL;
5063
5064 switch (ecc->algo) {
5065 case NAND_ECC_HAMMING:
5066 ecc->calculate = nand_calculate_ecc;
5067 ecc->correct = nand_correct_data;
5068 ecc->read_page = nand_read_page_swecc;
5069 ecc->read_subpage = nand_read_subpage;
5070 ecc->write_page = nand_write_page_swecc;
5071 ecc->read_page_raw = nand_read_page_raw;
5072 ecc->write_page_raw = nand_write_page_raw;
5073 ecc->read_oob = nand_read_oob_std;
5074 ecc->write_oob = nand_write_oob_std;
5075 if (!ecc->size)
5076 ecc->size = 256;
5077 ecc->bytes = 3;
5078 ecc->strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +02005079
5080 if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC))
5081 ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
5082
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005083 return 0;
5084 case NAND_ECC_BCH:
5085 if (!mtd_nand_has_bch()) {
5086 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5087 return -EINVAL;
5088 }
5089 ecc->calculate = nand_bch_calculate_ecc;
5090 ecc->correct = nand_bch_correct_data;
5091 ecc->read_page = nand_read_page_swecc;
5092 ecc->read_subpage = nand_read_subpage;
5093 ecc->write_page = nand_write_page_swecc;
5094 ecc->read_page_raw = nand_read_page_raw;
5095 ecc->write_page_raw = nand_write_page_raw;
5096 ecc->read_oob = nand_read_oob_std;
5097 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005098
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005099 /*
5100 * Board driver should supply ecc.size and ecc.strength
5101 * values to select how many bits are correctable.
5102 * Otherwise, default to 4 bits for large page devices.
5103 */
5104 if (!ecc->size && (mtd->oobsize >= 64)) {
5105 ecc->size = 512;
5106 ecc->strength = 4;
5107 }
5108
5109 /*
5110 * if no ecc placement scheme was provided pickup the default
5111 * large page one.
5112 */
5113 if (!mtd->ooblayout) {
5114 /* handle large page devices only */
5115 if (mtd->oobsize < 64) {
5116 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5117 return -EINVAL;
5118 }
5119
5120 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005121
5122 }
5123
5124 /*
5125 * We can only maximize ECC config when the default layout is
5126 * used, otherwise we don't know how many bytes can really be
5127 * used.
5128 */
5129 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5130 ecc->options & NAND_ECC_MAXIMIZE) {
5131 int steps, bytes;
5132
5133 /* Always prefer 1k blocks over 512bytes ones */
5134 ecc->size = 1024;
5135 steps = mtd->writesize / ecc->size;
5136
5137 /* Reserve 2 bytes for the BBM */
5138 bytes = (mtd->oobsize - 2) / steps;
5139 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005140 }
5141
5142 /* See nand_bch_init() for details. */
5143 ecc->bytes = 0;
5144 ecc->priv = nand_bch_init(mtd);
5145 if (!ecc->priv) {
5146 WARN(1, "BCH ECC initialization failed!\n");
5147 return -EINVAL;
5148 }
5149 return 0;
5150 default:
5151 WARN(1, "Unsupported ECC algorithm!\n");
5152 return -EINVAL;
5153 }
5154}
5155
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005156/**
5157 * nand_check_ecc_caps - check the sanity of preset ECC settings
5158 * @chip: nand chip info structure
5159 * @caps: ECC caps info structure
5160 * @oobavail: OOB size that the ECC engine can use
5161 *
5162 * When ECC step size and strength are already set, check if they are supported
5163 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5164 * On success, the calculated ECC bytes is set.
5165 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305166static int
5167nand_check_ecc_caps(struct nand_chip *chip,
5168 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005169{
5170 struct mtd_info *mtd = nand_to_mtd(chip);
5171 const struct nand_ecc_step_info *stepinfo;
5172 int preset_step = chip->ecc.size;
5173 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305174 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005175 int i, j;
5176
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005177 for (i = 0; i < caps->nstepinfos; i++) {
5178 stepinfo = &caps->stepinfos[i];
5179
5180 if (stepinfo->stepsize != preset_step)
5181 continue;
5182
5183 for (j = 0; j < stepinfo->nstrengths; j++) {
5184 if (stepinfo->strengths[j] != preset_strength)
5185 continue;
5186
5187 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5188 preset_strength);
5189 if (WARN_ON_ONCE(ecc_bytes < 0))
5190 return ecc_bytes;
5191
5192 if (ecc_bytes * nsteps > oobavail) {
5193 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5194 preset_step, preset_strength);
5195 return -ENOSPC;
5196 }
5197
5198 chip->ecc.bytes = ecc_bytes;
5199
5200 return 0;
5201 }
5202 }
5203
5204 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5205 preset_step, preset_strength);
5206
5207 return -ENOTSUPP;
5208}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005209
5210/**
5211 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5212 * @chip: nand chip info structure
5213 * @caps: ECC engine caps info structure
5214 * @oobavail: OOB size that the ECC engine can use
5215 *
5216 * If a chip's ECC requirement is provided, try to meet it with the least
5217 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5218 * On success, the chosen ECC settings are set.
5219 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305220static int
5221nand_match_ecc_req(struct nand_chip *chip,
5222 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005223{
5224 struct mtd_info *mtd = nand_to_mtd(chip);
5225 const struct nand_ecc_step_info *stepinfo;
5226 int req_step = chip->ecc_step_ds;
5227 int req_strength = chip->ecc_strength_ds;
5228 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5229 int best_step, best_strength, best_ecc_bytes;
5230 int best_ecc_bytes_total = INT_MAX;
5231 int i, j;
5232
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005233 /* No information provided by the NAND chip */
5234 if (!req_step || !req_strength)
5235 return -ENOTSUPP;
5236
5237 /* number of correctable bits the chip requires in a page */
5238 req_corr = mtd->writesize / req_step * req_strength;
5239
5240 for (i = 0; i < caps->nstepinfos; i++) {
5241 stepinfo = &caps->stepinfos[i];
5242 step_size = stepinfo->stepsize;
5243
5244 for (j = 0; j < stepinfo->nstrengths; j++) {
5245 strength = stepinfo->strengths[j];
5246
5247 /*
5248 * If both step size and strength are smaller than the
5249 * chip's requirement, it is not easy to compare the
5250 * resulted reliability.
5251 */
5252 if (step_size < req_step && strength < req_strength)
5253 continue;
5254
5255 if (mtd->writesize % step_size)
5256 continue;
5257
5258 nsteps = mtd->writesize / step_size;
5259
5260 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5261 if (WARN_ON_ONCE(ecc_bytes < 0))
5262 continue;
5263 ecc_bytes_total = ecc_bytes * nsteps;
5264
5265 if (ecc_bytes_total > oobavail ||
5266 strength * nsteps < req_corr)
5267 continue;
5268
5269 /*
5270 * We assume the best is to meet the chip's requrement
5271 * with the least number of ECC bytes.
5272 */
5273 if (ecc_bytes_total < best_ecc_bytes_total) {
5274 best_ecc_bytes_total = ecc_bytes_total;
5275 best_step = step_size;
5276 best_strength = strength;
5277 best_ecc_bytes = ecc_bytes;
5278 }
5279 }
5280 }
5281
5282 if (best_ecc_bytes_total == INT_MAX)
5283 return -ENOTSUPP;
5284
5285 chip->ecc.size = best_step;
5286 chip->ecc.strength = best_strength;
5287 chip->ecc.bytes = best_ecc_bytes;
5288
5289 return 0;
5290}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005291
5292/**
5293 * nand_maximize_ecc - choose the max ECC strength available
5294 * @chip: nand chip info structure
5295 * @caps: ECC engine caps info structure
5296 * @oobavail: OOB size that the ECC engine can use
5297 *
5298 * Choose the max ECC strength that is supported on the controller, and can fit
5299 * within the chip's OOB. On success, the chosen ECC settings are set.
5300 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305301static int
5302nand_maximize_ecc(struct nand_chip *chip,
5303 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005304{
5305 struct mtd_info *mtd = nand_to_mtd(chip);
5306 const struct nand_ecc_step_info *stepinfo;
5307 int step_size, strength, nsteps, ecc_bytes, corr;
5308 int best_corr = 0;
5309 int best_step = 0;
5310 int best_strength, best_ecc_bytes;
5311 int i, j;
5312
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005313 for (i = 0; i < caps->nstepinfos; i++) {
5314 stepinfo = &caps->stepinfos[i];
5315 step_size = stepinfo->stepsize;
5316
5317 /* If chip->ecc.size is already set, respect it */
5318 if (chip->ecc.size && step_size != chip->ecc.size)
5319 continue;
5320
5321 for (j = 0; j < stepinfo->nstrengths; j++) {
5322 strength = stepinfo->strengths[j];
5323
5324 if (mtd->writesize % step_size)
5325 continue;
5326
5327 nsteps = mtd->writesize / step_size;
5328
5329 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5330 if (WARN_ON_ONCE(ecc_bytes < 0))
5331 continue;
5332
5333 if (ecc_bytes * nsteps > oobavail)
5334 continue;
5335
5336 corr = strength * nsteps;
5337
5338 /*
5339 * If the number of correctable bits is the same,
5340 * bigger step_size has more reliability.
5341 */
5342 if (corr > best_corr ||
5343 (corr == best_corr && step_size > best_step)) {
5344 best_corr = corr;
5345 best_step = step_size;
5346 best_strength = strength;
5347 best_ecc_bytes = ecc_bytes;
5348 }
5349 }
5350 }
5351
5352 if (!best_corr)
5353 return -ENOTSUPP;
5354
5355 chip->ecc.size = best_step;
5356 chip->ecc.strength = best_strength;
5357 chip->ecc.bytes = best_ecc_bytes;
5358
5359 return 0;
5360}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005361
Abhishek Sahu181ace92018-06-20 12:57:28 +05305362/**
5363 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
5364 * @chip: nand chip info structure
5365 * @caps: ECC engine caps info structure
5366 * @oobavail: OOB size that the ECC engine can use
5367 *
5368 * Choose the ECC configuration according to following logic
5369 *
5370 * 1. If both ECC step size and ECC strength are already set (usually by DT)
5371 * then check if it is supported by this controller.
5372 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
5373 * 3. Otherwise, try to match the ECC step size and ECC strength closest
5374 * to the chip's requirement. If available OOB size can't fit the chip
5375 * requirement then fallback to the maximum ECC step size and ECC strength.
5376 *
5377 * On success, the chosen ECC settings are set.
5378 */
5379int nand_ecc_choose_conf(struct nand_chip *chip,
5380 const struct nand_ecc_caps *caps, int oobavail)
5381{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305382 struct mtd_info *mtd = nand_to_mtd(chip);
5383
5384 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
5385 return -EINVAL;
5386
Abhishek Sahu181ace92018-06-20 12:57:28 +05305387 if (chip->ecc.size && chip->ecc.strength)
5388 return nand_check_ecc_caps(chip, caps, oobavail);
5389
5390 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
5391 return nand_maximize_ecc(chip, caps, oobavail);
5392
5393 if (!nand_match_ecc_req(chip, caps, oobavail))
5394 return 0;
5395
5396 return nand_maximize_ecc(chip, caps, oobavail);
5397}
5398EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
5399
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005400/*
5401 * Check if the chip configuration meet the datasheet requirements.
5402
5403 * If our configuration corrects A bits per B bytes and the minimum
5404 * required correction level is X bits per Y bytes, then we must ensure
5405 * both of the following are true:
5406 *
5407 * (1) A / B >= X / Y
5408 * (2) A >= X
5409 *
5410 * Requirement (1) ensures we can correct for the required bitflip density.
5411 * Requirement (2) ensures we can correct even when all bitflips are clumped
5412 * in the same sector.
5413 */
Boris Brezillon08136212018-11-11 08:55:03 +01005414static bool nand_ecc_strength_good(struct nand_chip *chip)
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005415{
Boris Brezillon08136212018-11-11 08:55:03 +01005416 struct mtd_info *mtd = nand_to_mtd(chip);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005417 struct nand_ecc_ctrl *ecc = &chip->ecc;
5418 int corr, ds_corr;
5419
5420 if (ecc->size == 0 || chip->ecc_step_ds == 0)
5421 /* Not enough information */
5422 return true;
5423
5424 /*
5425 * We get the number of corrected bits per page to compare
5426 * the correction density.
5427 */
5428 corr = (mtd->writesize * ecc->strength) / ecc->size;
5429 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
5430
5431 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
5432}
David Woodhouse3b85c322006-09-25 17:06:53 +01005433
5434/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005435 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005436 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01005437 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005438 * This is the second phase of the normal nand_scan() function. It fills out
5439 * all the uninitialized function pointers with the defaults and scans for a
5440 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01005441 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005442static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01005443{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005444 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08005445 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005446 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01005447
Brian Norrise2414f42012-02-06 13:44:00 -08005448 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005449 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07005450 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005451 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07005452 }
Brian Norrise2414f42012-02-06 13:44:00 -08005453
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005454 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005455 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02005456 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005457
Boris Brezillonf84674b2017-06-02 12:18:24 +02005458 /*
5459 * FIXME: some NAND manufacturer drivers expect the first die to be
5460 * selected when manufacturer->init() is called. They should be fixed
5461 * to explictly select the relevant die when interacting with the NAND
5462 * chip.
5463 */
Boris Brezillon1d017852018-11-11 08:55:14 +01005464 nand_select_target(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005465 ret = nand_manufacturer_init(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01005466 nand_deselect_target(chip);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005467 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005468 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005469
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01005470 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005471 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005472
5473 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005474 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005475 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005476 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005477 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005478 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01005481 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 break;
5483 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005484 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02005485 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02005488 /*
5489 * Expose the whole OOB area to users if ECC_NONE
5490 * is passed. We could do that for all kind of
5491 * ->oobsize, but we must keep the old large/small
5492 * page with ECC layout when ->oobsize <= 128 for
5493 * compatibility reasons.
5494 */
5495 if (ecc->mode == NAND_ECC_NONE) {
5496 mtd_set_ooblayout(mtd,
5497 &nand_ooblayout_lp_ops);
5498 break;
5499 }
5500
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005501 WARN(1, "No oob scheme defined for oobsize %d\n",
5502 mtd->oobsize);
5503 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005504 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 }
5506 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005507
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005508 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005509 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005510 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01005511 */
David Woodhouse956e9442006-09-25 17:12:39 +01005512
Huang Shijie97de79e02013-10-18 14:20:53 +08005513 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005514 case NAND_ECC_HW_OOB_FIRST:
5515 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08005516 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005517 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5518 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005519 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005520 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005521 if (!ecc->read_page)
5522 ecc->read_page = nand_read_page_hwecc_oob_first;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005523 /* fall through */
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005524
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005525 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07005526 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005527 if (!ecc->read_page)
5528 ecc->read_page = nand_read_page_hwecc;
5529 if (!ecc->write_page)
5530 ecc->write_page = nand_write_page_hwecc;
5531 if (!ecc->read_page_raw)
5532 ecc->read_page_raw = nand_read_page_raw;
5533 if (!ecc->write_page_raw)
5534 ecc->write_page_raw = nand_write_page_raw;
5535 if (!ecc->read_oob)
5536 ecc->read_oob = nand_read_oob_std;
5537 if (!ecc->write_oob)
5538 ecc->write_oob = nand_write_oob_std;
5539 if (!ecc->read_subpage)
5540 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02005541 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08005542 ecc->write_subpage = nand_write_subpage_hwecc;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005543 /* fall through */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005544
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005545 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08005546 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5547 (!ecc->read_page ||
5548 ecc->read_page == nand_read_page_hwecc ||
5549 !ecc->write_page ||
5550 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005551 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5552 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005553 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005554 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07005555 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005556 if (!ecc->read_page)
5557 ecc->read_page = nand_read_page_syndrome;
5558 if (!ecc->write_page)
5559 ecc->write_page = nand_write_page_syndrome;
5560 if (!ecc->read_page_raw)
5561 ecc->read_page_raw = nand_read_page_raw_syndrome;
5562 if (!ecc->write_page_raw)
5563 ecc->write_page_raw = nand_write_page_raw_syndrome;
5564 if (!ecc->read_oob)
5565 ecc->read_oob = nand_read_oob_syndrome;
5566 if (!ecc->write_oob)
5567 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005568
Huang Shijie97de79e02013-10-18 14:20:53 +08005569 if (mtd->writesize >= ecc->size) {
5570 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005571 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
5572 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005573 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07005574 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005575 break;
Mike Dunne2788c92012-04-25 12:06:10 -07005576 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005577 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5578 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08005579 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02005580 ecc->algo = NAND_ECC_HAMMING;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005581 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005583 case NAND_ECC_SOFT:
Boris Brezillon08136212018-11-11 08:55:03 +01005584 ret = nand_set_ecc_soft_ops(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005585 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005586 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005587 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01005588 }
5589 break;
5590
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005591 case NAND_ECC_ON_DIE:
5592 if (!ecc->read_page || !ecc->write_page) {
5593 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
5594 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005595 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005596 }
5597 if (!ecc->read_oob)
5598 ecc->read_oob = nand_read_oob_std;
5599 if (!ecc->write_oob)
5600 ecc->write_oob = nand_write_oob_std;
5601 break;
5602
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005603 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005604 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08005605 ecc->read_page = nand_read_page_raw;
5606 ecc->write_page = nand_write_page_raw;
5607 ecc->read_oob = nand_read_oob_std;
5608 ecc->read_page_raw = nand_read_page_raw;
5609 ecc->write_page_raw = nand_write_page_raw;
5610 ecc->write_oob = nand_write_oob_std;
5611 ecc->size = mtd->writesize;
5612 ecc->bytes = 0;
5613 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005615
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005617 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5618 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005619 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005622 if (ecc->correct || ecc->calculate) {
5623 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5624 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5625 if (!ecc->calc_buf || !ecc->code_buf) {
5626 ret = -ENOMEM;
5627 goto err_nand_manuf_cleanup;
5628 }
5629 }
5630
Brian Norris9ce244b2011-08-30 18:45:37 -07005631 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005632 if (!ecc->read_oob_raw)
5633 ecc->read_oob_raw = ecc->read_oob;
5634 if (!ecc->write_oob_raw)
5635 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005636
Boris Brezillon846031d2016-02-03 20:11:00 +01005637 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005638 mtd->ecc_strength = ecc->strength;
5639 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005640
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005641 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005642 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005643 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005644 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005645 ecc->steps = mtd->writesize / ecc->size;
5646 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005647 WARN(1, "Invalid ECC parameters\n");
5648 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005649 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005651 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005652 if (ecc->total > mtd->oobsize) {
5653 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5654 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005655 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005656 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005657
Boris Brezillon846031d2016-02-03 20:11:00 +01005658 /*
5659 * The number of bytes available for a client to place data into
5660 * the out of band area.
5661 */
5662 ret = mtd_ooblayout_count_freebytes(mtd);
5663 if (ret < 0)
5664 ret = 0;
5665
5666 mtd->oobavail = ret;
5667
5668 /* ECC sanity check: warn if it's too weak */
Boris Brezillon08136212018-11-11 08:55:03 +01005669 if (!nand_ecc_strength_good(chip))
Boris Brezillon846031d2016-02-03 20:11:00 +01005670 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5671 mtd->name);
5672
Brian Norris8b6e50c2011-05-25 14:59:01 -07005673 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005674 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005675 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005676 case 2:
5677 mtd->subpage_sft = 1;
5678 break;
5679 case 4:
5680 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005681 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005682 mtd->subpage_sft = 2;
5683 break;
5684 }
5685 }
5686 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005689 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005691 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305692 switch (ecc->mode) {
5693 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305694 if (chip->page_shift > 9)
5695 chip->options |= NAND_SUBPAGE_READ;
5696 break;
5697
5698 default:
5699 break;
5700 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005701
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08005703 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02005704 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5705 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005706 mtd->_erase = nand_erase;
5707 mtd->_point = NULL;
5708 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005709 mtd->_panic_write = panic_nand_write;
5710 mtd->_read_oob = nand_read_oob;
5711 mtd->_write_oob = nand_write_oob;
5712 mtd->_sync = nand_sync;
5713 mtd->_lock = NULL;
5714 mtd->_unlock = NULL;
5715 mtd->_suspend = nand_suspend;
5716 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005717 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005718 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005719 mtd->_block_isbad = nand_block_isbad;
5720 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06005721 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01005722 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005724 /*
5725 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5726 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5727 * properly set.
5728 */
5729 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005730 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
Boris Brezillonf84674b2017-06-02 12:18:24 +02005732 /* Initialize the ->data_interface field. */
5733 ret = nand_init_data_interface(chip);
5734 if (ret)
5735 goto err_nand_manuf_cleanup;
5736
5737 /* Enter fastest possible mode on all dies. */
5738 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005739 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005740 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005741 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005742 }
5743
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005744 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005745 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
5748 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02005749 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07005750 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005751 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005752
Brian Norris44d41822017-05-01 17:04:50 -07005753 return 0;
5754
Boris Brezillonf84674b2017-06-02 12:18:24 +02005755
5756err_nand_manuf_cleanup:
5757 nand_manufacturer_cleanup(chip);
5758
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005759err_free_buf:
5760 kfree(chip->data_buf);
5761 kfree(ecc->code_buf);
5762 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07005763
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005764 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765}
5766
Miquel Raynal05b54c72018-07-19 01:05:46 +02005767static int nand_attach(struct nand_chip *chip)
5768{
5769 if (chip->controller->ops && chip->controller->ops->attach_chip)
5770 return chip->controller->ops->attach_chip(chip);
5771
5772 return 0;
5773}
5774
5775static void nand_detach(struct nand_chip *chip)
5776{
5777 if (chip->controller->ops && chip->controller->ops->detach_chip)
5778 chip->controller->ops->detach_chip(chip);
5779}
5780
David Woodhouse3b85c322006-09-25 17:06:53 +01005781/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005782 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005783 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02005784 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005785 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01005786 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005787 * This fills out all the uninitialized function pointers with the defaults.
5788 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005789 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005790 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005791int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005792 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01005793{
5794 int ret;
5795
Boris Brezillon800342d2018-08-04 22:59:23 +02005796 if (!maxchips)
5797 return -EINVAL;
5798
5799 ret = nand_scan_ident(chip, maxchips, ids);
5800 if (ret)
5801 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005802
5803 ret = nand_attach(chip);
5804 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005805 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005806
Boris Brezillon00ad3782018-09-06 14:05:14 +02005807 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005808 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005809 goto detach_chip;
5810
5811 return 0;
5812
5813detach_chip:
5814 nand_detach(chip);
5815cleanup_ident:
5816 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005817
David Woodhouse3b85c322006-09-25 17:06:53 +01005818 return ret;
5819}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005820EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01005821
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005823 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5824 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005825 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005826void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005828 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005829 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005830 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5831
Jesper Juhlfa671642005-11-07 01:01:27 -08005832 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005833 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005834 kfree(chip->data_buf);
5835 kfree(chip->ecc.code_buf);
5836 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07005837
5838 /* Free bad block descriptor memory */
5839 if (chip->badblock_pattern && chip->badblock_pattern->options
5840 & NAND_BBT_DYNAMICSTRUCT)
5841 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005842
5843 /* Free manufacturer priv data. */
5844 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005845
5846 /* Free controller specific allocations after chip identification */
5847 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005848
5849 /* Free identification phase allocations */
5850 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851}
Miquel Raynal05b54c72018-07-19 01:05:46 +02005852
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005853EXPORT_SYMBOL_GPL(nand_cleanup);
5854
5855/**
5856 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5857 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02005858 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005859 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02005860void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005861{
Boris Brezillon59ac2762018-09-06 14:05:15 +02005862 mtd_device_unregister(nand_to_mtd(chip));
5863 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005864}
David Woodhousee0c7d762006-05-13 18:07:53 +01005865EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005866
David Woodhousee0c7d762006-05-13 18:07:53 +01005867MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005868MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5869MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005870MODULE_DESCRIPTION("Generic NAND flash driver code");