blob: cf207d6e7a81f06f869291caeae8d61b384e9e19 [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);
423 int chipnr, page, status, len;
424
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 */
445 nand_reset(chip, chipnr);
446
Boris Brezillon1d017852018-11-11 08:55:14 +0100447 nand_select_target(chip, chipnr);
Boris Brezillon99f33512018-11-11 08:55:04 +0100448
449 /* Shift to get page */
450 page = (int)(to >> chip->page_shift);
451
452 /* Check, if it is write protected */
453 if (nand_check_wp(chip)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100454 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100455 return -EROFS;
456 }
457
458 /* Invalidate the page cache, if we write to the cached page */
459 if (page == chip->pagebuf)
460 chip->pagebuf = -1;
461
462 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
463
464 if (ops->mode == MTD_OPS_RAW)
465 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
466 else
467 status = chip->ecc.write_oob(chip, page & chip->pagemask);
468
Boris Brezillon1d017852018-11-11 08:55:14 +0100469 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100470
471 if (status)
472 return status;
473
474 ops->oobretlen = ops->ooblen;
475
476 return 0;
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700480 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200481 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700482 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700484 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700485 * specific driver. It provides the details for writing a bad block marker to a
486 * block.
487 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200488static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700489{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200490 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700491 struct mtd_oob_ops ops;
492 uint8_t buf[2] = { 0, 0 };
493 int ret = 0, res, i = 0;
494
Brian Norris0ec56dc2015-02-28 02:02:30 -0800495 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700496 ops.oobbuf = buf;
497 ops.ooboffs = chip->badblockpos;
498 if (chip->options & NAND_BUSWIDTH_16) {
499 ops.ooboffs &= ~0x01;
500 ops.len = ops.ooblen = 2;
501 } else {
502 ops.len = ops.ooblen = 1;
503 }
504 ops.mode = MTD_OPS_PLACE_OOB;
505
506 /* Write to first/last page(s) if necessary */
507 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
508 ofs += mtd->erasesize - mtd->writesize;
509 do {
Boris Brezillon08136212018-11-11 08:55:03 +0100510 res = nand_do_write_oob(chip, ofs, &ops);
Brian Norris5a0edb22013-07-30 17:52:58 -0700511 if (!ret)
512 ret = res;
513
514 i++;
515 ofs += mtd->writesize;
516 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
517
518 return ret;
519}
520
521/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200522 * nand_markbad_bbm - mark a block by updating the BBM
523 * @chip: NAND chip object
524 * @ofs: offset of the block to mark bad
525 */
526int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
527{
528 if (chip->legacy.block_markbad)
529 return chip->legacy.block_markbad(chip, ofs);
530
531 return nand_default_block_markbad(chip, ofs);
532}
533
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200534/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700535 * nand_block_markbad_lowlevel - mark a block bad
Boris Brezillon08136212018-11-11 08:55:03 +0100536 * @chip: NAND chip object
Brian Norris5a0edb22013-07-30 17:52:58 -0700537 * @ofs: offset from device start
538 *
539 * This function performs the generic NAND bad block marking steps (i.e., bad
540 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200541 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700542 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700543 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300544 *
Brian Norrise2414f42012-02-06 13:44:00 -0800545 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700546 * (2) write bad block marker to OOB area of affected block (unless flag
547 * NAND_BBT_NO_OOB_BBM is present)
548 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300549 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700550 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800551 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552*/
Boris Brezillon08136212018-11-11 08:55:03 +0100553static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Boris Brezillon08136212018-11-11 08:55:03 +0100555 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisb32843b2013-07-30 17:52:59 -0700556 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000557
Brian Norrisb32843b2013-07-30 17:52:59 -0700558 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800559 struct erase_info einfo;
560
561 /* Attempt erase before marking OOB */
562 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800563 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300564 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200565 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800566
Brian Norrisb32843b2013-07-30 17:52:59 -0700567 /* Write bad block marker to OOB */
Boris Brezillon013e6292018-11-20 11:57:20 +0100568 ret = nand_get_device(chip);
569 if (ret)
570 return ret;
571
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200572 ret = nand_markbad_bbm(chip, ofs);
Boris Brezillon08136212018-11-11 08:55:03 +0100573 nand_release_device(chip);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200574 }
Brian Norrise2414f42012-02-06 13:44:00 -0800575
Brian Norrisb32843b2013-07-30 17:52:59 -0700576 /* Mark block bad in BBT */
577 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200578 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800579 if (!ret)
580 ret = res;
581 }
582
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200583 if (!ret)
584 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300585
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200586 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000589/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800590 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700591 * @mtd: MTD device structure
592 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300593 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800594 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300595 */
596static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
597{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100598 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300599
600 if (!chip->bbt)
601 return 0;
602 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200603 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300604}
605
606/**
607 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Boris Brezillon08136212018-11-11 08:55:03 +0100608 * @chip: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300609 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700610 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 *
612 * Check, if the block is bad. Either by reading the bad block table or
613 * calling of the scan function.
614 */
Boris Brezillon08136212018-11-11 08:55:03 +0100615static int nand_block_checkbad(struct nand_chip *chip, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200618 if (chip->bbt)
619 return nand_isbad_bbt(chip, ofs, allowbbt);
620
621 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200624/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100625 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
626 * @chip: NAND chip structure
627 * @timeout_ms: Timeout in ms
628 *
629 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
630 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
631 * returned.
632 *
633 * This helper is intended to be used when the controller does not have access
634 * to the NAND R/B pin.
635 *
636 * Be aware that calling this helper from an ->exec_op() implementation means
637 * ->exec_op() must be re-entrant.
638 *
639 * Return 0 if the NAND chip is ready, a negative error otherwise.
640 */
641int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
642{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200643 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100644 u8 status = 0;
645 int ret;
646
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100647 if (!nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +0100648 return -ENOTSUPP;
649
Boris Brezillon3057fce2018-05-04 21:24:31 +0200650 /* Wait tWB before polling the STATUS reg. */
651 timings = nand_get_sdr_timings(&chip->data_interface);
652 ndelay(PSEC_TO_NSEC(timings->tWB_max));
653
Miquel Raynal8878b122017-11-09 14:16:45 +0100654 ret = nand_status_op(chip, NULL);
655 if (ret)
656 return ret;
657
658 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
659 do {
660 ret = nand_read_data_op(chip, &status, sizeof(status), true);
661 if (ret)
662 break;
663
664 if (status & NAND_STATUS_READY)
665 break;
666
667 /*
668 * Typical lowest execution time for a tR on most NANDs is 10us,
669 * use this as polling delay before doing something smarter (ie.
670 * deriving a delay from the timeout value, timeout_ms/ratio).
671 */
672 udelay(10);
673 } while (time_before(jiffies, timeout_ms));
674
675 /*
676 * We have to exit READ_STATUS mode in order to read real data on the
677 * bus in case the WAITRDY instruction is preceding a DATA_IN
678 * instruction.
679 */
680 nand_exit_status_op(chip);
681
682 if (ret)
683 return ret;
684
685 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
686};
687EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
688
689/**
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +0200690 * nand_gpio_waitrdy - Poll R/B GPIO pin until ready
691 * @chip: NAND chip structure
692 * @gpiod: GPIO descriptor of R/B pin
693 * @timeout_ms: Timeout in ms
694 *
695 * Poll the R/B GPIO pin until it becomes ready. If that does not happen
696 * whitin the specified timeout, -ETIMEDOUT is returned.
697 *
698 * This helper is intended to be used when the controller has access to the
699 * NAND R/B pin over GPIO.
700 *
701 * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise.
702 */
703int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
704 unsigned long timeout_ms)
705{
706 /* Wait until R/B pin indicates chip is ready or timeout occurs */
707 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
708 do {
709 if (gpiod_get_value_cansleep(gpiod))
710 return 0;
711
712 cond_resched();
713 } while (time_before(jiffies, timeout_ms));
714
715 return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT;
716};
717EXPORT_SYMBOL_GPL(nand_gpio_waitrdy);
718
719/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700720 * panic_nand_wait - [GENERIC] wait until the command is done
Brian Norris8b6e50c2011-05-25 14:59:01 -0700721 * @chip: NAND chip structure
722 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200723 *
724 * Wait for command done. This is a helper function for nand_wait used when
725 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400726 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200727 */
Boris Brezillon3d4af7c2018-09-07 00:38:49 +0200728void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200729{
730 int i;
731 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200732 if (chip->legacy.dev_ready) {
733 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200734 break;
735 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100736 int ret;
737 u8 status;
738
739 ret = nand_read_data_op(chip, &status, sizeof(status),
740 true);
741 if (ret)
742 return;
743
744 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200745 break;
746 }
747 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200748 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200749}
750
Miquel Raynal789157e2018-03-19 14:47:28 +0100751static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +0100752{
Miquel Raynal789157e2018-03-19 14:47:28 +0100753 return (chip->parameters.supports_set_get_features &&
754 test_bit(addr, chip->parameters.get_feature_list));
755}
756
757static bool nand_supports_set_features(struct nand_chip *chip, int addr)
758{
759 return (chip->parameters.supports_set_get_features &&
760 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +0100761}
762
763/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200764 * nand_reset_data_interface - Reset data interface and timings
765 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100766 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200767 *
768 * Reset the Data interface and timings to ONFI mode 0.
769 *
770 * Returns 0 for success or negative error code otherwise.
771 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100772static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200773{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200774 int ret;
775
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100776 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200777 return 0;
778
779 /*
780 * The ONFI specification says:
781 * "
782 * To transition from NV-DDR or NV-DDR2 to the SDR data
783 * interface, the host shall use the Reset (FFh) command
784 * using SDR timing mode 0. A device in any timing mode is
785 * required to recognize Reset (FFh) command issued in SDR
786 * timing mode 0.
787 * "
788 *
789 * Configure the data interface in SDR mode and set the
790 * timings to timing mode 0.
791 */
792
Miquel Raynal17fa8042017-11-30 18:01:31 +0100793 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100794 ret = chip->controller->ops->setup_data_interface(chip, chipnr,
795 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200796 if (ret)
797 pr_err("Failed to configure data interface to SDR timing mode 0\n");
798
799 return ret;
800}
801
802/**
803 * nand_setup_data_interface - Setup the best data interface and timings
804 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100805 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200806 *
807 * Find and configure the best data interface and NAND timings supported by
808 * the chip and the driver.
809 * First tries to retrieve supported timing modes from ONFI information,
810 * and if the NAND chip does not support ONFI, relies on the
811 * ->onfi_timing_mode_default specified in the nand_ids table.
812 *
813 * Returns 0 for success or negative error code otherwise.
814 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100815static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200816{
Miquel Raynal97baea12018-03-19 14:47:20 +0100817 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
818 chip->onfi_timing_mode_default,
819 };
Boris Brezillond8e725d2016-09-15 10:32:50 +0200820 int ret;
821
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100822 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200823 return 0;
824
Miquel Raynal993447b2018-03-19 14:47:21 +0100825 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +0100826 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100827 nand_select_target(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +0100828 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
829 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100830 nand_deselect_target(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200831 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +0100832 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +0200833 }
834
Miquel Raynal97baea12018-03-19 14:47:20 +0100835 /* Change the mode on the controller side */
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100836 ret = chip->controller->ops->setup_data_interface(chip, chipnr,
837 &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +0100838 if (ret)
839 return ret;
840
841 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +0100842 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +0100843 return 0;
844
845 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon1d017852018-11-11 08:55:14 +0100846 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100847 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
848 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100849 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100850 if (ret)
851 goto err_reset_chip;
852
853 if (tmode_param[0] != chip->onfi_timing_mode_default) {
854 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
855 chip->onfi_timing_mode_default);
856 goto err_reset_chip;
857 }
858
859 return 0;
860
861err_reset_chip:
862 /*
863 * Fallback to mode 0 if the chip explicitly did not ack the chosen
864 * timing mode.
865 */
866 nand_reset_data_interface(chip, chipnr);
Boris Brezillon1d017852018-11-11 08:55:14 +0100867 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100868 nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +0100869 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100870
Boris Brezillond8e725d2016-09-15 10:32:50 +0200871 return ret;
872}
873
874/**
875 * nand_init_data_interface - find the best data interface and timings
876 * @chip: The NAND chip
877 *
878 * Find the best data interface and NAND timings supported by the chip
879 * and the driver.
880 * First tries to retrieve supported timing modes from ONFI information,
881 * and if the NAND chip does not support ONFI, relies on the
882 * ->onfi_timing_mode_default specified in the nand_ids table. After this
883 * function nand_chip->data_interface is initialized with the best timing mode
884 * available.
885 *
886 * Returns 0 for success or negative error code otherwise.
887 */
888static int nand_init_data_interface(struct nand_chip *chip)
889{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200890 int modes, mode, ret;
891
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100892 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200893 return 0;
894
895 /*
896 * First try to identify the best timings from ONFI parameters and
897 * if the NAND does not support ONFI, fallback to the default ONFI
898 * timing mode.
899 */
Boris Brezillon462f35d2018-09-07 00:38:47 +0200900 if (chip->parameters.onfi) {
901 modes = chip->parameters.onfi->async_timing_mode;
902 } else {
Boris Brezillond8e725d2016-09-15 10:32:50 +0200903 if (!chip->onfi_timing_mode_default)
904 return 0;
905
906 modes = GENMASK(chip->onfi_timing_mode_default, 0);
907 }
908
Boris Brezillond8e725d2016-09-15 10:32:50 +0200909 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +0100910 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200911 if (ret)
912 continue;
913
Miquel Raynald787b8b2017-12-22 18:12:41 +0100914 /*
915 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
916 * controller supports the requested timings.
917 */
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100918 ret = chip->controller->ops->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +0100919 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +0100920 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200921 if (!ret) {
922 chip->onfi_timing_mode_default = mode;
923 break;
924 }
925 }
926
927 return 0;
928}
929
Boris Brezillond8e725d2016-09-15 10:32:50 +0200930/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100931 * nand_fill_column_cycles - fill the column cycles of an address
932 * @chip: The NAND chip
933 * @addrs: Array of address cycles to fill
934 * @offset_in_page: The offset in the page
935 *
936 * Fills the first or the first two bytes of the @addrs field depending
937 * on the NAND bus width and the page size.
938 *
939 * Returns the number of cycles needed to encode the column, or a negative
940 * error code in case one of the arguments is invalid.
941 */
942static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
943 unsigned int offset_in_page)
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100944{
Miquel Raynal8878b122017-11-09 14:16:45 +0100945 struct mtd_info *mtd = nand_to_mtd(chip);
946
947 /* Make sure the offset is less than the actual page size. */
948 if (offset_in_page > mtd->writesize + mtd->oobsize)
949 return -EINVAL;
950
951 /*
952 * On small page NANDs, there's a dedicated command to access the OOB
953 * area, and the column address is relative to the start of the OOB
954 * area, not the start of the page. Asjust the address accordingly.
955 */
956 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
957 offset_in_page -= mtd->writesize;
958
959 /*
960 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
961 * wide, then it must be divided by 2.
962 */
963 if (chip->options & NAND_BUSWIDTH_16) {
964 if (WARN_ON(offset_in_page % 2))
965 return -EINVAL;
966
967 offset_in_page /= 2;
968 }
969
970 addrs[0] = offset_in_page;
971
972 /*
973 * Small page NANDs use 1 cycle for the columns, while large page NANDs
974 * need 2
975 */
976 if (mtd->writesize <= 512)
977 return 1;
978
979 addrs[1] = offset_in_page >> 8;
980
981 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Miquel Raynal8878b122017-11-09 14:16:45 +0100984static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
985 unsigned int offset_in_page, void *buf,
986 unsigned int len)
987{
988 struct mtd_info *mtd = nand_to_mtd(chip);
989 const struct nand_sdr_timings *sdr =
990 nand_get_sdr_timings(&chip->data_interface);
991 u8 addrs[4];
992 struct nand_op_instr instrs[] = {
993 NAND_OP_CMD(NAND_CMD_READ0, 0),
994 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
995 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
996 PSEC_TO_NSEC(sdr->tRR_min)),
997 NAND_OP_DATA_IN(len, buf, 0),
998 };
Boris Brezillonae2294b2018-11-11 08:55:15 +0100999 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001000 int ret;
1001
1002 /* Drop the DATA_IN instruction if len is set to 0. */
1003 if (!len)
1004 op.ninstrs--;
1005
1006 if (offset_in_page >= mtd->writesize)
1007 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1008 else if (offset_in_page >= 256 &&
1009 !(chip->options & NAND_BUSWIDTH_16))
1010 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1011
1012 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1013 if (ret < 0)
1014 return ret;
1015
1016 addrs[1] = page;
1017 addrs[2] = page >> 8;
1018
1019 if (chip->options & NAND_ROW_ADDR_3) {
1020 addrs[3] = page >> 16;
1021 instrs[1].ctx.addr.naddrs++;
1022 }
1023
1024 return nand_exec_op(chip, &op);
1025}
1026
1027static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1028 unsigned int offset_in_page, void *buf,
1029 unsigned int len)
1030{
1031 const struct nand_sdr_timings *sdr =
1032 nand_get_sdr_timings(&chip->data_interface);
1033 u8 addrs[5];
1034 struct nand_op_instr instrs[] = {
1035 NAND_OP_CMD(NAND_CMD_READ0, 0),
1036 NAND_OP_ADDR(4, addrs, 0),
1037 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1038 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1039 PSEC_TO_NSEC(sdr->tRR_min)),
1040 NAND_OP_DATA_IN(len, buf, 0),
1041 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001042 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001043 int ret;
1044
1045 /* Drop the DATA_IN instruction if len is set to 0. */
1046 if (!len)
1047 op.ninstrs--;
1048
1049 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1050 if (ret < 0)
1051 return ret;
1052
1053 addrs[2] = page;
1054 addrs[3] = page >> 8;
1055
1056 if (chip->options & NAND_ROW_ADDR_3) {
1057 addrs[4] = page >> 16;
1058 instrs[1].ctx.addr.naddrs++;
1059 }
1060
1061 return nand_exec_op(chip, &op);
1062}
1063
1064/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001065 * nand_read_page_op - Do a READ PAGE operation
1066 * @chip: The NAND chip
1067 * @page: page to read
1068 * @offset_in_page: offset within the page
1069 * @buf: buffer used to store the data
1070 * @len: length of the buffer
1071 *
1072 * This function issues a READ PAGE operation.
1073 * This function does not select/unselect the CS line.
1074 *
1075 * Returns 0 on success, a negative error code otherwise.
1076 */
1077int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1078 unsigned int offset_in_page, void *buf, unsigned int len)
1079{
1080 struct mtd_info *mtd = nand_to_mtd(chip);
1081
1082 if (len && !buf)
1083 return -EINVAL;
1084
1085 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1086 return -EINVAL;
1087
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001088 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001089 if (mtd->writesize > 512)
1090 return nand_lp_exec_read_page_op(chip, page,
1091 offset_in_page, buf,
1092 len);
1093
1094 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1095 buf, len);
1096 }
1097
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001098 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001099 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001100 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001101
1102 return 0;
1103}
1104EXPORT_SYMBOL_GPL(nand_read_page_op);
1105
1106/**
1107 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1108 * @chip: The NAND chip
1109 * @page: parameter page to read
1110 * @buf: buffer used to store the data
1111 * @len: length of the buffer
1112 *
1113 * This function issues a READ PARAMETER PAGE operation.
1114 * This function does not select/unselect the CS line.
1115 *
1116 * Returns 0 on success, a negative error code otherwise.
1117 */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02001118int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1119 unsigned int len)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001120{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001121 unsigned int i;
1122 u8 *p = buf;
1123
1124 if (len && !buf)
1125 return -EINVAL;
1126
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001127 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001128 const struct nand_sdr_timings *sdr =
1129 nand_get_sdr_timings(&chip->data_interface);
1130 struct nand_op_instr instrs[] = {
1131 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1132 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1133 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1134 PSEC_TO_NSEC(sdr->tRR_min)),
1135 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1136 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001137 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001138
1139 /* Drop the DATA_IN instruction if len is set to 0. */
1140 if (!len)
1141 op.ninstrs--;
1142
1143 return nand_exec_op(chip, &op);
1144 }
1145
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001146 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001147 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001148 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001149
1150 return 0;
1151}
1152
1153/**
1154 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1155 * @chip: The NAND chip
1156 * @offset_in_page: offset within the page
1157 * @buf: buffer used to store the data
1158 * @len: length of the buffer
1159 * @force_8bit: force 8-bit bus access
1160 *
1161 * This function issues a CHANGE READ COLUMN operation.
1162 * This function does not select/unselect the CS line.
1163 *
1164 * Returns 0 on success, a negative error code otherwise.
1165 */
1166int nand_change_read_column_op(struct nand_chip *chip,
1167 unsigned int offset_in_page, void *buf,
1168 unsigned int len, bool force_8bit)
1169{
1170 struct mtd_info *mtd = nand_to_mtd(chip);
1171
1172 if (len && !buf)
1173 return -EINVAL;
1174
1175 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1176 return -EINVAL;
1177
Miquel Raynal8878b122017-11-09 14:16:45 +01001178 /* Small page NANDs do not support column change. */
1179 if (mtd->writesize <= 512)
1180 return -ENOTSUPP;
1181
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001182 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001183 const struct nand_sdr_timings *sdr =
1184 nand_get_sdr_timings(&chip->data_interface);
1185 u8 addrs[2] = {};
1186 struct nand_op_instr instrs[] = {
1187 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1188 NAND_OP_ADDR(2, addrs, 0),
1189 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1190 PSEC_TO_NSEC(sdr->tCCS_min)),
1191 NAND_OP_DATA_IN(len, buf, 0),
1192 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001193 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001194 int ret;
1195
1196 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1197 if (ret < 0)
1198 return ret;
1199
1200 /* Drop the DATA_IN instruction if len is set to 0. */
1201 if (!len)
1202 op.ninstrs--;
1203
1204 instrs[3].ctx.data.force_8bit = force_8bit;
1205
1206 return nand_exec_op(chip, &op);
1207 }
1208
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001209 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001210 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001211 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001212
1213 return 0;
1214}
1215EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1216
1217/**
1218 * nand_read_oob_op - Do a READ OOB operation
1219 * @chip: The NAND chip
1220 * @page: page to read
1221 * @offset_in_oob: offset within the OOB area
1222 * @buf: buffer used to store the data
1223 * @len: length of the buffer
1224 *
1225 * This function issues a READ OOB operation.
1226 * This function does not select/unselect the CS line.
1227 *
1228 * Returns 0 on success, a negative error code otherwise.
1229 */
1230int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1231 unsigned int offset_in_oob, void *buf, unsigned int len)
1232{
1233 struct mtd_info *mtd = nand_to_mtd(chip);
1234
1235 if (len && !buf)
1236 return -EINVAL;
1237
1238 if (offset_in_oob + len > mtd->oobsize)
1239 return -EINVAL;
1240
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001241 if (nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +01001242 return nand_read_page_op(chip, page,
1243 mtd->writesize + offset_in_oob,
1244 buf, len);
1245
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001246 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001247 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001248 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001249
1250 return 0;
1251}
1252EXPORT_SYMBOL_GPL(nand_read_oob_op);
1253
Miquel Raynal8878b122017-11-09 14:16:45 +01001254static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1255 unsigned int offset_in_page, const void *buf,
1256 unsigned int len, bool prog)
1257{
1258 struct mtd_info *mtd = nand_to_mtd(chip);
1259 const struct nand_sdr_timings *sdr =
1260 nand_get_sdr_timings(&chip->data_interface);
1261 u8 addrs[5] = {};
1262 struct nand_op_instr instrs[] = {
1263 /*
1264 * The first instruction will be dropped if we're dealing
1265 * with a large page NAND and adjusted if we're dealing
1266 * with a small page NAND and the page offset is > 255.
1267 */
1268 NAND_OP_CMD(NAND_CMD_READ0, 0),
1269 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1270 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1271 NAND_OP_DATA_OUT(len, buf, 0),
1272 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1273 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1274 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001275 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001276 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1277 int ret;
1278 u8 status;
1279
1280 if (naddrs < 0)
1281 return naddrs;
1282
1283 addrs[naddrs++] = page;
1284 addrs[naddrs++] = page >> 8;
1285 if (chip->options & NAND_ROW_ADDR_3)
1286 addrs[naddrs++] = page >> 16;
1287
1288 instrs[2].ctx.addr.naddrs = naddrs;
1289
1290 /* Drop the last two instructions if we're not programming the page. */
1291 if (!prog) {
1292 op.ninstrs -= 2;
1293 /* Also drop the DATA_OUT instruction if empty. */
1294 if (!len)
1295 op.ninstrs--;
1296 }
1297
1298 if (mtd->writesize <= 512) {
1299 /*
1300 * Small pages need some more tweaking: we have to adjust the
1301 * first instruction depending on the page offset we're trying
1302 * to access.
1303 */
1304 if (offset_in_page >= mtd->writesize)
1305 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1306 else if (offset_in_page >= 256 &&
1307 !(chip->options & NAND_BUSWIDTH_16))
1308 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1309 } else {
1310 /*
1311 * Drop the first command if we're dealing with a large page
1312 * NAND.
1313 */
1314 op.instrs++;
1315 op.ninstrs--;
1316 }
1317
1318 ret = nand_exec_op(chip, &op);
1319 if (!prog || ret)
1320 return ret;
1321
1322 ret = nand_status_op(chip, &status);
1323 if (ret)
1324 return ret;
1325
1326 return status;
1327}
1328
Boris Brezillon97d90da2017-11-30 18:01:29 +01001329/**
1330 * nand_prog_page_begin_op - starts a PROG PAGE operation
1331 * @chip: The NAND chip
1332 * @page: page to write
1333 * @offset_in_page: offset within the page
1334 * @buf: buffer containing the data to write to the page
1335 * @len: length of the buffer
1336 *
1337 * This function issues the first half of a PROG PAGE operation.
1338 * This function does not select/unselect the CS line.
1339 *
1340 * Returns 0 on success, a negative error code otherwise.
1341 */
1342int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1343 unsigned int offset_in_page, const void *buf,
1344 unsigned int len)
1345{
1346 struct mtd_info *mtd = nand_to_mtd(chip);
1347
1348 if (len && !buf)
1349 return -EINVAL;
1350
1351 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1352 return -EINVAL;
1353
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001354 if (nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +01001355 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1356 len, false);
1357
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001358 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001359
1360 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001361 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001362
1363 return 0;
1364}
1365EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1366
1367/**
1368 * nand_prog_page_end_op - ends a PROG PAGE operation
1369 * @chip: The NAND chip
1370 *
1371 * This function issues the second half of a PROG PAGE operation.
1372 * This function does not select/unselect the CS line.
1373 *
1374 * Returns 0 on success, a negative error code otherwise.
1375 */
1376int nand_prog_page_end_op(struct nand_chip *chip)
1377{
Miquel Raynal8878b122017-11-09 14:16:45 +01001378 int ret;
1379 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001380
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001381 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001382 const struct nand_sdr_timings *sdr =
1383 nand_get_sdr_timings(&chip->data_interface);
1384 struct nand_op_instr instrs[] = {
1385 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1386 PSEC_TO_NSEC(sdr->tWB_max)),
1387 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1388 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001389 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001390
Miquel Raynal8878b122017-11-09 14:16:45 +01001391 ret = nand_exec_op(chip, &op);
1392 if (ret)
1393 return ret;
1394
1395 ret = nand_status_op(chip, &status);
1396 if (ret)
1397 return ret;
1398 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001399 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001400 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001401 if (ret < 0)
1402 return ret;
1403
1404 status = ret;
1405 }
1406
Boris Brezillon97d90da2017-11-30 18:01:29 +01001407 if (status & NAND_STATUS_FAIL)
1408 return -EIO;
1409
1410 return 0;
1411}
1412EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1413
1414/**
1415 * nand_prog_page_op - Do a full PROG PAGE operation
1416 * @chip: The NAND chip
1417 * @page: page to write
1418 * @offset_in_page: offset within the page
1419 * @buf: buffer containing the data to write to the page
1420 * @len: length of the buffer
1421 *
1422 * This function issues a full PROG PAGE operation.
1423 * This function does not select/unselect the CS line.
1424 *
1425 * Returns 0 on success, a negative error code otherwise.
1426 */
1427int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1428 unsigned int offset_in_page, const void *buf,
1429 unsigned int len)
1430{
1431 struct mtd_info *mtd = nand_to_mtd(chip);
1432 int status;
1433
1434 if (!len || !buf)
1435 return -EINVAL;
1436
1437 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1438 return -EINVAL;
1439
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001440 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001441 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1442 len, true);
1443 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001444 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1445 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001446 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001447 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001448 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001449 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001450
Boris Brezillon97d90da2017-11-30 18:01:29 +01001451 if (status & NAND_STATUS_FAIL)
1452 return -EIO;
1453
1454 return 0;
1455}
1456EXPORT_SYMBOL_GPL(nand_prog_page_op);
1457
1458/**
1459 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1460 * @chip: The NAND chip
1461 * @offset_in_page: offset within the page
1462 * @buf: buffer containing the data to send to the NAND
1463 * @len: length of the buffer
1464 * @force_8bit: force 8-bit bus access
1465 *
1466 * This function issues a CHANGE WRITE COLUMN operation.
1467 * This function does not select/unselect the CS line.
1468 *
1469 * Returns 0 on success, a negative error code otherwise.
1470 */
1471int nand_change_write_column_op(struct nand_chip *chip,
1472 unsigned int offset_in_page,
1473 const void *buf, unsigned int len,
1474 bool force_8bit)
1475{
1476 struct mtd_info *mtd = nand_to_mtd(chip);
1477
1478 if (len && !buf)
1479 return -EINVAL;
1480
1481 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1482 return -EINVAL;
1483
Miquel Raynal8878b122017-11-09 14:16:45 +01001484 /* Small page NANDs do not support column change. */
1485 if (mtd->writesize <= 512)
1486 return -ENOTSUPP;
1487
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001488 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001489 const struct nand_sdr_timings *sdr =
1490 nand_get_sdr_timings(&chip->data_interface);
1491 u8 addrs[2];
1492 struct nand_op_instr instrs[] = {
1493 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1494 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1495 NAND_OP_DATA_OUT(len, buf, 0),
1496 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001497 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001498 int ret;
1499
1500 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1501 if (ret < 0)
1502 return ret;
1503
1504 instrs[2].ctx.data.force_8bit = force_8bit;
1505
1506 /* Drop the DATA_OUT instruction if len is set to 0. */
1507 if (!len)
1508 op.ninstrs--;
1509
1510 return nand_exec_op(chip, &op);
1511 }
1512
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001513 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001514 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001515 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001516
1517 return 0;
1518}
1519EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1520
1521/**
1522 * nand_readid_op - Do a READID operation
1523 * @chip: The NAND chip
1524 * @addr: address cycle to pass after the READID command
1525 * @buf: buffer used to store the ID
1526 * @len: length of the buffer
1527 *
1528 * This function sends a READID command and reads back the ID returned by the
1529 * NAND.
1530 * This function does not select/unselect the CS line.
1531 *
1532 * Returns 0 on success, a negative error code otherwise.
1533 */
1534int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1535 unsigned int len)
1536{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001537 unsigned int i;
1538 u8 *id = buf;
1539
1540 if (len && !buf)
1541 return -EINVAL;
1542
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001543 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001544 const struct nand_sdr_timings *sdr =
1545 nand_get_sdr_timings(&chip->data_interface);
1546 struct nand_op_instr instrs[] = {
1547 NAND_OP_CMD(NAND_CMD_READID, 0),
1548 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1549 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1550 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001551 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001552
1553 /* Drop the DATA_IN instruction if len is set to 0. */
1554 if (!len)
1555 op.ninstrs--;
1556
1557 return nand_exec_op(chip, &op);
1558 }
1559
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001560 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001561
1562 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001563 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001564
1565 return 0;
1566}
1567EXPORT_SYMBOL_GPL(nand_readid_op);
1568
1569/**
1570 * nand_status_op - Do a STATUS operation
1571 * @chip: The NAND chip
1572 * @status: out variable to store the NAND status
1573 *
1574 * This function sends a STATUS command and reads back the status returned by
1575 * the NAND.
1576 * This function does not select/unselect the CS line.
1577 *
1578 * Returns 0 on success, a negative error code otherwise.
1579 */
1580int nand_status_op(struct nand_chip *chip, u8 *status)
1581{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001582 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001583 const struct nand_sdr_timings *sdr =
1584 nand_get_sdr_timings(&chip->data_interface);
1585 struct nand_op_instr instrs[] = {
1586 NAND_OP_CMD(NAND_CMD_STATUS,
1587 PSEC_TO_NSEC(sdr->tADL_min)),
1588 NAND_OP_8BIT_DATA_IN(1, status, 0),
1589 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001590 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001591
1592 if (!status)
1593 op.ninstrs--;
1594
1595 return nand_exec_op(chip, &op);
1596 }
1597
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001598 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001599 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001600 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001601
1602 return 0;
1603}
1604EXPORT_SYMBOL_GPL(nand_status_op);
1605
1606/**
1607 * nand_exit_status_op - Exit a STATUS operation
1608 * @chip: The NAND chip
1609 *
1610 * This function sends a READ0 command to cancel the effect of the STATUS
1611 * command to avoid reading only the status until a new read command is sent.
1612 *
1613 * This function does not select/unselect the CS line.
1614 *
1615 * Returns 0 on success, a negative error code otherwise.
1616 */
1617int nand_exit_status_op(struct nand_chip *chip)
1618{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001619 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001620 struct nand_op_instr instrs[] = {
1621 NAND_OP_CMD(NAND_CMD_READ0, 0),
1622 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001623 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001624
1625 return nand_exec_op(chip, &op);
1626 }
1627
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001628 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001629
1630 return 0;
1631}
Boris Brezillon97d90da2017-11-30 18:01:29 +01001632
1633/**
1634 * nand_erase_op - Do an erase operation
1635 * @chip: The NAND chip
1636 * @eraseblock: block to erase
1637 *
1638 * This function sends an ERASE command and waits for the NAND to be ready
1639 * before returning.
1640 * This function does not select/unselect the CS line.
1641 *
1642 * Returns 0 on success, a negative error code otherwise.
1643 */
1644int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1645{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001646 unsigned int page = eraseblock <<
1647 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01001648 int ret;
1649 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001650
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001651 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001652 const struct nand_sdr_timings *sdr =
1653 nand_get_sdr_timings(&chip->data_interface);
1654 u8 addrs[3] = { page, page >> 8, page >> 16 };
1655 struct nand_op_instr instrs[] = {
1656 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
1657 NAND_OP_ADDR(2, addrs, 0),
1658 NAND_OP_CMD(NAND_CMD_ERASE2,
1659 PSEC_TO_MSEC(sdr->tWB_max)),
1660 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
1661 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001662 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001663
Miquel Raynal8878b122017-11-09 14:16:45 +01001664 if (chip->options & NAND_ROW_ADDR_3)
1665 instrs[1].ctx.addr.naddrs++;
1666
1667 ret = nand_exec_op(chip, &op);
1668 if (ret)
1669 return ret;
1670
1671 ret = nand_status_op(chip, &status);
1672 if (ret)
1673 return ret;
1674 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001675 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
1676 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01001677
Boris Brezillon8395b752018-09-07 00:38:37 +02001678 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001679 if (ret < 0)
1680 return ret;
1681
1682 status = ret;
1683 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001684
1685 if (status & NAND_STATUS_FAIL)
1686 return -EIO;
1687
1688 return 0;
1689}
1690EXPORT_SYMBOL_GPL(nand_erase_op);
1691
1692/**
1693 * nand_set_features_op - Do a SET FEATURES operation
1694 * @chip: The NAND chip
1695 * @feature: feature id
1696 * @data: 4 bytes of data
1697 *
1698 * This function sends a SET FEATURES command and waits for the NAND to be
1699 * ready before returning.
1700 * This function does not select/unselect the CS line.
1701 *
1702 * Returns 0 on success, a negative error code otherwise.
1703 */
1704static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1705 const void *data)
1706{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001707 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01001708 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001709
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001710 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001711 const struct nand_sdr_timings *sdr =
1712 nand_get_sdr_timings(&chip->data_interface);
1713 struct nand_op_instr instrs[] = {
1714 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
1715 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
1716 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
1717 PSEC_TO_NSEC(sdr->tWB_max)),
1718 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
1719 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001720 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001721
Boris Brezillon782d1962018-05-11 14:44:07 +02001722 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01001723 }
1724
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001725 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02001726 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001727 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02001728
Boris Brezillon8395b752018-09-07 00:38:37 +02001729 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02001730 if (ret < 0)
1731 return ret;
1732
1733 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001734 return -EIO;
1735
1736 return 0;
1737}
1738
1739/**
1740 * nand_get_features_op - Do a GET FEATURES operation
1741 * @chip: The NAND chip
1742 * @feature: feature id
1743 * @data: 4 bytes of data
1744 *
1745 * This function sends a GET FEATURES command and waits for the NAND to be
1746 * ready before returning.
1747 * This function does not select/unselect the CS line.
1748 *
1749 * Returns 0 on success, a negative error code otherwise.
1750 */
1751static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1752 void *data)
1753{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001754 u8 *params = data;
1755 int i;
1756
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001757 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001758 const struct nand_sdr_timings *sdr =
1759 nand_get_sdr_timings(&chip->data_interface);
1760 struct nand_op_instr instrs[] = {
1761 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
1762 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
1763 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
1764 PSEC_TO_NSEC(sdr->tRR_min)),
1765 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
1766 data, 0),
1767 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001768 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001769
1770 return nand_exec_op(chip, &op);
1771 }
1772
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001773 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001774 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001775 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001776
1777 return 0;
1778}
1779
Boris Brezillon52f05b62018-07-27 09:44:18 +02001780static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
1781 unsigned int delay_ns)
1782{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001783 if (nand_has_exec_op(chip)) {
Boris Brezillon52f05b62018-07-27 09:44:18 +02001784 struct nand_op_instr instrs[] = {
1785 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
1786 PSEC_TO_NSEC(delay_ns)),
1787 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001788 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001789
1790 return nand_exec_op(chip, &op);
1791 }
1792
1793 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02001794 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001795 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001796 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001797 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001798
1799 return 0;
1800}
1801
Boris Brezillon97d90da2017-11-30 18:01:29 +01001802/**
1803 * nand_reset_op - Do a reset operation
1804 * @chip: The NAND chip
1805 *
1806 * This function sends a RESET command and waits for the NAND to be ready
1807 * before returning.
1808 * This function does not select/unselect the CS line.
1809 *
1810 * Returns 0 on success, a negative error code otherwise.
1811 */
1812int nand_reset_op(struct nand_chip *chip)
1813{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001814 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001815 const struct nand_sdr_timings *sdr =
1816 nand_get_sdr_timings(&chip->data_interface);
1817 struct nand_op_instr instrs[] = {
1818 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
1819 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
1820 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001821 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001822
1823 return nand_exec_op(chip, &op);
1824 }
1825
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001826 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001827
1828 return 0;
1829}
1830EXPORT_SYMBOL_GPL(nand_reset_op);
1831
1832/**
1833 * nand_read_data_op - Read data from the NAND
1834 * @chip: The NAND chip
1835 * @buf: buffer used to store the data
1836 * @len: length of the buffer
1837 * @force_8bit: force 8-bit bus access
1838 *
1839 * This function does a raw data read on the bus. Usually used after launching
1840 * another NAND operation like nand_read_page_op().
1841 * This function does not select/unselect the CS line.
1842 *
1843 * Returns 0 on success, a negative error code otherwise.
1844 */
1845int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1846 bool force_8bit)
1847{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001848 if (!len || !buf)
1849 return -EINVAL;
1850
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001851 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001852 struct nand_op_instr instrs[] = {
1853 NAND_OP_DATA_IN(len, buf, 0),
1854 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001855 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001856
1857 instrs[0].ctx.data.force_8bit = force_8bit;
1858
1859 return nand_exec_op(chip, &op);
1860 }
1861
Boris Brezillon97d90da2017-11-30 18:01:29 +01001862 if (force_8bit) {
1863 u8 *p = buf;
1864 unsigned int i;
1865
1866 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001867 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001868 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001869 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001870 }
1871
1872 return 0;
1873}
1874EXPORT_SYMBOL_GPL(nand_read_data_op);
1875
1876/**
1877 * nand_write_data_op - Write data from the NAND
1878 * @chip: The NAND chip
1879 * @buf: buffer containing the data to send on the bus
1880 * @len: length of the buffer
1881 * @force_8bit: force 8-bit bus access
1882 *
1883 * This function does a raw data write on the bus. Usually used after launching
1884 * another NAND operation like nand_write_page_begin_op().
1885 * This function does not select/unselect the CS line.
1886 *
1887 * Returns 0 on success, a negative error code otherwise.
1888 */
1889int nand_write_data_op(struct nand_chip *chip, const void *buf,
1890 unsigned int len, bool force_8bit)
1891{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001892 if (!len || !buf)
1893 return -EINVAL;
1894
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001895 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001896 struct nand_op_instr instrs[] = {
1897 NAND_OP_DATA_OUT(len, buf, 0),
1898 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001899 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001900
1901 instrs[0].ctx.data.force_8bit = force_8bit;
1902
1903 return nand_exec_op(chip, &op);
1904 }
1905
Boris Brezillon97d90da2017-11-30 18:01:29 +01001906 if (force_8bit) {
1907 const u8 *p = buf;
1908 unsigned int i;
1909
1910 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001911 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001912 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001913 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001914 }
1915
1916 return 0;
1917}
1918EXPORT_SYMBOL_GPL(nand_write_data_op);
1919
1920/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001921 * struct nand_op_parser_ctx - Context used by the parser
1922 * @instrs: array of all the instructions that must be addressed
1923 * @ninstrs: length of the @instrs array
1924 * @subop: Sub-operation to be passed to the NAND controller
1925 *
1926 * This structure is used by the core to split NAND operations into
1927 * sub-operations that can be handled by the NAND controller.
1928 */
1929struct nand_op_parser_ctx {
1930 const struct nand_op_instr *instrs;
1931 unsigned int ninstrs;
1932 struct nand_subop subop;
1933};
1934
1935/**
1936 * nand_op_parser_must_split_instr - Checks if an instruction must be split
1937 * @pat: the parser pattern element that matches @instr
1938 * @instr: pointer to the instruction to check
1939 * @start_offset: this is an in/out parameter. If @instr has already been
1940 * split, then @start_offset is the offset from which to start
1941 * (either an address cycle or an offset in the data buffer).
1942 * Conversely, if the function returns true (ie. instr must be
1943 * split), this parameter is updated to point to the first
1944 * data/address cycle that has not been taken care of.
1945 *
1946 * Some NAND controllers are limited and cannot send X address cycles with a
1947 * unique operation, or cannot read/write more than Y bytes at the same time.
1948 * In this case, split the instruction that does not fit in a single
1949 * controller-operation into two or more chunks.
1950 *
1951 * Returns true if the instruction must be split, false otherwise.
1952 * The @start_offset parameter is also updated to the offset at which the next
1953 * bundle of instruction must start (if an address or a data instruction).
1954 */
1955static bool
1956nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
1957 const struct nand_op_instr *instr,
1958 unsigned int *start_offset)
1959{
1960 switch (pat->type) {
1961 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001962 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01001963 break;
1964
1965 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001966 pat->ctx.addr.maxcycles) {
1967 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01001968 return true;
1969 }
1970 break;
1971
1972 case NAND_OP_DATA_IN_INSTR:
1973 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001974 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01001975 break;
1976
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001977 if (instr->ctx.data.len - *start_offset >
1978 pat->ctx.data.maxlen) {
1979 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01001980 return true;
1981 }
1982 break;
1983
1984 default:
1985 break;
1986 }
1987
1988 return false;
1989}
1990
1991/**
1992 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
1993 * remaining in the parser context
1994 * @pat: the pattern to test
1995 * @ctx: the parser context structure to match with the pattern @pat
1996 *
1997 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
1998 * Returns true if this is the case, false ortherwise. When true is returned,
1999 * @ctx->subop is updated with the set of instructions to be passed to the
2000 * controller driver.
2001 */
2002static bool
2003nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2004 struct nand_op_parser_ctx *ctx)
2005{
2006 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2007 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2008 const struct nand_op_instr *instr = ctx->subop.instrs;
2009 unsigned int i, ninstrs;
2010
2011 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2012 /*
2013 * The pattern instruction does not match the operation
2014 * instruction. If the instruction is marked optional in the
2015 * pattern definition, we skip the pattern element and continue
2016 * to the next one. If the element is mandatory, there's no
2017 * match and we can return false directly.
2018 */
2019 if (instr->type != pat->elems[i].type) {
2020 if (!pat->elems[i].optional)
2021 return false;
2022
2023 continue;
2024 }
2025
2026 /*
2027 * Now check the pattern element constraints. If the pattern is
2028 * not able to handle the whole instruction in a single step,
2029 * we have to split it.
2030 * The last_instr_end_off value comes back updated to point to
2031 * the position where we have to split the instruction (the
2032 * start of the next subop chunk).
2033 */
2034 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2035 &instr_offset)) {
2036 ninstrs++;
2037 i++;
2038 break;
2039 }
2040
2041 instr++;
2042 ninstrs++;
2043 instr_offset = 0;
2044 }
2045
2046 /*
2047 * This can happen if all instructions of a pattern are optional.
2048 * Still, if there's not at least one instruction handled by this
2049 * pattern, this is not a match, and we should try the next one (if
2050 * any).
2051 */
2052 if (!ninstrs)
2053 return false;
2054
2055 /*
2056 * We had a match on the pattern head, but the pattern may be longer
2057 * than the instructions we're asked to execute. We need to make sure
2058 * there's no mandatory elements in the pattern tail.
2059 */
2060 for (; i < pat->nelems; i++) {
2061 if (!pat->elems[i].optional)
2062 return false;
2063 }
2064
2065 /*
2066 * We have a match: update the subop structure accordingly and return
2067 * true.
2068 */
2069 ctx->subop.ninstrs = ninstrs;
2070 ctx->subop.last_instr_end_off = instr_offset;
2071
2072 return true;
2073}
2074
2075#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2076static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2077{
2078 const struct nand_op_instr *instr;
2079 char *prefix = " ";
2080 unsigned int i;
2081
2082 pr_debug("executing subop:\n");
2083
2084 for (i = 0; i < ctx->ninstrs; i++) {
2085 instr = &ctx->instrs[i];
2086
2087 if (instr == &ctx->subop.instrs[0])
2088 prefix = " ->";
2089
2090 switch (instr->type) {
2091 case NAND_OP_CMD_INSTR:
2092 pr_debug("%sCMD [0x%02x]\n", prefix,
2093 instr->ctx.cmd.opcode);
2094 break;
2095 case NAND_OP_ADDR_INSTR:
2096 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2097 instr->ctx.addr.naddrs,
2098 instr->ctx.addr.naddrs < 64 ?
2099 instr->ctx.addr.naddrs : 64,
2100 instr->ctx.addr.addrs);
2101 break;
2102 case NAND_OP_DATA_IN_INSTR:
2103 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2104 instr->ctx.data.len,
2105 instr->ctx.data.force_8bit ?
2106 ", force 8-bit" : "");
2107 break;
2108 case NAND_OP_DATA_OUT_INSTR:
2109 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2110 instr->ctx.data.len,
2111 instr->ctx.data.force_8bit ?
2112 ", force 8-bit" : "");
2113 break;
2114 case NAND_OP_WAITRDY_INSTR:
2115 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2116 instr->ctx.waitrdy.timeout_ms);
2117 break;
2118 }
2119
2120 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2121 prefix = " ";
2122 }
2123}
2124#else
2125static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2126{
2127 /* NOP */
2128}
2129#endif
2130
2131/**
2132 * nand_op_parser_exec_op - exec_op parser
2133 * @chip: the NAND chip
2134 * @parser: patterns description provided by the controller driver
2135 * @op: the NAND operation to address
2136 * @check_only: when true, the function only checks if @op can be handled but
2137 * does not execute the operation
2138 *
2139 * Helper function designed to ease integration of NAND controller drivers that
2140 * only support a limited set of instruction sequences. The supported sequences
2141 * are described in @parser, and the framework takes care of splitting @op into
2142 * multiple sub-operations (if required) and pass them back to the ->exec()
2143 * callback of the matching pattern if @check_only is set to false.
2144 *
2145 * NAND controller drivers should call this function from their own ->exec_op()
2146 * implementation.
2147 *
2148 * Returns 0 on success, a negative error code otherwise. A failure can be
2149 * caused by an unsupported operation (none of the supported patterns is able
2150 * to handle the requested operation), or an error returned by one of the
2151 * matching pattern->exec() hook.
2152 */
2153int nand_op_parser_exec_op(struct nand_chip *chip,
2154 const struct nand_op_parser *parser,
2155 const struct nand_operation *op, bool check_only)
2156{
2157 struct nand_op_parser_ctx ctx = {
2158 .subop.instrs = op->instrs,
2159 .instrs = op->instrs,
2160 .ninstrs = op->ninstrs,
2161 };
2162 unsigned int i;
2163
2164 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2165 int ret;
2166
2167 for (i = 0; i < parser->npatterns; i++) {
2168 const struct nand_op_parser_pattern *pattern;
2169
2170 pattern = &parser->patterns[i];
2171 if (!nand_op_parser_match_pat(pattern, &ctx))
2172 continue;
2173
2174 nand_op_parser_trace(&ctx);
2175
2176 if (check_only)
2177 break;
2178
2179 ret = pattern->exec(chip, &ctx.subop);
2180 if (ret)
2181 return ret;
2182
2183 break;
2184 }
2185
2186 if (i == parser->npatterns) {
2187 pr_debug("->exec_op() parser: pattern not found!\n");
2188 return -ENOTSUPP;
2189 }
2190
2191 /*
2192 * Update the context structure by pointing to the start of the
2193 * next subop.
2194 */
2195 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2196 if (ctx.subop.last_instr_end_off)
2197 ctx.subop.instrs -= 1;
2198
2199 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2200 }
2201
2202 return 0;
2203}
2204EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2205
2206static bool nand_instr_is_data(const struct nand_op_instr *instr)
2207{
2208 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2209 instr->type == NAND_OP_DATA_OUT_INSTR);
2210}
2211
2212static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2213 unsigned int instr_idx)
2214{
2215 return subop && instr_idx < subop->ninstrs;
2216}
2217
Miquel Raynal760c4352018-07-19 00:09:12 +02002218static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2219 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002220{
2221 if (instr_idx)
2222 return 0;
2223
2224 return subop->first_instr_start_off;
2225}
2226
2227/**
2228 * nand_subop_get_addr_start_off - Get the start offset in an address array
2229 * @subop: The entire sub-operation
2230 * @instr_idx: Index of the instruction inside the sub-operation
2231 *
2232 * During driver development, one could be tempted to directly use the
2233 * ->addr.addrs field of address instructions. This is wrong as address
2234 * instructions might be split.
2235 *
2236 * Given an address instruction, returns the offset of the first cycle to issue.
2237 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002238unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2239 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002240{
Miquel Raynal760c4352018-07-19 00:09:12 +02002241 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2242 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2243 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002244
2245 return nand_subop_get_start_off(subop, instr_idx);
2246}
2247EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2248
2249/**
2250 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2251 * @subop: The entire sub-operation
2252 * @instr_idx: Index of the instruction inside the sub-operation
2253 *
2254 * During driver development, one could be tempted to directly use the
2255 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2256 * might be split.
2257 *
2258 * Given an address instruction, returns the number of address cycle to issue.
2259 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002260unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2261 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002262{
2263 int start_off, end_off;
2264
Miquel Raynal760c4352018-07-19 00:09:12 +02002265 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2266 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2267 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002268
2269 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2270
2271 if (instr_idx == subop->ninstrs - 1 &&
2272 subop->last_instr_end_off)
2273 end_off = subop->last_instr_end_off;
2274 else
2275 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2276
2277 return end_off - start_off;
2278}
2279EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2280
2281/**
2282 * nand_subop_get_data_start_off - Get the start offset in a data array
2283 * @subop: The entire sub-operation
2284 * @instr_idx: Index of the instruction inside the sub-operation
2285 *
2286 * During driver development, one could be tempted to directly use the
2287 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2288 * instructions might be split.
2289 *
2290 * Given a data instruction, returns the offset to start from.
2291 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002292unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2293 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002294{
Miquel Raynal760c4352018-07-19 00:09:12 +02002295 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2296 !nand_instr_is_data(&subop->instrs[instr_idx])))
2297 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002298
2299 return nand_subop_get_start_off(subop, instr_idx);
2300}
2301EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2302
2303/**
2304 * nand_subop_get_data_len - Get the number of bytes to retrieve
2305 * @subop: The entire sub-operation
2306 * @instr_idx: Index of the instruction inside the sub-operation
2307 *
2308 * During driver development, one could be tempted to directly use the
2309 * ->data->len field of a data instruction. This is wrong as data instructions
2310 * might be split.
2311 *
2312 * Returns the length of the chunk of data to send/receive.
2313 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002314unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2315 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002316{
2317 int start_off = 0, end_off;
2318
Miquel Raynal760c4352018-07-19 00:09:12 +02002319 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2320 !nand_instr_is_data(&subop->instrs[instr_idx])))
2321 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002322
2323 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2324
2325 if (instr_idx == subop->ninstrs - 1 &&
2326 subop->last_instr_end_off)
2327 end_off = subop->last_instr_end_off;
2328 else
2329 end_off = subop->instrs[instr_idx].ctx.data.len;
2330
2331 return end_off - start_off;
2332}
2333EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002336 * nand_reset - Reset and initialize a NAND device
2337 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002338 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002339 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002340 * Save the timings data structure, then apply SDR timings mode 0 (see
2341 * nand_reset_data_interface for details), do the reset operation, and
2342 * apply back the previous timings.
2343 *
2344 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002345 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002346int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002347{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002348 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002349 int ret;
2350
Boris Brezillon104e4422017-03-16 09:35:58 +01002351 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002352 if (ret)
2353 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002354
Boris Brezillon73f907f2016-10-24 16:46:20 +02002355 /*
2356 * The CS line has to be released before we can apply the new NAND
Boris Brezillon1d017852018-11-11 08:55:14 +01002357 * interface settings, hence this weird nand_select_target()
2358 * nand_deselect_target() dance.
Boris Brezillon73f907f2016-10-24 16:46:20 +02002359 */
Boris Brezillon1d017852018-11-11 08:55:14 +01002360 nand_select_target(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002361 ret = nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01002362 nand_deselect_target(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002363 if (ret)
2364 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002365
Miquel Raynal107b7d62018-03-19 14:47:25 +01002366 /*
2367 * A nand_reset_data_interface() put both the NAND chip and the NAND
2368 * controller in timings mode 0. If the default mode for this chip is
2369 * also 0, no need to proceed to the change again. Plus, at probe time,
2370 * nand_setup_data_interface() uses ->set/get_features() which would
2371 * fail anyway as the parameter page is not available yet.
2372 */
2373 if (!chip->onfi_timing_mode_default)
2374 return 0;
2375
Miquel Raynal17fa8042017-11-30 18:01:31 +01002376 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002377 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002378 if (ret)
2379 return ret;
2380
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002381 return 0;
2382}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002383EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002384
2385/**
Boris Brezillon45240362018-09-07 00:38:40 +02002386 * nand_get_features - wrapper to perform a GET_FEATURE
2387 * @chip: NAND chip info structure
2388 * @addr: feature address
2389 * @subfeature_param: the subfeature parameters, a four bytes array
2390 *
2391 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2392 * operation cannot be handled.
2393 */
2394int nand_get_features(struct nand_chip *chip, int addr,
2395 u8 *subfeature_param)
2396{
2397 if (!nand_supports_get_features(chip, addr))
2398 return -ENOTSUPP;
2399
2400 if (chip->legacy.get_features)
2401 return chip->legacy.get_features(chip, addr, subfeature_param);
2402
2403 return nand_get_features_op(chip, addr, subfeature_param);
2404}
Boris Brezillon45240362018-09-07 00:38:40 +02002405
2406/**
2407 * nand_set_features - wrapper to perform a SET_FEATURE
2408 * @chip: NAND chip info structure
2409 * @addr: feature address
2410 * @subfeature_param: the subfeature parameters, a four bytes array
2411 *
2412 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2413 * operation cannot be handled.
2414 */
2415int nand_set_features(struct nand_chip *chip, int addr,
2416 u8 *subfeature_param)
2417{
2418 if (!nand_supports_set_features(chip, addr))
2419 return -ENOTSUPP;
2420
2421 if (chip->legacy.set_features)
2422 return chip->legacy.set_features(chip, addr, subfeature_param);
2423
2424 return nand_set_features_op(chip, addr, subfeature_param);
2425}
Boris Brezillon45240362018-09-07 00:38:40 +02002426
2427/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002428 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2429 * @buf: buffer to test
2430 * @len: buffer length
2431 * @bitflips_threshold: maximum number of bitflips
2432 *
2433 * Check if a buffer contains only 0xff, which means the underlying region
2434 * has been erased and is ready to be programmed.
2435 * The bitflips_threshold specify the maximum number of bitflips before
2436 * considering the region is not erased.
2437 * Note: The logic of this function has been extracted from the memweight
2438 * implementation, except that nand_check_erased_buf function exit before
2439 * testing the whole buffer if the number of bitflips exceed the
2440 * bitflips_threshold value.
2441 *
2442 * Returns a positive number of bitflips less than or equal to
2443 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2444 * threshold.
2445 */
2446static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2447{
2448 const unsigned char *bitmap = buf;
2449 int bitflips = 0;
2450 int weight;
2451
2452 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2453 len--, bitmap++) {
2454 weight = hweight8(*bitmap);
2455 bitflips += BITS_PER_BYTE - weight;
2456 if (unlikely(bitflips > bitflips_threshold))
2457 return -EBADMSG;
2458 }
2459
2460 for (; len >= sizeof(long);
2461 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002462 unsigned long d = *((unsigned long *)bitmap);
2463 if (d == ~0UL)
2464 continue;
2465 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002466 bitflips += BITS_PER_LONG - weight;
2467 if (unlikely(bitflips > bitflips_threshold))
2468 return -EBADMSG;
2469 }
2470
2471 for (; len > 0; len--, bitmap++) {
2472 weight = hweight8(*bitmap);
2473 bitflips += BITS_PER_BYTE - weight;
2474 if (unlikely(bitflips > bitflips_threshold))
2475 return -EBADMSG;
2476 }
2477
2478 return bitflips;
2479}
2480
2481/**
2482 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2483 * 0xff data
2484 * @data: data buffer to test
2485 * @datalen: data length
2486 * @ecc: ECC buffer
2487 * @ecclen: ECC length
2488 * @extraoob: extra OOB buffer
2489 * @extraooblen: extra OOB length
2490 * @bitflips_threshold: maximum number of bitflips
2491 *
2492 * Check if a data buffer and its associated ECC and OOB data contains only
2493 * 0xff pattern, which means the underlying region has been erased and is
2494 * ready to be programmed.
2495 * The bitflips_threshold specify the maximum number of bitflips before
2496 * considering the region as not erased.
2497 *
2498 * Note:
2499 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2500 * different from the NAND page size. When fixing bitflips, ECC engines will
2501 * report the number of errors per chunk, and the NAND core infrastructure
2502 * expect you to return the maximum number of bitflips for the whole page.
2503 * This is why you should always use this function on a single chunk and
2504 * not on the whole page. After checking each chunk you should update your
2505 * max_bitflips value accordingly.
2506 * 2/ When checking for bitflips in erased pages you should not only check
2507 * the payload data but also their associated ECC data, because a user might
2508 * have programmed almost all bits to 1 but a few. In this case, we
2509 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2510 * this case.
2511 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2512 * data are protected by the ECC engine.
2513 * It could also be used if you support subpages and want to attach some
2514 * extra OOB data to an ECC chunk.
2515 *
2516 * Returns a positive number of bitflips less than or equal to
2517 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2518 * threshold. In case of success, the passed buffers are filled with 0xff.
2519 */
2520int nand_check_erased_ecc_chunk(void *data, int datalen,
2521 void *ecc, int ecclen,
2522 void *extraoob, int extraooblen,
2523 int bitflips_threshold)
2524{
2525 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2526
2527 data_bitflips = nand_check_erased_buf(data, datalen,
2528 bitflips_threshold);
2529 if (data_bitflips < 0)
2530 return data_bitflips;
2531
2532 bitflips_threshold -= data_bitflips;
2533
2534 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2535 if (ecc_bitflips < 0)
2536 return ecc_bitflips;
2537
2538 bitflips_threshold -= ecc_bitflips;
2539
2540 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2541 bitflips_threshold);
2542 if (extraoob_bitflips < 0)
2543 return extraoob_bitflips;
2544
2545 if (data_bitflips)
2546 memset(data, 0xff, datalen);
2547
2548 if (ecc_bitflips)
2549 memset(ecc, 0xff, ecclen);
2550
2551 if (extraoob_bitflips)
2552 memset(extraoob, 0xff, extraooblen);
2553
2554 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2555}
2556EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2557
2558/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002559 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002560 * @chip: nand chip info structure
2561 * @buf: buffer to store read data
2562 * @oob_required: caller requires OOB data read to chip->oob_poi
2563 * @page: page number to read
2564 *
2565 * Returns -ENOTSUPP unconditionally.
2566 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002567int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2568 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002569{
2570 return -ENOTSUPP;
2571}
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002572
2573/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002574 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002575 * @chip: nand chip info structure
2576 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002577 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002578 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002579 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002580 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002581 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002582int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2583 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002584{
Boris Brezillonb9761682018-09-06 14:05:20 +02002585 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002586 int ret;
2587
Boris Brezillon25f815f2017-11-30 18:01:30 +01002588 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002589 if (ret)
2590 return ret;
2591
2592 if (oob_required) {
2593 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2594 false);
2595 if (ret)
2596 return ret;
2597 }
2598
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002599 return 0;
2600}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002601EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002602
2603/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002604 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002605 * @chip: nand chip info structure
2606 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002607 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002608 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002609 *
2610 * We need a special oob layout and handling even when OOB isn't used.
2611 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002612static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07002613 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002614{
Boris Brezillonb9761682018-09-06 14:05:20 +02002615 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08002616 int eccsize = chip->ecc.size;
2617 int eccbytes = chip->ecc.bytes;
2618 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002619 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002620
Boris Brezillon25f815f2017-11-30 18:01:30 +01002621 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2622 if (ret)
2623 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002624
2625 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002626 ret = nand_read_data_op(chip, buf, eccsize, false);
2627 if (ret)
2628 return ret;
2629
David Brownell52ff49d2009-03-04 12:01:36 -08002630 buf += eccsize;
2631
2632 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002633 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2634 false);
2635 if (ret)
2636 return ret;
2637
David Brownell52ff49d2009-03-04 12:01:36 -08002638 oob += chip->ecc.prepad;
2639 }
2640
Boris Brezillon97d90da2017-11-30 18:01:29 +01002641 ret = nand_read_data_op(chip, oob, eccbytes, false);
2642 if (ret)
2643 return ret;
2644
David Brownell52ff49d2009-03-04 12:01:36 -08002645 oob += eccbytes;
2646
2647 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002648 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2649 false);
2650 if (ret)
2651 return ret;
2652
David Brownell52ff49d2009-03-04 12:01:36 -08002653 oob += chip->ecc.postpad;
2654 }
2655 }
2656
2657 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002658 if (size) {
2659 ret = nand_read_data_op(chip, oob, size, false);
2660 if (ret)
2661 return ret;
2662 }
David Brownell52ff49d2009-03-04 12:01:36 -08002663
2664 return 0;
2665}
2666
2667/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002668 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002669 * @chip: nand chip info structure
2670 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002671 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002672 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002673 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002674static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
2675 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
Boris Brezillonb9761682018-09-06 14:05:20 +02002677 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002678 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002679 int eccbytes = chip->ecc.bytes;
2680 int eccsteps = chip->ecc.steps;
2681 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002682 uint8_t *ecc_calc = chip->ecc.calc_buf;
2683 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002684 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002685
Boris Brezillonb9761682018-09-06 14:05:20 +02002686 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002687
2688 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002689 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002690
Boris Brezillon846031d2016-02-03 20:11:00 +01002691 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2692 chip->ecc.total);
2693 if (ret)
2694 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002695
2696 eccsteps = chip->ecc.steps;
2697 p = buf;
2698
2699 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2700 int stat;
2701
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002702 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07002703 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002704 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002705 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002706 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002707 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2708 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002709 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002710 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01002711}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302714 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002715 * @chip: nand chip info structure
2716 * @data_offs: offset of requested data within the page
2717 * @readlen: data length
2718 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08002719 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01002720 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002721static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
2722 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01002723{
Boris Brezillonb9761682018-09-06 14:05:20 +02002724 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002725 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002726 uint8_t *p;
2727 int data_col_addr, i, gaps = 0;
2728 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
2729 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01002730 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07002731 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01002732 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01002733
Brian Norris7854d3f2011-06-23 14:12:08 -07002734 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01002735 start_step = data_offs / chip->ecc.size;
2736 end_step = (data_offs + readlen - 1) / chip->ecc.size;
2737 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10302738 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01002739
Brian Norris8b6e50c2011-05-25 14:59:01 -07002740 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01002741 datafrag_len = num_steps * chip->ecc.size;
2742 eccfrag_len = num_steps * chip->ecc.bytes;
2743
2744 data_col_addr = start_step * chip->ecc.size;
2745 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01002746 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01002747 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002748 if (ret)
2749 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002750
Brian Norris8b6e50c2011-05-25 14:59:01 -07002751 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01002752 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002753 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01002754
Brian Norris8b6e50c2011-05-25 14:59:01 -07002755 /*
2756 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07002757 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07002758 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002759 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
2760 if (ret)
2761 return ret;
2762
2763 if (oobregion.length < eccfrag_len)
2764 gaps = 1;
2765
Alexey Korolev3d459552008-05-15 17:23:18 +01002766 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002767 ret = nand_change_read_column_op(chip, mtd->writesize,
2768 chip->oob_poi, mtd->oobsize,
2769 false);
2770 if (ret)
2771 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002772 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002773 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002774 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07002775 * about buswidth alignment in read_buf.
2776 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002777 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01002778 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01002779 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002780 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01002781 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
2782 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002783 aligned_len++;
2784
Boris Brezillon97d90da2017-11-30 18:01:29 +01002785 ret = nand_change_read_column_op(chip,
2786 mtd->writesize + aligned_pos,
2787 &chip->oob_poi[aligned_pos],
2788 aligned_len, false);
2789 if (ret)
2790 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002791 }
2792
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002793 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01002794 chip->oob_poi, index, eccfrag_len);
2795 if (ret)
2796 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002797
2798 p = bufpoi + data_col_addr;
2799 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
2800 int stat;
2801
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002802 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002803 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002804 if (stat == -EBADMSG &&
2805 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2806 /* check for empty pages with bitflips */
2807 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002808 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002809 chip->ecc.bytes,
2810 NULL, 0,
2811 chip->ecc.strength);
2812 }
2813
Mike Dunn3f91e942012-04-25 12:06:09 -07002814 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002815 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002816 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01002817 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002818 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2819 }
Alexey Korolev3d459552008-05-15 17:23:18 +01002820 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002821 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01002822}
2823
2824/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002825 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002826 * @chip: nand chip info structure
2827 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002828 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002829 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002830 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002831 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002832 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002833static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
2834 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002835{
Boris Brezillonb9761682018-09-06 14:05:20 +02002836 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002837 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002838 int eccbytes = chip->ecc.bytes;
2839 int eccsteps = chip->ecc.steps;
2840 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002841 uint8_t *ecc_calc = chip->ecc.calc_buf;
2842 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002843 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002844
Boris Brezillon25f815f2017-11-30 18:01:30 +01002845 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2846 if (ret)
2847 return ret;
2848
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002849 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02002850 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002851
2852 ret = nand_read_data_op(chip, p, eccsize, false);
2853 if (ret)
2854 return ret;
2855
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002856 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002857 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002858
2859 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2860 if (ret)
2861 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002862
Boris Brezillon846031d2016-02-03 20:11:00 +01002863 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2864 chip->ecc.total);
2865 if (ret)
2866 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002867
2868 eccsteps = chip->ecc.steps;
2869 p = buf;
2870
2871 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2872 int stat;
2873
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002874 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002875 if (stat == -EBADMSG &&
2876 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2877 /* check for empty pages with bitflips */
2878 stat = nand_check_erased_ecc_chunk(p, eccsize,
2879 &ecc_code[i], eccbytes,
2880 NULL, 0,
2881 chip->ecc.strength);
2882 }
2883
Mike Dunn3f91e942012-04-25 12:06:09 -07002884 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002885 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002886 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002887 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002888 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2889 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002890 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002891 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002892}
2893
2894/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002895 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07002896 * @chip: nand chip info structure
2897 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002898 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002899 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002900 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002901 * Hardware ECC for large page chips, require OOB to be read first. For this
2902 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2903 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2904 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2905 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002906 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002907static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
2908 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002909{
Boris Brezillonb9761682018-09-06 14:05:20 +02002910 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002911 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002912 int eccbytes = chip->ecc.bytes;
2913 int eccsteps = chip->ecc.steps;
2914 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002915 uint8_t *ecc_code = chip->ecc.code_buf;
2916 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002917 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002918
2919 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01002920 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2921 if (ret)
2922 return ret;
2923
2924 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2925 if (ret)
2926 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002927
Boris Brezillon846031d2016-02-03 20:11:00 +01002928 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2929 chip->ecc.total);
2930 if (ret)
2931 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002932
2933 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2934 int stat;
2935
Boris Brezillonec476362018-09-06 14:05:17 +02002936 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002937
2938 ret = nand_read_data_op(chip, p, eccsize, false);
2939 if (ret)
2940 return ret;
2941
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002942 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002943
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002944 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002945 if (stat == -EBADMSG &&
2946 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2947 /* check for empty pages with bitflips */
2948 stat = nand_check_erased_ecc_chunk(p, eccsize,
2949 &ecc_code[i], eccbytes,
2950 NULL, 0,
2951 chip->ecc.strength);
2952 }
2953
Mike Dunn3f91e942012-04-25 12:06:09 -07002954 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002955 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002956 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002957 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002958 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2959 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002960 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002961 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002962}
2963
2964/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002965 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07002966 * @chip: nand chip info structure
2967 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002968 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002969 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002970 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002971 * The hw generator calculates the error syndrome automatically. Therefore we
2972 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002973 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002974static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
2975 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002976{
Boris Brezillonb9761682018-09-06 14:05:20 +02002977 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002978 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002979 int eccbytes = chip->ecc.bytes;
2980 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002981 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002982 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002983 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07002984 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002985
Boris Brezillon25f815f2017-11-30 18:01:30 +01002986 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2987 if (ret)
2988 return ret;
2989
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002990 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2991 int stat;
2992
Boris Brezillonec476362018-09-06 14:05:17 +02002993 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002994
2995 ret = nand_read_data_op(chip, p, eccsize, false);
2996 if (ret)
2997 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002998
2999 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003000 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3001 false);
3002 if (ret)
3003 return ret;
3004
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003005 oob += chip->ecc.prepad;
3006 }
3007
Boris Brezillonec476362018-09-06 14:05:17 +02003008 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003009
3010 ret = nand_read_data_op(chip, oob, eccbytes, false);
3011 if (ret)
3012 return ret;
3013
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003014 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003015
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003016 oob += eccbytes;
3017
3018 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003019 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3020 false);
3021 if (ret)
3022 return ret;
3023
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003024 oob += chip->ecc.postpad;
3025 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003026
3027 if (stat == -EBADMSG &&
3028 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3029 /* check for empty pages with bitflips */
3030 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3031 oob - eccpadbytes,
3032 eccpadbytes,
3033 NULL, 0,
3034 chip->ecc.strength);
3035 }
3036
3037 if (stat < 0) {
3038 mtd->ecc_stats.failed++;
3039 } else {
3040 mtd->ecc_stats.corrected += stat;
3041 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3042 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003043 }
3044
3045 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003046 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003047 if (i) {
3048 ret = nand_read_data_op(chip, oob, i, false);
3049 if (ret)
3050 return ret;
3051 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003052
Mike Dunn3f91e942012-04-25 12:06:09 -07003053 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003054}
3055
3056/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003057 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon08136212018-11-11 08:55:03 +01003058 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003059 * @oob: oob destination address
3060 * @ops: oob ops structure
3061 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003062 */
Boris Brezillon08136212018-11-11 08:55:03 +01003063static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003064 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003065{
Boris Brezillon08136212018-11-11 08:55:03 +01003066 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003067 int ret;
3068
Florian Fainellif8ac0412010-09-07 13:23:43 +02003069 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003070
Brian Norris0612b9d2011-08-30 18:45:40 -07003071 case MTD_OPS_PLACE_OOB:
3072 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003073 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3074 return oob + len;
3075
Boris Brezillon846031d2016-02-03 20:11:00 +01003076 case MTD_OPS_AUTO_OOB:
3077 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3078 ops->ooboffs, len);
3079 BUG_ON(ret);
3080 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003081
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003082 default:
3083 BUG();
3084 }
3085 return NULL;
3086}
3087
3088/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003089 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003090 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003091 * @retry_mode: the retry mode to use
3092 *
3093 * Some vendors supply a special command to shift the Vt threshold, to be used
3094 * when there are too many bitflips in a page (i.e., ECC error). After setting
3095 * a new threshold, the host should retry reading the page.
3096 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003097static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003098{
Brian Norrisba84fb52014-01-03 15:13:33 -08003099 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3100
3101 if (retry_mode >= chip->read_retries)
3102 return -EINVAL;
3103
3104 if (!chip->setup_read_retry)
3105 return -EOPNOTSUPP;
3106
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003107 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003108}
3109
Boris Brezillon85e08e52018-07-27 09:44:17 +02003110static void nand_wait_readrdy(struct nand_chip *chip)
3111{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003112 const struct nand_sdr_timings *sdr;
3113
Boris Brezillon85e08e52018-07-27 09:44:17 +02003114 if (!(chip->options & NAND_NEED_READRDY))
3115 return;
3116
Boris Brezillon52f05b62018-07-27 09:44:18 +02003117 sdr = nand_get_sdr_timings(&chip->data_interface);
3118 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003119}
3120
Brian Norrisba84fb52014-01-03 15:13:33 -08003121/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003122 * nand_do_read_ops - [INTERN] Read data with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003123 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003124 * @from: offset to read from
3125 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003126 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003127 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003128 */
Boris Brezillon08136212018-11-11 08:55:03 +01003129static int nand_do_read_ops(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003130 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003131{
Brian Norrise47f3db2012-05-02 10:14:56 -07003132 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris Brezillon08136212018-11-11 08:55:03 +01003133 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003134 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003135 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003136 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003137 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003138
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003139 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003140 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003141 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003142 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003143 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003145 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003146 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003148 realpage = (int)(from >> chip->page_shift);
3149 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003151 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003153 buf = ops->datbuf;
3154 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003155 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003156
Florian Fainellif8ac0412010-09-07 13:23:43 +02003157 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003158 unsigned int ecc_failures = mtd->ecc_stats.failed;
3159
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003160 bytes = min(mtd->writesize - col, readlen);
3161 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003162
Kamal Dasu66507c72014-05-01 20:51:19 -04003163 if (!aligned)
3164 use_bufpoi = 1;
3165 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003166 use_bufpoi = !virt_addr_valid(buf) ||
3167 !IS_ALIGNED((unsigned long)buf,
3168 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003169 else
3170 use_bufpoi = 0;
3171
Brian Norris8b6e50c2011-05-25 14:59:01 -07003172 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003173 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003174 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003175
3176 if (use_bufpoi && aligned)
3177 pr_debug("%s: using read bounce buffer for buf@%p\n",
3178 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Brian Norrisba84fb52014-01-03 15:13:33 -08003180read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003181 /*
3182 * Now read the page into the buffer. Absent an error,
3183 * the read methods return max bitflips per ecc step.
3184 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003185 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003186 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003187 oob_required,
3188 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003189 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3190 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003191 ret = chip->ecc.read_subpage(chip, col, bytes,
3192 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003193 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003194 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003195 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003196 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003197 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003198 /* Invalidate page cache */
3199 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003200 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003201 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003202
3203 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003204 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003205 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003206 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003207 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003208 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003209 chip->pagebuf_bitflips = ret;
3210 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003211 /* Invalidate page cache */
3212 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003213 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003214 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003216
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003217 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003218 int toread = min(oobreadlen, max_oobsize);
3219
3220 if (toread) {
Boris Brezillon08136212018-11-11 08:55:03 +01003221 oob = nand_transfer_oob(chip, oob, ops,
3222 toread);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003223 oobreadlen -= toread;
3224 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003225 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003226
Boris Brezillon85e08e52018-07-27 09:44:17 +02003227 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003228
Brian Norrisba84fb52014-01-03 15:13:33 -08003229 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003230 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003231 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003232 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003233 retry_mode);
3234 if (ret < 0)
3235 break;
3236
3237 /* Reset failures; retry */
3238 mtd->ecc_stats.failed = ecc_failures;
3239 goto read_retry;
3240 } else {
3241 /* No more retry modes; real failure */
3242 ecc_fail = true;
3243 }
3244 }
3245
3246 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003247 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003248 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003249 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003250 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003251 max_bitflips = max_t(unsigned int, max_bitflips,
3252 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003255 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003256
Brian Norrisba84fb52014-01-03 15:13:33 -08003257 /* Reset to retry mode 0 */
3258 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003259 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003260 if (ret < 0)
3261 break;
3262 retry_mode = 0;
3263 }
3264
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003265 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Brian Norris8b6e50c2011-05-25 14:59:01 -07003268 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 col = 0;
3270 /* Increment page address */
3271 realpage++;
3272
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003273 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 /* Check, if we cross a chip boundary */
3275 if (!page) {
3276 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003277 nand_deselect_target(chip);
3278 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003281 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003283 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003284 if (oob)
3285 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Mike Dunn3f91e942012-04-25 12:06:09 -07003287 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003288 return ret;
3289
Brian Norrisb72f3df2013-12-03 11:04:14 -08003290 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003291 return -EBADMSG;
3292
Mike Dunnedbc45402012-04-25 12:06:11 -07003293 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003294}
3295
3296/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003297 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003298 * @chip: nand chip info structure
3299 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003300 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003301int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003302{
Boris Brezillonb9761682018-09-06 14:05:20 +02003303 struct mtd_info *mtd = nand_to_mtd(chip);
3304
Boris Brezillon97d90da2017-11-30 18:01:29 +01003305 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003306}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003307EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003308
3309/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003310 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003311 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003312 * @chip: nand chip info structure
3313 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003314 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003315static int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003316{
Boris Brezillonb9761682018-09-06 14:05:20 +02003317 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003318 int length = mtd->oobsize;
3319 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3320 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003321 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003322 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003323
Boris Brezillon97d90da2017-11-30 18:01:29 +01003324 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3325 if (ret)
3326 return ret;
3327
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003328 for (i = 0; i < chip->ecc.steps; i++) {
3329 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003330 int ret;
3331
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003332 pos = eccsize + i * (eccsize + chunk);
3333 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003334 ret = nand_change_read_column_op(chip, pos,
3335 NULL, 0,
3336 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003337 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003338 ret = nand_read_page_op(chip, page, pos, NULL,
3339 0);
3340
3341 if (ret)
3342 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003343 } else
3344 sndrnd = 1;
3345 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003346
3347 ret = nand_read_data_op(chip, bufpoi, toread, false);
3348 if (ret)
3349 return ret;
3350
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003351 bufpoi += toread;
3352 length -= toread;
3353 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003354 if (length > 0) {
3355 ret = nand_read_data_op(chip, bufpoi, length, false);
3356 if (ret)
3357 return ret;
3358 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003359
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003360 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003361}
3362
3363/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003364 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003365 * @chip: nand chip info structure
3366 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003367 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003368int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003369{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003370 struct mtd_info *mtd = nand_to_mtd(chip);
3371
Boris Brezillon97d90da2017-11-30 18:01:29 +01003372 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3373 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003374}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003375EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003376
3377/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003378 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003379 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003380 * @chip: nand chip info structure
3381 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003382 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003383static int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003384{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003385 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003386 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3387 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003388 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003389 const uint8_t *bufpoi = chip->oob_poi;
3390
3391 /*
3392 * data-ecc-data-ecc ... ecc-oob
3393 * or
3394 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3395 */
3396 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3397 pos = steps * (eccsize + chunk);
3398 steps = 0;
3399 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003400 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003401
Boris Brezillon97d90da2017-11-30 18:01:29 +01003402 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3403 if (ret)
3404 return ret;
3405
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003406 for (i = 0; i < steps; i++) {
3407 if (sndcmd) {
3408 if (mtd->writesize <= 512) {
3409 uint32_t fill = 0xFFFFFFFF;
3410
3411 len = eccsize;
3412 while (len > 0) {
3413 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003414
3415 ret = nand_write_data_op(chip, &fill,
3416 num, false);
3417 if (ret)
3418 return ret;
3419
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003420 len -= num;
3421 }
3422 } else {
3423 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003424 ret = nand_change_write_column_op(chip, pos,
3425 NULL, 0,
3426 false);
3427 if (ret)
3428 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003429 }
3430 } else
3431 sndcmd = 1;
3432 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003433
3434 ret = nand_write_data_op(chip, bufpoi, len, false);
3435 if (ret)
3436 return ret;
3437
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003438 bufpoi += len;
3439 length -= len;
3440 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003441 if (length > 0) {
3442 ret = nand_write_data_op(chip, bufpoi, length, false);
3443 if (ret)
3444 return ret;
3445 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003446
Boris Brezillon97d90da2017-11-30 18:01:29 +01003447 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003448}
3449
3450/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003451 * nand_do_read_oob - [INTERN] NAND read out-of-band
Boris Brezillon08136212018-11-11 08:55:03 +01003452 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003453 * @from: offset to read from
3454 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003456 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 */
Boris Brezillon08136212018-11-11 08:55:03 +01003458static int nand_do_read_oob(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003459 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
Boris Brezillon08136212018-11-11 08:55:03 +01003461 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003462 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003463 int page, realpage, chipnr;
Brian Norris041e4572011-06-23 16:45:24 -07003464 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003465 int readlen = ops->ooblen;
3466 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003467 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003468 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Brian Norris289c0522011-07-19 10:06:09 -07003470 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303471 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Brian Norris041e4572011-06-23 16:45:24 -07003473 stats = mtd->ecc_stats;
3474
Boris BREZILLON29f10582016-03-07 10:46:52 +01003475 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003476
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003477 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003478 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003480 /* Shift to get page */
3481 realpage = (int)(from >> chip->page_shift);
3482 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Florian Fainellif8ac0412010-09-07 13:23:43 +02003484 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003485 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003486 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003487 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003488 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003489
3490 if (ret < 0)
3491 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003492
3493 len = min(len, readlen);
Boris Brezillon08136212018-11-11 08:55:03 +01003494 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003495
Boris Brezillon85e08e52018-07-27 09:44:17 +02003496 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003497
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003498 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3499
Vitaly Wool70145682006-11-03 18:20:38 +03003500 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003501 if (!readlen)
3502 break;
3503
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003504 /* Increment page address */
3505 realpage++;
3506
3507 page = realpage & chip->pagemask;
3508 /* Check, if we cross a chip boundary */
3509 if (!page) {
3510 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003511 nand_deselect_target(chip);
3512 nand_select_target(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003515 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003517 ops->oobretlen = ops->ooblen - readlen;
3518
3519 if (ret < 0)
3520 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003521
3522 if (mtd->ecc_stats.failed - stats.failed)
3523 return -EBADMSG;
3524
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003525 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
3527
3528/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003529 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003530 * @mtd: MTD device structure
3531 * @from: offset to read from
3532 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003534 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003536static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3537 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
Boris Brezillon08136212018-11-11 08:55:03 +01003539 struct nand_chip *chip = mtd_to_nand(mtd);
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003540 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003541
3542 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003544 if (ops->mode != MTD_OPS_PLACE_OOB &&
3545 ops->mode != MTD_OPS_AUTO_OOB &&
3546 ops->mode != MTD_OPS_RAW)
3547 return -ENOTSUPP;
3548
Boris Brezillon013e6292018-11-20 11:57:20 +01003549 ret = nand_get_device(chip);
3550 if (ret)
3551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003553 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01003554 ret = nand_do_read_oob(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003555 else
Boris Brezillon08136212018-11-11 08:55:03 +01003556 ret = nand_do_read_ops(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003557
Boris Brezillon08136212018-11-11 08:55:03 +01003558 nand_release_device(chip);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003559 return ret;
3560}
3561
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003562/**
3563 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003564 * @chip: nand chip info structure
3565 * @buf: data buffer
3566 * @oob_required: must write chip->oob_poi to OOB
3567 * @page: page number to write
3568 *
3569 * Returns -ENOTSUPP unconditionally.
3570 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003571int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3572 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003573{
3574 return -ENOTSUPP;
3575}
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003576
3577/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003578 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003579 * @chip: nand chip info structure
3580 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003581 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003582 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003583 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003584 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003585 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003586int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3587 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003588{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003589 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003590 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003591
Boris Brezillon25f815f2017-11-30 18:01:30 +01003592 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003593 if (ret)
3594 return ret;
3595
3596 if (oob_required) {
3597 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3598 false);
3599 if (ret)
3600 return ret;
3601 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003602
Boris Brezillon25f815f2017-11-30 18:01:30 +01003603 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003605EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003607/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003608 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003609 * @chip: nand chip info structure
3610 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003611 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003612 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003613 *
3614 * We need a special oob layout and handling even when ECC isn't checked.
3615 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003616static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003617 const uint8_t *buf, int oob_required,
3618 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003619{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003620 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003621 int eccsize = chip->ecc.size;
3622 int eccbytes = chip->ecc.bytes;
3623 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003624 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003625
Boris Brezillon25f815f2017-11-30 18:01:30 +01003626 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3627 if (ret)
3628 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003629
3630 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003631 ret = nand_write_data_op(chip, buf, eccsize, false);
3632 if (ret)
3633 return ret;
3634
David Brownell52ff49d2009-03-04 12:01:36 -08003635 buf += eccsize;
3636
3637 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003638 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3639 false);
3640 if (ret)
3641 return ret;
3642
David Brownell52ff49d2009-03-04 12:01:36 -08003643 oob += chip->ecc.prepad;
3644 }
3645
Boris Brezillon97d90da2017-11-30 18:01:29 +01003646 ret = nand_write_data_op(chip, oob, eccbytes, false);
3647 if (ret)
3648 return ret;
3649
David Brownell52ff49d2009-03-04 12:01:36 -08003650 oob += eccbytes;
3651
3652 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003653 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3654 false);
3655 if (ret)
3656 return ret;
3657
David Brownell52ff49d2009-03-04 12:01:36 -08003658 oob += chip->ecc.postpad;
3659 }
3660 }
3661
3662 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003663 if (size) {
3664 ret = nand_write_data_op(chip, oob, size, false);
3665 if (ret)
3666 return ret;
3667 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003668
Boris Brezillon25f815f2017-11-30 18:01:30 +01003669 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003670}
3671/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003672 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003673 * @chip: nand chip info structure
3674 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003675 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003676 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003677 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003678static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
3679 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003680{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003681 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003682 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003683 int eccbytes = chip->ecc.bytes;
3684 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003685 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003686 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003687
Brian Norris7854d3f2011-06-23 14:12:08 -07003688 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003689 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003690 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003691
Boris Brezillon846031d2016-02-03 20:11:00 +01003692 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3693 chip->ecc.total);
3694 if (ret)
3695 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003696
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003697 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003698}
3699
3700/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003701 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003702 * @chip: nand chip info structure
3703 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003704 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003705 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003706 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003707static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
3708 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003709{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003710 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003711 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003712 int eccbytes = chip->ecc.bytes;
3713 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003714 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003715 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003716
Boris Brezillon25f815f2017-11-30 18:01:30 +01003717 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3718 if (ret)
3719 return ret;
3720
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003721 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003722 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003723
3724 ret = nand_write_data_op(chip, p, eccsize, false);
3725 if (ret)
3726 return ret;
3727
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003728 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003729 }
3730
Boris Brezillon846031d2016-02-03 20:11:00 +01003731 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3732 chip->ecc.total);
3733 if (ret)
3734 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003735
Boris Brezillon97d90da2017-11-30 18:01:29 +01003736 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3737 if (ret)
3738 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003739
Boris Brezillon25f815f2017-11-30 18:01:30 +01003740 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003741}
3742
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303743
3744/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08003745 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303746 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07003747 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303748 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07003749 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303750 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003751 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303752 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003753static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
3754 uint32_t data_len, const uint8_t *buf,
3755 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303756{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003757 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303758 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003759 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303760 int ecc_size = chip->ecc.size;
3761 int ecc_bytes = chip->ecc.bytes;
3762 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303763 uint32_t start_step = offset / ecc_size;
3764 uint32_t end_step = (offset + data_len - 1) / ecc_size;
3765 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01003766 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303767
Boris Brezillon25f815f2017-11-30 18:01:30 +01003768 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3769 if (ret)
3770 return ret;
3771
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303772 for (step = 0; step < ecc_steps; step++) {
3773 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02003774 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303775
3776 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003777 ret = nand_write_data_op(chip, buf, ecc_size, false);
3778 if (ret)
3779 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303780
3781 /* mask ECC of un-touched subpages by padding 0xFF */
3782 if ((step < start_step) || (step > end_step))
3783 memset(ecc_calc, 0xff, ecc_bytes);
3784 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003785 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303786
3787 /* mask OOB of un-touched subpages by padding 0xFF */
3788 /* if oob_required, preserve OOB metadata of written subpage */
3789 if (!oob_required || (step < start_step) || (step > end_step))
3790 memset(oob_buf, 0xff, oob_bytes);
3791
Brian Norrisd6a950802013-08-08 17:16:36 -07003792 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303793 ecc_calc += ecc_bytes;
3794 oob_buf += oob_bytes;
3795 }
3796
3797 /* copy calculated ECC for whole page to chip->buffer->oob */
3798 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003799 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01003800 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3801 chip->ecc.total);
3802 if (ret)
3803 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303804
3805 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003806 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3807 if (ret)
3808 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303809
Boris Brezillon25f815f2017-11-30 18:01:30 +01003810 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303811}
3812
3813
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003814/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003815 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003816 * @chip: nand chip info structure
3817 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003818 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003819 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003820 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003821 * The hw generator calculates the error syndrome automatically. Therefore we
3822 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003823 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003824static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
3825 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003826{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003827 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003828 int i, eccsize = chip->ecc.size;
3829 int eccbytes = chip->ecc.bytes;
3830 int eccsteps = chip->ecc.steps;
3831 const uint8_t *p = buf;
3832 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003833 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003834
Boris Brezillon25f815f2017-11-30 18:01:30 +01003835 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3836 if (ret)
3837 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003838
3839 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003840 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003841
3842 ret = nand_write_data_op(chip, p, eccsize, false);
3843 if (ret)
3844 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003845
3846 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003847 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3848 false);
3849 if (ret)
3850 return ret;
3851
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003852 oob += chip->ecc.prepad;
3853 }
3854
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003855 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003856
3857 ret = nand_write_data_op(chip, oob, eccbytes, false);
3858 if (ret)
3859 return ret;
3860
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003861 oob += eccbytes;
3862
3863 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003864 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3865 false);
3866 if (ret)
3867 return ret;
3868
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003869 oob += chip->ecc.postpad;
3870 }
3871 }
3872
3873 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003874 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003875 if (i) {
3876 ret = nand_write_data_op(chip, oob, i, false);
3877 if (ret)
3878 return ret;
3879 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003880
Boris Brezillon25f815f2017-11-30 18:01:30 +01003881 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003882}
3883
3884/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003885 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07003886 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303887 * @offset: address offset within the page
3888 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07003889 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07003890 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07003891 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003892 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003893 */
Boris Brezillon08136212018-11-11 08:55:03 +01003894static int nand_write_page(struct nand_chip *chip, uint32_t offset,
3895 int data_len, const uint8_t *buf, int oob_required,
3896 int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003897{
Boris Brezillon08136212018-11-11 08:55:03 +01003898 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303899 int status, subpage;
3900
3901 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3902 chip->ecc.write_subpage)
3903 subpage = offset || (data_len < mtd->writesize);
3904 else
3905 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003906
David Woodhouse956e9442006-09-25 17:12:39 +01003907 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003908 status = chip->ecc.write_page_raw(chip, buf, oob_required,
3909 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303910 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003911 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
3912 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003913 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003914 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08003915
3916 if (status < 0)
3917 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003918
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003919 return 0;
3920}
3921
Florian Fainellif8ac0412010-09-07 13:23:43 +02003922#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003923
3924/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003925 * nand_do_write_ops - [INTERN] NAND write with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003926 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003927 * @to: offset to write to
3928 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003929 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003930 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003931 */
Boris Brezillon08136212018-11-11 08:55:03 +01003932static int nand_do_write_ops(struct nand_chip *chip, loff_t to,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003933 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003934{
Boris Brezillon08136212018-11-11 08:55:03 +01003935 struct mtd_info *mtd = nand_to_mtd(chip);
Corentin Labbe73600b62017-09-02 10:49:38 +02003936 int chipnr, realpage, page, column;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003937 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02003938
3939 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003940 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003941
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003942 uint8_t *oob = ops->oobbuf;
3943 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303944 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07003945 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003946
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003947 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003948 if (!writelen)
3949 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003950
Brian Norris8b6e50c2011-05-25 14:59:01 -07003951 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003952 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003953 pr_notice("%s: attempt to write non page aligned data\n",
3954 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003955 return -EINVAL;
3956 }
3957
Thomas Gleixner29072b92006-09-28 15:38:36 +02003958 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003959
Thomas Gleixner6a930962006-06-28 00:11:45 +02003960 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003961 nand_select_target(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02003962
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003963 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01003964 if (nand_check_wp(chip)) {
Huang Shijieb0bb6902012-11-19 14:43:29 +08003965 ret = -EIO;
3966 goto err_out;
3967 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003968
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003969 realpage = (int)(to >> chip->page_shift);
3970 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003971
3972 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07003973 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3974 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003975 chip->pagebuf = -1;
3976
Maxim Levitsky782ce792010-02-22 20:39:36 +02003977 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003978 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3979 ret = -EINVAL;
3980 goto err_out;
3981 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02003982
Florian Fainellif8ac0412010-09-07 13:23:43 +02003983 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003984 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003985 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003986 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02003987 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02003988
Kamal Dasu66507c72014-05-01 20:51:19 -04003989 if (part_pagewr)
3990 use_bufpoi = 1;
3991 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003992 use_bufpoi = !virt_addr_valid(buf) ||
3993 !IS_ALIGNED((unsigned long)buf,
3994 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003995 else
3996 use_bufpoi = 0;
3997
3998 /* Partial page write?, or need to use bounce buffer */
3999 if (use_bufpoi) {
4000 pr_debug("%s: using write bounce buffer for buf@%p\n",
4001 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004002 if (part_pagewr)
4003 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004004 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004005 memset(chip->data_buf, 0xff, mtd->writesize);
4006 memcpy(&chip->data_buf[column], buf, bytes);
4007 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004008 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004009
Maxim Levitsky782ce792010-02-22 20:39:36 +02004010 if (unlikely(oob)) {
4011 size_t len = min(oobwritelen, oobmaxlen);
Boris Brezillon08136212018-11-11 08:55:03 +01004012 oob = nand_fill_oob(chip, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004013 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004014 } else {
4015 /* We still need to erase leftover OOB data */
4016 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004017 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004018
Boris Brezillon08136212018-11-11 08:55:03 +01004019 ret = nand_write_page(chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004020 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004021 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004022 if (ret)
4023 break;
4024
4025 writelen -= bytes;
4026 if (!writelen)
4027 break;
4028
Thomas Gleixner29072b92006-09-28 15:38:36 +02004029 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004030 buf += bytes;
4031 realpage++;
4032
4033 page = realpage & chip->pagemask;
4034 /* Check, if we cross a chip boundary */
4035 if (!page) {
4036 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004037 nand_deselect_target(chip);
4038 nand_select_target(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004039 }
4040 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004041
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004042 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004043 if (unlikely(oob))
4044 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004045
4046err_out:
Boris Brezillon1d017852018-11-11 08:55:14 +01004047 nand_deselect_target(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004048 return ret;
4049}
4050
4051/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004052 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004053 * @mtd: MTD device structure
4054 * @to: offset to write to
4055 * @len: number of bytes to write
4056 * @retlen: pointer to variable to store the number of written bytes
4057 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004058 *
4059 * NAND write with ECC. Used when performing writes in interrupt context, this
4060 * may for example be called by mtdoops when writing an oops while in panic.
4061 */
4062static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4063 size_t *retlen, const uint8_t *buf)
4064{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004065 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004066 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004067 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004068 int ret;
4069
Boris Brezillon1d017852018-11-11 08:55:14 +01004070 nand_select_target(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004071
4072 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004073 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004074
Brian Norris0ec56dc2015-02-28 02:02:30 -08004075 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004076 ops.len = len;
4077 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004078 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004079
Boris Brezillon08136212018-11-11 08:55:03 +01004080 ret = nand_do_write_ops(chip, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004081
Brian Norris4a89ff82011-08-30 18:45:45 -07004082 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004083 return ret;
4084}
4085
4086/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004087 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004088 * @mtd: MTD device structure
4089 * @to: offset to write to
4090 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004091 */
4092static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4093 struct mtd_oob_ops *ops)
4094{
Boris Brezillon08136212018-11-11 08:55:03 +01004095 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004096 int ret = -ENOTSUPP;
4097
4098 ops->retlen = 0;
4099
Boris Brezillon013e6292018-11-20 11:57:20 +01004100 ret = nand_get_device(chip);
4101 if (ret)
4102 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004103
Florian Fainellif8ac0412010-09-07 13:23:43 +02004104 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004105 case MTD_OPS_PLACE_OOB:
4106 case MTD_OPS_AUTO_OOB:
4107 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004108 break;
4109
4110 default:
4111 goto out;
4112 }
4113
4114 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01004115 ret = nand_do_write_oob(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004116 else
Boris Brezillon08136212018-11-11 08:55:03 +01004117 ret = nand_do_write_ops(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004118
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004119out:
Boris Brezillon08136212018-11-11 08:55:03 +01004120 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 return ret;
4122}
4123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004126 * @mtd: MTD device structure
4127 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004129 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004131static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004133 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004137 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004138 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004139 * @instr: erase instruction
4140 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004142 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004144int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004145 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
Masahiro Yamada2d73f3d2019-01-21 15:32:07 +09004147 int page, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004148 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Brian Norris289c0522011-07-19 10:06:09 -07004150 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4151 __func__, (unsigned long long)instr->addr,
4152 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
Boris Brezillon08136212018-11-11 08:55:03 +01004154 if (check_offs_len(chip, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 /* Grab the lock and see if the device is available */
Boris Brezillon013e6292018-11-20 11:57:20 +01004158 ret = nand_get_device(chip);
4159 if (ret)
4160 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
4162 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004163 page = (int)(instr->addr >> chip->page_shift);
4164 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
4166 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004167 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169 /* Select the NAND device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004170 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01004173 if (nand_check_wp(chip)) {
Brian Norris289c0522011-07-19 10:06:09 -07004174 pr_debug("%s: device is write protected!\n",
4175 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004176 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 goto erase_exit;
4178 }
4179
4180 /* Loop through the pages */
4181 len = instr->len;
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004184 /* Check if we have a bad block, we do not erase bad blocks! */
Boris Brezillon08136212018-11-11 08:55:03 +01004185 if (nand_block_checkbad(chip, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304186 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004187 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4188 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004189 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 goto erase_exit;
4191 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004192
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004193 /*
4194 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004195 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004196 */
4197 if (page <= chip->pagebuf && chip->pagebuf <
4198 (page + pages_per_block))
4199 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Masahiro Yamada2d73f3d2019-01-21 15:32:07 +09004201 ret = nand_erase_op(chip, (page & chip->pagemask) >>
4202 (chip->phys_erase_shift - chip->page_shift));
4203 if (ret) {
Brian Norris289c0522011-07-19 10:06:09 -07004204 pr_debug("%s: failed erase, page 0x%08x\n",
4205 __func__, page);
Adrian Hunter69423d92008-12-10 13:37:21 +00004206 instr->fail_addr =
4207 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 goto erase_exit;
4209 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004210
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004212 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 page += pages_per_block;
4214
4215 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004216 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004218 nand_deselect_target(chip);
4219 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 }
4221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004223 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004224erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004227 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004228 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 /* Return more or less happy */
4231 return ret;
4232}
4233
4234/**
4235 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004236 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004238 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004240static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241{
Boris Brezillon08136212018-11-11 08:55:03 +01004242 struct nand_chip *chip = mtd_to_nand(mtd);
4243
Brian Norris289c0522011-07-19 10:06:09 -07004244 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
4246 /* Grab the lock and see if the device is available */
Boris Brezillon013e6292018-11-20 11:57:20 +01004247 WARN_ON(nand_get_device(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 /* Release it and go back */
Boris Brezillon08136212018-11-11 08:55:03 +01004249 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250}
4251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004253 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004254 * @mtd: MTD device structure
4255 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004257static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304259 struct nand_chip *chip = mtd_to_nand(mtd);
4260 int chipnr = (int)(offs >> chip->chip_shift);
4261 int ret;
4262
4263 /* Select the NAND device */
Boris Brezillon013e6292018-11-20 11:57:20 +01004264 ret = nand_get_device(chip);
4265 if (ret)
4266 return ret;
4267
Boris Brezillon1d017852018-11-11 08:55:14 +01004268 nand_select_target(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304269
Boris Brezillon08136212018-11-11 08:55:03 +01004270 ret = nand_block_checkbad(chip, offs, 0);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304271
Boris Brezillon1d017852018-11-11 08:55:14 +01004272 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004273 nand_release_device(chip);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304274
4275 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276}
4277
4278/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004279 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004280 * @mtd: MTD device structure
4281 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004283static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 int ret;
4286
Florian Fainellif8ac0412010-09-07 13:23:43 +02004287 ret = nand_block_isbad(mtd, ofs);
4288 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004289 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 if (ret > 0)
4291 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004292 return ret;
4293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Boris Brezillon08136212018-11-11 08:55:03 +01004295 return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
4298/**
Zach Brown56718422017-01-10 13:30:20 -06004299 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4300 * @mtd: MTD device structure
4301 * @ofs: offset relative to mtd start
4302 * @len: length of mtd
4303 */
4304static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4305{
4306 struct nand_chip *chip = mtd_to_nand(mtd);
4307 u32 part_start_block;
4308 u32 part_end_block;
4309 u32 part_start_die;
4310 u32 part_end_die;
4311
4312 /*
4313 * max_bb_per_die and blocks_per_die used to determine
4314 * the maximum bad block count.
4315 */
4316 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4317 return -ENOTSUPP;
4318
4319 /* Get the start and end of the partition in erase blocks. */
4320 part_start_block = mtd_div_by_eb(ofs, mtd);
4321 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4322
4323 /* Get the start and end LUNs of the partition. */
4324 part_start_die = part_start_block / chip->blocks_per_die;
4325 part_end_die = part_end_block / chip->blocks_per_die;
4326
4327 /*
4328 * Look up the bad blocks per unit and multiply by the number of units
4329 * that the partition spans.
4330 */
4331 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4332}
4333
4334/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004335 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004336 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004337 */
4338static int nand_suspend(struct mtd_info *mtd)
4339{
Boris Brezillon013e6292018-11-20 11:57:20 +01004340 struct nand_chip *chip = mtd_to_nand(mtd);
4341
4342 mutex_lock(&chip->lock);
4343 chip->suspended = 1;
4344 mutex_unlock(&chip->lock);
4345
4346 return 0;
Vitaly Wool962034f2005-09-15 14:58:53 +01004347}
4348
4349/**
4350 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004351 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004352 */
4353static void nand_resume(struct mtd_info *mtd)
4354{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004355 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004356
Boris Brezillon013e6292018-11-20 11:57:20 +01004357 mutex_lock(&chip->lock);
4358 if (chip->suspended)
4359 chip->suspended = 0;
Vitaly Wool962034f2005-09-15 14:58:53 +01004360 else
Brian Norrisd0370212011-07-19 10:06:08 -07004361 pr_err("%s called for a chip which is not in suspended state\n",
4362 __func__);
Boris Brezillon013e6292018-11-20 11:57:20 +01004363 mutex_unlock(&chip->lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01004364}
4365
Scott Branden72ea4032014-11-20 11:18:05 -08004366/**
4367 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4368 * prevent further operations
4369 * @mtd: MTD device structure
4370 */
4371static void nand_shutdown(struct mtd_info *mtd)
4372{
Boris Brezillon013e6292018-11-20 11:57:20 +01004373 nand_suspend(mtd);
Scott Branden72ea4032014-11-20 11:18:05 -08004374}
4375
Brian Norris8b6e50c2011-05-25 14:59:01 -07004376/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004377static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004378{
Boris Brezillon7b6a9b22018-11-20 10:02:39 +01004379 /* If no controller is provided, use the dummy, legacy one. */
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004380 if (!chip->controller) {
Boris Brezillon7b6a9b22018-11-20 10:02:39 +01004381 chip->controller = &chip->legacy.dummy_controller;
Miquel Raynal7da45132018-07-17 09:08:02 +02004382 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004383 }
4384
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01004385 nand_legacy_set_defaults(chip);
4386
Masahiro Yamada477544c2017-03-30 17:15:05 +09004387 if (!chip->buf_align)
4388 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004389}
4390
Brian Norris8b6e50c2011-05-25 14:59:01 -07004391/* Sanitize ONFI strings so we can safely print them */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004392void sanitize_string(uint8_t *s, size_t len)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004393{
4394 ssize_t i;
4395
Brian Norris8b6e50c2011-05-25 14:59:01 -07004396 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004397 s[len - 1] = 0;
4398
Brian Norris8b6e50c2011-05-25 14:59:01 -07004399 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004400 for (i = 0; i < len - 1; i++) {
4401 if (s[i] < ' ' || s[i] > 127)
4402 s[i] = '?';
4403 }
4404
Brian Norris8b6e50c2011-05-25 14:59:01 -07004405 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004406 strim(s);
4407}
4408
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004409/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07004410 * nand_id_has_period - Check if an ID string has a given wraparound period
4411 * @id_data: the ID string
4412 * @arrlen: the length of the @id_data array
4413 * @period: the period of repitition
4414 *
4415 * Check if an ID string is repeated within a given sequence of bytes at
4416 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08004417 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07004418 * if the repetition has a period of @period; otherwise, returns zero.
4419 */
4420static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4421{
4422 int i, j;
4423 for (i = 0; i < period; i++)
4424 for (j = i + period; j < arrlen; j += period)
4425 if (id_data[i] != id_data[j])
4426 return 0;
4427 return 1;
4428}
4429
4430/*
4431 * nand_id_len - Get the length of an ID string returned by CMD_READID
4432 * @id_data: the ID string
4433 * @arrlen: the length of the @id_data array
4434
4435 * Returns the length of the ID string, according to known wraparound/trailing
4436 * zero patterns. If no pattern exists, returns the length of the array.
4437 */
4438static int nand_id_len(u8 *id_data, int arrlen)
4439{
4440 int last_nonzero, period;
4441
4442 /* Find last non-zero byte */
4443 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4444 if (id_data[last_nonzero])
4445 break;
4446
4447 /* All zeros */
4448 if (last_nonzero < 0)
4449 return 0;
4450
4451 /* Calculate wraparound period */
4452 for (period = 1; period < arrlen; period++)
4453 if (nand_id_has_period(id_data, arrlen, period))
4454 break;
4455
4456 /* There's a repeated pattern */
4457 if (period < arrlen)
4458 return period;
4459
4460 /* There are trailing zeros */
4461 if (last_nonzero < arrlen - 1)
4462 return last_nonzero + 1;
4463
4464 /* No pattern detected */
4465 return arrlen;
4466}
4467
Huang Shijie7db906b2013-09-25 14:58:11 +08004468/* Extract the bits of per cell from the 3rd byte of the extended ID */
4469static int nand_get_bits_per_cell(u8 cellinfo)
4470{
4471 int bits;
4472
4473 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4474 bits >>= NAND_CI_CELLTYPE_SHIFT;
4475 return bits + 1;
4476}
4477
Brian Norrise3b88bd2012-09-24 20:40:52 -07004478/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004479 * Many new NAND share similar device ID codes, which represent the size of the
4480 * chip. The rest of the parameters must be decoded according to generic or
4481 * manufacturer-specific "extended ID" decoding patterns.
4482 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004483void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004484{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004485 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02004486 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004487 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004488 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08004489 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004490 /* The 4th id byte is the important one */
4491 extid = id_data[3];
4492
Boris Brezillon01389b62016-06-08 10:30:18 +02004493 /* Calc pagesize */
4494 mtd->writesize = 1024 << (extid & 0x03);
4495 extid >>= 2;
4496 /* Calc oobsize */
4497 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4498 extid >>= 2;
4499 /* Calc blocksize. Blocksize is multiples of 64KiB */
4500 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4501 extid >>= 2;
4502 /* Get buswidth information */
4503 if (extid & 0x1)
4504 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004505}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004506EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004507
4508/*
Brian Norrisf23a4812012-09-24 20:40:51 -07004509 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4510 * decodes a matching ID table entry and assigns the MTD size parameters for
4511 * the chip.
4512 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004513static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07004514{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004515 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07004516
4517 mtd->erasesize = type->erasesize;
4518 mtd->writesize = type->pagesize;
4519 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07004520
Huang Shijie1c195e92013-09-25 14:58:12 +08004521 /* All legacy ID NAND are small-page, SLC */
4522 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07004523}
4524
4525/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004526 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4527 * heuristic patterns using various detected parameters (e.g., manufacturer,
4528 * page size, cell-type information).
4529 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004530static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07004531{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004532 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004533
4534 /* Set the bad block position */
4535 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4536 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4537 else
4538 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004539}
4540
Huang Shijieec6e87e2013-03-15 11:01:00 +08004541static inline bool is_full_id_nand(struct nand_flash_dev *type)
4542{
4543 return type->id_len;
4544}
4545
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004546static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02004547 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004548{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004549 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02004550 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004551
Huang Shijieec6e87e2013-03-15 11:01:00 +08004552 if (!strncmp(type->id, id_data, type->id_len)) {
4553 mtd->writesize = type->pagesize;
4554 mtd->erasesize = type->erasesize;
4555 mtd->oobsize = type->oobsize;
4556
Huang Shijie7db906b2013-09-25 14:58:11 +08004557 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004558 chip->chipsize = (uint64_t)type->chipsize << 20;
4559 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004560 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4561 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004562 chip->onfi_timing_mode_default =
4563 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004564
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004565 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4566 if (!chip->parameters.model)
4567 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004568
Huang Shijieec6e87e2013-03-15 11:01:00 +08004569 return true;
4570 }
4571 return false;
4572}
4573
Brian Norris7e74c2d2012-09-24 20:40:49 -07004574/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004575 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4576 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4577 * table.
4578 */
4579static void nand_manufacturer_detect(struct nand_chip *chip)
4580{
4581 /*
4582 * Try manufacturer detection if available and use
4583 * nand_decode_ext_id() otherwise.
4584 */
4585 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004586 chip->manufacturer.desc->ops->detect) {
4587 /* The 3rd id byte holds MLC / multichip data */
4588 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004589 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004590 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004591 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004592 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004593}
4594
4595/*
4596 * Manufacturer initialization. This function is called for all NANDs including
4597 * ONFI and JEDEC compliant ones.
4598 * Manufacturer drivers should put all their specific initialization code in
4599 * their ->init() hook.
4600 */
4601static int nand_manufacturer_init(struct nand_chip *chip)
4602{
4603 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4604 !chip->manufacturer.desc->ops->init)
4605 return 0;
4606
4607 return chip->manufacturer.desc->ops->init(chip);
4608}
4609
4610/*
4611 * Manufacturer cleanup. This function is called for all NANDs including
4612 * ONFI and JEDEC compliant ones.
4613 * Manufacturer drivers should put all their specific cleanup code in their
4614 * ->cleanup() hook.
4615 */
4616static void nand_manufacturer_cleanup(struct nand_chip *chip)
4617{
4618 /* Release manufacturer private data */
4619 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4620 chip->manufacturer.desc->ops->cleanup)
4621 chip->manufacturer.desc->ops->cleanup(chip);
4622}
4623
Boris Brezillon348d56a2018-09-07 00:38:48 +02004624static const char *
4625nand_manufacturer_name(const struct nand_manufacturer *manufacturer)
4626{
4627 return manufacturer ? manufacturer->name : "Unknown";
4628}
4629
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004630/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004631 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004632 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004633static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004634{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004635 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004636 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004637 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004638 u8 *id_data = chip->id.data;
4639 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Karl Beldanef89a882008-09-15 14:37:29 +02004641 /*
4642 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004643 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004644 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004645 ret = nand_reset(chip, 0);
4646 if (ret)
4647 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004648
4649 /* Select the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004650 nand_select_target(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004651
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004653 ret = nand_readid_op(chip, 0, id_data, 2);
4654 if (ret)
4655 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
4657 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004658 maf_id = id_data[0];
4659 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Brian Norris8b6e50c2011-05-25 14:59:01 -07004661 /*
4662 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004663 * interface concerns can cause random data which looks like a
4664 * possibly credible NAND flash to appear. If the two results do
4665 * not match, ignore the device completely.
4666 */
4667
Brian Norris4aef9b72012-09-24 20:40:48 -07004668 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004669 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
4670 if (ret)
4671 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01004672
Boris Brezillon7f501f02016-05-24 19:20:05 +02004673 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004674 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004675 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004676 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004677 }
4678
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02004679 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02004680
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004681 /* Try to identify manufacturer */
4682 manufacturer = nand_get_manufacturer(maf_id);
4683 chip->manufacturer.desc = manufacturer;
4684
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004685 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004686 type = nand_flash_ids;
4687
Boris Brezillon29a198a2016-05-24 20:17:48 +02004688 /*
4689 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4690 * override it.
4691 * This is required to make sure initial NAND bus width set by the
4692 * NAND controller driver is coherent with the real NAND bus width
4693 * (extracted by auto-detection code).
4694 */
4695 busw = chip->options & NAND_BUSWIDTH_16;
4696
4697 /*
4698 * The flag is only set (never cleared), reset it to its default value
4699 * before starting auto-detection.
4700 */
4701 chip->options &= ~NAND_BUSWIDTH_16;
4702
Huang Shijieec6e87e2013-03-15 11:01:00 +08004703 for (; type->name != NULL; type++) {
4704 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004705 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004706 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004707 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004708 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004709 }
4710 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004711
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004712 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004713 /* Check if the chip is ONFI compliant */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004714 ret = nand_onfi_detect(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01004715 if (ret < 0)
4716 return ret;
4717 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004718 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004719
4720 /* Check if the chip is JEDEC compliant */
Boris Brezillon8ae3fbf2018-09-07 00:38:51 +02004721 ret = nand_jedec_detect(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01004722 if (ret < 0)
4723 return ret;
4724 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08004725 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004726 }
4727
David Woodhouse5e81e882010-02-26 18:32:56 +00004728 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004729 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004730
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004731 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4732 if (!chip->parameters.model)
4733 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004734
Adrian Hunter69423d92008-12-10 13:37:21 +00004735 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004736
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004737 if (!type->pagesize)
4738 nand_manufacturer_detect(chip);
4739 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004740 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004741
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004742 /* Get chip options */
4743 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004744
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004745ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01004746 if (!mtd->name)
4747 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004748
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004749 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004750 WARN_ON(busw & NAND_BUSWIDTH_16);
4751 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004752 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4753 /*
4754 * Check, if buswidth is correct. Hardware drivers should set
4755 * chip correct!
4756 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004757 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004758 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004759 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4760 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004761 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4762 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004763 ret = -EINVAL;
4764
4765 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004766 }
4767
Boris Brezillon7f501f02016-05-24 19:20:05 +02004768 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004769
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004770 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004771 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004772 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004773 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004774
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004775 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004776 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004777 if (chip->chipsize & 0xffffffff)
4778 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004779 else {
4780 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4781 chip->chip_shift += 32 - 1;
4782 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004783
Masahiro Yamada14157f82017-09-13 11:05:50 +09004784 if (chip->chip_shift - chip->page_shift > 16)
4785 chip->options |= NAND_ROW_ADDR_3;
4786
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004787 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004788
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004789 nand_legacy_adjust_cmdfunc(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004790
Ezequiel Garcia20171642013-11-25 08:30:31 -03004791 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004792 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01004793 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4794 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02004795 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004796 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004797 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004798 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004799
4800free_detect_allocation:
4801 kfree(chip->parameters.model);
4802
4803 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004804}
4805
Boris Brezillond48f62b2016-04-01 14:54:32 +02004806static const char * const nand_ecc_modes[] = {
4807 [NAND_ECC_NONE] = "none",
4808 [NAND_ECC_SOFT] = "soft",
4809 [NAND_ECC_HW] = "hw",
4810 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4811 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004812 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004813};
4814
4815static int of_get_nand_ecc_mode(struct device_node *np)
4816{
4817 const char *pm;
4818 int err, i;
4819
4820 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4821 if (err < 0)
4822 return err;
4823
4824 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4825 if (!strcasecmp(pm, nand_ecc_modes[i]))
4826 return i;
4827
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004828 /*
4829 * For backward compatibility we support few obsoleted values that don't
4830 * have their mappings into nand_ecc_modes_t anymore (they were merged
4831 * with other enums).
4832 */
4833 if (!strcasecmp(pm, "soft_bch"))
4834 return NAND_ECC_SOFT;
4835
Boris Brezillond48f62b2016-04-01 14:54:32 +02004836 return -ENODEV;
4837}
4838
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004839static const char * const nand_ecc_algos[] = {
4840 [NAND_ECC_HAMMING] = "hamming",
4841 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02004842 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004843};
4844
Boris Brezillond48f62b2016-04-01 14:54:32 +02004845static int of_get_nand_ecc_algo(struct device_node *np)
4846{
4847 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004848 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004849
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004850 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4851 if (!err) {
4852 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4853 if (!strcasecmp(pm, nand_ecc_algos[i]))
4854 return i;
4855 return -ENODEV;
4856 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004857
4858 /*
4859 * For backward compatibility we also read "nand-ecc-mode" checking
4860 * for some obsoleted values that were specifying ECC algorithm.
4861 */
4862 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4863 if (err < 0)
4864 return err;
4865
4866 if (!strcasecmp(pm, "soft"))
4867 return NAND_ECC_HAMMING;
4868 else if (!strcasecmp(pm, "soft_bch"))
4869 return NAND_ECC_BCH;
4870
4871 return -ENODEV;
4872}
4873
4874static int of_get_nand_ecc_step_size(struct device_node *np)
4875{
4876 int ret;
4877 u32 val;
4878
4879 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4880 return ret ? ret : val;
4881}
4882
4883static int of_get_nand_ecc_strength(struct device_node *np)
4884{
4885 int ret;
4886 u32 val;
4887
4888 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4889 return ret ? ret : val;
4890}
4891
4892static int of_get_nand_bus_width(struct device_node *np)
4893{
4894 u32 val;
4895
4896 if (of_property_read_u32(np, "nand-bus-width", &val))
4897 return 8;
4898
4899 switch (val) {
4900 case 8:
4901 case 16:
4902 return val;
4903 default:
4904 return -EIO;
4905 }
4906}
4907
4908static bool of_get_nand_on_flash_bbt(struct device_node *np)
4909{
4910 return of_property_read_bool(np, "nand-on-flash-bbt");
4911}
4912
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004913static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004914{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004915 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004916 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004917
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004918 if (!dn)
4919 return 0;
4920
Brian Norris5844fee2015-01-23 00:22:27 -08004921 if (of_get_nand_bus_width(dn) == 16)
4922 chip->options |= NAND_BUSWIDTH_16;
4923
Stefan Agnerf922bd72018-06-24 23:27:23 +02004924 if (of_property_read_bool(dn, "nand-is-boot-medium"))
4925 chip->options |= NAND_IS_BOOT_MEDIUM;
4926
Brian Norris5844fee2015-01-23 00:22:27 -08004927 if (of_get_nand_on_flash_bbt(dn))
4928 chip->bbt_options |= NAND_BBT_USE_FLASH;
4929
4930 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004931 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004932 ecc_strength = of_get_nand_ecc_strength(dn);
4933 ecc_step = of_get_nand_ecc_step_size(dn);
4934
Brian Norris5844fee2015-01-23 00:22:27 -08004935 if (ecc_mode >= 0)
4936 chip->ecc.mode = ecc_mode;
4937
Rafał Miłecki79082452016-03-23 11:19:02 +01004938 if (ecc_algo >= 0)
4939 chip->ecc.algo = ecc_algo;
4940
Brian Norris5844fee2015-01-23 00:22:27 -08004941 if (ecc_strength >= 0)
4942 chip->ecc.strength = ecc_strength;
4943
4944 if (ecc_step > 0)
4945 chip->ecc.size = ecc_step;
4946
Boris Brezillonba78ee02016-06-08 17:04:22 +02004947 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4948 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4949
Brian Norris5844fee2015-01-23 00:22:27 -08004950 return 0;
4951}
4952
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004953/**
Miquel Raynal98732da2018-07-25 15:31:50 +02004954 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02004955 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004956 * @maxchips: number of chips to scan for
4957 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004958 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004959 * This is the first phase of the normal nand_scan() function. It reads the
4960 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004961 *
Miquel Raynal98732da2018-07-25 15:31:50 +02004962 * This helper used to be called directly from controller drivers that needed
4963 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
4964 * prevented dynamic allocations during this phase which was unconvenient and
4965 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004966 */
Boris Brezillon871a4072018-08-04 22:59:22 +02004967static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02004968 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004969{
Boris Brezillon00ad3782018-09-06 14:05:14 +02004970 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02004971 int nand_maf_id, nand_dev_id;
4972 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08004973 int ret;
4974
Boris Brezillonae2294b2018-11-11 08:55:15 +01004975 /* Assume all dies are deselected when we enter nand_scan_ident(). */
4976 chip->cur_cs = -1;
4977
Boris Brezillon013e6292018-11-20 11:57:20 +01004978 mutex_init(&chip->lock);
4979
Miquel Raynal17fa8042017-11-30 18:01:31 +01004980 /* Enforce the right timings for reset/detection */
4981 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
4982
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004983 ret = nand_dt_init(chip);
4984 if (ret)
4985 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004986
Brian Norrisf7a8e382016-01-05 10:39:45 -08004987 if (!mtd->name && mtd->dev.parent)
4988 mtd->name = dev_name(mtd->dev.parent);
4989
Boris Brezillon1d017852018-11-11 08:55:14 +01004990 /*
4991 * Start with chips->numchips = maxchips to let nand_select_target() do
4992 * its job. chip->numchips will be adjusted after.
4993 */
4994 chip->numchips = maxchips;
4995
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004996 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004997 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004998
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01004999 ret = nand_legacy_check_hooks(chip);
5000 if (ret)
5001 return ret;
5002
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005003 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005004 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005005 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005006 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005007 pr_warn("No NAND device found\n");
Boris Brezillon1d017852018-11-11 08:55:14 +01005008 nand_deselect_target(chip);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005009 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 }
5011
Boris Brezillon7f501f02016-05-24 19:20:05 +02005012 nand_maf_id = chip->id.data[0];
5013 nand_dev_id = chip->id.data[1];
5014
Boris Brezillon1d017852018-11-11 08:55:14 +01005015 nand_deselect_target(chip);
Huang Shijie07300162012-11-09 16:23:45 +08005016
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005017 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005018 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005019 u8 id[2];
5020
Karl Beldanef89a882008-09-15 14:37:29 +02005021 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005022 nand_reset(chip, i);
5023
Boris Brezillon1d017852018-11-11 08:55:14 +01005024 nand_select_target(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005026 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005028 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon1d017852018-11-11 08:55:14 +01005029 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 break;
Huang Shijie07300162012-11-09 16:23:45 +08005031 }
Boris Brezillon1d017852018-11-11 08:55:14 +01005032 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 }
5034 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005035 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005036
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005038 chip->numchips = i;
5039 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
David Woodhouse3b85c322006-09-25 17:06:53 +01005041 return 0;
5042}
5043
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005044static void nand_scan_ident_cleanup(struct nand_chip *chip)
5045{
5046 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005047 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005048}
5049
Boris Brezillon08136212018-11-11 08:55:03 +01005050static int nand_set_ecc_soft_ops(struct nand_chip *chip)
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005051{
Boris Brezillon08136212018-11-11 08:55:03 +01005052 struct mtd_info *mtd = nand_to_mtd(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005053 struct nand_ecc_ctrl *ecc = &chip->ecc;
5054
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005055 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005056 return -EINVAL;
5057
5058 switch (ecc->algo) {
5059 case NAND_ECC_HAMMING:
5060 ecc->calculate = nand_calculate_ecc;
5061 ecc->correct = nand_correct_data;
5062 ecc->read_page = nand_read_page_swecc;
5063 ecc->read_subpage = nand_read_subpage;
5064 ecc->write_page = nand_write_page_swecc;
5065 ecc->read_page_raw = nand_read_page_raw;
5066 ecc->write_page_raw = nand_write_page_raw;
5067 ecc->read_oob = nand_read_oob_std;
5068 ecc->write_oob = nand_write_oob_std;
5069 if (!ecc->size)
5070 ecc->size = 256;
5071 ecc->bytes = 3;
5072 ecc->strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +02005073
5074 if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC))
5075 ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
5076
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005077 return 0;
5078 case NAND_ECC_BCH:
5079 if (!mtd_nand_has_bch()) {
5080 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5081 return -EINVAL;
5082 }
5083 ecc->calculate = nand_bch_calculate_ecc;
5084 ecc->correct = nand_bch_correct_data;
5085 ecc->read_page = nand_read_page_swecc;
5086 ecc->read_subpage = nand_read_subpage;
5087 ecc->write_page = nand_write_page_swecc;
5088 ecc->read_page_raw = nand_read_page_raw;
5089 ecc->write_page_raw = nand_write_page_raw;
5090 ecc->read_oob = nand_read_oob_std;
5091 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005092
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005093 /*
5094 * Board driver should supply ecc.size and ecc.strength
5095 * values to select how many bits are correctable.
5096 * Otherwise, default to 4 bits for large page devices.
5097 */
5098 if (!ecc->size && (mtd->oobsize >= 64)) {
5099 ecc->size = 512;
5100 ecc->strength = 4;
5101 }
5102
5103 /*
5104 * if no ecc placement scheme was provided pickup the default
5105 * large page one.
5106 */
5107 if (!mtd->ooblayout) {
5108 /* handle large page devices only */
5109 if (mtd->oobsize < 64) {
5110 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5111 return -EINVAL;
5112 }
5113
5114 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005115
5116 }
5117
5118 /*
5119 * We can only maximize ECC config when the default layout is
5120 * used, otherwise we don't know how many bytes can really be
5121 * used.
5122 */
5123 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5124 ecc->options & NAND_ECC_MAXIMIZE) {
5125 int steps, bytes;
5126
5127 /* Always prefer 1k blocks over 512bytes ones */
5128 ecc->size = 1024;
5129 steps = mtd->writesize / ecc->size;
5130
5131 /* Reserve 2 bytes for the BBM */
5132 bytes = (mtd->oobsize - 2) / steps;
5133 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005134 }
5135
5136 /* See nand_bch_init() for details. */
5137 ecc->bytes = 0;
5138 ecc->priv = nand_bch_init(mtd);
5139 if (!ecc->priv) {
5140 WARN(1, "BCH ECC initialization failed!\n");
5141 return -EINVAL;
5142 }
5143 return 0;
5144 default:
5145 WARN(1, "Unsupported ECC algorithm!\n");
5146 return -EINVAL;
5147 }
5148}
5149
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005150/**
5151 * nand_check_ecc_caps - check the sanity of preset ECC settings
5152 * @chip: nand chip info structure
5153 * @caps: ECC caps info structure
5154 * @oobavail: OOB size that the ECC engine can use
5155 *
5156 * When ECC step size and strength are already set, check if they are supported
5157 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5158 * On success, the calculated ECC bytes is set.
5159 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305160static int
5161nand_check_ecc_caps(struct nand_chip *chip,
5162 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005163{
5164 struct mtd_info *mtd = nand_to_mtd(chip);
5165 const struct nand_ecc_step_info *stepinfo;
5166 int preset_step = chip->ecc.size;
5167 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305168 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005169 int i, j;
5170
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005171 for (i = 0; i < caps->nstepinfos; i++) {
5172 stepinfo = &caps->stepinfos[i];
5173
5174 if (stepinfo->stepsize != preset_step)
5175 continue;
5176
5177 for (j = 0; j < stepinfo->nstrengths; j++) {
5178 if (stepinfo->strengths[j] != preset_strength)
5179 continue;
5180
5181 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5182 preset_strength);
5183 if (WARN_ON_ONCE(ecc_bytes < 0))
5184 return ecc_bytes;
5185
5186 if (ecc_bytes * nsteps > oobavail) {
5187 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5188 preset_step, preset_strength);
5189 return -ENOSPC;
5190 }
5191
5192 chip->ecc.bytes = ecc_bytes;
5193
5194 return 0;
5195 }
5196 }
5197
5198 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5199 preset_step, preset_strength);
5200
5201 return -ENOTSUPP;
5202}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005203
5204/**
5205 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5206 * @chip: nand chip info structure
5207 * @caps: ECC engine caps info structure
5208 * @oobavail: OOB size that the ECC engine can use
5209 *
5210 * If a chip's ECC requirement is provided, try to meet it with the least
5211 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5212 * On success, the chosen ECC settings are set.
5213 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305214static int
5215nand_match_ecc_req(struct nand_chip *chip,
5216 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005217{
5218 struct mtd_info *mtd = nand_to_mtd(chip);
5219 const struct nand_ecc_step_info *stepinfo;
5220 int req_step = chip->ecc_step_ds;
5221 int req_strength = chip->ecc_strength_ds;
5222 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5223 int best_step, best_strength, best_ecc_bytes;
5224 int best_ecc_bytes_total = INT_MAX;
5225 int i, j;
5226
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005227 /* No information provided by the NAND chip */
5228 if (!req_step || !req_strength)
5229 return -ENOTSUPP;
5230
5231 /* number of correctable bits the chip requires in a page */
5232 req_corr = mtd->writesize / req_step * req_strength;
5233
5234 for (i = 0; i < caps->nstepinfos; i++) {
5235 stepinfo = &caps->stepinfos[i];
5236 step_size = stepinfo->stepsize;
5237
5238 for (j = 0; j < stepinfo->nstrengths; j++) {
5239 strength = stepinfo->strengths[j];
5240
5241 /*
5242 * If both step size and strength are smaller than the
5243 * chip's requirement, it is not easy to compare the
5244 * resulted reliability.
5245 */
5246 if (step_size < req_step && strength < req_strength)
5247 continue;
5248
5249 if (mtd->writesize % step_size)
5250 continue;
5251
5252 nsteps = mtd->writesize / step_size;
5253
5254 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5255 if (WARN_ON_ONCE(ecc_bytes < 0))
5256 continue;
5257 ecc_bytes_total = ecc_bytes * nsteps;
5258
5259 if (ecc_bytes_total > oobavail ||
5260 strength * nsteps < req_corr)
5261 continue;
5262
5263 /*
5264 * We assume the best is to meet the chip's requrement
5265 * with the least number of ECC bytes.
5266 */
5267 if (ecc_bytes_total < best_ecc_bytes_total) {
5268 best_ecc_bytes_total = ecc_bytes_total;
5269 best_step = step_size;
5270 best_strength = strength;
5271 best_ecc_bytes = ecc_bytes;
5272 }
5273 }
5274 }
5275
5276 if (best_ecc_bytes_total == INT_MAX)
5277 return -ENOTSUPP;
5278
5279 chip->ecc.size = best_step;
5280 chip->ecc.strength = best_strength;
5281 chip->ecc.bytes = best_ecc_bytes;
5282
5283 return 0;
5284}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005285
5286/**
5287 * nand_maximize_ecc - choose the max ECC strength available
5288 * @chip: nand chip info structure
5289 * @caps: ECC engine caps info structure
5290 * @oobavail: OOB size that the ECC engine can use
5291 *
5292 * Choose the max ECC strength that is supported on the controller, and can fit
5293 * within the chip's OOB. On success, the chosen ECC settings are set.
5294 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305295static int
5296nand_maximize_ecc(struct nand_chip *chip,
5297 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005298{
5299 struct mtd_info *mtd = nand_to_mtd(chip);
5300 const struct nand_ecc_step_info *stepinfo;
5301 int step_size, strength, nsteps, ecc_bytes, corr;
5302 int best_corr = 0;
5303 int best_step = 0;
5304 int best_strength, best_ecc_bytes;
5305 int i, j;
5306
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005307 for (i = 0; i < caps->nstepinfos; i++) {
5308 stepinfo = &caps->stepinfos[i];
5309 step_size = stepinfo->stepsize;
5310
5311 /* If chip->ecc.size is already set, respect it */
5312 if (chip->ecc.size && step_size != chip->ecc.size)
5313 continue;
5314
5315 for (j = 0; j < stepinfo->nstrengths; j++) {
5316 strength = stepinfo->strengths[j];
5317
5318 if (mtd->writesize % step_size)
5319 continue;
5320
5321 nsteps = mtd->writesize / step_size;
5322
5323 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5324 if (WARN_ON_ONCE(ecc_bytes < 0))
5325 continue;
5326
5327 if (ecc_bytes * nsteps > oobavail)
5328 continue;
5329
5330 corr = strength * nsteps;
5331
5332 /*
5333 * If the number of correctable bits is the same,
5334 * bigger step_size has more reliability.
5335 */
5336 if (corr > best_corr ||
5337 (corr == best_corr && step_size > best_step)) {
5338 best_corr = corr;
5339 best_step = step_size;
5340 best_strength = strength;
5341 best_ecc_bytes = ecc_bytes;
5342 }
5343 }
5344 }
5345
5346 if (!best_corr)
5347 return -ENOTSUPP;
5348
5349 chip->ecc.size = best_step;
5350 chip->ecc.strength = best_strength;
5351 chip->ecc.bytes = best_ecc_bytes;
5352
5353 return 0;
5354}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005355
Abhishek Sahu181ace92018-06-20 12:57:28 +05305356/**
5357 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
5358 * @chip: nand chip info structure
5359 * @caps: ECC engine caps info structure
5360 * @oobavail: OOB size that the ECC engine can use
5361 *
5362 * Choose the ECC configuration according to following logic
5363 *
5364 * 1. If both ECC step size and ECC strength are already set (usually by DT)
5365 * then check if it is supported by this controller.
5366 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
5367 * 3. Otherwise, try to match the ECC step size and ECC strength closest
5368 * to the chip's requirement. If available OOB size can't fit the chip
5369 * requirement then fallback to the maximum ECC step size and ECC strength.
5370 *
5371 * On success, the chosen ECC settings are set.
5372 */
5373int nand_ecc_choose_conf(struct nand_chip *chip,
5374 const struct nand_ecc_caps *caps, int oobavail)
5375{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305376 struct mtd_info *mtd = nand_to_mtd(chip);
5377
5378 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
5379 return -EINVAL;
5380
Abhishek Sahu181ace92018-06-20 12:57:28 +05305381 if (chip->ecc.size && chip->ecc.strength)
5382 return nand_check_ecc_caps(chip, caps, oobavail);
5383
5384 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
5385 return nand_maximize_ecc(chip, caps, oobavail);
5386
5387 if (!nand_match_ecc_req(chip, caps, oobavail))
5388 return 0;
5389
5390 return nand_maximize_ecc(chip, caps, oobavail);
5391}
5392EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
5393
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005394/*
5395 * Check if the chip configuration meet the datasheet requirements.
5396
5397 * If our configuration corrects A bits per B bytes and the minimum
5398 * required correction level is X bits per Y bytes, then we must ensure
5399 * both of the following are true:
5400 *
5401 * (1) A / B >= X / Y
5402 * (2) A >= X
5403 *
5404 * Requirement (1) ensures we can correct for the required bitflip density.
5405 * Requirement (2) ensures we can correct even when all bitflips are clumped
5406 * in the same sector.
5407 */
Boris Brezillon08136212018-11-11 08:55:03 +01005408static bool nand_ecc_strength_good(struct nand_chip *chip)
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005409{
Boris Brezillon08136212018-11-11 08:55:03 +01005410 struct mtd_info *mtd = nand_to_mtd(chip);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005411 struct nand_ecc_ctrl *ecc = &chip->ecc;
5412 int corr, ds_corr;
5413
5414 if (ecc->size == 0 || chip->ecc_step_ds == 0)
5415 /* Not enough information */
5416 return true;
5417
5418 /*
5419 * We get the number of corrected bits per page to compare
5420 * the correction density.
5421 */
5422 corr = (mtd->writesize * ecc->strength) / ecc->size;
5423 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
5424
5425 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
5426}
David Woodhouse3b85c322006-09-25 17:06:53 +01005427
5428/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005429 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005430 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01005431 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005432 * This is the second phase of the normal nand_scan() function. It fills out
5433 * all the uninitialized function pointers with the defaults and scans for a
5434 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01005435 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005436static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01005437{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005438 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08005439 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005440 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01005441
Brian Norrise2414f42012-02-06 13:44:00 -08005442 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005443 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07005444 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005445 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07005446 }
Brian Norrise2414f42012-02-06 13:44:00 -08005447
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005448 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005449 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02005450 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005451
Boris Brezillonf84674b2017-06-02 12:18:24 +02005452 /*
5453 * FIXME: some NAND manufacturer drivers expect the first die to be
5454 * selected when manufacturer->init() is called. They should be fixed
5455 * to explictly select the relevant die when interacting with the NAND
5456 * chip.
5457 */
Boris Brezillon1d017852018-11-11 08:55:14 +01005458 nand_select_target(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005459 ret = nand_manufacturer_init(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01005460 nand_deselect_target(chip);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005461 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005462 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005463
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01005464 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005465 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005466
5467 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005468 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005469 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005470 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005471 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005472 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01005475 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 break;
5477 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005478 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02005479 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02005482 /*
5483 * Expose the whole OOB area to users if ECC_NONE
5484 * is passed. We could do that for all kind of
5485 * ->oobsize, but we must keep the old large/small
5486 * page with ECC layout when ->oobsize <= 128 for
5487 * compatibility reasons.
5488 */
5489 if (ecc->mode == NAND_ECC_NONE) {
5490 mtd_set_ooblayout(mtd,
5491 &nand_ooblayout_lp_ops);
5492 break;
5493 }
5494
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005495 WARN(1, "No oob scheme defined for oobsize %d\n",
5496 mtd->oobsize);
5497 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005498 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 }
5500 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005501
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005502 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005503 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005504 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01005505 */
David Woodhouse956e9442006-09-25 17:12:39 +01005506
Huang Shijie97de79e02013-10-18 14:20:53 +08005507 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005508 case NAND_ECC_HW_OOB_FIRST:
5509 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08005510 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005511 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5512 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005513 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005514 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005515 if (!ecc->read_page)
5516 ecc->read_page = nand_read_page_hwecc_oob_first;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005517 /* fall through */
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005518
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005519 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07005520 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005521 if (!ecc->read_page)
5522 ecc->read_page = nand_read_page_hwecc;
5523 if (!ecc->write_page)
5524 ecc->write_page = nand_write_page_hwecc;
5525 if (!ecc->read_page_raw)
5526 ecc->read_page_raw = nand_read_page_raw;
5527 if (!ecc->write_page_raw)
5528 ecc->write_page_raw = nand_write_page_raw;
5529 if (!ecc->read_oob)
5530 ecc->read_oob = nand_read_oob_std;
5531 if (!ecc->write_oob)
5532 ecc->write_oob = nand_write_oob_std;
5533 if (!ecc->read_subpage)
5534 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02005535 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08005536 ecc->write_subpage = nand_write_subpage_hwecc;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005537 /* fall through */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005538
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005539 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08005540 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5541 (!ecc->read_page ||
5542 ecc->read_page == nand_read_page_hwecc ||
5543 !ecc->write_page ||
5544 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005545 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5546 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005547 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005548 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07005549 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005550 if (!ecc->read_page)
5551 ecc->read_page = nand_read_page_syndrome;
5552 if (!ecc->write_page)
5553 ecc->write_page = nand_write_page_syndrome;
5554 if (!ecc->read_page_raw)
5555 ecc->read_page_raw = nand_read_page_raw_syndrome;
5556 if (!ecc->write_page_raw)
5557 ecc->write_page_raw = nand_write_page_raw_syndrome;
5558 if (!ecc->read_oob)
5559 ecc->read_oob = nand_read_oob_syndrome;
5560 if (!ecc->write_oob)
5561 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005562
Huang Shijie97de79e02013-10-18 14:20:53 +08005563 if (mtd->writesize >= ecc->size) {
5564 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005565 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
5566 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005567 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07005568 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005569 break;
Mike Dunne2788c92012-04-25 12:06:10 -07005570 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005571 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5572 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08005573 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02005574 ecc->algo = NAND_ECC_HAMMING;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005575 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005577 case NAND_ECC_SOFT:
Boris Brezillon08136212018-11-11 08:55:03 +01005578 ret = nand_set_ecc_soft_ops(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005579 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005580 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005581 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01005582 }
5583 break;
5584
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005585 case NAND_ECC_ON_DIE:
5586 if (!ecc->read_page || !ecc->write_page) {
5587 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
5588 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005589 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005590 }
5591 if (!ecc->read_oob)
5592 ecc->read_oob = nand_read_oob_std;
5593 if (!ecc->write_oob)
5594 ecc->write_oob = nand_write_oob_std;
5595 break;
5596
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005597 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005598 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08005599 ecc->read_page = nand_read_page_raw;
5600 ecc->write_page = nand_write_page_raw;
5601 ecc->read_oob = nand_read_oob_std;
5602 ecc->read_page_raw = nand_read_page_raw;
5603 ecc->write_page_raw = nand_write_page_raw;
5604 ecc->write_oob = nand_write_oob_std;
5605 ecc->size = mtd->writesize;
5606 ecc->bytes = 0;
5607 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005609
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005611 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5612 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005613 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005616 if (ecc->correct || ecc->calculate) {
5617 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5618 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5619 if (!ecc->calc_buf || !ecc->code_buf) {
5620 ret = -ENOMEM;
5621 goto err_nand_manuf_cleanup;
5622 }
5623 }
5624
Brian Norris9ce244b2011-08-30 18:45:37 -07005625 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005626 if (!ecc->read_oob_raw)
5627 ecc->read_oob_raw = ecc->read_oob;
5628 if (!ecc->write_oob_raw)
5629 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005630
Boris Brezillon846031d2016-02-03 20:11:00 +01005631 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005632 mtd->ecc_strength = ecc->strength;
5633 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005634
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005635 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005636 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005637 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005638 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005639 ecc->steps = mtd->writesize / ecc->size;
5640 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005641 WARN(1, "Invalid ECC parameters\n");
5642 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005643 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005645 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005646 if (ecc->total > mtd->oobsize) {
5647 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5648 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005649 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005650 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005651
Boris Brezillon846031d2016-02-03 20:11:00 +01005652 /*
5653 * The number of bytes available for a client to place data into
5654 * the out of band area.
5655 */
5656 ret = mtd_ooblayout_count_freebytes(mtd);
5657 if (ret < 0)
5658 ret = 0;
5659
5660 mtd->oobavail = ret;
5661
5662 /* ECC sanity check: warn if it's too weak */
Boris Brezillon08136212018-11-11 08:55:03 +01005663 if (!nand_ecc_strength_good(chip))
Boris Brezillon846031d2016-02-03 20:11:00 +01005664 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5665 mtd->name);
5666
Brian Norris8b6e50c2011-05-25 14:59:01 -07005667 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005668 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005669 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005670 case 2:
5671 mtd->subpage_sft = 1;
5672 break;
5673 case 4:
5674 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005675 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005676 mtd->subpage_sft = 2;
5677 break;
5678 }
5679 }
5680 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5681
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005683 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005685 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305686 switch (ecc->mode) {
5687 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305688 if (chip->page_shift > 9)
5689 chip->options |= NAND_SUBPAGE_READ;
5690 break;
5691
5692 default:
5693 break;
5694 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005695
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08005697 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02005698 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5699 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005700 mtd->_erase = nand_erase;
5701 mtd->_point = NULL;
5702 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005703 mtd->_panic_write = panic_nand_write;
5704 mtd->_read_oob = nand_read_oob;
5705 mtd->_write_oob = nand_write_oob;
5706 mtd->_sync = nand_sync;
5707 mtd->_lock = NULL;
5708 mtd->_unlock = NULL;
5709 mtd->_suspend = nand_suspend;
5710 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005711 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005712 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005713 mtd->_block_isbad = nand_block_isbad;
5714 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06005715 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01005716 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005718 /*
5719 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5720 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5721 * properly set.
5722 */
5723 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005724 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
Boris Brezillonf84674b2017-06-02 12:18:24 +02005726 /* Initialize the ->data_interface field. */
5727 ret = nand_init_data_interface(chip);
5728 if (ret)
5729 goto err_nand_manuf_cleanup;
5730
5731 /* Enter fastest possible mode on all dies. */
5732 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005733 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005734 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005735 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005736 }
5737
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005738 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005739 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
5742 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02005743 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07005744 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005745 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005746
Brian Norris44d41822017-05-01 17:04:50 -07005747 return 0;
5748
Boris Brezillonf84674b2017-06-02 12:18:24 +02005749
5750err_nand_manuf_cleanup:
5751 nand_manufacturer_cleanup(chip);
5752
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005753err_free_buf:
5754 kfree(chip->data_buf);
5755 kfree(ecc->code_buf);
5756 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07005757
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005758 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759}
5760
Miquel Raynal05b54c72018-07-19 01:05:46 +02005761static int nand_attach(struct nand_chip *chip)
5762{
5763 if (chip->controller->ops && chip->controller->ops->attach_chip)
5764 return chip->controller->ops->attach_chip(chip);
5765
5766 return 0;
5767}
5768
5769static void nand_detach(struct nand_chip *chip)
5770{
5771 if (chip->controller->ops && chip->controller->ops->detach_chip)
5772 chip->controller->ops->detach_chip(chip);
5773}
5774
David Woodhouse3b85c322006-09-25 17:06:53 +01005775/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005776 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005777 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02005778 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005779 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01005780 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005781 * This fills out all the uninitialized function pointers with the defaults.
5782 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005783 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005784 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005785int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005786 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01005787{
5788 int ret;
5789
Boris Brezillon800342d2018-08-04 22:59:23 +02005790 if (!maxchips)
5791 return -EINVAL;
5792
5793 ret = nand_scan_ident(chip, maxchips, ids);
5794 if (ret)
5795 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005796
5797 ret = nand_attach(chip);
5798 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005799 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005800
Boris Brezillon00ad3782018-09-06 14:05:14 +02005801 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005802 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005803 goto detach_chip;
5804
5805 return 0;
5806
5807detach_chip:
5808 nand_detach(chip);
5809cleanup_ident:
5810 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005811
David Woodhouse3b85c322006-09-25 17:06:53 +01005812 return ret;
5813}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005814EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01005815
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005817 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5818 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005819 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005820void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005822 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005823 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005824 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5825
Jesper Juhlfa671642005-11-07 01:01:27 -08005826 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005827 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005828 kfree(chip->data_buf);
5829 kfree(chip->ecc.code_buf);
5830 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07005831
5832 /* Free bad block descriptor memory */
5833 if (chip->badblock_pattern && chip->badblock_pattern->options
5834 & NAND_BBT_DYNAMICSTRUCT)
5835 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005836
5837 /* Free manufacturer priv data. */
5838 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005839
5840 /* Free controller specific allocations after chip identification */
5841 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005842
5843 /* Free identification phase allocations */
5844 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845}
Miquel Raynal05b54c72018-07-19 01:05:46 +02005846
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005847EXPORT_SYMBOL_GPL(nand_cleanup);
5848
5849/**
5850 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5851 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02005852 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005853 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02005854void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005855{
Boris Brezillon59ac2762018-09-06 14:05:15 +02005856 mtd_device_unregister(nand_to_mtd(chip));
5857 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005858}
David Woodhousee0c7d762006-05-13 18:07:53 +01005859EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005860
David Woodhousee0c7d762006-05-13 18:07:53 +01005861MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005862MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5863MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005864MODULE_DESCRIPTION("Generic NAND flash driver code");