blob: 4646add22114808df4273a615f15be040e6a48f3 [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 /*
Boris Brezillon32813e22018-10-29 11:58:29 +0100243 * cs should always lie between 0 and nanddev_ntargets(), when that's
244 * not the case it's a bug and the caller should be fixed.
Boris Brezillon1d017852018-11-11 08:55:14 +0100245 */
Boris Brezillon32813e22018-10-29 11:58:29 +0100246 if (WARN_ON(cs > nanddev_ntargets(&chip->base)))
Boris Brezillon1d017852018-11-11 08:55:14 +0100247 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
Randy Dunlap455e7b32019-01-27 18:21:42 -0800376 * @chip: NAND chip object
Boris Brezillon99f33512018-11-11 08:55:04 +0100377 * @oob: oob data buffer
378 * @len: oob data write length
379 * @ops: oob ops structure
380 */
381static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
382 struct mtd_oob_ops *ops)
383{
384 struct mtd_info *mtd = nand_to_mtd(chip);
385 int ret;
386
387 /*
388 * Initialise to all 0xFF, to avoid the possibility of left over OOB
389 * data from a previous OOB read.
390 */
391 memset(chip->oob_poi, 0xff, mtd->oobsize);
392
393 switch (ops->mode) {
394
395 case MTD_OPS_PLACE_OOB:
396 case MTD_OPS_RAW:
397 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
398 return oob + len;
399
400 case MTD_OPS_AUTO_OOB:
401 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
402 ops->ooboffs, len);
403 BUG_ON(ret);
404 return oob + len;
405
406 default:
407 BUG();
408 }
409 return NULL;
410}
411
412/**
413 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
414 * @chip: NAND chip object
415 * @to: offset to write to
416 * @ops: oob operation description structure
417 *
418 * NAND write out-of-band.
419 */
420static int nand_do_write_oob(struct nand_chip *chip, loff_t to,
421 struct mtd_oob_ops *ops)
422{
423 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +0900424 int chipnr, page, status, len, ret;
Boris Brezillon99f33512018-11-11 08:55:04 +0100425
426 pr_debug("%s: to = 0x%08x, len = %i\n",
427 __func__, (unsigned int)to, (int)ops->ooblen);
428
429 len = mtd_oobavail(mtd, ops);
430
431 /* Do not allow write past end of page */
432 if ((ops->ooboffs + ops->ooblen) > len) {
433 pr_debug("%s: attempt to write past end of page\n",
434 __func__);
435 return -EINVAL;
436 }
437
438 chipnr = (int)(to >> chip->chip_shift);
439
440 /*
441 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
442 * of my DiskOnChip 2000 test units) will clear the whole data page too
443 * if we don't do this. I have no clue why, but I seem to have 'fixed'
444 * it in the doc2000 driver in August 1999. dwmw2.
445 */
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +0900446 ret = nand_reset(chip, chipnr);
447 if (ret)
448 return ret;
Boris Brezillon99f33512018-11-11 08:55:04 +0100449
Boris Brezillon1d017852018-11-11 08:55:14 +0100450 nand_select_target(chip, chipnr);
Boris Brezillon99f33512018-11-11 08:55:04 +0100451
452 /* Shift to get page */
453 page = (int)(to >> chip->page_shift);
454
455 /* Check, if it is write protected */
456 if (nand_check_wp(chip)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100457 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100458 return -EROFS;
459 }
460
461 /* Invalidate the page cache, if we write to the cached page */
Boris Brezillond9745412018-10-28 16:12:45 +0100462 if (page == chip->pagecache.page)
463 chip->pagecache.page = -1;
Boris Brezillon99f33512018-11-11 08:55:04 +0100464
465 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
466
467 if (ops->mode == MTD_OPS_RAW)
468 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
469 else
470 status = chip->ecc.write_oob(chip, page & chip->pagemask);
471
Boris Brezillon1d017852018-11-11 08:55:14 +0100472 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100473
474 if (status)
475 return status;
476
477 ops->oobretlen = ops->ooblen;
478
479 return 0;
480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700483 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200484 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700485 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700487 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700488 * specific driver. It provides the details for writing a bad block marker to a
489 * block.
490 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200491static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700492{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200493 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700494 struct mtd_oob_ops ops;
495 uint8_t buf[2] = { 0, 0 };
496 int ret = 0, res, i = 0;
497
Brian Norris0ec56dc2015-02-28 02:02:30 -0800498 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700499 ops.oobbuf = buf;
500 ops.ooboffs = chip->badblockpos;
501 if (chip->options & NAND_BUSWIDTH_16) {
502 ops.ooboffs &= ~0x01;
503 ops.len = ops.ooblen = 2;
504 } else {
505 ops.len = ops.ooblen = 1;
506 }
507 ops.mode = MTD_OPS_PLACE_OOB;
508
509 /* Write to first/last page(s) if necessary */
510 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
511 ofs += mtd->erasesize - mtd->writesize;
512 do {
Boris Brezillon08136212018-11-11 08:55:03 +0100513 res = nand_do_write_oob(chip, ofs, &ops);
Brian Norris5a0edb22013-07-30 17:52:58 -0700514 if (!ret)
515 ret = res;
516
517 i++;
518 ofs += mtd->writesize;
519 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
520
521 return ret;
522}
523
524/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200525 * nand_markbad_bbm - mark a block by updating the BBM
526 * @chip: NAND chip object
527 * @ofs: offset of the block to mark bad
528 */
529int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
530{
531 if (chip->legacy.block_markbad)
532 return chip->legacy.block_markbad(chip, ofs);
533
534 return nand_default_block_markbad(chip, ofs);
535}
536
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200537/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700538 * nand_block_markbad_lowlevel - mark a block bad
Boris Brezillon08136212018-11-11 08:55:03 +0100539 * @chip: NAND chip object
Brian Norris5a0edb22013-07-30 17:52:58 -0700540 * @ofs: offset from device start
541 *
542 * This function performs the generic NAND bad block marking steps (i.e., bad
543 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200544 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700545 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700546 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300547 *
Brian Norrise2414f42012-02-06 13:44:00 -0800548 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700549 * (2) write bad block marker to OOB area of affected block (unless flag
550 * NAND_BBT_NO_OOB_BBM is present)
551 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300552 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700553 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800554 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555*/
Boris Brezillon08136212018-11-11 08:55:03 +0100556static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Boris Brezillon08136212018-11-11 08:55:03 +0100558 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisb32843b2013-07-30 17:52:59 -0700559 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000560
Brian Norrisb32843b2013-07-30 17:52:59 -0700561 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800562 struct erase_info einfo;
563
564 /* Attempt erase before marking OOB */
565 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800566 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300567 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200568 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800569
Brian Norrisb32843b2013-07-30 17:52:59 -0700570 /* Write bad block marker to OOB */
Boris Brezillon013e6292018-11-20 11:57:20 +0100571 ret = nand_get_device(chip);
572 if (ret)
573 return ret;
574
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200575 ret = nand_markbad_bbm(chip, ofs);
Boris Brezillon08136212018-11-11 08:55:03 +0100576 nand_release_device(chip);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200577 }
Brian Norrise2414f42012-02-06 13:44:00 -0800578
Brian Norrisb32843b2013-07-30 17:52:59 -0700579 /* Mark block bad in BBT */
580 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200581 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800582 if (!ret)
583 ret = res;
584 }
585
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200586 if (!ret)
587 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300588
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200589 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000592/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800593 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700594 * @mtd: MTD device structure
595 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300596 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800597 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300598 */
599static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
600{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100601 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300602
603 if (!chip->bbt)
604 return 0;
605 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200606 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300607}
608
609/**
610 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Boris Brezillon08136212018-11-11 08:55:03 +0100611 * @chip: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300612 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700613 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 *
615 * Check, if the block is bad. Either by reading the bad block table or
616 * calling of the scan function.
617 */
Boris Brezillon08136212018-11-11 08:55:03 +0100618static int nand_block_checkbad(struct nand_chip *chip, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200621 if (chip->bbt)
622 return nand_isbad_bbt(chip, ofs, allowbbt);
623
624 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200627/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100628 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
629 * @chip: NAND chip structure
630 * @timeout_ms: Timeout in ms
631 *
632 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
633 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
634 * returned.
635 *
636 * This helper is intended to be used when the controller does not have access
637 * to the NAND R/B pin.
638 *
639 * Be aware that calling this helper from an ->exec_op() implementation means
640 * ->exec_op() must be re-entrant.
641 *
642 * Return 0 if the NAND chip is ready, a negative error otherwise.
643 */
644int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
645{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200646 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100647 u8 status = 0;
648 int ret;
649
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100650 if (!nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +0100651 return -ENOTSUPP;
652
Boris Brezillon3057fce2018-05-04 21:24:31 +0200653 /* Wait tWB before polling the STATUS reg. */
654 timings = nand_get_sdr_timings(&chip->data_interface);
655 ndelay(PSEC_TO_NSEC(timings->tWB_max));
656
Miquel Raynal8878b122017-11-09 14:16:45 +0100657 ret = nand_status_op(chip, NULL);
658 if (ret)
659 return ret;
660
661 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
662 do {
663 ret = nand_read_data_op(chip, &status, sizeof(status), true);
664 if (ret)
665 break;
666
667 if (status & NAND_STATUS_READY)
668 break;
669
670 /*
671 * Typical lowest execution time for a tR on most NANDs is 10us,
672 * use this as polling delay before doing something smarter (ie.
673 * deriving a delay from the timeout value, timeout_ms/ratio).
674 */
675 udelay(10);
676 } while (time_before(jiffies, timeout_ms));
677
678 /*
679 * We have to exit READ_STATUS mode in order to read real data on the
680 * bus in case the WAITRDY instruction is preceding a DATA_IN
681 * instruction.
682 */
683 nand_exit_status_op(chip);
684
685 if (ret)
686 return ret;
687
688 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
689};
690EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
691
692/**
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +0200693 * nand_gpio_waitrdy - Poll R/B GPIO pin until ready
694 * @chip: NAND chip structure
695 * @gpiod: GPIO descriptor of R/B pin
696 * @timeout_ms: Timeout in ms
697 *
698 * Poll the R/B GPIO pin until it becomes ready. If that does not happen
699 * whitin the specified timeout, -ETIMEDOUT is returned.
700 *
701 * This helper is intended to be used when the controller has access to the
702 * NAND R/B pin over GPIO.
703 *
704 * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise.
705 */
706int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
707 unsigned long timeout_ms)
708{
709 /* Wait until R/B pin indicates chip is ready or timeout occurs */
710 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
711 do {
712 if (gpiod_get_value_cansleep(gpiod))
713 return 0;
714
715 cond_resched();
716 } while (time_before(jiffies, timeout_ms));
717
718 return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT;
719};
720EXPORT_SYMBOL_GPL(nand_gpio_waitrdy);
721
722/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700723 * panic_nand_wait - [GENERIC] wait until the command is done
Brian Norris8b6e50c2011-05-25 14:59:01 -0700724 * @chip: NAND chip structure
725 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200726 *
727 * Wait for command done. This is a helper function for nand_wait used when
728 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400729 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200730 */
Boris Brezillon3d4af7c2018-09-07 00:38:49 +0200731void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200732{
733 int i;
734 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200735 if (chip->legacy.dev_ready) {
736 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200737 break;
738 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100739 int ret;
740 u8 status;
741
742 ret = nand_read_data_op(chip, &status, sizeof(status),
743 true);
744 if (ret)
745 return;
746
747 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200748 break;
749 }
750 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200751 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200752}
753
Miquel Raynal789157e2018-03-19 14:47:28 +0100754static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +0100755{
Miquel Raynal789157e2018-03-19 14:47:28 +0100756 return (chip->parameters.supports_set_get_features &&
757 test_bit(addr, chip->parameters.get_feature_list));
758}
759
760static bool nand_supports_set_features(struct nand_chip *chip, int addr)
761{
762 return (chip->parameters.supports_set_get_features &&
763 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +0100764}
765
766/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200767 * nand_reset_data_interface - Reset data interface and timings
768 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100769 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200770 *
771 * Reset the Data interface and timings to ONFI mode 0.
772 *
773 * Returns 0 for success or negative error code otherwise.
774 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100775static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200776{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200777 int ret;
778
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100779 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200780 return 0;
781
782 /*
783 * The ONFI specification says:
784 * "
785 * To transition from NV-DDR or NV-DDR2 to the SDR data
786 * interface, the host shall use the Reset (FFh) command
787 * using SDR timing mode 0. A device in any timing mode is
788 * required to recognize Reset (FFh) command issued in SDR
789 * timing mode 0.
790 * "
791 *
792 * Configure the data interface in SDR mode and set the
793 * timings to timing mode 0.
794 */
795
Miquel Raynal17fa8042017-11-30 18:01:31 +0100796 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100797 ret = chip->controller->ops->setup_data_interface(chip, chipnr,
798 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200799 if (ret)
800 pr_err("Failed to configure data interface to SDR timing mode 0\n");
801
802 return ret;
803}
804
805/**
806 * nand_setup_data_interface - Setup the best data interface and timings
807 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100808 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200809 *
810 * Find and configure the best data interface and NAND timings supported by
811 * the chip and the driver.
812 * First tries to retrieve supported timing modes from ONFI information,
813 * and if the NAND chip does not support ONFI, relies on the
814 * ->onfi_timing_mode_default specified in the nand_ids table.
815 *
816 * Returns 0 for success or negative error code otherwise.
817 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100818static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200819{
Miquel Raynal97baea12018-03-19 14:47:20 +0100820 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
821 chip->onfi_timing_mode_default,
822 };
Boris Brezillond8e725d2016-09-15 10:32:50 +0200823 int ret;
824
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100825 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200826 return 0;
827
Miquel Raynal993447b2018-03-19 14:47:21 +0100828 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +0100829 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100830 nand_select_target(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +0100831 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
832 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100833 nand_deselect_target(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200834 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +0100835 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +0200836 }
837
Miquel Raynal97baea12018-03-19 14:47:20 +0100838 /* Change the mode on the controller side */
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100839 ret = chip->controller->ops->setup_data_interface(chip, chipnr,
840 &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +0100841 if (ret)
842 return ret;
843
844 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +0100845 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +0100846 return 0;
847
848 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon1d017852018-11-11 08:55:14 +0100849 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100850 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
851 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100852 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100853 if (ret)
854 goto err_reset_chip;
855
856 if (tmode_param[0] != chip->onfi_timing_mode_default) {
857 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
858 chip->onfi_timing_mode_default);
859 goto err_reset_chip;
860 }
861
862 return 0;
863
864err_reset_chip:
865 /*
866 * Fallback to mode 0 if the chip explicitly did not ack the chosen
867 * timing mode.
868 */
869 nand_reset_data_interface(chip, chipnr);
Boris Brezillon1d017852018-11-11 08:55:14 +0100870 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100871 nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +0100872 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100873
Boris Brezillond8e725d2016-09-15 10:32:50 +0200874 return ret;
875}
876
877/**
878 * nand_init_data_interface - find the best data interface and timings
879 * @chip: The NAND chip
880 *
881 * Find the best data interface and NAND timings supported by the chip
882 * and the driver.
883 * First tries to retrieve supported timing modes from ONFI information,
884 * and if the NAND chip does not support ONFI, relies on the
885 * ->onfi_timing_mode_default specified in the nand_ids table. After this
886 * function nand_chip->data_interface is initialized with the best timing mode
887 * available.
888 *
889 * Returns 0 for success or negative error code otherwise.
890 */
891static int nand_init_data_interface(struct nand_chip *chip)
892{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200893 int modes, mode, ret;
894
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100895 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200896 return 0;
897
898 /*
899 * First try to identify the best timings from ONFI parameters and
900 * if the NAND does not support ONFI, fallback to the default ONFI
901 * timing mode.
902 */
Boris Brezillon462f35d2018-09-07 00:38:47 +0200903 if (chip->parameters.onfi) {
904 modes = chip->parameters.onfi->async_timing_mode;
905 } else {
Boris Brezillond8e725d2016-09-15 10:32:50 +0200906 if (!chip->onfi_timing_mode_default)
907 return 0;
908
909 modes = GENMASK(chip->onfi_timing_mode_default, 0);
910 }
911
Boris Brezillond8e725d2016-09-15 10:32:50 +0200912 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +0100913 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200914 if (ret)
915 continue;
916
Miquel Raynald787b8b2017-12-22 18:12:41 +0100917 /*
918 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
919 * controller supports the requested timings.
920 */
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100921 ret = chip->controller->ops->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +0100922 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +0100923 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200924 if (!ret) {
925 chip->onfi_timing_mode_default = mode;
926 break;
927 }
928 }
929
930 return 0;
931}
932
Boris Brezillond8e725d2016-09-15 10:32:50 +0200933/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100934 * nand_fill_column_cycles - fill the column cycles of an address
935 * @chip: The NAND chip
936 * @addrs: Array of address cycles to fill
937 * @offset_in_page: The offset in the page
938 *
939 * Fills the first or the first two bytes of the @addrs field depending
940 * on the NAND bus width and the page size.
941 *
942 * Returns the number of cycles needed to encode the column, or a negative
943 * error code in case one of the arguments is invalid.
944 */
945static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
946 unsigned int offset_in_page)
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100947{
Miquel Raynal8878b122017-11-09 14:16:45 +0100948 struct mtd_info *mtd = nand_to_mtd(chip);
949
950 /* Make sure the offset is less than the actual page size. */
951 if (offset_in_page > mtd->writesize + mtd->oobsize)
952 return -EINVAL;
953
954 /*
955 * On small page NANDs, there's a dedicated command to access the OOB
956 * area, and the column address is relative to the start of the OOB
957 * area, not the start of the page. Asjust the address accordingly.
958 */
959 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
960 offset_in_page -= mtd->writesize;
961
962 /*
963 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
964 * wide, then it must be divided by 2.
965 */
966 if (chip->options & NAND_BUSWIDTH_16) {
967 if (WARN_ON(offset_in_page % 2))
968 return -EINVAL;
969
970 offset_in_page /= 2;
971 }
972
973 addrs[0] = offset_in_page;
974
975 /*
976 * Small page NANDs use 1 cycle for the columns, while large page NANDs
977 * need 2
978 */
979 if (mtd->writesize <= 512)
980 return 1;
981
982 addrs[1] = offset_in_page >> 8;
983
984 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Miquel Raynal8878b122017-11-09 14:16:45 +0100987static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
988 unsigned int offset_in_page, void *buf,
989 unsigned int len)
990{
991 struct mtd_info *mtd = nand_to_mtd(chip);
992 const struct nand_sdr_timings *sdr =
993 nand_get_sdr_timings(&chip->data_interface);
994 u8 addrs[4];
995 struct nand_op_instr instrs[] = {
996 NAND_OP_CMD(NAND_CMD_READ0, 0),
997 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
998 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
999 PSEC_TO_NSEC(sdr->tRR_min)),
1000 NAND_OP_DATA_IN(len, buf, 0),
1001 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001002 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001003 int ret;
1004
1005 /* Drop the DATA_IN instruction if len is set to 0. */
1006 if (!len)
1007 op.ninstrs--;
1008
1009 if (offset_in_page >= mtd->writesize)
1010 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1011 else if (offset_in_page >= 256 &&
1012 !(chip->options & NAND_BUSWIDTH_16))
1013 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1014
1015 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1016 if (ret < 0)
1017 return ret;
1018
1019 addrs[1] = page;
1020 addrs[2] = page >> 8;
1021
1022 if (chip->options & NAND_ROW_ADDR_3) {
1023 addrs[3] = page >> 16;
1024 instrs[1].ctx.addr.naddrs++;
1025 }
1026
1027 return nand_exec_op(chip, &op);
1028}
1029
1030static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1031 unsigned int offset_in_page, void *buf,
1032 unsigned int len)
1033{
1034 const struct nand_sdr_timings *sdr =
1035 nand_get_sdr_timings(&chip->data_interface);
1036 u8 addrs[5];
1037 struct nand_op_instr instrs[] = {
1038 NAND_OP_CMD(NAND_CMD_READ0, 0),
1039 NAND_OP_ADDR(4, addrs, 0),
1040 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1041 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1042 PSEC_TO_NSEC(sdr->tRR_min)),
1043 NAND_OP_DATA_IN(len, buf, 0),
1044 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001045 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001046 int ret;
1047
1048 /* Drop the DATA_IN instruction if len is set to 0. */
1049 if (!len)
1050 op.ninstrs--;
1051
1052 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1053 if (ret < 0)
1054 return ret;
1055
1056 addrs[2] = page;
1057 addrs[3] = page >> 8;
1058
1059 if (chip->options & NAND_ROW_ADDR_3) {
1060 addrs[4] = page >> 16;
1061 instrs[1].ctx.addr.naddrs++;
1062 }
1063
1064 return nand_exec_op(chip, &op);
1065}
1066
1067/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001068 * nand_read_page_op - Do a READ PAGE operation
1069 * @chip: The NAND chip
1070 * @page: page to read
1071 * @offset_in_page: offset within the page
1072 * @buf: buffer used to store the data
1073 * @len: length of the buffer
1074 *
1075 * This function issues a READ PAGE operation.
1076 * This function does not select/unselect the CS line.
1077 *
1078 * Returns 0 on success, a negative error code otherwise.
1079 */
1080int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1081 unsigned int offset_in_page, void *buf, unsigned int len)
1082{
1083 struct mtd_info *mtd = nand_to_mtd(chip);
1084
1085 if (len && !buf)
1086 return -EINVAL;
1087
1088 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1089 return -EINVAL;
1090
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001091 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001092 if (mtd->writesize > 512)
1093 return nand_lp_exec_read_page_op(chip, page,
1094 offset_in_page, buf,
1095 len);
1096
1097 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1098 buf, len);
1099 }
1100
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001101 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001102 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001103 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001104
1105 return 0;
1106}
1107EXPORT_SYMBOL_GPL(nand_read_page_op);
1108
1109/**
1110 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1111 * @chip: The NAND chip
1112 * @page: parameter page to read
1113 * @buf: buffer used to store the data
1114 * @len: length of the buffer
1115 *
1116 * This function issues a READ PARAMETER PAGE operation.
1117 * This function does not select/unselect the CS line.
1118 *
1119 * Returns 0 on success, a negative error code otherwise.
1120 */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02001121int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1122 unsigned int len)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001123{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001124 unsigned int i;
1125 u8 *p = buf;
1126
1127 if (len && !buf)
1128 return -EINVAL;
1129
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001130 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001131 const struct nand_sdr_timings *sdr =
1132 nand_get_sdr_timings(&chip->data_interface);
1133 struct nand_op_instr instrs[] = {
1134 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1135 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1136 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1137 PSEC_TO_NSEC(sdr->tRR_min)),
1138 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1139 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001140 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001141
1142 /* Drop the DATA_IN instruction if len is set to 0. */
1143 if (!len)
1144 op.ninstrs--;
1145
1146 return nand_exec_op(chip, &op);
1147 }
1148
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001149 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001150 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001151 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001152
1153 return 0;
1154}
1155
1156/**
1157 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1158 * @chip: The NAND chip
1159 * @offset_in_page: offset within the page
1160 * @buf: buffer used to store the data
1161 * @len: length of the buffer
1162 * @force_8bit: force 8-bit bus access
1163 *
1164 * This function issues a CHANGE READ COLUMN operation.
1165 * This function does not select/unselect the CS line.
1166 *
1167 * Returns 0 on success, a negative error code otherwise.
1168 */
1169int nand_change_read_column_op(struct nand_chip *chip,
1170 unsigned int offset_in_page, void *buf,
1171 unsigned int len, bool force_8bit)
1172{
1173 struct mtd_info *mtd = nand_to_mtd(chip);
1174
1175 if (len && !buf)
1176 return -EINVAL;
1177
1178 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1179 return -EINVAL;
1180
Miquel Raynal8878b122017-11-09 14:16:45 +01001181 /* Small page NANDs do not support column change. */
1182 if (mtd->writesize <= 512)
1183 return -ENOTSUPP;
1184
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001185 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001186 const struct nand_sdr_timings *sdr =
1187 nand_get_sdr_timings(&chip->data_interface);
1188 u8 addrs[2] = {};
1189 struct nand_op_instr instrs[] = {
1190 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1191 NAND_OP_ADDR(2, addrs, 0),
1192 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1193 PSEC_TO_NSEC(sdr->tCCS_min)),
1194 NAND_OP_DATA_IN(len, buf, 0),
1195 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001196 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001197 int ret;
1198
1199 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1200 if (ret < 0)
1201 return ret;
1202
1203 /* Drop the DATA_IN instruction if len is set to 0. */
1204 if (!len)
1205 op.ninstrs--;
1206
1207 instrs[3].ctx.data.force_8bit = force_8bit;
1208
1209 return nand_exec_op(chip, &op);
1210 }
1211
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001212 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001213 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001214 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001215
1216 return 0;
1217}
1218EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1219
1220/**
1221 * nand_read_oob_op - Do a READ OOB operation
1222 * @chip: The NAND chip
1223 * @page: page to read
1224 * @offset_in_oob: offset within the OOB area
1225 * @buf: buffer used to store the data
1226 * @len: length of the buffer
1227 *
1228 * This function issues a READ OOB operation.
1229 * This function does not select/unselect the CS line.
1230 *
1231 * Returns 0 on success, a negative error code otherwise.
1232 */
1233int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1234 unsigned int offset_in_oob, void *buf, unsigned int len)
1235{
1236 struct mtd_info *mtd = nand_to_mtd(chip);
1237
1238 if (len && !buf)
1239 return -EINVAL;
1240
1241 if (offset_in_oob + len > mtd->oobsize)
1242 return -EINVAL;
1243
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001244 if (nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +01001245 return nand_read_page_op(chip, page,
1246 mtd->writesize + offset_in_oob,
1247 buf, len);
1248
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001249 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001250 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001251 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001252
1253 return 0;
1254}
1255EXPORT_SYMBOL_GPL(nand_read_oob_op);
1256
Miquel Raynal8878b122017-11-09 14:16:45 +01001257static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1258 unsigned int offset_in_page, const void *buf,
1259 unsigned int len, bool prog)
1260{
1261 struct mtd_info *mtd = nand_to_mtd(chip);
1262 const struct nand_sdr_timings *sdr =
1263 nand_get_sdr_timings(&chip->data_interface);
1264 u8 addrs[5] = {};
1265 struct nand_op_instr instrs[] = {
1266 /*
1267 * The first instruction will be dropped if we're dealing
1268 * with a large page NAND and adjusted if we're dealing
1269 * with a small page NAND and the page offset is > 255.
1270 */
1271 NAND_OP_CMD(NAND_CMD_READ0, 0),
1272 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1273 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1274 NAND_OP_DATA_OUT(len, buf, 0),
1275 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1276 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1277 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001278 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001279 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1280 int ret;
1281 u8 status;
1282
1283 if (naddrs < 0)
1284 return naddrs;
1285
1286 addrs[naddrs++] = page;
1287 addrs[naddrs++] = page >> 8;
1288 if (chip->options & NAND_ROW_ADDR_3)
1289 addrs[naddrs++] = page >> 16;
1290
1291 instrs[2].ctx.addr.naddrs = naddrs;
1292
1293 /* Drop the last two instructions if we're not programming the page. */
1294 if (!prog) {
1295 op.ninstrs -= 2;
1296 /* Also drop the DATA_OUT instruction if empty. */
1297 if (!len)
1298 op.ninstrs--;
1299 }
1300
1301 if (mtd->writesize <= 512) {
1302 /*
1303 * Small pages need some more tweaking: we have to adjust the
1304 * first instruction depending on the page offset we're trying
1305 * to access.
1306 */
1307 if (offset_in_page >= mtd->writesize)
1308 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1309 else if (offset_in_page >= 256 &&
1310 !(chip->options & NAND_BUSWIDTH_16))
1311 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1312 } else {
1313 /*
1314 * Drop the first command if we're dealing with a large page
1315 * NAND.
1316 */
1317 op.instrs++;
1318 op.ninstrs--;
1319 }
1320
1321 ret = nand_exec_op(chip, &op);
1322 if (!prog || ret)
1323 return ret;
1324
1325 ret = nand_status_op(chip, &status);
1326 if (ret)
1327 return ret;
1328
1329 return status;
1330}
1331
Boris Brezillon97d90da2017-11-30 18:01:29 +01001332/**
1333 * nand_prog_page_begin_op - starts a PROG PAGE operation
1334 * @chip: The NAND chip
1335 * @page: page to write
1336 * @offset_in_page: offset within the page
1337 * @buf: buffer containing the data to write to the page
1338 * @len: length of the buffer
1339 *
1340 * This function issues the first half of a PROG PAGE operation.
1341 * This function does not select/unselect the CS line.
1342 *
1343 * Returns 0 on success, a negative error code otherwise.
1344 */
1345int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1346 unsigned int offset_in_page, const void *buf,
1347 unsigned int len)
1348{
1349 struct mtd_info *mtd = nand_to_mtd(chip);
1350
1351 if (len && !buf)
1352 return -EINVAL;
1353
1354 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1355 return -EINVAL;
1356
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001357 if (nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +01001358 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1359 len, false);
1360
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001361 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001362
1363 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001364 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001365
1366 return 0;
1367}
1368EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1369
1370/**
1371 * nand_prog_page_end_op - ends a PROG PAGE operation
1372 * @chip: The NAND chip
1373 *
1374 * This function issues the second half of a PROG PAGE operation.
1375 * This function does not select/unselect the CS line.
1376 *
1377 * Returns 0 on success, a negative error code otherwise.
1378 */
1379int nand_prog_page_end_op(struct nand_chip *chip)
1380{
Miquel Raynal8878b122017-11-09 14:16:45 +01001381 int ret;
1382 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001383
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001384 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001385 const struct nand_sdr_timings *sdr =
1386 nand_get_sdr_timings(&chip->data_interface);
1387 struct nand_op_instr instrs[] = {
1388 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1389 PSEC_TO_NSEC(sdr->tWB_max)),
1390 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1391 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001392 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001393
Miquel Raynal8878b122017-11-09 14:16:45 +01001394 ret = nand_exec_op(chip, &op);
1395 if (ret)
1396 return ret;
1397
1398 ret = nand_status_op(chip, &status);
1399 if (ret)
1400 return ret;
1401 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001402 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001403 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001404 if (ret < 0)
1405 return ret;
1406
1407 status = ret;
1408 }
1409
Boris Brezillon97d90da2017-11-30 18:01:29 +01001410 if (status & NAND_STATUS_FAIL)
1411 return -EIO;
1412
1413 return 0;
1414}
1415EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1416
1417/**
1418 * nand_prog_page_op - Do a full PROG PAGE operation
1419 * @chip: The NAND chip
1420 * @page: page to write
1421 * @offset_in_page: offset within the page
1422 * @buf: buffer containing the data to write to the page
1423 * @len: length of the buffer
1424 *
1425 * This function issues a full PROG PAGE operation.
1426 * This function does not select/unselect the CS line.
1427 *
1428 * Returns 0 on success, a negative error code otherwise.
1429 */
1430int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1431 unsigned int offset_in_page, const void *buf,
1432 unsigned int len)
1433{
1434 struct mtd_info *mtd = nand_to_mtd(chip);
1435 int status;
1436
1437 if (!len || !buf)
1438 return -EINVAL;
1439
1440 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1441 return -EINVAL;
1442
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001443 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001444 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1445 len, true);
1446 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001447 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1448 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001449 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001450 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001451 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001452 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001453
Boris Brezillon97d90da2017-11-30 18:01:29 +01001454 if (status & NAND_STATUS_FAIL)
1455 return -EIO;
1456
1457 return 0;
1458}
1459EXPORT_SYMBOL_GPL(nand_prog_page_op);
1460
1461/**
1462 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1463 * @chip: The NAND chip
1464 * @offset_in_page: offset within the page
1465 * @buf: buffer containing the data to send to the NAND
1466 * @len: length of the buffer
1467 * @force_8bit: force 8-bit bus access
1468 *
1469 * This function issues a CHANGE WRITE COLUMN operation.
1470 * This function does not select/unselect the CS line.
1471 *
1472 * Returns 0 on success, a negative error code otherwise.
1473 */
1474int nand_change_write_column_op(struct nand_chip *chip,
1475 unsigned int offset_in_page,
1476 const void *buf, unsigned int len,
1477 bool force_8bit)
1478{
1479 struct mtd_info *mtd = nand_to_mtd(chip);
1480
1481 if (len && !buf)
1482 return -EINVAL;
1483
1484 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1485 return -EINVAL;
1486
Miquel Raynal8878b122017-11-09 14:16:45 +01001487 /* Small page NANDs do not support column change. */
1488 if (mtd->writesize <= 512)
1489 return -ENOTSUPP;
1490
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001491 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001492 const struct nand_sdr_timings *sdr =
1493 nand_get_sdr_timings(&chip->data_interface);
1494 u8 addrs[2];
1495 struct nand_op_instr instrs[] = {
1496 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1497 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1498 NAND_OP_DATA_OUT(len, buf, 0),
1499 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001500 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001501 int ret;
1502
1503 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1504 if (ret < 0)
1505 return ret;
1506
1507 instrs[2].ctx.data.force_8bit = force_8bit;
1508
1509 /* Drop the DATA_OUT instruction if len is set to 0. */
1510 if (!len)
1511 op.ninstrs--;
1512
1513 return nand_exec_op(chip, &op);
1514 }
1515
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001516 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001517 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001518 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001519
1520 return 0;
1521}
1522EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1523
1524/**
1525 * nand_readid_op - Do a READID operation
1526 * @chip: The NAND chip
1527 * @addr: address cycle to pass after the READID command
1528 * @buf: buffer used to store the ID
1529 * @len: length of the buffer
1530 *
1531 * This function sends a READID command and reads back the ID returned by the
1532 * NAND.
1533 * This function does not select/unselect the CS line.
1534 *
1535 * Returns 0 on success, a negative error code otherwise.
1536 */
1537int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1538 unsigned int len)
1539{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001540 unsigned int i;
1541 u8 *id = buf;
1542
1543 if (len && !buf)
1544 return -EINVAL;
1545
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001546 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001547 const struct nand_sdr_timings *sdr =
1548 nand_get_sdr_timings(&chip->data_interface);
1549 struct nand_op_instr instrs[] = {
1550 NAND_OP_CMD(NAND_CMD_READID, 0),
1551 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1552 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1553 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001554 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001555
1556 /* Drop the DATA_IN instruction if len is set to 0. */
1557 if (!len)
1558 op.ninstrs--;
1559
1560 return nand_exec_op(chip, &op);
1561 }
1562
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001563 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001564
1565 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001566 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001567
1568 return 0;
1569}
1570EXPORT_SYMBOL_GPL(nand_readid_op);
1571
1572/**
1573 * nand_status_op - Do a STATUS operation
1574 * @chip: The NAND chip
1575 * @status: out variable to store the NAND status
1576 *
1577 * This function sends a STATUS command and reads back the status returned by
1578 * the NAND.
1579 * This function does not select/unselect the CS line.
1580 *
1581 * Returns 0 on success, a negative error code otherwise.
1582 */
1583int nand_status_op(struct nand_chip *chip, u8 *status)
1584{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001585 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001586 const struct nand_sdr_timings *sdr =
1587 nand_get_sdr_timings(&chip->data_interface);
1588 struct nand_op_instr instrs[] = {
1589 NAND_OP_CMD(NAND_CMD_STATUS,
1590 PSEC_TO_NSEC(sdr->tADL_min)),
1591 NAND_OP_8BIT_DATA_IN(1, status, 0),
1592 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001593 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001594
1595 if (!status)
1596 op.ninstrs--;
1597
1598 return nand_exec_op(chip, &op);
1599 }
1600
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001601 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001602 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001603 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001604
1605 return 0;
1606}
1607EXPORT_SYMBOL_GPL(nand_status_op);
1608
1609/**
1610 * nand_exit_status_op - Exit a STATUS operation
1611 * @chip: The NAND chip
1612 *
1613 * This function sends a READ0 command to cancel the effect of the STATUS
1614 * command to avoid reading only the status until a new read command is sent.
1615 *
1616 * This function does not select/unselect the CS line.
1617 *
1618 * Returns 0 on success, a negative error code otherwise.
1619 */
1620int nand_exit_status_op(struct nand_chip *chip)
1621{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001622 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001623 struct nand_op_instr instrs[] = {
1624 NAND_OP_CMD(NAND_CMD_READ0, 0),
1625 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001626 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001627
1628 return nand_exec_op(chip, &op);
1629 }
1630
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001631 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001632
1633 return 0;
1634}
Boris Brezillon97d90da2017-11-30 18:01:29 +01001635
1636/**
1637 * nand_erase_op - Do an erase operation
1638 * @chip: The NAND chip
1639 * @eraseblock: block to erase
1640 *
1641 * This function sends an ERASE command and waits for the NAND to be ready
1642 * before returning.
1643 * This function does not select/unselect the CS line.
1644 *
1645 * Returns 0 on success, a negative error code otherwise.
1646 */
1647int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1648{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001649 unsigned int page = eraseblock <<
1650 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01001651 int ret;
1652 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001653
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001654 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001655 const struct nand_sdr_timings *sdr =
1656 nand_get_sdr_timings(&chip->data_interface);
1657 u8 addrs[3] = { page, page >> 8, page >> 16 };
1658 struct nand_op_instr instrs[] = {
1659 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
1660 NAND_OP_ADDR(2, addrs, 0),
1661 NAND_OP_CMD(NAND_CMD_ERASE2,
1662 PSEC_TO_MSEC(sdr->tWB_max)),
1663 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
1664 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001665 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001666
Miquel Raynal8878b122017-11-09 14:16:45 +01001667 if (chip->options & NAND_ROW_ADDR_3)
1668 instrs[1].ctx.addr.naddrs++;
1669
1670 ret = nand_exec_op(chip, &op);
1671 if (ret)
1672 return ret;
1673
1674 ret = nand_status_op(chip, &status);
1675 if (ret)
1676 return ret;
1677 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001678 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
1679 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01001680
Boris Brezillon8395b752018-09-07 00:38:37 +02001681 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001682 if (ret < 0)
1683 return ret;
1684
1685 status = ret;
1686 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001687
1688 if (status & NAND_STATUS_FAIL)
1689 return -EIO;
1690
1691 return 0;
1692}
1693EXPORT_SYMBOL_GPL(nand_erase_op);
1694
1695/**
1696 * nand_set_features_op - Do a SET FEATURES operation
1697 * @chip: The NAND chip
1698 * @feature: feature id
1699 * @data: 4 bytes of data
1700 *
1701 * This function sends a SET FEATURES command and waits for the NAND to be
1702 * ready before returning.
1703 * This function does not select/unselect the CS line.
1704 *
1705 * Returns 0 on success, a negative error code otherwise.
1706 */
1707static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1708 const void *data)
1709{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001710 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01001711 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001712
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001713 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001714 const struct nand_sdr_timings *sdr =
1715 nand_get_sdr_timings(&chip->data_interface);
1716 struct nand_op_instr instrs[] = {
1717 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
1718 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
1719 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
1720 PSEC_TO_NSEC(sdr->tWB_max)),
1721 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
1722 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001723 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001724
Boris Brezillon782d1962018-05-11 14:44:07 +02001725 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01001726 }
1727
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001728 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02001729 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001730 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02001731
Boris Brezillon8395b752018-09-07 00:38:37 +02001732 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02001733 if (ret < 0)
1734 return ret;
1735
1736 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001737 return -EIO;
1738
1739 return 0;
1740}
1741
1742/**
1743 * nand_get_features_op - Do a GET FEATURES operation
1744 * @chip: The NAND chip
1745 * @feature: feature id
1746 * @data: 4 bytes of data
1747 *
1748 * This function sends a GET FEATURES command and waits for the NAND to be
1749 * ready before returning.
1750 * This function does not select/unselect the CS line.
1751 *
1752 * Returns 0 on success, a negative error code otherwise.
1753 */
1754static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1755 void *data)
1756{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001757 u8 *params = data;
1758 int i;
1759
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001760 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001761 const struct nand_sdr_timings *sdr =
1762 nand_get_sdr_timings(&chip->data_interface);
1763 struct nand_op_instr instrs[] = {
1764 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
1765 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
1766 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
1767 PSEC_TO_NSEC(sdr->tRR_min)),
1768 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
1769 data, 0),
1770 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001771 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001772
1773 return nand_exec_op(chip, &op);
1774 }
1775
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001776 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001777 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001778 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001779
1780 return 0;
1781}
1782
Boris Brezillon52f05b62018-07-27 09:44:18 +02001783static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
1784 unsigned int delay_ns)
1785{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001786 if (nand_has_exec_op(chip)) {
Boris Brezillon52f05b62018-07-27 09:44:18 +02001787 struct nand_op_instr instrs[] = {
1788 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
1789 PSEC_TO_NSEC(delay_ns)),
1790 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001791 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001792
1793 return nand_exec_op(chip, &op);
1794 }
1795
1796 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02001797 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001798 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001799 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001800 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001801
1802 return 0;
1803}
1804
Boris Brezillon97d90da2017-11-30 18:01:29 +01001805/**
1806 * nand_reset_op - Do a reset operation
1807 * @chip: The NAND chip
1808 *
1809 * This function sends a RESET command and waits for the NAND to be ready
1810 * before returning.
1811 * This function does not select/unselect the CS line.
1812 *
1813 * Returns 0 on success, a negative error code otherwise.
1814 */
1815int nand_reset_op(struct nand_chip *chip)
1816{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001817 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001818 const struct nand_sdr_timings *sdr =
1819 nand_get_sdr_timings(&chip->data_interface);
1820 struct nand_op_instr instrs[] = {
1821 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
1822 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
1823 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001824 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001825
1826 return nand_exec_op(chip, &op);
1827 }
1828
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001829 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001830
1831 return 0;
1832}
1833EXPORT_SYMBOL_GPL(nand_reset_op);
1834
1835/**
1836 * nand_read_data_op - Read data from the NAND
1837 * @chip: The NAND chip
1838 * @buf: buffer used to store the data
1839 * @len: length of the buffer
1840 * @force_8bit: force 8-bit bus access
1841 *
1842 * This function does a raw data read on the bus. Usually used after launching
1843 * another NAND operation like nand_read_page_op().
1844 * This function does not select/unselect the CS line.
1845 *
1846 * Returns 0 on success, a negative error code otherwise.
1847 */
1848int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1849 bool force_8bit)
1850{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001851 if (!len || !buf)
1852 return -EINVAL;
1853
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001854 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001855 struct nand_op_instr instrs[] = {
1856 NAND_OP_DATA_IN(len, buf, 0),
1857 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001858 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001859
1860 instrs[0].ctx.data.force_8bit = force_8bit;
1861
1862 return nand_exec_op(chip, &op);
1863 }
1864
Boris Brezillon97d90da2017-11-30 18:01:29 +01001865 if (force_8bit) {
1866 u8 *p = buf;
1867 unsigned int i;
1868
1869 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001870 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001871 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001872 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001873 }
1874
1875 return 0;
1876}
1877EXPORT_SYMBOL_GPL(nand_read_data_op);
1878
1879/**
1880 * nand_write_data_op - Write data from the NAND
1881 * @chip: The NAND chip
1882 * @buf: buffer containing the data to send on the bus
1883 * @len: length of the buffer
1884 * @force_8bit: force 8-bit bus access
1885 *
1886 * This function does a raw data write on the bus. Usually used after launching
1887 * another NAND operation like nand_write_page_begin_op().
1888 * This function does not select/unselect the CS line.
1889 *
1890 * Returns 0 on success, a negative error code otherwise.
1891 */
1892int nand_write_data_op(struct nand_chip *chip, const void *buf,
1893 unsigned int len, bool force_8bit)
1894{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001895 if (!len || !buf)
1896 return -EINVAL;
1897
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001898 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001899 struct nand_op_instr instrs[] = {
1900 NAND_OP_DATA_OUT(len, buf, 0),
1901 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001902 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001903
1904 instrs[0].ctx.data.force_8bit = force_8bit;
1905
1906 return nand_exec_op(chip, &op);
1907 }
1908
Boris Brezillon97d90da2017-11-30 18:01:29 +01001909 if (force_8bit) {
1910 const u8 *p = buf;
1911 unsigned int i;
1912
1913 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001914 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001915 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001916 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001917 }
1918
1919 return 0;
1920}
1921EXPORT_SYMBOL_GPL(nand_write_data_op);
1922
1923/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001924 * struct nand_op_parser_ctx - Context used by the parser
1925 * @instrs: array of all the instructions that must be addressed
1926 * @ninstrs: length of the @instrs array
1927 * @subop: Sub-operation to be passed to the NAND controller
1928 *
1929 * This structure is used by the core to split NAND operations into
1930 * sub-operations that can be handled by the NAND controller.
1931 */
1932struct nand_op_parser_ctx {
1933 const struct nand_op_instr *instrs;
1934 unsigned int ninstrs;
1935 struct nand_subop subop;
1936};
1937
1938/**
1939 * nand_op_parser_must_split_instr - Checks if an instruction must be split
1940 * @pat: the parser pattern element that matches @instr
1941 * @instr: pointer to the instruction to check
1942 * @start_offset: this is an in/out parameter. If @instr has already been
1943 * split, then @start_offset is the offset from which to start
1944 * (either an address cycle or an offset in the data buffer).
1945 * Conversely, if the function returns true (ie. instr must be
1946 * split), this parameter is updated to point to the first
1947 * data/address cycle that has not been taken care of.
1948 *
1949 * Some NAND controllers are limited and cannot send X address cycles with a
1950 * unique operation, or cannot read/write more than Y bytes at the same time.
1951 * In this case, split the instruction that does not fit in a single
1952 * controller-operation into two or more chunks.
1953 *
1954 * Returns true if the instruction must be split, false otherwise.
1955 * The @start_offset parameter is also updated to the offset at which the next
1956 * bundle of instruction must start (if an address or a data instruction).
1957 */
1958static bool
1959nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
1960 const struct nand_op_instr *instr,
1961 unsigned int *start_offset)
1962{
1963 switch (pat->type) {
1964 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001965 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01001966 break;
1967
1968 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001969 pat->ctx.addr.maxcycles) {
1970 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01001971 return true;
1972 }
1973 break;
1974
1975 case NAND_OP_DATA_IN_INSTR:
1976 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001977 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01001978 break;
1979
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001980 if (instr->ctx.data.len - *start_offset >
1981 pat->ctx.data.maxlen) {
1982 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01001983 return true;
1984 }
1985 break;
1986
1987 default:
1988 break;
1989 }
1990
1991 return false;
1992}
1993
1994/**
1995 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
1996 * remaining in the parser context
1997 * @pat: the pattern to test
1998 * @ctx: the parser context structure to match with the pattern @pat
1999 *
2000 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2001 * Returns true if this is the case, false ortherwise. When true is returned,
2002 * @ctx->subop is updated with the set of instructions to be passed to the
2003 * controller driver.
2004 */
2005static bool
2006nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2007 struct nand_op_parser_ctx *ctx)
2008{
2009 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2010 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2011 const struct nand_op_instr *instr = ctx->subop.instrs;
2012 unsigned int i, ninstrs;
2013
2014 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2015 /*
2016 * The pattern instruction does not match the operation
2017 * instruction. If the instruction is marked optional in the
2018 * pattern definition, we skip the pattern element and continue
2019 * to the next one. If the element is mandatory, there's no
2020 * match and we can return false directly.
2021 */
2022 if (instr->type != pat->elems[i].type) {
2023 if (!pat->elems[i].optional)
2024 return false;
2025
2026 continue;
2027 }
2028
2029 /*
2030 * Now check the pattern element constraints. If the pattern is
2031 * not able to handle the whole instruction in a single step,
2032 * we have to split it.
2033 * The last_instr_end_off value comes back updated to point to
2034 * the position where we have to split the instruction (the
2035 * start of the next subop chunk).
2036 */
2037 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2038 &instr_offset)) {
2039 ninstrs++;
2040 i++;
2041 break;
2042 }
2043
2044 instr++;
2045 ninstrs++;
2046 instr_offset = 0;
2047 }
2048
2049 /*
2050 * This can happen if all instructions of a pattern are optional.
2051 * Still, if there's not at least one instruction handled by this
2052 * pattern, this is not a match, and we should try the next one (if
2053 * any).
2054 */
2055 if (!ninstrs)
2056 return false;
2057
2058 /*
2059 * We had a match on the pattern head, but the pattern may be longer
2060 * than the instructions we're asked to execute. We need to make sure
2061 * there's no mandatory elements in the pattern tail.
2062 */
2063 for (; i < pat->nelems; i++) {
2064 if (!pat->elems[i].optional)
2065 return false;
2066 }
2067
2068 /*
2069 * We have a match: update the subop structure accordingly and return
2070 * true.
2071 */
2072 ctx->subop.ninstrs = ninstrs;
2073 ctx->subop.last_instr_end_off = instr_offset;
2074
2075 return true;
2076}
2077
2078#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2079static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2080{
2081 const struct nand_op_instr *instr;
2082 char *prefix = " ";
2083 unsigned int i;
2084
2085 pr_debug("executing subop:\n");
2086
2087 for (i = 0; i < ctx->ninstrs; i++) {
2088 instr = &ctx->instrs[i];
2089
2090 if (instr == &ctx->subop.instrs[0])
2091 prefix = " ->";
2092
2093 switch (instr->type) {
2094 case NAND_OP_CMD_INSTR:
2095 pr_debug("%sCMD [0x%02x]\n", prefix,
2096 instr->ctx.cmd.opcode);
2097 break;
2098 case NAND_OP_ADDR_INSTR:
2099 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2100 instr->ctx.addr.naddrs,
2101 instr->ctx.addr.naddrs < 64 ?
2102 instr->ctx.addr.naddrs : 64,
2103 instr->ctx.addr.addrs);
2104 break;
2105 case NAND_OP_DATA_IN_INSTR:
2106 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2107 instr->ctx.data.len,
2108 instr->ctx.data.force_8bit ?
2109 ", force 8-bit" : "");
2110 break;
2111 case NAND_OP_DATA_OUT_INSTR:
2112 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2113 instr->ctx.data.len,
2114 instr->ctx.data.force_8bit ?
2115 ", force 8-bit" : "");
2116 break;
2117 case NAND_OP_WAITRDY_INSTR:
2118 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2119 instr->ctx.waitrdy.timeout_ms);
2120 break;
2121 }
2122
2123 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2124 prefix = " ";
2125 }
2126}
2127#else
2128static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2129{
2130 /* NOP */
2131}
2132#endif
2133
2134/**
2135 * nand_op_parser_exec_op - exec_op parser
2136 * @chip: the NAND chip
2137 * @parser: patterns description provided by the controller driver
2138 * @op: the NAND operation to address
2139 * @check_only: when true, the function only checks if @op can be handled but
2140 * does not execute the operation
2141 *
2142 * Helper function designed to ease integration of NAND controller drivers that
2143 * only support a limited set of instruction sequences. The supported sequences
2144 * are described in @parser, and the framework takes care of splitting @op into
2145 * multiple sub-operations (if required) and pass them back to the ->exec()
2146 * callback of the matching pattern if @check_only is set to false.
2147 *
2148 * NAND controller drivers should call this function from their own ->exec_op()
2149 * implementation.
2150 *
2151 * Returns 0 on success, a negative error code otherwise. A failure can be
2152 * caused by an unsupported operation (none of the supported patterns is able
2153 * to handle the requested operation), or an error returned by one of the
2154 * matching pattern->exec() hook.
2155 */
2156int nand_op_parser_exec_op(struct nand_chip *chip,
2157 const struct nand_op_parser *parser,
2158 const struct nand_operation *op, bool check_only)
2159{
2160 struct nand_op_parser_ctx ctx = {
2161 .subop.instrs = op->instrs,
2162 .instrs = op->instrs,
2163 .ninstrs = op->ninstrs,
2164 };
2165 unsigned int i;
2166
2167 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2168 int ret;
2169
2170 for (i = 0; i < parser->npatterns; i++) {
2171 const struct nand_op_parser_pattern *pattern;
2172
2173 pattern = &parser->patterns[i];
2174 if (!nand_op_parser_match_pat(pattern, &ctx))
2175 continue;
2176
2177 nand_op_parser_trace(&ctx);
2178
2179 if (check_only)
2180 break;
2181
2182 ret = pattern->exec(chip, &ctx.subop);
2183 if (ret)
2184 return ret;
2185
2186 break;
2187 }
2188
2189 if (i == parser->npatterns) {
2190 pr_debug("->exec_op() parser: pattern not found!\n");
2191 return -ENOTSUPP;
2192 }
2193
2194 /*
2195 * Update the context structure by pointing to the start of the
2196 * next subop.
2197 */
2198 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2199 if (ctx.subop.last_instr_end_off)
2200 ctx.subop.instrs -= 1;
2201
2202 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2203 }
2204
2205 return 0;
2206}
2207EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2208
2209static bool nand_instr_is_data(const struct nand_op_instr *instr)
2210{
2211 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2212 instr->type == NAND_OP_DATA_OUT_INSTR);
2213}
2214
2215static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2216 unsigned int instr_idx)
2217{
2218 return subop && instr_idx < subop->ninstrs;
2219}
2220
Miquel Raynal760c4352018-07-19 00:09:12 +02002221static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2222 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002223{
2224 if (instr_idx)
2225 return 0;
2226
2227 return subop->first_instr_start_off;
2228}
2229
2230/**
2231 * nand_subop_get_addr_start_off - Get the start offset in an address array
2232 * @subop: The entire sub-operation
2233 * @instr_idx: Index of the instruction inside the sub-operation
2234 *
2235 * During driver development, one could be tempted to directly use the
2236 * ->addr.addrs field of address instructions. This is wrong as address
2237 * instructions might be split.
2238 *
2239 * Given an address instruction, returns the offset of the first cycle to issue.
2240 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002241unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2242 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002243{
Miquel Raynal760c4352018-07-19 00:09:12 +02002244 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2245 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2246 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002247
2248 return nand_subop_get_start_off(subop, instr_idx);
2249}
2250EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2251
2252/**
2253 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2254 * @subop: The entire sub-operation
2255 * @instr_idx: Index of the instruction inside the sub-operation
2256 *
2257 * During driver development, one could be tempted to directly use the
2258 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2259 * might be split.
2260 *
2261 * Given an address instruction, returns the number of address cycle to issue.
2262 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002263unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2264 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002265{
2266 int start_off, end_off;
2267
Miquel Raynal760c4352018-07-19 00:09:12 +02002268 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2269 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2270 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002271
2272 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2273
2274 if (instr_idx == subop->ninstrs - 1 &&
2275 subop->last_instr_end_off)
2276 end_off = subop->last_instr_end_off;
2277 else
2278 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2279
2280 return end_off - start_off;
2281}
2282EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2283
2284/**
2285 * nand_subop_get_data_start_off - Get the start offset in a data array
2286 * @subop: The entire sub-operation
2287 * @instr_idx: Index of the instruction inside the sub-operation
2288 *
2289 * During driver development, one could be tempted to directly use the
2290 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2291 * instructions might be split.
2292 *
2293 * Given a data instruction, returns the offset to start from.
2294 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002295unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2296 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002297{
Miquel Raynal760c4352018-07-19 00:09:12 +02002298 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2299 !nand_instr_is_data(&subop->instrs[instr_idx])))
2300 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002301
2302 return nand_subop_get_start_off(subop, instr_idx);
2303}
2304EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2305
2306/**
2307 * nand_subop_get_data_len - Get the number of bytes to retrieve
2308 * @subop: The entire sub-operation
2309 * @instr_idx: Index of the instruction inside the sub-operation
2310 *
2311 * During driver development, one could be tempted to directly use the
2312 * ->data->len field of a data instruction. This is wrong as data instructions
2313 * might be split.
2314 *
2315 * Returns the length of the chunk of data to send/receive.
2316 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002317unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2318 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002319{
2320 int start_off = 0, end_off;
2321
Miquel Raynal760c4352018-07-19 00:09:12 +02002322 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2323 !nand_instr_is_data(&subop->instrs[instr_idx])))
2324 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002325
2326 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2327
2328 if (instr_idx == subop->ninstrs - 1 &&
2329 subop->last_instr_end_off)
2330 end_off = subop->last_instr_end_off;
2331 else
2332 end_off = subop->instrs[instr_idx].ctx.data.len;
2333
2334 return end_off - start_off;
2335}
2336EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002339 * nand_reset - Reset and initialize a NAND device
2340 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002341 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002342 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002343 * Save the timings data structure, then apply SDR timings mode 0 (see
2344 * nand_reset_data_interface for details), do the reset operation, and
2345 * apply back the previous timings.
2346 *
2347 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002348 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002349int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002350{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002351 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002352 int ret;
2353
Boris Brezillon104e4422017-03-16 09:35:58 +01002354 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002355 if (ret)
2356 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002357
Boris Brezillon73f907f2016-10-24 16:46:20 +02002358 /*
2359 * The CS line has to be released before we can apply the new NAND
Boris Brezillon1d017852018-11-11 08:55:14 +01002360 * interface settings, hence this weird nand_select_target()
2361 * nand_deselect_target() dance.
Boris Brezillon73f907f2016-10-24 16:46:20 +02002362 */
Boris Brezillon1d017852018-11-11 08:55:14 +01002363 nand_select_target(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002364 ret = nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01002365 nand_deselect_target(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002366 if (ret)
2367 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002368
Miquel Raynal107b7d62018-03-19 14:47:25 +01002369 /*
2370 * A nand_reset_data_interface() put both the NAND chip and the NAND
2371 * controller in timings mode 0. If the default mode for this chip is
2372 * also 0, no need to proceed to the change again. Plus, at probe time,
2373 * nand_setup_data_interface() uses ->set/get_features() which would
2374 * fail anyway as the parameter page is not available yet.
2375 */
2376 if (!chip->onfi_timing_mode_default)
2377 return 0;
2378
Miquel Raynal17fa8042017-11-30 18:01:31 +01002379 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002380 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002381 if (ret)
2382 return ret;
2383
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002384 return 0;
2385}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002386EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002387
2388/**
Boris Brezillon45240362018-09-07 00:38:40 +02002389 * nand_get_features - wrapper to perform a GET_FEATURE
2390 * @chip: NAND chip info structure
2391 * @addr: feature address
2392 * @subfeature_param: the subfeature parameters, a four bytes array
2393 *
2394 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2395 * operation cannot be handled.
2396 */
2397int nand_get_features(struct nand_chip *chip, int addr,
2398 u8 *subfeature_param)
2399{
2400 if (!nand_supports_get_features(chip, addr))
2401 return -ENOTSUPP;
2402
2403 if (chip->legacy.get_features)
2404 return chip->legacy.get_features(chip, addr, subfeature_param);
2405
2406 return nand_get_features_op(chip, addr, subfeature_param);
2407}
Boris Brezillon45240362018-09-07 00:38:40 +02002408
2409/**
2410 * nand_set_features - wrapper to perform a SET_FEATURE
2411 * @chip: NAND chip info structure
2412 * @addr: feature address
2413 * @subfeature_param: the subfeature parameters, a four bytes array
2414 *
2415 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2416 * operation cannot be handled.
2417 */
2418int nand_set_features(struct nand_chip *chip, int addr,
2419 u8 *subfeature_param)
2420{
2421 if (!nand_supports_set_features(chip, addr))
2422 return -ENOTSUPP;
2423
2424 if (chip->legacy.set_features)
2425 return chip->legacy.set_features(chip, addr, subfeature_param);
2426
2427 return nand_set_features_op(chip, addr, subfeature_param);
2428}
Boris Brezillon45240362018-09-07 00:38:40 +02002429
2430/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002431 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2432 * @buf: buffer to test
2433 * @len: buffer length
2434 * @bitflips_threshold: maximum number of bitflips
2435 *
2436 * Check if a buffer contains only 0xff, which means the underlying region
2437 * has been erased and is ready to be programmed.
2438 * The bitflips_threshold specify the maximum number of bitflips before
2439 * considering the region is not erased.
2440 * Note: The logic of this function has been extracted from the memweight
2441 * implementation, except that nand_check_erased_buf function exit before
2442 * testing the whole buffer if the number of bitflips exceed the
2443 * bitflips_threshold value.
2444 *
2445 * Returns a positive number of bitflips less than or equal to
2446 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2447 * threshold.
2448 */
2449static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2450{
2451 const unsigned char *bitmap = buf;
2452 int bitflips = 0;
2453 int weight;
2454
2455 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2456 len--, bitmap++) {
2457 weight = hweight8(*bitmap);
2458 bitflips += BITS_PER_BYTE - weight;
2459 if (unlikely(bitflips > bitflips_threshold))
2460 return -EBADMSG;
2461 }
2462
2463 for (; len >= sizeof(long);
2464 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002465 unsigned long d = *((unsigned long *)bitmap);
2466 if (d == ~0UL)
2467 continue;
2468 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002469 bitflips += BITS_PER_LONG - weight;
2470 if (unlikely(bitflips > bitflips_threshold))
2471 return -EBADMSG;
2472 }
2473
2474 for (; len > 0; len--, bitmap++) {
2475 weight = hweight8(*bitmap);
2476 bitflips += BITS_PER_BYTE - weight;
2477 if (unlikely(bitflips > bitflips_threshold))
2478 return -EBADMSG;
2479 }
2480
2481 return bitflips;
2482}
2483
2484/**
2485 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2486 * 0xff data
2487 * @data: data buffer to test
2488 * @datalen: data length
2489 * @ecc: ECC buffer
2490 * @ecclen: ECC length
2491 * @extraoob: extra OOB buffer
2492 * @extraooblen: extra OOB length
2493 * @bitflips_threshold: maximum number of bitflips
2494 *
2495 * Check if a data buffer and its associated ECC and OOB data contains only
2496 * 0xff pattern, which means the underlying region has been erased and is
2497 * ready to be programmed.
2498 * The bitflips_threshold specify the maximum number of bitflips before
2499 * considering the region as not erased.
2500 *
2501 * Note:
2502 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2503 * different from the NAND page size. When fixing bitflips, ECC engines will
2504 * report the number of errors per chunk, and the NAND core infrastructure
2505 * expect you to return the maximum number of bitflips for the whole page.
2506 * This is why you should always use this function on a single chunk and
2507 * not on the whole page. After checking each chunk you should update your
2508 * max_bitflips value accordingly.
2509 * 2/ When checking for bitflips in erased pages you should not only check
2510 * the payload data but also their associated ECC data, because a user might
2511 * have programmed almost all bits to 1 but a few. In this case, we
2512 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2513 * this case.
2514 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2515 * data are protected by the ECC engine.
2516 * It could also be used if you support subpages and want to attach some
2517 * extra OOB data to an ECC chunk.
2518 *
2519 * Returns a positive number of bitflips less than or equal to
2520 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2521 * threshold. In case of success, the passed buffers are filled with 0xff.
2522 */
2523int nand_check_erased_ecc_chunk(void *data, int datalen,
2524 void *ecc, int ecclen,
2525 void *extraoob, int extraooblen,
2526 int bitflips_threshold)
2527{
2528 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2529
2530 data_bitflips = nand_check_erased_buf(data, datalen,
2531 bitflips_threshold);
2532 if (data_bitflips < 0)
2533 return data_bitflips;
2534
2535 bitflips_threshold -= data_bitflips;
2536
2537 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2538 if (ecc_bitflips < 0)
2539 return ecc_bitflips;
2540
2541 bitflips_threshold -= ecc_bitflips;
2542
2543 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2544 bitflips_threshold);
2545 if (extraoob_bitflips < 0)
2546 return extraoob_bitflips;
2547
2548 if (data_bitflips)
2549 memset(data, 0xff, datalen);
2550
2551 if (ecc_bitflips)
2552 memset(ecc, 0xff, ecclen);
2553
2554 if (extraoob_bitflips)
2555 memset(extraoob, 0xff, extraooblen);
2556
2557 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2558}
2559EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2560
2561/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002562 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002563 * @chip: nand chip info structure
2564 * @buf: buffer to store read data
2565 * @oob_required: caller requires OOB data read to chip->oob_poi
2566 * @page: page number to read
2567 *
2568 * Returns -ENOTSUPP unconditionally.
2569 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002570int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2571 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002572{
2573 return -ENOTSUPP;
2574}
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002575
2576/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002577 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002578 * @chip: nand chip info structure
2579 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002580 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002581 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002582 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002583 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002584 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002585int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2586 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002587{
Boris Brezillonb9761682018-09-06 14:05:20 +02002588 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002589 int ret;
2590
Boris Brezillon25f815f2017-11-30 18:01:30 +01002591 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002592 if (ret)
2593 return ret;
2594
2595 if (oob_required) {
2596 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2597 false);
2598 if (ret)
2599 return ret;
2600 }
2601
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002602 return 0;
2603}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002604EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002605
2606/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002607 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002608 * @chip: nand chip info structure
2609 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002610 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002611 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002612 *
2613 * We need a special oob layout and handling even when OOB isn't used.
2614 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002615static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07002616 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002617{
Boris Brezillonb9761682018-09-06 14:05:20 +02002618 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08002619 int eccsize = chip->ecc.size;
2620 int eccbytes = chip->ecc.bytes;
2621 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002622 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002623
Boris Brezillon25f815f2017-11-30 18:01:30 +01002624 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2625 if (ret)
2626 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002627
2628 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002629 ret = nand_read_data_op(chip, buf, eccsize, false);
2630 if (ret)
2631 return ret;
2632
David Brownell52ff49d2009-03-04 12:01:36 -08002633 buf += eccsize;
2634
2635 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002636 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2637 false);
2638 if (ret)
2639 return ret;
2640
David Brownell52ff49d2009-03-04 12:01:36 -08002641 oob += chip->ecc.prepad;
2642 }
2643
Boris Brezillon97d90da2017-11-30 18:01:29 +01002644 ret = nand_read_data_op(chip, oob, eccbytes, false);
2645 if (ret)
2646 return ret;
2647
David Brownell52ff49d2009-03-04 12:01:36 -08002648 oob += eccbytes;
2649
2650 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002651 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2652 false);
2653 if (ret)
2654 return ret;
2655
David Brownell52ff49d2009-03-04 12:01:36 -08002656 oob += chip->ecc.postpad;
2657 }
2658 }
2659
2660 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002661 if (size) {
2662 ret = nand_read_data_op(chip, oob, size, false);
2663 if (ret)
2664 return ret;
2665 }
David Brownell52ff49d2009-03-04 12:01:36 -08002666
2667 return 0;
2668}
2669
2670/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002671 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002672 * @chip: nand chip info structure
2673 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002674 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002675 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002676 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002677static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
2678 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
Boris Brezillonb9761682018-09-06 14:05:20 +02002680 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002681 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002682 int eccbytes = chip->ecc.bytes;
2683 int eccsteps = chip->ecc.steps;
2684 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002685 uint8_t *ecc_calc = chip->ecc.calc_buf;
2686 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002687 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002688
Boris Brezillonb9761682018-09-06 14:05:20 +02002689 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002690
2691 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002692 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002693
Boris Brezillon846031d2016-02-03 20:11:00 +01002694 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2695 chip->ecc.total);
2696 if (ret)
2697 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002698
2699 eccsteps = chip->ecc.steps;
2700 p = buf;
2701
2702 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2703 int stat;
2704
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002705 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07002706 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002707 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002708 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002709 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002710 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2711 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002712 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002713 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01002714}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302717 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002718 * @chip: nand chip info structure
2719 * @data_offs: offset of requested data within the page
2720 * @readlen: data length
2721 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08002722 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01002723 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002724static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
2725 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01002726{
Boris Brezillonb9761682018-09-06 14:05:20 +02002727 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002728 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002729 uint8_t *p;
2730 int data_col_addr, i, gaps = 0;
2731 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
2732 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01002733 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07002734 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01002735 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01002736
Brian Norris7854d3f2011-06-23 14:12:08 -07002737 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01002738 start_step = data_offs / chip->ecc.size;
2739 end_step = (data_offs + readlen - 1) / chip->ecc.size;
2740 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10302741 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01002742
Brian Norris8b6e50c2011-05-25 14:59:01 -07002743 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01002744 datafrag_len = num_steps * chip->ecc.size;
2745 eccfrag_len = num_steps * chip->ecc.bytes;
2746
2747 data_col_addr = start_step * chip->ecc.size;
2748 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01002749 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01002750 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002751 if (ret)
2752 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002753
Brian Norris8b6e50c2011-05-25 14:59:01 -07002754 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01002755 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002756 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01002757
Brian Norris8b6e50c2011-05-25 14:59:01 -07002758 /*
2759 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07002760 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07002761 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002762 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
2763 if (ret)
2764 return ret;
2765
2766 if (oobregion.length < eccfrag_len)
2767 gaps = 1;
2768
Alexey Korolev3d459552008-05-15 17:23:18 +01002769 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002770 ret = nand_change_read_column_op(chip, mtd->writesize,
2771 chip->oob_poi, mtd->oobsize,
2772 false);
2773 if (ret)
2774 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002775 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002776 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002777 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07002778 * about buswidth alignment in read_buf.
2779 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002780 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01002781 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01002782 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002783 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01002784 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
2785 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002786 aligned_len++;
2787
Boris Brezillon97d90da2017-11-30 18:01:29 +01002788 ret = nand_change_read_column_op(chip,
2789 mtd->writesize + aligned_pos,
2790 &chip->oob_poi[aligned_pos],
2791 aligned_len, false);
2792 if (ret)
2793 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002794 }
2795
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002796 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01002797 chip->oob_poi, index, eccfrag_len);
2798 if (ret)
2799 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002800
2801 p = bufpoi + data_col_addr;
2802 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
2803 int stat;
2804
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002805 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002806 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002807 if (stat == -EBADMSG &&
2808 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2809 /* check for empty pages with bitflips */
2810 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002811 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002812 chip->ecc.bytes,
2813 NULL, 0,
2814 chip->ecc.strength);
2815 }
2816
Mike Dunn3f91e942012-04-25 12:06:09 -07002817 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002818 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002819 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01002820 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002821 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2822 }
Alexey Korolev3d459552008-05-15 17:23:18 +01002823 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002824 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01002825}
2826
2827/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002828 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002829 * @chip: nand chip info structure
2830 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002831 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002832 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002833 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002834 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002835 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002836static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
2837 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002838{
Boris Brezillonb9761682018-09-06 14:05:20 +02002839 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002840 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002841 int eccbytes = chip->ecc.bytes;
2842 int eccsteps = chip->ecc.steps;
2843 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002844 uint8_t *ecc_calc = chip->ecc.calc_buf;
2845 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002846 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002847
Boris Brezillon25f815f2017-11-30 18:01:30 +01002848 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2849 if (ret)
2850 return ret;
2851
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002852 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02002853 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002854
2855 ret = nand_read_data_op(chip, p, eccsize, false);
2856 if (ret)
2857 return ret;
2858
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002859 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002860 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002861
2862 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2863 if (ret)
2864 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002865
Boris Brezillon846031d2016-02-03 20:11:00 +01002866 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2867 chip->ecc.total);
2868 if (ret)
2869 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002870
2871 eccsteps = chip->ecc.steps;
2872 p = buf;
2873
2874 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2875 int stat;
2876
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002877 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002878 if (stat == -EBADMSG &&
2879 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2880 /* check for empty pages with bitflips */
2881 stat = nand_check_erased_ecc_chunk(p, eccsize,
2882 &ecc_code[i], eccbytes,
2883 NULL, 0,
2884 chip->ecc.strength);
2885 }
2886
Mike Dunn3f91e942012-04-25 12:06:09 -07002887 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002888 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002889 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002890 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002891 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2892 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002893 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002894 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002895}
2896
2897/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002898 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07002899 * @chip: nand chip info structure
2900 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002901 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002902 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002903 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002904 * Hardware ECC for large page chips, require OOB to be read first. For this
2905 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2906 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2907 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2908 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002909 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002910static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
2911 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002912{
Boris Brezillonb9761682018-09-06 14:05:20 +02002913 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002914 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002915 int eccbytes = chip->ecc.bytes;
2916 int eccsteps = chip->ecc.steps;
2917 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002918 uint8_t *ecc_code = chip->ecc.code_buf;
2919 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002920 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002921
2922 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01002923 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2924 if (ret)
2925 return ret;
2926
2927 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2928 if (ret)
2929 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002930
Boris Brezillon846031d2016-02-03 20:11:00 +01002931 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2932 chip->ecc.total);
2933 if (ret)
2934 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002935
2936 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2937 int stat;
2938
Boris Brezillonec476362018-09-06 14:05:17 +02002939 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002940
2941 ret = nand_read_data_op(chip, p, eccsize, false);
2942 if (ret)
2943 return ret;
2944
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002945 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002946
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002947 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002948 if (stat == -EBADMSG &&
2949 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2950 /* check for empty pages with bitflips */
2951 stat = nand_check_erased_ecc_chunk(p, eccsize,
2952 &ecc_code[i], eccbytes,
2953 NULL, 0,
2954 chip->ecc.strength);
2955 }
2956
Mike Dunn3f91e942012-04-25 12:06:09 -07002957 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002958 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002959 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002960 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002961 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2962 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002963 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002964 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002965}
2966
2967/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002968 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07002969 * @chip: nand chip info structure
2970 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002971 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002972 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002973 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002974 * The hw generator calculates the error syndrome automatically. Therefore we
2975 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002976 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002977static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
2978 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002979{
Boris Brezillonb9761682018-09-06 14:05:20 +02002980 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002981 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002982 int eccbytes = chip->ecc.bytes;
2983 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002984 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002985 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002986 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07002987 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002988
Boris Brezillon25f815f2017-11-30 18:01:30 +01002989 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2990 if (ret)
2991 return ret;
2992
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002993 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2994 int stat;
2995
Boris Brezillonec476362018-09-06 14:05:17 +02002996 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002997
2998 ret = nand_read_data_op(chip, p, eccsize, false);
2999 if (ret)
3000 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003001
3002 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003003 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3004 false);
3005 if (ret)
3006 return ret;
3007
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003008 oob += chip->ecc.prepad;
3009 }
3010
Boris Brezillonec476362018-09-06 14:05:17 +02003011 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003012
3013 ret = nand_read_data_op(chip, oob, eccbytes, false);
3014 if (ret)
3015 return ret;
3016
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003017 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003018
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003019 oob += eccbytes;
3020
3021 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003022 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3023 false);
3024 if (ret)
3025 return ret;
3026
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003027 oob += chip->ecc.postpad;
3028 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003029
3030 if (stat == -EBADMSG &&
3031 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3032 /* check for empty pages with bitflips */
3033 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3034 oob - eccpadbytes,
3035 eccpadbytes,
3036 NULL, 0,
3037 chip->ecc.strength);
3038 }
3039
3040 if (stat < 0) {
3041 mtd->ecc_stats.failed++;
3042 } else {
3043 mtd->ecc_stats.corrected += stat;
3044 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3045 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003046 }
3047
3048 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003049 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003050 if (i) {
3051 ret = nand_read_data_op(chip, oob, i, false);
3052 if (ret)
3053 return ret;
3054 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003055
Mike Dunn3f91e942012-04-25 12:06:09 -07003056 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003057}
3058
3059/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003060 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon08136212018-11-11 08:55:03 +01003061 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003062 * @oob: oob destination address
3063 * @ops: oob ops structure
3064 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003065 */
Boris Brezillon08136212018-11-11 08:55:03 +01003066static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003067 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003068{
Boris Brezillon08136212018-11-11 08:55:03 +01003069 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003070 int ret;
3071
Florian Fainellif8ac0412010-09-07 13:23:43 +02003072 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003073
Brian Norris0612b9d2011-08-30 18:45:40 -07003074 case MTD_OPS_PLACE_OOB:
3075 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003076 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3077 return oob + len;
3078
Boris Brezillon846031d2016-02-03 20:11:00 +01003079 case MTD_OPS_AUTO_OOB:
3080 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3081 ops->ooboffs, len);
3082 BUG_ON(ret);
3083 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003084
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003085 default:
3086 BUG();
3087 }
3088 return NULL;
3089}
3090
3091/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003092 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003093 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003094 * @retry_mode: the retry mode to use
3095 *
3096 * Some vendors supply a special command to shift the Vt threshold, to be used
3097 * when there are too many bitflips in a page (i.e., ECC error). After setting
3098 * a new threshold, the host should retry reading the page.
3099 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003100static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003101{
Brian Norrisba84fb52014-01-03 15:13:33 -08003102 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3103
3104 if (retry_mode >= chip->read_retries)
3105 return -EINVAL;
3106
3107 if (!chip->setup_read_retry)
3108 return -EOPNOTSUPP;
3109
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003110 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003111}
3112
Boris Brezillon85e08e52018-07-27 09:44:17 +02003113static void nand_wait_readrdy(struct nand_chip *chip)
3114{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003115 const struct nand_sdr_timings *sdr;
3116
Boris Brezillon85e08e52018-07-27 09:44:17 +02003117 if (!(chip->options & NAND_NEED_READRDY))
3118 return;
3119
Boris Brezillon52f05b62018-07-27 09:44:18 +02003120 sdr = nand_get_sdr_timings(&chip->data_interface);
3121 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003122}
3123
Brian Norrisba84fb52014-01-03 15:13:33 -08003124/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003125 * nand_do_read_ops - [INTERN] Read data with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003126 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003127 * @from: offset to read from
3128 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003129 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003130 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003131 */
Boris Brezillon08136212018-11-11 08:55:03 +01003132static int nand_do_read_ops(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003133 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003134{
Brian Norrise47f3db2012-05-02 10:14:56 -07003135 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris Brezillon08136212018-11-11 08:55:03 +01003136 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003137 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003138 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003139 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003140 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003141
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003142 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003143 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003144 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003145 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003146 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003148 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003149 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003151 realpage = (int)(from >> chip->page_shift);
3152 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003154 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003156 buf = ops->datbuf;
3157 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003158 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003159
Florian Fainellif8ac0412010-09-07 13:23:43 +02003160 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003161 unsigned int ecc_failures = mtd->ecc_stats.failed;
3162
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003163 bytes = min(mtd->writesize - col, readlen);
3164 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003165
Kamal Dasu66507c72014-05-01 20:51:19 -04003166 if (!aligned)
3167 use_bufpoi = 1;
3168 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003169 use_bufpoi = !virt_addr_valid(buf) ||
3170 !IS_ALIGNED((unsigned long)buf,
3171 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003172 else
3173 use_bufpoi = 0;
3174
Brian Norris8b6e50c2011-05-25 14:59:01 -07003175 /* Is the current page in the buffer? */
Boris Brezillond9745412018-10-28 16:12:45 +01003176 if (realpage != chip->pagecache.page || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003177 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003178
3179 if (use_bufpoi && aligned)
3180 pr_debug("%s: using read bounce buffer for buf@%p\n",
3181 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Brian Norrisba84fb52014-01-03 15:13:33 -08003183read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003184 /*
3185 * Now read the page into the buffer. Absent an error,
3186 * the read methods return max bitflips per ecc step.
3187 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003188 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003189 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003190 oob_required,
3191 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003192 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3193 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003194 ret = chip->ecc.read_subpage(chip, col, bytes,
3195 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003196 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003197 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003198 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003199 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003200 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003201 /* Invalidate page cache */
Boris Brezillond9745412018-10-28 16:12:45 +01003202 chip->pagecache.page = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003203 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003204 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003205
3206 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003207 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003208 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003209 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003210 (ops->mode != MTD_OPS_RAW)) {
Boris Brezillond9745412018-10-28 16:12:45 +01003211 chip->pagecache.page = realpage;
3212 chip->pagecache.bitflips = ret;
Mike Dunnedbc45402012-04-25 12:06:11 -07003213 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003214 /* Invalidate page cache */
Boris Brezillond9745412018-10-28 16:12:45 +01003215 chip->pagecache.page = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003216 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003217 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003219
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003220 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003221 int toread = min(oobreadlen, max_oobsize);
3222
3223 if (toread) {
Boris Brezillon08136212018-11-11 08:55:03 +01003224 oob = nand_transfer_oob(chip, oob, ops,
3225 toread);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003226 oobreadlen -= toread;
3227 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003228 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003229
Boris Brezillon85e08e52018-07-27 09:44:17 +02003230 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003231
Brian Norrisba84fb52014-01-03 15:13:33 -08003232 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003233 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003234 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003235 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003236 retry_mode);
3237 if (ret < 0)
3238 break;
3239
3240 /* Reset failures; retry */
3241 mtd->ecc_stats.failed = ecc_failures;
3242 goto read_retry;
3243 } else {
3244 /* No more retry modes; real failure */
3245 ecc_fail = true;
3246 }
3247 }
3248
3249 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003250 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003251 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003252 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003253 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003254 max_bitflips = max_t(unsigned int, max_bitflips,
Boris Brezillond9745412018-10-28 16:12:45 +01003255 chip->pagecache.bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003258 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003259
Brian Norrisba84fb52014-01-03 15:13:33 -08003260 /* Reset to retry mode 0 */
3261 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003262 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003263 if (ret < 0)
3264 break;
3265 retry_mode = 0;
3266 }
3267
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003268 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Brian Norris8b6e50c2011-05-25 14:59:01 -07003271 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 col = 0;
3273 /* Increment page address */
3274 realpage++;
3275
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003276 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 /* Check, if we cross a chip boundary */
3278 if (!page) {
3279 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003280 nand_deselect_target(chip);
3281 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003284 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003286 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003287 if (oob)
3288 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Mike Dunn3f91e942012-04-25 12:06:09 -07003290 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003291 return ret;
3292
Brian Norrisb72f3df2013-12-03 11:04:14 -08003293 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003294 return -EBADMSG;
3295
Mike Dunnedbc45402012-04-25 12:06:11 -07003296 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003297}
3298
3299/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003300 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003301 * @chip: nand chip info structure
3302 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003303 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003304int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003305{
Boris Brezillonb9761682018-09-06 14:05:20 +02003306 struct mtd_info *mtd = nand_to_mtd(chip);
3307
Boris Brezillon97d90da2017-11-30 18:01:29 +01003308 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003309}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003310EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003311
3312/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003313 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003314 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003315 * @chip: nand chip info structure
3316 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003317 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003318static int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003319{
Boris Brezillonb9761682018-09-06 14:05:20 +02003320 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003321 int length = mtd->oobsize;
3322 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3323 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003324 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003325 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003326
Boris Brezillon97d90da2017-11-30 18:01:29 +01003327 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3328 if (ret)
3329 return ret;
3330
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003331 for (i = 0; i < chip->ecc.steps; i++) {
3332 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003333 int ret;
3334
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003335 pos = eccsize + i * (eccsize + chunk);
3336 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003337 ret = nand_change_read_column_op(chip, pos,
3338 NULL, 0,
3339 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003340 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003341 ret = nand_read_page_op(chip, page, pos, NULL,
3342 0);
3343
3344 if (ret)
3345 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003346 } else
3347 sndrnd = 1;
3348 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003349
3350 ret = nand_read_data_op(chip, bufpoi, toread, false);
3351 if (ret)
3352 return ret;
3353
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003354 bufpoi += toread;
3355 length -= toread;
3356 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003357 if (length > 0) {
3358 ret = nand_read_data_op(chip, bufpoi, length, false);
3359 if (ret)
3360 return ret;
3361 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003362
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003363 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003364}
3365
3366/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003367 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003368 * @chip: nand chip info structure
3369 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003370 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003371int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003372{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003373 struct mtd_info *mtd = nand_to_mtd(chip);
3374
Boris Brezillon97d90da2017-11-30 18:01:29 +01003375 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3376 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003377}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003378EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003379
3380/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003381 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003382 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003383 * @chip: nand chip info structure
3384 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003385 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003386static int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003387{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003388 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003389 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3390 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003391 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003392 const uint8_t *bufpoi = chip->oob_poi;
3393
3394 /*
3395 * data-ecc-data-ecc ... ecc-oob
3396 * or
3397 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3398 */
3399 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3400 pos = steps * (eccsize + chunk);
3401 steps = 0;
3402 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003403 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003404
Boris Brezillon97d90da2017-11-30 18:01:29 +01003405 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3406 if (ret)
3407 return ret;
3408
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003409 for (i = 0; i < steps; i++) {
3410 if (sndcmd) {
3411 if (mtd->writesize <= 512) {
3412 uint32_t fill = 0xFFFFFFFF;
3413
3414 len = eccsize;
3415 while (len > 0) {
3416 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003417
3418 ret = nand_write_data_op(chip, &fill,
3419 num, false);
3420 if (ret)
3421 return ret;
3422
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003423 len -= num;
3424 }
3425 } else {
3426 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003427 ret = nand_change_write_column_op(chip, pos,
3428 NULL, 0,
3429 false);
3430 if (ret)
3431 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003432 }
3433 } else
3434 sndcmd = 1;
3435 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003436
3437 ret = nand_write_data_op(chip, bufpoi, len, false);
3438 if (ret)
3439 return ret;
3440
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003441 bufpoi += len;
3442 length -= len;
3443 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003444 if (length > 0) {
3445 ret = nand_write_data_op(chip, bufpoi, length, false);
3446 if (ret)
3447 return ret;
3448 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003449
Boris Brezillon97d90da2017-11-30 18:01:29 +01003450 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003451}
3452
3453/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003454 * nand_do_read_oob - [INTERN] NAND read out-of-band
Boris Brezillon08136212018-11-11 08:55:03 +01003455 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003456 * @from: offset to read from
3457 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003459 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 */
Boris Brezillon08136212018-11-11 08:55:03 +01003461static int nand_do_read_oob(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003462 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
Boris Brezillon08136212018-11-11 08:55:03 +01003464 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003465 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003466 int page, realpage, chipnr;
Brian Norris041e4572011-06-23 16:45:24 -07003467 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003468 int readlen = ops->ooblen;
3469 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003470 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003471 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Brian Norris289c0522011-07-19 10:06:09 -07003473 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303474 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Brian Norris041e4572011-06-23 16:45:24 -07003476 stats = mtd->ecc_stats;
3477
Boris BREZILLON29f10582016-03-07 10:46:52 +01003478 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003479
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003480 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003481 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003483 /* Shift to get page */
3484 realpage = (int)(from >> chip->page_shift);
3485 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Florian Fainellif8ac0412010-09-07 13:23:43 +02003487 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003488 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003489 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003490 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003491 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003492
3493 if (ret < 0)
3494 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003495
3496 len = min(len, readlen);
Boris Brezillon08136212018-11-11 08:55:03 +01003497 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003498
Boris Brezillon85e08e52018-07-27 09:44:17 +02003499 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003500
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003501 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3502
Vitaly Wool70145682006-11-03 18:20:38 +03003503 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003504 if (!readlen)
3505 break;
3506
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003507 /* Increment page address */
3508 realpage++;
3509
3510 page = realpage & chip->pagemask;
3511 /* Check, if we cross a chip boundary */
3512 if (!page) {
3513 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003514 nand_deselect_target(chip);
3515 nand_select_target(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003518 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003520 ops->oobretlen = ops->ooblen - readlen;
3521
3522 if (ret < 0)
3523 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003524
3525 if (mtd->ecc_stats.failed - stats.failed)
3526 return -EBADMSG;
3527
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003528 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529}
3530
3531/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003532 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003533 * @mtd: MTD device structure
3534 * @from: offset to read from
3535 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003537 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003539static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3540 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541{
Boris Brezillon08136212018-11-11 08:55:03 +01003542 struct nand_chip *chip = mtd_to_nand(mtd);
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003543 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003544
3545 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003547 if (ops->mode != MTD_OPS_PLACE_OOB &&
3548 ops->mode != MTD_OPS_AUTO_OOB &&
3549 ops->mode != MTD_OPS_RAW)
3550 return -ENOTSUPP;
3551
Boris Brezillon013e6292018-11-20 11:57:20 +01003552 ret = nand_get_device(chip);
3553 if (ret)
3554 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003556 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01003557 ret = nand_do_read_oob(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003558 else
Boris Brezillon08136212018-11-11 08:55:03 +01003559 ret = nand_do_read_ops(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003560
Boris Brezillon08136212018-11-11 08:55:03 +01003561 nand_release_device(chip);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003562 return ret;
3563}
3564
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003565/**
3566 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003567 * @chip: nand chip info structure
3568 * @buf: data buffer
3569 * @oob_required: must write chip->oob_poi to OOB
3570 * @page: page number to write
3571 *
3572 * Returns -ENOTSUPP unconditionally.
3573 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003574int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3575 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003576{
3577 return -ENOTSUPP;
3578}
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003579
3580/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003581 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003582 * @chip: nand chip info structure
3583 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003584 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003585 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003586 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003587 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003588 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003589int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3590 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003591{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003592 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003593 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003594
Boris Brezillon25f815f2017-11-30 18:01:30 +01003595 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003596 if (ret)
3597 return ret;
3598
3599 if (oob_required) {
3600 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3601 false);
3602 if (ret)
3603 return ret;
3604 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003605
Boris Brezillon25f815f2017-11-30 18:01:30 +01003606 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003608EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003610/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003611 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003612 * @chip: nand chip info structure
3613 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003614 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003615 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003616 *
3617 * We need a special oob layout and handling even when ECC isn't checked.
3618 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003619static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003620 const uint8_t *buf, int oob_required,
3621 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003622{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003623 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003624 int eccsize = chip->ecc.size;
3625 int eccbytes = chip->ecc.bytes;
3626 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003627 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003628
Boris Brezillon25f815f2017-11-30 18:01:30 +01003629 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3630 if (ret)
3631 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003632
3633 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003634 ret = nand_write_data_op(chip, buf, eccsize, false);
3635 if (ret)
3636 return ret;
3637
David Brownell52ff49d2009-03-04 12:01:36 -08003638 buf += eccsize;
3639
3640 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003641 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3642 false);
3643 if (ret)
3644 return ret;
3645
David Brownell52ff49d2009-03-04 12:01:36 -08003646 oob += chip->ecc.prepad;
3647 }
3648
Boris Brezillon97d90da2017-11-30 18:01:29 +01003649 ret = nand_write_data_op(chip, oob, eccbytes, false);
3650 if (ret)
3651 return ret;
3652
David Brownell52ff49d2009-03-04 12:01:36 -08003653 oob += eccbytes;
3654
3655 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003656 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3657 false);
3658 if (ret)
3659 return ret;
3660
David Brownell52ff49d2009-03-04 12:01:36 -08003661 oob += chip->ecc.postpad;
3662 }
3663 }
3664
3665 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003666 if (size) {
3667 ret = nand_write_data_op(chip, oob, size, false);
3668 if (ret)
3669 return ret;
3670 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003671
Boris Brezillon25f815f2017-11-30 18:01:30 +01003672 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003673}
3674/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003675 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003676 * @chip: nand chip info structure
3677 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003678 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003679 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003680 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003681static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
3682 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003683{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003684 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003685 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003686 int eccbytes = chip->ecc.bytes;
3687 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003688 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003689 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003690
Brian Norris7854d3f2011-06-23 14:12:08 -07003691 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003692 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003693 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003694
Boris Brezillon846031d2016-02-03 20:11:00 +01003695 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3696 chip->ecc.total);
3697 if (ret)
3698 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003699
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003700 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003701}
3702
3703/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003704 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003705 * @chip: nand chip info structure
3706 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003707 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003708 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003709 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003710static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
3711 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003712{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003713 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003714 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003715 int eccbytes = chip->ecc.bytes;
3716 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003717 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003718 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003719
Boris Brezillon25f815f2017-11-30 18:01:30 +01003720 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3721 if (ret)
3722 return ret;
3723
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003724 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003725 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003726
3727 ret = nand_write_data_op(chip, p, eccsize, false);
3728 if (ret)
3729 return ret;
3730
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003731 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003732 }
3733
Boris Brezillon846031d2016-02-03 20:11:00 +01003734 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3735 chip->ecc.total);
3736 if (ret)
3737 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003738
Boris Brezillon97d90da2017-11-30 18:01:29 +01003739 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3740 if (ret)
3741 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003742
Boris Brezillon25f815f2017-11-30 18:01:30 +01003743 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003744}
3745
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303746
3747/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08003748 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303749 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07003750 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303751 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07003752 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303753 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003754 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303755 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003756static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
3757 uint32_t data_len, const uint8_t *buf,
3758 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303759{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003760 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303761 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003762 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303763 int ecc_size = chip->ecc.size;
3764 int ecc_bytes = chip->ecc.bytes;
3765 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303766 uint32_t start_step = offset / ecc_size;
3767 uint32_t end_step = (offset + data_len - 1) / ecc_size;
3768 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01003769 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303770
Boris Brezillon25f815f2017-11-30 18:01:30 +01003771 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3772 if (ret)
3773 return ret;
3774
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303775 for (step = 0; step < ecc_steps; step++) {
3776 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02003777 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303778
3779 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003780 ret = nand_write_data_op(chip, buf, ecc_size, false);
3781 if (ret)
3782 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303783
3784 /* mask ECC of un-touched subpages by padding 0xFF */
3785 if ((step < start_step) || (step > end_step))
3786 memset(ecc_calc, 0xff, ecc_bytes);
3787 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003788 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303789
3790 /* mask OOB of un-touched subpages by padding 0xFF */
3791 /* if oob_required, preserve OOB metadata of written subpage */
3792 if (!oob_required || (step < start_step) || (step > end_step))
3793 memset(oob_buf, 0xff, oob_bytes);
3794
Brian Norrisd6a950802013-08-08 17:16:36 -07003795 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303796 ecc_calc += ecc_bytes;
3797 oob_buf += oob_bytes;
3798 }
3799
3800 /* copy calculated ECC for whole page to chip->buffer->oob */
3801 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003802 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01003803 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3804 chip->ecc.total);
3805 if (ret)
3806 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303807
3808 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003809 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3810 if (ret)
3811 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303812
Boris Brezillon25f815f2017-11-30 18:01:30 +01003813 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303814}
3815
3816
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003817/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003818 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003819 * @chip: nand chip info structure
3820 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003821 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003822 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003823 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003824 * The hw generator calculates the error syndrome automatically. Therefore we
3825 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003826 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003827static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
3828 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003829{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003830 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003831 int i, eccsize = chip->ecc.size;
3832 int eccbytes = chip->ecc.bytes;
3833 int eccsteps = chip->ecc.steps;
3834 const uint8_t *p = buf;
3835 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003836 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003837
Boris Brezillon25f815f2017-11-30 18:01:30 +01003838 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3839 if (ret)
3840 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003841
3842 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003843 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003844
3845 ret = nand_write_data_op(chip, p, eccsize, false);
3846 if (ret)
3847 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003848
3849 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003850 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3851 false);
3852 if (ret)
3853 return ret;
3854
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003855 oob += chip->ecc.prepad;
3856 }
3857
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003858 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003859
3860 ret = nand_write_data_op(chip, oob, eccbytes, false);
3861 if (ret)
3862 return ret;
3863
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003864 oob += eccbytes;
3865
3866 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003867 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3868 false);
3869 if (ret)
3870 return ret;
3871
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003872 oob += chip->ecc.postpad;
3873 }
3874 }
3875
3876 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003877 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003878 if (i) {
3879 ret = nand_write_data_op(chip, oob, i, false);
3880 if (ret)
3881 return ret;
3882 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003883
Boris Brezillon25f815f2017-11-30 18:01:30 +01003884 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003885}
3886
3887/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003888 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07003889 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303890 * @offset: address offset within the page
3891 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07003892 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07003893 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07003894 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003895 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003896 */
Boris Brezillon08136212018-11-11 08:55:03 +01003897static int nand_write_page(struct nand_chip *chip, uint32_t offset,
3898 int data_len, const uint8_t *buf, int oob_required,
3899 int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003900{
Boris Brezillon08136212018-11-11 08:55:03 +01003901 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303902 int status, subpage;
3903
3904 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3905 chip->ecc.write_subpage)
3906 subpage = offset || (data_len < mtd->writesize);
3907 else
3908 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003909
David Woodhouse956e9442006-09-25 17:12:39 +01003910 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003911 status = chip->ecc.write_page_raw(chip, buf, oob_required,
3912 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303913 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003914 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
3915 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003916 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003917 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08003918
3919 if (status < 0)
3920 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003921
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003922 return 0;
3923}
3924
Florian Fainellif8ac0412010-09-07 13:23:43 +02003925#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003926
3927/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003928 * nand_do_write_ops - [INTERN] NAND write with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003929 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003930 * @to: offset to write to
3931 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003932 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003933 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003934 */
Boris Brezillon08136212018-11-11 08:55:03 +01003935static int nand_do_write_ops(struct nand_chip *chip, loff_t to,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003936 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003937{
Boris Brezillon08136212018-11-11 08:55:03 +01003938 struct mtd_info *mtd = nand_to_mtd(chip);
Corentin Labbe73600b62017-09-02 10:49:38 +02003939 int chipnr, realpage, page, column;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003940 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02003941
3942 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003943 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003944
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003945 uint8_t *oob = ops->oobbuf;
3946 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303947 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07003948 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003949
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003950 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003951 if (!writelen)
3952 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003953
Brian Norris8b6e50c2011-05-25 14:59:01 -07003954 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003955 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003956 pr_notice("%s: attempt to write non page aligned data\n",
3957 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003958 return -EINVAL;
3959 }
3960
Thomas Gleixner29072b92006-09-28 15:38:36 +02003961 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003962
Thomas Gleixner6a930962006-06-28 00:11:45 +02003963 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003964 nand_select_target(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02003965
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003966 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01003967 if (nand_check_wp(chip)) {
Huang Shijieb0bb6902012-11-19 14:43:29 +08003968 ret = -EIO;
3969 goto err_out;
3970 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003971
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003972 realpage = (int)(to >> chip->page_shift);
3973 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003974
3975 /* Invalidate the page cache, when we write to the cached page */
Boris Brezillond9745412018-10-28 16:12:45 +01003976 if (to <= ((loff_t)chip->pagecache.page << chip->page_shift) &&
3977 ((loff_t)chip->pagecache.page << chip->page_shift) < (to + ops->len))
3978 chip->pagecache.page = -1;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003979
Maxim Levitsky782ce792010-02-22 20:39:36 +02003980 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003981 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3982 ret = -EINVAL;
3983 goto err_out;
3984 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02003985
Florian Fainellif8ac0412010-09-07 13:23:43 +02003986 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003987 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003988 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003989 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02003990 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02003991
Kamal Dasu66507c72014-05-01 20:51:19 -04003992 if (part_pagewr)
3993 use_bufpoi = 1;
3994 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003995 use_bufpoi = !virt_addr_valid(buf) ||
3996 !IS_ALIGNED((unsigned long)buf,
3997 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003998 else
3999 use_bufpoi = 0;
4000
4001 /* Partial page write?, or need to use bounce buffer */
4002 if (use_bufpoi) {
4003 pr_debug("%s: using write bounce buffer for buf@%p\n",
4004 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004005 if (part_pagewr)
4006 bytes = min_t(int, bytes - column, writelen);
Boris Brezilloneeab7172018-10-28 15:27:55 +01004007 wbuf = nand_get_data_buf(chip);
4008 memset(wbuf, 0xff, mtd->writesize);
4009 memcpy(&wbuf[column], buf, bytes);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004010 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004011
Maxim Levitsky782ce792010-02-22 20:39:36 +02004012 if (unlikely(oob)) {
4013 size_t len = min(oobwritelen, oobmaxlen);
Boris Brezillon08136212018-11-11 08:55:03 +01004014 oob = nand_fill_oob(chip, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004015 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004016 } else {
4017 /* We still need to erase leftover OOB data */
4018 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004019 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004020
Boris Brezillon08136212018-11-11 08:55:03 +01004021 ret = nand_write_page(chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004022 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004023 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004024 if (ret)
4025 break;
4026
4027 writelen -= bytes;
4028 if (!writelen)
4029 break;
4030
Thomas Gleixner29072b92006-09-28 15:38:36 +02004031 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004032 buf += bytes;
4033 realpage++;
4034
4035 page = realpage & chip->pagemask;
4036 /* Check, if we cross a chip boundary */
4037 if (!page) {
4038 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004039 nand_deselect_target(chip);
4040 nand_select_target(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004041 }
4042 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004043
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004044 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004045 if (unlikely(oob))
4046 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004047
4048err_out:
Boris Brezillon1d017852018-11-11 08:55:14 +01004049 nand_deselect_target(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004050 return ret;
4051}
4052
4053/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004054 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004055 * @mtd: MTD device structure
4056 * @to: offset to write to
4057 * @len: number of bytes to write
4058 * @retlen: pointer to variable to store the number of written bytes
4059 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004060 *
4061 * NAND write with ECC. Used when performing writes in interrupt context, this
4062 * may for example be called by mtdoops when writing an oops while in panic.
4063 */
4064static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4065 size_t *retlen, const uint8_t *buf)
4066{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004067 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004068 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004069 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004070 int ret;
4071
Boris Brezillon1d017852018-11-11 08:55:14 +01004072 nand_select_target(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004073
4074 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004075 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004076
Brian Norris0ec56dc2015-02-28 02:02:30 -08004077 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004078 ops.len = len;
4079 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004080 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004081
Boris Brezillon08136212018-11-11 08:55:03 +01004082 ret = nand_do_write_ops(chip, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004083
Brian Norris4a89ff82011-08-30 18:45:45 -07004084 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004085 return ret;
4086}
4087
4088/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004089 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004090 * @mtd: MTD device structure
4091 * @to: offset to write to
4092 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004093 */
4094static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4095 struct mtd_oob_ops *ops)
4096{
Boris Brezillon08136212018-11-11 08:55:03 +01004097 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004098 int ret = -ENOTSUPP;
4099
4100 ops->retlen = 0;
4101
Boris Brezillon013e6292018-11-20 11:57:20 +01004102 ret = nand_get_device(chip);
4103 if (ret)
4104 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004105
Florian Fainellif8ac0412010-09-07 13:23:43 +02004106 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004107 case MTD_OPS_PLACE_OOB:
4108 case MTD_OPS_AUTO_OOB:
4109 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004110 break;
4111
4112 default:
4113 goto out;
4114 }
4115
4116 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01004117 ret = nand_do_write_oob(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004118 else
Boris Brezillon08136212018-11-11 08:55:03 +01004119 ret = nand_do_write_ops(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004120
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004121out:
Boris Brezillon08136212018-11-11 08:55:03 +01004122 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 return ret;
4124}
4125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004128 * @mtd: MTD device structure
4129 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004131 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004133static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004135 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004139 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004140 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004141 * @instr: erase instruction
4142 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004144 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004146int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004147 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148{
Masahiro Yamada2d73f3d2019-01-21 15:32:07 +09004149 int page, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004150 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Brian Norris289c0522011-07-19 10:06:09 -07004152 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4153 __func__, (unsigned long long)instr->addr,
4154 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Boris Brezillon08136212018-11-11 08:55:03 +01004156 if (check_offs_len(chip, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 /* Grab the lock and see if the device is available */
Boris Brezillon013e6292018-11-20 11:57:20 +01004160 ret = nand_get_device(chip);
4161 if (ret)
4162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
4164 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004165 page = (int)(instr->addr >> chip->page_shift);
4166 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
4168 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004169 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
4171 /* Select the NAND device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004172 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01004175 if (nand_check_wp(chip)) {
Brian Norris289c0522011-07-19 10:06:09 -07004176 pr_debug("%s: device is write protected!\n",
4177 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004178 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 goto erase_exit;
4180 }
4181
4182 /* Loop through the pages */
4183 len = instr->len;
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004186 /* Check if we have a bad block, we do not erase bad blocks! */
Boris Brezillon08136212018-11-11 08:55:03 +01004187 if (nand_block_checkbad(chip, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304188 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004189 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4190 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004191 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 goto erase_exit;
4193 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004194
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004195 /*
4196 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004197 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004198 */
Boris Brezillond9745412018-10-28 16:12:45 +01004199 if (page <= chip->pagecache.page && chip->pagecache.page <
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004200 (page + pages_per_block))
Boris Brezillond9745412018-10-28 16:12:45 +01004201 chip->pagecache.page = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Masahiro Yamada2d73f3d2019-01-21 15:32:07 +09004203 ret = nand_erase_op(chip, (page & chip->pagemask) >>
4204 (chip->phys_erase_shift - chip->page_shift));
4205 if (ret) {
Brian Norris289c0522011-07-19 10:06:09 -07004206 pr_debug("%s: failed erase, page 0x%08x\n",
4207 __func__, page);
Adrian Hunter69423d92008-12-10 13:37:21 +00004208 instr->fail_addr =
4209 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 goto erase_exit;
4211 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004212
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004214 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 page += pages_per_block;
4216
4217 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004218 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004220 nand_deselect_target(chip);
4221 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 }
4223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004225 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004226erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004229 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004230 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 /* Return more or less happy */
4233 return ret;
4234}
4235
4236/**
4237 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004238 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004240 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004242static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
Boris Brezillon08136212018-11-11 08:55:03 +01004244 struct nand_chip *chip = mtd_to_nand(mtd);
4245
Brian Norris289c0522011-07-19 10:06:09 -07004246 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
4248 /* Grab the lock and see if the device is available */
Boris Brezillon013e6292018-11-20 11:57:20 +01004249 WARN_ON(nand_get_device(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 /* Release it and go back */
Boris Brezillon08136212018-11-11 08:55:03 +01004251 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004255 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004256 * @mtd: MTD device structure
4257 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004259static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304261 struct nand_chip *chip = mtd_to_nand(mtd);
4262 int chipnr = (int)(offs >> chip->chip_shift);
4263 int ret;
4264
4265 /* Select the NAND device */
Boris Brezillon013e6292018-11-20 11:57:20 +01004266 ret = nand_get_device(chip);
4267 if (ret)
4268 return ret;
4269
Boris Brezillon1d017852018-11-11 08:55:14 +01004270 nand_select_target(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304271
Boris Brezillon08136212018-11-11 08:55:03 +01004272 ret = nand_block_checkbad(chip, offs, 0);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304273
Boris Brezillon1d017852018-11-11 08:55:14 +01004274 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004275 nand_release_device(chip);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304276
4277 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278}
4279
4280/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004281 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004282 * @mtd: MTD device structure
4283 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004285static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 int ret;
4288
Florian Fainellif8ac0412010-09-07 13:23:43 +02004289 ret = nand_block_isbad(mtd, ofs);
4290 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004291 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 if (ret > 0)
4293 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004294 return ret;
4295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Boris Brezillon08136212018-11-11 08:55:03 +01004297 return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298}
4299
4300/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004301 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004302 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004303 */
4304static int nand_suspend(struct mtd_info *mtd)
4305{
Boris Brezillon013e6292018-11-20 11:57:20 +01004306 struct nand_chip *chip = mtd_to_nand(mtd);
4307
4308 mutex_lock(&chip->lock);
4309 chip->suspended = 1;
4310 mutex_unlock(&chip->lock);
4311
4312 return 0;
Vitaly Wool962034f2005-09-15 14:58:53 +01004313}
4314
4315/**
4316 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004317 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004318 */
4319static void nand_resume(struct mtd_info *mtd)
4320{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004321 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004322
Boris Brezillon013e6292018-11-20 11:57:20 +01004323 mutex_lock(&chip->lock);
4324 if (chip->suspended)
4325 chip->suspended = 0;
Vitaly Wool962034f2005-09-15 14:58:53 +01004326 else
Brian Norrisd0370212011-07-19 10:06:08 -07004327 pr_err("%s called for a chip which is not in suspended state\n",
4328 __func__);
Boris Brezillon013e6292018-11-20 11:57:20 +01004329 mutex_unlock(&chip->lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01004330}
4331
Scott Branden72ea4032014-11-20 11:18:05 -08004332/**
4333 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4334 * prevent further operations
4335 * @mtd: MTD device structure
4336 */
4337static void nand_shutdown(struct mtd_info *mtd)
4338{
Boris Brezillon013e6292018-11-20 11:57:20 +01004339 nand_suspend(mtd);
Scott Branden72ea4032014-11-20 11:18:05 -08004340}
4341
Brian Norris8b6e50c2011-05-25 14:59:01 -07004342/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004343static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004344{
Boris Brezillon7b6a9b22018-11-20 10:02:39 +01004345 /* If no controller is provided, use the dummy, legacy one. */
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004346 if (!chip->controller) {
Boris Brezillon7b6a9b22018-11-20 10:02:39 +01004347 chip->controller = &chip->legacy.dummy_controller;
Miquel Raynal7da45132018-07-17 09:08:02 +02004348 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004349 }
4350
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01004351 nand_legacy_set_defaults(chip);
4352
Masahiro Yamada477544c2017-03-30 17:15:05 +09004353 if (!chip->buf_align)
4354 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004355}
4356
Brian Norris8b6e50c2011-05-25 14:59:01 -07004357/* Sanitize ONFI strings so we can safely print them */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004358void sanitize_string(uint8_t *s, size_t len)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004359{
4360 ssize_t i;
4361
Brian Norris8b6e50c2011-05-25 14:59:01 -07004362 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004363 s[len - 1] = 0;
4364
Brian Norris8b6e50c2011-05-25 14:59:01 -07004365 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004366 for (i = 0; i < len - 1; i++) {
4367 if (s[i] < ' ' || s[i] > 127)
4368 s[i] = '?';
4369 }
4370
Brian Norris8b6e50c2011-05-25 14:59:01 -07004371 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004372 strim(s);
4373}
4374
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004375/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07004376 * nand_id_has_period - Check if an ID string has a given wraparound period
4377 * @id_data: the ID string
4378 * @arrlen: the length of the @id_data array
4379 * @period: the period of repitition
4380 *
4381 * Check if an ID string is repeated within a given sequence of bytes at
4382 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08004383 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07004384 * if the repetition has a period of @period; otherwise, returns zero.
4385 */
4386static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4387{
4388 int i, j;
4389 for (i = 0; i < period; i++)
4390 for (j = i + period; j < arrlen; j += period)
4391 if (id_data[i] != id_data[j])
4392 return 0;
4393 return 1;
4394}
4395
4396/*
4397 * nand_id_len - Get the length of an ID string returned by CMD_READID
4398 * @id_data: the ID string
4399 * @arrlen: the length of the @id_data array
4400
4401 * Returns the length of the ID string, according to known wraparound/trailing
4402 * zero patterns. If no pattern exists, returns the length of the array.
4403 */
4404static int nand_id_len(u8 *id_data, int arrlen)
4405{
4406 int last_nonzero, period;
4407
4408 /* Find last non-zero byte */
4409 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4410 if (id_data[last_nonzero])
4411 break;
4412
4413 /* All zeros */
4414 if (last_nonzero < 0)
4415 return 0;
4416
4417 /* Calculate wraparound period */
4418 for (period = 1; period < arrlen; period++)
4419 if (nand_id_has_period(id_data, arrlen, period))
4420 break;
4421
4422 /* There's a repeated pattern */
4423 if (period < arrlen)
4424 return period;
4425
4426 /* There are trailing zeros */
4427 if (last_nonzero < arrlen - 1)
4428 return last_nonzero + 1;
4429
4430 /* No pattern detected */
4431 return arrlen;
4432}
4433
Huang Shijie7db906b2013-09-25 14:58:11 +08004434/* Extract the bits of per cell from the 3rd byte of the extended ID */
4435static int nand_get_bits_per_cell(u8 cellinfo)
4436{
4437 int bits;
4438
4439 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4440 bits >>= NAND_CI_CELLTYPE_SHIFT;
4441 return bits + 1;
4442}
4443
Brian Norrise3b88bd2012-09-24 20:40:52 -07004444/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004445 * Many new NAND share similar device ID codes, which represent the size of the
4446 * chip. The rest of the parameters must be decoded according to generic or
4447 * manufacturer-specific "extended ID" decoding patterns.
4448 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004449void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004450{
Boris Brezillon629a4422018-10-25 17:10:37 +02004451 struct nand_memory_organization *memorg;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004452 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02004453 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004454 u8 *id_data = chip->id.data;
Boris Brezillon629a4422018-10-25 17:10:37 +02004455
4456 memorg = nanddev_get_memorg(&chip->base);
4457
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004458 /* The 3rd id byte holds MLC / multichip data */
Boris Brezillon629a4422018-10-25 17:10:37 +02004459 memorg->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004460 /* The 4th id byte is the important one */
4461 extid = id_data[3];
4462
Boris Brezillon01389b62016-06-08 10:30:18 +02004463 /* Calc pagesize */
Boris Brezillon629a4422018-10-25 17:10:37 +02004464 memorg->pagesize = 1024 << (extid & 0x03);
4465 mtd->writesize = memorg->pagesize;
Boris Brezillon01389b62016-06-08 10:30:18 +02004466 extid >>= 2;
4467 /* Calc oobsize */
Boris Brezillon629a4422018-10-25 17:10:37 +02004468 memorg->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4469 mtd->oobsize = memorg->oobsize;
Boris Brezillon01389b62016-06-08 10:30:18 +02004470 extid >>= 2;
4471 /* Calc blocksize. Blocksize is multiples of 64KiB */
Boris Brezillon629a4422018-10-25 17:10:37 +02004472 memorg->pages_per_eraseblock = ((64 * 1024) << (extid & 0x03)) /
4473 memorg->pagesize;
Boris Brezillon01389b62016-06-08 10:30:18 +02004474 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4475 extid >>= 2;
4476 /* Get buswidth information */
4477 if (extid & 0x1)
4478 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004479}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004480EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004481
4482/*
Brian Norrisf23a4812012-09-24 20:40:51 -07004483 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4484 * decodes a matching ID table entry and assigns the MTD size parameters for
4485 * the chip.
4486 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004487static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07004488{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004489 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon629a4422018-10-25 17:10:37 +02004490 struct nand_memory_organization *memorg;
Brian Norrisf23a4812012-09-24 20:40:51 -07004491
Boris Brezillon629a4422018-10-25 17:10:37 +02004492 memorg = nanddev_get_memorg(&chip->base);
4493
4494 memorg->pages_per_eraseblock = type->erasesize / type->pagesize;
Brian Norrisf23a4812012-09-24 20:40:51 -07004495 mtd->erasesize = type->erasesize;
Boris Brezillon629a4422018-10-25 17:10:37 +02004496 memorg->pagesize = type->pagesize;
4497 mtd->writesize = memorg->pagesize;
4498 memorg->oobsize = memorg->pagesize / 32;
4499 mtd->oobsize = memorg->oobsize;
Brian Norrisf23a4812012-09-24 20:40:51 -07004500
Huang Shijie1c195e92013-09-25 14:58:12 +08004501 /* All legacy ID NAND are small-page, SLC */
Boris Brezillon629a4422018-10-25 17:10:37 +02004502 memorg->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07004503}
4504
4505/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004506 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4507 * heuristic patterns using various detected parameters (e.g., manufacturer,
4508 * page size, cell-type information).
4509 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004510static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07004511{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004512 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004513
4514 /* Set the bad block position */
4515 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4516 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4517 else
4518 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004519}
4520
Huang Shijieec6e87e2013-03-15 11:01:00 +08004521static inline bool is_full_id_nand(struct nand_flash_dev *type)
4522{
4523 return type->id_len;
4524}
4525
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004526static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02004527 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004528{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004529 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon629a4422018-10-25 17:10:37 +02004530 struct nand_memory_organization *memorg;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004531 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004532
Boris Brezillon629a4422018-10-25 17:10:37 +02004533 memorg = nanddev_get_memorg(&chip->base);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004534
Boris Brezillon629a4422018-10-25 17:10:37 +02004535 if (!strncmp(type->id, id_data, type->id_len)) {
4536 memorg->pagesize = type->pagesize;
4537 mtd->writesize = memorg->pagesize;
4538 memorg->pages_per_eraseblock = type->erasesize /
4539 type->pagesize;
4540 mtd->erasesize = type->erasesize;
4541 memorg->oobsize = type->oobsize;
4542 mtd->oobsize = memorg->oobsize;
4543
4544 memorg->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Boris Brezillon629a4422018-10-25 17:10:37 +02004545 memorg->eraseblocks_per_lun =
4546 DIV_ROUND_DOWN_ULL((u64)type->chipsize << 20,
4547 memorg->pagesize *
4548 memorg->pages_per_eraseblock);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004549 chip->options |= type->options;
Boris Brezillon6a1b66d2018-11-04 16:09:42 +01004550 chip->base.eccreq.strength = NAND_ECC_STRENGTH(type);
4551 chip->base.eccreq.step_size = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004552 chip->onfi_timing_mode_default =
4553 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004554
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004555 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4556 if (!chip->parameters.model)
4557 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004558
Huang Shijieec6e87e2013-03-15 11:01:00 +08004559 return true;
4560 }
4561 return false;
4562}
4563
Brian Norris7e74c2d2012-09-24 20:40:49 -07004564/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004565 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4566 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4567 * table.
4568 */
4569static void nand_manufacturer_detect(struct nand_chip *chip)
4570{
4571 /*
4572 * Try manufacturer detection if available and use
4573 * nand_decode_ext_id() otherwise.
4574 */
4575 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004576 chip->manufacturer.desc->ops->detect) {
Boris Brezillon629a4422018-10-25 17:10:37 +02004577 struct nand_memory_organization *memorg;
4578
4579 memorg = nanddev_get_memorg(&chip->base);
4580
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004581 /* The 3rd id byte holds MLC / multichip data */
Boris Brezillon629a4422018-10-25 17:10:37 +02004582 memorg->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004583 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004584 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004585 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004586 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004587}
4588
4589/*
4590 * Manufacturer initialization. This function is called for all NANDs including
4591 * ONFI and JEDEC compliant ones.
4592 * Manufacturer drivers should put all their specific initialization code in
4593 * their ->init() hook.
4594 */
4595static int nand_manufacturer_init(struct nand_chip *chip)
4596{
4597 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4598 !chip->manufacturer.desc->ops->init)
4599 return 0;
4600
4601 return chip->manufacturer.desc->ops->init(chip);
4602}
4603
4604/*
4605 * Manufacturer cleanup. This function is called for all NANDs including
4606 * ONFI and JEDEC compliant ones.
4607 * Manufacturer drivers should put all their specific cleanup code in their
4608 * ->cleanup() hook.
4609 */
4610static void nand_manufacturer_cleanup(struct nand_chip *chip)
4611{
4612 /* Release manufacturer private data */
4613 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4614 chip->manufacturer.desc->ops->cleanup)
4615 chip->manufacturer.desc->ops->cleanup(chip);
4616}
4617
Boris Brezillon348d56a2018-09-07 00:38:48 +02004618static const char *
4619nand_manufacturer_name(const struct nand_manufacturer *manufacturer)
4620{
4621 return manufacturer ? manufacturer->name : "Unknown";
4622}
4623
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004624/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004625 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004626 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004627static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004628{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004629 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004630 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon629a4422018-10-25 17:10:37 +02004631 struct nand_memory_organization *memorg;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004632 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004633 u8 *id_data = chip->id.data;
4634 u8 maf_id, dev_id;
Boris Brezillon6c836d52018-10-29 11:22:16 +01004635 u64 targetsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Karl Beldanef89a882008-09-15 14:37:29 +02004637 /*
Boris Brezillon629a4422018-10-25 17:10:37 +02004638 * Let's start by initializing memorg fields that might be left
4639 * unassigned by the ID-based detection logic.
4640 */
4641 memorg = nanddev_get_memorg(&chip->base);
4642 memorg->planes_per_lun = 1;
4643 memorg->luns_per_target = 1;
4644 memorg->ntargets = 1;
4645
4646 /*
Karl Beldanef89a882008-09-15 14:37:29 +02004647 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004648 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004649 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004650 ret = nand_reset(chip, 0);
4651 if (ret)
4652 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004653
4654 /* Select the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004655 nand_select_target(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004658 ret = nand_readid_op(chip, 0, id_data, 2);
4659 if (ret)
4660 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
4662 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004663 maf_id = id_data[0];
4664 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Brian Norris8b6e50c2011-05-25 14:59:01 -07004666 /*
4667 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004668 * interface concerns can cause random data which looks like a
4669 * possibly credible NAND flash to appear. If the two results do
4670 * not match, ignore the device completely.
4671 */
4672
Brian Norris4aef9b72012-09-24 20:40:48 -07004673 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004674 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
4675 if (ret)
4676 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01004677
Boris Brezillon7f501f02016-05-24 19:20:05 +02004678 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004679 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004680 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004681 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004682 }
4683
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02004684 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02004685
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004686 /* Try to identify manufacturer */
4687 manufacturer = nand_get_manufacturer(maf_id);
4688 chip->manufacturer.desc = manufacturer;
4689
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004690 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004691 type = nand_flash_ids;
4692
Boris Brezillon29a198a2016-05-24 20:17:48 +02004693 /*
4694 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4695 * override it.
4696 * This is required to make sure initial NAND bus width set by the
4697 * NAND controller driver is coherent with the real NAND bus width
4698 * (extracted by auto-detection code).
4699 */
4700 busw = chip->options & NAND_BUSWIDTH_16;
4701
4702 /*
4703 * The flag is only set (never cleared), reset it to its default value
4704 * before starting auto-detection.
4705 */
4706 chip->options &= ~NAND_BUSWIDTH_16;
4707
Huang Shijieec6e87e2013-03-15 11:01:00 +08004708 for (; type->name != NULL; type++) {
4709 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004710 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004711 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004712 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004713 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004714 }
4715 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004716
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004717 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004718 /* Check if the chip is ONFI compliant */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004719 ret = nand_onfi_detect(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01004720 if (ret < 0)
4721 return ret;
4722 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004723 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004724
4725 /* Check if the chip is JEDEC compliant */
Boris Brezillon8ae3fbf2018-09-07 00:38:51 +02004726 ret = nand_jedec_detect(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01004727 if (ret < 0)
4728 return ret;
4729 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08004730 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004731 }
4732
David Woodhouse5e81e882010-02-26 18:32:56 +00004733 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004734 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004735
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004736 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4737 if (!chip->parameters.model)
4738 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004739
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004740 if (!type->pagesize)
4741 nand_manufacturer_detect(chip);
4742 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004743 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004744
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004745 /* Get chip options */
4746 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004747
Boris Brezillon629a4422018-10-25 17:10:37 +02004748 memorg->eraseblocks_per_lun =
4749 DIV_ROUND_DOWN_ULL((u64)type->chipsize << 20,
4750 memorg->pagesize *
4751 memorg->pages_per_eraseblock);
4752
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004753ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01004754 if (!mtd->name)
4755 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004756
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004757 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004758 WARN_ON(busw & NAND_BUSWIDTH_16);
4759 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004760 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4761 /*
4762 * Check, if buswidth is correct. Hardware drivers should set
4763 * chip correct!
4764 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004765 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004766 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004767 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4768 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004769 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4770 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004771 ret = -EINVAL;
4772
4773 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004774 }
4775
Boris Brezillon7f501f02016-05-24 19:20:05 +02004776 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004777
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004778 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004779 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004780 /* Convert chipsize to number of pages per chip -1 */
Boris Brezillon6c836d52018-10-29 11:22:16 +01004781 targetsize = nanddev_target_size(&chip->base);
4782 chip->pagemask = (targetsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004783
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004784 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004785 ffs(mtd->erasesize) - 1;
Boris Brezillon6c836d52018-10-29 11:22:16 +01004786 if (targetsize & 0xffffffff)
4787 chip->chip_shift = ffs((unsigned)targetsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004788 else {
Boris Brezillon6c836d52018-10-29 11:22:16 +01004789 chip->chip_shift = ffs((unsigned)(targetsize >> 32));
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004790 chip->chip_shift += 32 - 1;
4791 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004792
Masahiro Yamada14157f82017-09-13 11:05:50 +09004793 if (chip->chip_shift - chip->page_shift > 16)
4794 chip->options |= NAND_ROW_ADDR_3;
4795
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004796 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004797
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004798 nand_legacy_adjust_cmdfunc(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004799
Ezequiel Garcia20171642013-11-25 08:30:31 -03004800 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004801 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01004802 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4803 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02004804 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Boris Brezillon6c836d52018-10-29 11:22:16 +01004805 (int)(targetsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004806 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004807 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004808
4809free_detect_allocation:
4810 kfree(chip->parameters.model);
4811
4812 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004813}
4814
Boris Brezillond48f62b2016-04-01 14:54:32 +02004815static const char * const nand_ecc_modes[] = {
4816 [NAND_ECC_NONE] = "none",
4817 [NAND_ECC_SOFT] = "soft",
4818 [NAND_ECC_HW] = "hw",
4819 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4820 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004821 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004822};
4823
4824static int of_get_nand_ecc_mode(struct device_node *np)
4825{
4826 const char *pm;
4827 int err, i;
4828
4829 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4830 if (err < 0)
4831 return err;
4832
4833 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4834 if (!strcasecmp(pm, nand_ecc_modes[i]))
4835 return i;
4836
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004837 /*
4838 * For backward compatibility we support few obsoleted values that don't
4839 * have their mappings into nand_ecc_modes_t anymore (they were merged
4840 * with other enums).
4841 */
4842 if (!strcasecmp(pm, "soft_bch"))
4843 return NAND_ECC_SOFT;
4844
Boris Brezillond48f62b2016-04-01 14:54:32 +02004845 return -ENODEV;
4846}
4847
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004848static const char * const nand_ecc_algos[] = {
4849 [NAND_ECC_HAMMING] = "hamming",
4850 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02004851 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004852};
4853
Boris Brezillond48f62b2016-04-01 14:54:32 +02004854static int of_get_nand_ecc_algo(struct device_node *np)
4855{
4856 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004857 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004858
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004859 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4860 if (!err) {
4861 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4862 if (!strcasecmp(pm, nand_ecc_algos[i]))
4863 return i;
4864 return -ENODEV;
4865 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004866
4867 /*
4868 * For backward compatibility we also read "nand-ecc-mode" checking
4869 * for some obsoleted values that were specifying ECC algorithm.
4870 */
4871 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4872 if (err < 0)
4873 return err;
4874
4875 if (!strcasecmp(pm, "soft"))
4876 return NAND_ECC_HAMMING;
4877 else if (!strcasecmp(pm, "soft_bch"))
4878 return NAND_ECC_BCH;
4879
4880 return -ENODEV;
4881}
4882
4883static int of_get_nand_ecc_step_size(struct device_node *np)
4884{
4885 int ret;
4886 u32 val;
4887
4888 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4889 return ret ? ret : val;
4890}
4891
4892static int of_get_nand_ecc_strength(struct device_node *np)
4893{
4894 int ret;
4895 u32 val;
4896
4897 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4898 return ret ? ret : val;
4899}
4900
4901static int of_get_nand_bus_width(struct device_node *np)
4902{
4903 u32 val;
4904
4905 if (of_property_read_u32(np, "nand-bus-width", &val))
4906 return 8;
4907
4908 switch (val) {
4909 case 8:
4910 case 16:
4911 return val;
4912 default:
4913 return -EIO;
4914 }
4915}
4916
4917static bool of_get_nand_on_flash_bbt(struct device_node *np)
4918{
4919 return of_property_read_bool(np, "nand-on-flash-bbt");
4920}
4921
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004922static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004923{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004924 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004925 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004926
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004927 if (!dn)
4928 return 0;
4929
Brian Norris5844fee2015-01-23 00:22:27 -08004930 if (of_get_nand_bus_width(dn) == 16)
4931 chip->options |= NAND_BUSWIDTH_16;
4932
Stefan Agnerf922bd72018-06-24 23:27:23 +02004933 if (of_property_read_bool(dn, "nand-is-boot-medium"))
4934 chip->options |= NAND_IS_BOOT_MEDIUM;
4935
Brian Norris5844fee2015-01-23 00:22:27 -08004936 if (of_get_nand_on_flash_bbt(dn))
4937 chip->bbt_options |= NAND_BBT_USE_FLASH;
4938
4939 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004940 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004941 ecc_strength = of_get_nand_ecc_strength(dn);
4942 ecc_step = of_get_nand_ecc_step_size(dn);
4943
Brian Norris5844fee2015-01-23 00:22:27 -08004944 if (ecc_mode >= 0)
4945 chip->ecc.mode = ecc_mode;
4946
Rafał Miłecki79082452016-03-23 11:19:02 +01004947 if (ecc_algo >= 0)
4948 chip->ecc.algo = ecc_algo;
4949
Brian Norris5844fee2015-01-23 00:22:27 -08004950 if (ecc_strength >= 0)
4951 chip->ecc.strength = ecc_strength;
4952
4953 if (ecc_step > 0)
4954 chip->ecc.size = ecc_step;
4955
Boris Brezillonba78ee02016-06-08 17:04:22 +02004956 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4957 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4958
Brian Norris5844fee2015-01-23 00:22:27 -08004959 return 0;
4960}
4961
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004962/**
Miquel Raynal98732da2018-07-25 15:31:50 +02004963 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02004964 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004965 * @maxchips: number of chips to scan for
4966 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004967 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004968 * This is the first phase of the normal nand_scan() function. It reads the
4969 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004970 *
Miquel Raynal98732da2018-07-25 15:31:50 +02004971 * This helper used to be called directly from controller drivers that needed
4972 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
4973 * prevented dynamic allocations during this phase which was unconvenient and
4974 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004975 */
Boris Brezillon871a4072018-08-04 22:59:22 +02004976static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02004977 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004978{
Boris Brezillon00ad3782018-09-06 14:05:14 +02004979 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon629a4422018-10-25 17:10:37 +02004980 struct nand_memory_organization *memorg;
Boris Brezillon871a4072018-08-04 22:59:22 +02004981 int nand_maf_id, nand_dev_id;
4982 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08004983 int ret;
4984
Boris Brezillon629a4422018-10-25 17:10:37 +02004985 memorg = nanddev_get_memorg(&chip->base);
4986
Boris Brezillonae2294b2018-11-11 08:55:15 +01004987 /* Assume all dies are deselected when we enter nand_scan_ident(). */
4988 chip->cur_cs = -1;
4989
Boris Brezillon013e6292018-11-20 11:57:20 +01004990 mutex_init(&chip->lock);
4991
Miquel Raynal17fa8042017-11-30 18:01:31 +01004992 /* Enforce the right timings for reset/detection */
4993 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
4994
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004995 ret = nand_dt_init(chip);
4996 if (ret)
4997 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004998
Brian Norrisf7a8e382016-01-05 10:39:45 -08004999 if (!mtd->name && mtd->dev.parent)
5000 mtd->name = dev_name(mtd->dev.parent);
5001
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005002 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005003 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005004
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01005005 ret = nand_legacy_check_hooks(chip);
5006 if (ret)
5007 return ret;
5008
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005009 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005010 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005011 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005012 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005013 pr_warn("No NAND device found\n");
Boris Brezillon1d017852018-11-11 08:55:14 +01005014 nand_deselect_target(chip);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005015 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 }
5017
Boris Brezillon7f501f02016-05-24 19:20:05 +02005018 nand_maf_id = chip->id.data[0];
5019 nand_dev_id = chip->id.data[1];
5020
Boris Brezillon1d017852018-11-11 08:55:14 +01005021 nand_deselect_target(chip);
Huang Shijie07300162012-11-09 16:23:45 +08005022
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005023 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005024 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005025 u8 id[2];
5026
Karl Beldanef89a882008-09-15 14:37:29 +02005027 /* See comment in nand_get_flash_type for reset */
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +09005028 ret = nand_reset(chip, i);
5029 if (ret)
5030 break;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005031
Boris Brezillon1d017852018-11-11 08:55:14 +01005032 nand_select_target(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 /* Send the command for reading device ID */
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +09005034 ret = nand_readid_op(chip, 0, id, sizeof(id));
5035 if (ret)
5036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005038 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon1d017852018-11-11 08:55:14 +01005039 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 break;
Huang Shijie07300162012-11-09 16:23:45 +08005041 }
Boris Brezillon1d017852018-11-11 08:55:14 +01005042 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 }
5044 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005045 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 /* Store the number of chips and calc total size for mtd */
Boris Brezillon629a4422018-10-25 17:10:37 +02005048 memorg->ntargets = i;
Boris Brezillon6c836d52018-10-29 11:22:16 +01005049 mtd->size = i * nanddev_target_size(&chip->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
David Woodhouse3b85c322006-09-25 17:06:53 +01005051 return 0;
5052}
5053
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005054static void nand_scan_ident_cleanup(struct nand_chip *chip)
5055{
5056 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005057 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005058}
5059
Boris Brezillon08136212018-11-11 08:55:03 +01005060static int nand_set_ecc_soft_ops(struct nand_chip *chip)
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005061{
Boris Brezillon08136212018-11-11 08:55:03 +01005062 struct mtd_info *mtd = nand_to_mtd(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005063 struct nand_ecc_ctrl *ecc = &chip->ecc;
5064
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005065 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005066 return -EINVAL;
5067
5068 switch (ecc->algo) {
5069 case NAND_ECC_HAMMING:
5070 ecc->calculate = nand_calculate_ecc;
5071 ecc->correct = nand_correct_data;
5072 ecc->read_page = nand_read_page_swecc;
5073 ecc->read_subpage = nand_read_subpage;
5074 ecc->write_page = nand_write_page_swecc;
5075 ecc->read_page_raw = nand_read_page_raw;
5076 ecc->write_page_raw = nand_write_page_raw;
5077 ecc->read_oob = nand_read_oob_std;
5078 ecc->write_oob = nand_write_oob_std;
5079 if (!ecc->size)
5080 ecc->size = 256;
5081 ecc->bytes = 3;
5082 ecc->strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +02005083
5084 if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC))
5085 ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
5086
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005087 return 0;
5088 case NAND_ECC_BCH:
5089 if (!mtd_nand_has_bch()) {
5090 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5091 return -EINVAL;
5092 }
5093 ecc->calculate = nand_bch_calculate_ecc;
5094 ecc->correct = nand_bch_correct_data;
5095 ecc->read_page = nand_read_page_swecc;
5096 ecc->read_subpage = nand_read_subpage;
5097 ecc->write_page = nand_write_page_swecc;
5098 ecc->read_page_raw = nand_read_page_raw;
5099 ecc->write_page_raw = nand_write_page_raw;
5100 ecc->read_oob = nand_read_oob_std;
5101 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005102
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005103 /*
5104 * Board driver should supply ecc.size and ecc.strength
5105 * values to select how many bits are correctable.
5106 * Otherwise, default to 4 bits for large page devices.
5107 */
5108 if (!ecc->size && (mtd->oobsize >= 64)) {
5109 ecc->size = 512;
5110 ecc->strength = 4;
5111 }
5112
5113 /*
5114 * if no ecc placement scheme was provided pickup the default
5115 * large page one.
5116 */
5117 if (!mtd->ooblayout) {
5118 /* handle large page devices only */
5119 if (mtd->oobsize < 64) {
5120 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5121 return -EINVAL;
5122 }
5123
5124 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005125
5126 }
5127
5128 /*
5129 * We can only maximize ECC config when the default layout is
5130 * used, otherwise we don't know how many bytes can really be
5131 * used.
5132 */
5133 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5134 ecc->options & NAND_ECC_MAXIMIZE) {
5135 int steps, bytes;
5136
5137 /* Always prefer 1k blocks over 512bytes ones */
5138 ecc->size = 1024;
5139 steps = mtd->writesize / ecc->size;
5140
5141 /* Reserve 2 bytes for the BBM */
5142 bytes = (mtd->oobsize - 2) / steps;
5143 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005144 }
5145
5146 /* See nand_bch_init() for details. */
5147 ecc->bytes = 0;
5148 ecc->priv = nand_bch_init(mtd);
5149 if (!ecc->priv) {
5150 WARN(1, "BCH ECC initialization failed!\n");
5151 return -EINVAL;
5152 }
5153 return 0;
5154 default:
5155 WARN(1, "Unsupported ECC algorithm!\n");
5156 return -EINVAL;
5157 }
5158}
5159
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005160/**
5161 * nand_check_ecc_caps - check the sanity of preset ECC settings
5162 * @chip: nand chip info structure
5163 * @caps: ECC caps info structure
5164 * @oobavail: OOB size that the ECC engine can use
5165 *
5166 * When ECC step size and strength are already set, check if they are supported
5167 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5168 * On success, the calculated ECC bytes is set.
5169 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305170static int
5171nand_check_ecc_caps(struct nand_chip *chip,
5172 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005173{
5174 struct mtd_info *mtd = nand_to_mtd(chip);
5175 const struct nand_ecc_step_info *stepinfo;
5176 int preset_step = chip->ecc.size;
5177 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305178 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005179 int i, j;
5180
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005181 for (i = 0; i < caps->nstepinfos; i++) {
5182 stepinfo = &caps->stepinfos[i];
5183
5184 if (stepinfo->stepsize != preset_step)
5185 continue;
5186
5187 for (j = 0; j < stepinfo->nstrengths; j++) {
5188 if (stepinfo->strengths[j] != preset_strength)
5189 continue;
5190
5191 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5192 preset_strength);
5193 if (WARN_ON_ONCE(ecc_bytes < 0))
5194 return ecc_bytes;
5195
5196 if (ecc_bytes * nsteps > oobavail) {
5197 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5198 preset_step, preset_strength);
5199 return -ENOSPC;
5200 }
5201
5202 chip->ecc.bytes = ecc_bytes;
5203
5204 return 0;
5205 }
5206 }
5207
5208 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5209 preset_step, preset_strength);
5210
5211 return -ENOTSUPP;
5212}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005213
5214/**
5215 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5216 * @chip: nand chip info structure
5217 * @caps: ECC engine caps info structure
5218 * @oobavail: OOB size that the ECC engine can use
5219 *
5220 * If a chip's ECC requirement is provided, try to meet it with the least
5221 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5222 * On success, the chosen ECC settings are set.
5223 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305224static int
5225nand_match_ecc_req(struct nand_chip *chip,
5226 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005227{
5228 struct mtd_info *mtd = nand_to_mtd(chip);
5229 const struct nand_ecc_step_info *stepinfo;
Boris Brezillon6a1b66d2018-11-04 16:09:42 +01005230 int req_step = chip->base.eccreq.step_size;
5231 int req_strength = chip->base.eccreq.strength;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005232 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5233 int best_step, best_strength, best_ecc_bytes;
5234 int best_ecc_bytes_total = INT_MAX;
5235 int i, j;
5236
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005237 /* No information provided by the NAND chip */
5238 if (!req_step || !req_strength)
5239 return -ENOTSUPP;
5240
5241 /* number of correctable bits the chip requires in a page */
5242 req_corr = mtd->writesize / req_step * req_strength;
5243
5244 for (i = 0; i < caps->nstepinfos; i++) {
5245 stepinfo = &caps->stepinfos[i];
5246 step_size = stepinfo->stepsize;
5247
5248 for (j = 0; j < stepinfo->nstrengths; j++) {
5249 strength = stepinfo->strengths[j];
5250
5251 /*
5252 * If both step size and strength are smaller than the
5253 * chip's requirement, it is not easy to compare the
5254 * resulted reliability.
5255 */
5256 if (step_size < req_step && strength < req_strength)
5257 continue;
5258
5259 if (mtd->writesize % step_size)
5260 continue;
5261
5262 nsteps = mtd->writesize / step_size;
5263
5264 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5265 if (WARN_ON_ONCE(ecc_bytes < 0))
5266 continue;
5267 ecc_bytes_total = ecc_bytes * nsteps;
5268
5269 if (ecc_bytes_total > oobavail ||
5270 strength * nsteps < req_corr)
5271 continue;
5272
5273 /*
5274 * We assume the best is to meet the chip's requrement
5275 * with the least number of ECC bytes.
5276 */
5277 if (ecc_bytes_total < best_ecc_bytes_total) {
5278 best_ecc_bytes_total = ecc_bytes_total;
5279 best_step = step_size;
5280 best_strength = strength;
5281 best_ecc_bytes = ecc_bytes;
5282 }
5283 }
5284 }
5285
5286 if (best_ecc_bytes_total == INT_MAX)
5287 return -ENOTSUPP;
5288
5289 chip->ecc.size = best_step;
5290 chip->ecc.strength = best_strength;
5291 chip->ecc.bytes = best_ecc_bytes;
5292
5293 return 0;
5294}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005295
5296/**
5297 * nand_maximize_ecc - choose the max ECC strength available
5298 * @chip: nand chip info structure
5299 * @caps: ECC engine caps info structure
5300 * @oobavail: OOB size that the ECC engine can use
5301 *
5302 * Choose the max ECC strength that is supported on the controller, and can fit
5303 * within the chip's OOB. On success, the chosen ECC settings are set.
5304 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305305static int
5306nand_maximize_ecc(struct nand_chip *chip,
5307 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005308{
5309 struct mtd_info *mtd = nand_to_mtd(chip);
5310 const struct nand_ecc_step_info *stepinfo;
5311 int step_size, strength, nsteps, ecc_bytes, corr;
5312 int best_corr = 0;
5313 int best_step = 0;
5314 int best_strength, best_ecc_bytes;
5315 int i, j;
5316
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005317 for (i = 0; i < caps->nstepinfos; i++) {
5318 stepinfo = &caps->stepinfos[i];
5319 step_size = stepinfo->stepsize;
5320
5321 /* If chip->ecc.size is already set, respect it */
5322 if (chip->ecc.size && step_size != chip->ecc.size)
5323 continue;
5324
5325 for (j = 0; j < stepinfo->nstrengths; j++) {
5326 strength = stepinfo->strengths[j];
5327
5328 if (mtd->writesize % step_size)
5329 continue;
5330
5331 nsteps = mtd->writesize / step_size;
5332
5333 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5334 if (WARN_ON_ONCE(ecc_bytes < 0))
5335 continue;
5336
5337 if (ecc_bytes * nsteps > oobavail)
5338 continue;
5339
5340 corr = strength * nsteps;
5341
5342 /*
5343 * If the number of correctable bits is the same,
5344 * bigger step_size has more reliability.
5345 */
5346 if (corr > best_corr ||
5347 (corr == best_corr && step_size > best_step)) {
5348 best_corr = corr;
5349 best_step = step_size;
5350 best_strength = strength;
5351 best_ecc_bytes = ecc_bytes;
5352 }
5353 }
5354 }
5355
5356 if (!best_corr)
5357 return -ENOTSUPP;
5358
5359 chip->ecc.size = best_step;
5360 chip->ecc.strength = best_strength;
5361 chip->ecc.bytes = best_ecc_bytes;
5362
5363 return 0;
5364}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005365
Abhishek Sahu181ace92018-06-20 12:57:28 +05305366/**
5367 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
5368 * @chip: nand chip info structure
5369 * @caps: ECC engine caps info structure
5370 * @oobavail: OOB size that the ECC engine can use
5371 *
5372 * Choose the ECC configuration according to following logic
5373 *
5374 * 1. If both ECC step size and ECC strength are already set (usually by DT)
5375 * then check if it is supported by this controller.
5376 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
5377 * 3. Otherwise, try to match the ECC step size and ECC strength closest
5378 * to the chip's requirement. If available OOB size can't fit the chip
5379 * requirement then fallback to the maximum ECC step size and ECC strength.
5380 *
5381 * On success, the chosen ECC settings are set.
5382 */
5383int nand_ecc_choose_conf(struct nand_chip *chip,
5384 const struct nand_ecc_caps *caps, int oobavail)
5385{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305386 struct mtd_info *mtd = nand_to_mtd(chip);
5387
5388 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
5389 return -EINVAL;
5390
Abhishek Sahu181ace92018-06-20 12:57:28 +05305391 if (chip->ecc.size && chip->ecc.strength)
5392 return nand_check_ecc_caps(chip, caps, oobavail);
5393
5394 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
5395 return nand_maximize_ecc(chip, caps, oobavail);
5396
5397 if (!nand_match_ecc_req(chip, caps, oobavail))
5398 return 0;
5399
5400 return nand_maximize_ecc(chip, caps, oobavail);
5401}
5402EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
5403
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005404/*
5405 * Check if the chip configuration meet the datasheet requirements.
5406
5407 * If our configuration corrects A bits per B bytes and the minimum
5408 * required correction level is X bits per Y bytes, then we must ensure
5409 * both of the following are true:
5410 *
5411 * (1) A / B >= X / Y
5412 * (2) A >= X
5413 *
5414 * Requirement (1) ensures we can correct for the required bitflip density.
5415 * Requirement (2) ensures we can correct even when all bitflips are clumped
5416 * in the same sector.
5417 */
Boris Brezillon08136212018-11-11 08:55:03 +01005418static bool nand_ecc_strength_good(struct nand_chip *chip)
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005419{
Boris Brezillon08136212018-11-11 08:55:03 +01005420 struct mtd_info *mtd = nand_to_mtd(chip);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005421 struct nand_ecc_ctrl *ecc = &chip->ecc;
5422 int corr, ds_corr;
5423
Boris Brezillon6a1b66d2018-11-04 16:09:42 +01005424 if (ecc->size == 0 || chip->base.eccreq.step_size == 0)
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005425 /* Not enough information */
5426 return true;
5427
5428 /*
5429 * We get the number of corrected bits per page to compare
5430 * the correction density.
5431 */
5432 corr = (mtd->writesize * ecc->strength) / ecc->size;
Boris Brezillon6a1b66d2018-11-04 16:09:42 +01005433 ds_corr = (mtd->writesize * chip->base.eccreq.strength) /
5434 chip->base.eccreq.step_size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005435
Boris Brezillon6a1b66d2018-11-04 16:09:42 +01005436 return corr >= ds_corr && ecc->strength >= chip->base.eccreq.strength;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005437}
David Woodhouse3b85c322006-09-25 17:06:53 +01005438
Boris Brezillona7ab0852018-10-25 22:10:36 +02005439static int rawnand_erase(struct nand_device *nand, const struct nand_pos *pos)
5440{
5441 struct nand_chip *chip = container_of(nand, struct nand_chip,
5442 base);
5443 unsigned int eb = nanddev_pos_to_row(nand, pos);
5444 int ret;
5445
5446 eb >>= nand->rowconv.eraseblock_addr_shift;
5447
5448 nand_select_target(chip, pos->target);
5449 ret = nand_erase_op(chip, eb);
5450 nand_deselect_target(chip);
5451
5452 return ret;
5453}
5454
5455static int rawnand_markbad(struct nand_device *nand,
5456 const struct nand_pos *pos)
5457{
5458 struct nand_chip *chip = container_of(nand, struct nand_chip,
5459 base);
5460
5461 return nand_markbad_bbm(chip, nanddev_pos_to_offs(nand, pos));
5462}
5463
5464static bool rawnand_isbad(struct nand_device *nand, const struct nand_pos *pos)
5465{
5466 struct nand_chip *chip = container_of(nand, struct nand_chip,
5467 base);
5468 int ret;
5469
5470 nand_select_target(chip, pos->target);
5471 ret = nand_isbad_bbm(chip, nanddev_pos_to_offs(nand, pos));
5472 nand_deselect_target(chip);
5473
5474 return ret;
5475}
5476
5477static const struct nand_ops rawnand_ops = {
5478 .erase = rawnand_erase,
5479 .markbad = rawnand_markbad,
5480 .isbad = rawnand_isbad,
5481};
5482
David Woodhouse3b85c322006-09-25 17:06:53 +01005483/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005484 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005485 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01005486 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005487 * This is the second phase of the normal nand_scan() function. It fills out
5488 * all the uninitialized function pointers with the defaults and scans for a
5489 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01005490 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005491static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01005492{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005493 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08005494 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005495 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01005496
Brian Norrise2414f42012-02-06 13:44:00 -08005497 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005498 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07005499 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005500 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07005501 }
Brian Norrise2414f42012-02-06 13:44:00 -08005502
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005503 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005504 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02005505 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005506
Boris Brezillonf84674b2017-06-02 12:18:24 +02005507 /*
5508 * FIXME: some NAND manufacturer drivers expect the first die to be
5509 * selected when manufacturer->init() is called. They should be fixed
5510 * to explictly select the relevant die when interacting with the NAND
5511 * chip.
5512 */
Boris Brezillon1d017852018-11-11 08:55:14 +01005513 nand_select_target(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005514 ret = nand_manufacturer_init(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01005515 nand_deselect_target(chip);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005516 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005517 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005518
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01005519 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005520 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005521
5522 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005523 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005524 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005525 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005526 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005527 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01005530 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 break;
5532 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005533 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02005534 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005535 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02005537 /*
5538 * Expose the whole OOB area to users if ECC_NONE
5539 * is passed. We could do that for all kind of
5540 * ->oobsize, but we must keep the old large/small
5541 * page with ECC layout when ->oobsize <= 128 for
5542 * compatibility reasons.
5543 */
5544 if (ecc->mode == NAND_ECC_NONE) {
5545 mtd_set_ooblayout(mtd,
5546 &nand_ooblayout_lp_ops);
5547 break;
5548 }
5549
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005550 WARN(1, "No oob scheme defined for oobsize %d\n",
5551 mtd->oobsize);
5552 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005553 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 }
5555 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005556
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005557 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005558 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005559 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01005560 */
David Woodhouse956e9442006-09-25 17:12:39 +01005561
Huang Shijie97de79e02013-10-18 14:20:53 +08005562 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005563 case NAND_ECC_HW_OOB_FIRST:
5564 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08005565 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005566 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5567 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005568 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005569 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005570 if (!ecc->read_page)
5571 ecc->read_page = nand_read_page_hwecc_oob_first;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005572 /* fall through */
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005573
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005574 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07005575 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005576 if (!ecc->read_page)
5577 ecc->read_page = nand_read_page_hwecc;
5578 if (!ecc->write_page)
5579 ecc->write_page = nand_write_page_hwecc;
5580 if (!ecc->read_page_raw)
5581 ecc->read_page_raw = nand_read_page_raw;
5582 if (!ecc->write_page_raw)
5583 ecc->write_page_raw = nand_write_page_raw;
5584 if (!ecc->read_oob)
5585 ecc->read_oob = nand_read_oob_std;
5586 if (!ecc->write_oob)
5587 ecc->write_oob = nand_write_oob_std;
5588 if (!ecc->read_subpage)
5589 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02005590 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08005591 ecc->write_subpage = nand_write_subpage_hwecc;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005592 /* fall through */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005593
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005594 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08005595 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5596 (!ecc->read_page ||
5597 ecc->read_page == nand_read_page_hwecc ||
5598 !ecc->write_page ||
5599 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005600 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5601 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005602 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005603 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07005604 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005605 if (!ecc->read_page)
5606 ecc->read_page = nand_read_page_syndrome;
5607 if (!ecc->write_page)
5608 ecc->write_page = nand_write_page_syndrome;
5609 if (!ecc->read_page_raw)
5610 ecc->read_page_raw = nand_read_page_raw_syndrome;
5611 if (!ecc->write_page_raw)
5612 ecc->write_page_raw = nand_write_page_raw_syndrome;
5613 if (!ecc->read_oob)
5614 ecc->read_oob = nand_read_oob_syndrome;
5615 if (!ecc->write_oob)
5616 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005617
Huang Shijie97de79e02013-10-18 14:20:53 +08005618 if (mtd->writesize >= ecc->size) {
5619 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005620 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
5621 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005622 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07005623 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005624 break;
Mike Dunne2788c92012-04-25 12:06:10 -07005625 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005626 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5627 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08005628 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02005629 ecc->algo = NAND_ECC_HAMMING;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005630 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005632 case NAND_ECC_SOFT:
Boris Brezillon08136212018-11-11 08:55:03 +01005633 ret = nand_set_ecc_soft_ops(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005634 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005635 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005636 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01005637 }
5638 break;
5639
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005640 case NAND_ECC_ON_DIE:
5641 if (!ecc->read_page || !ecc->write_page) {
5642 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
5643 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005644 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005645 }
5646 if (!ecc->read_oob)
5647 ecc->read_oob = nand_read_oob_std;
5648 if (!ecc->write_oob)
5649 ecc->write_oob = nand_write_oob_std;
5650 break;
5651
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005652 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005653 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08005654 ecc->read_page = nand_read_page_raw;
5655 ecc->write_page = nand_write_page_raw;
5656 ecc->read_oob = nand_read_oob_std;
5657 ecc->read_page_raw = nand_read_page_raw;
5658 ecc->write_page_raw = nand_write_page_raw;
5659 ecc->write_oob = nand_write_oob_std;
5660 ecc->size = mtd->writesize;
5661 ecc->bytes = 0;
5662 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005666 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5667 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005668 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005671 if (ecc->correct || ecc->calculate) {
5672 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5673 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5674 if (!ecc->calc_buf || !ecc->code_buf) {
5675 ret = -ENOMEM;
5676 goto err_nand_manuf_cleanup;
5677 }
5678 }
5679
Brian Norris9ce244b2011-08-30 18:45:37 -07005680 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005681 if (!ecc->read_oob_raw)
5682 ecc->read_oob_raw = ecc->read_oob;
5683 if (!ecc->write_oob_raw)
5684 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005685
Boris Brezillon846031d2016-02-03 20:11:00 +01005686 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005687 mtd->ecc_strength = ecc->strength;
5688 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005689
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005690 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005691 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005692 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005693 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005694 ecc->steps = mtd->writesize / ecc->size;
5695 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005696 WARN(1, "Invalid ECC parameters\n");
5697 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005698 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005700 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005701 if (ecc->total > mtd->oobsize) {
5702 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5703 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005704 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005705 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005706
Boris Brezillon846031d2016-02-03 20:11:00 +01005707 /*
5708 * The number of bytes available for a client to place data into
5709 * the out of band area.
5710 */
5711 ret = mtd_ooblayout_count_freebytes(mtd);
5712 if (ret < 0)
5713 ret = 0;
5714
5715 mtd->oobavail = ret;
5716
5717 /* ECC sanity check: warn if it's too weak */
Boris Brezillon08136212018-11-11 08:55:03 +01005718 if (!nand_ecc_strength_good(chip))
Boris Brezillon846031d2016-02-03 20:11:00 +01005719 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5720 mtd->name);
5721
Brian Norris8b6e50c2011-05-25 14:59:01 -07005722 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005723 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005724 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005725 case 2:
5726 mtd->subpage_sft = 1;
5727 break;
5728 case 4:
5729 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005730 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005731 mtd->subpage_sft = 2;
5732 break;
5733 }
5734 }
5735 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5736
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 /* Invalidate the pagebuffer reference */
Boris Brezillond9745412018-10-28 16:12:45 +01005738 chip->pagecache.page = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005740 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305741 switch (ecc->mode) {
5742 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305743 if (chip->page_shift > 9)
5744 chip->options |= NAND_SUBPAGE_READ;
5745 break;
5746
5747 default:
5748 break;
5749 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005750
Boris Brezillona7ab0852018-10-25 22:10:36 +02005751 ret = nanddev_init(&chip->base, &rawnand_ops, mtd->owner);
5752 if (ret)
5753 goto err_nand_manuf_cleanup;
5754
5755 /* Adjust the MTD_CAP_ flags when NAND_ROM is set. */
5756 if (chip->options & NAND_ROM)
5757 mtd->flags = MTD_CAP_ROM;
5758
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 /* Fill in remaining MTD driver data */
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005760 mtd->_erase = nand_erase;
5761 mtd->_point = NULL;
5762 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005763 mtd->_panic_write = panic_nand_write;
5764 mtd->_read_oob = nand_read_oob;
5765 mtd->_write_oob = nand_write_oob;
5766 mtd->_sync = nand_sync;
5767 mtd->_lock = NULL;
5768 mtd->_unlock = NULL;
5769 mtd->_suspend = nand_suspend;
5770 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005771 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005772 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005773 mtd->_block_isbad = nand_block_isbad;
5774 mtd->_block_markbad = nand_block_markbad;
Boris Brezillon7beb37e2018-11-04 14:50:28 +01005775 mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005777 /*
5778 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5779 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5780 * properly set.
5781 */
5782 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005783 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
Boris Brezillonf84674b2017-06-02 12:18:24 +02005785 /* Initialize the ->data_interface field. */
5786 ret = nand_init_data_interface(chip);
5787 if (ret)
Boris Brezillona7ab0852018-10-25 22:10:36 +02005788 goto err_nanddev_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005789
5790 /* Enter fastest possible mode on all dies. */
Boris Brezillon32813e22018-10-29 11:58:29 +01005791 for (i = 0; i < nanddev_ntargets(&chip->base); i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005792 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005793 if (ret)
Boris Brezillona7ab0852018-10-25 22:10:36 +02005794 goto err_nanddev_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005795 }
5796
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005797 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005798 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
5801 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02005802 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07005803 if (ret)
Boris Brezillona7ab0852018-10-25 22:10:36 +02005804 goto err_nanddev_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005805
Brian Norris44d41822017-05-01 17:04:50 -07005806 return 0;
5807
Boris Brezillonf84674b2017-06-02 12:18:24 +02005808
Boris Brezillona7ab0852018-10-25 22:10:36 +02005809err_nanddev_cleanup:
5810 nanddev_cleanup(&chip->base);
5811
Boris Brezillonf84674b2017-06-02 12:18:24 +02005812err_nand_manuf_cleanup:
5813 nand_manufacturer_cleanup(chip);
5814
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005815err_free_buf:
5816 kfree(chip->data_buf);
5817 kfree(ecc->code_buf);
5818 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07005819
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005820 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821}
5822
Miquel Raynal05b54c72018-07-19 01:05:46 +02005823static int nand_attach(struct nand_chip *chip)
5824{
5825 if (chip->controller->ops && chip->controller->ops->attach_chip)
5826 return chip->controller->ops->attach_chip(chip);
5827
5828 return 0;
5829}
5830
5831static void nand_detach(struct nand_chip *chip)
5832{
5833 if (chip->controller->ops && chip->controller->ops->detach_chip)
5834 chip->controller->ops->detach_chip(chip);
5835}
5836
David Woodhouse3b85c322006-09-25 17:06:53 +01005837/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005838 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005839 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02005840 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005841 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01005842 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005843 * This fills out all the uninitialized function pointers with the defaults.
5844 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005845 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005846 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005847int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005848 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01005849{
5850 int ret;
5851
Boris Brezillon800342d2018-08-04 22:59:23 +02005852 if (!maxchips)
5853 return -EINVAL;
5854
5855 ret = nand_scan_ident(chip, maxchips, ids);
5856 if (ret)
5857 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005858
5859 ret = nand_attach(chip);
5860 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005861 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005862
Boris Brezillon00ad3782018-09-06 14:05:14 +02005863 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005864 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005865 goto detach_chip;
5866
5867 return 0;
5868
5869detach_chip:
5870 nand_detach(chip);
5871cleanup_ident:
5872 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005873
David Woodhouse3b85c322006-09-25 17:06:53 +01005874 return ret;
5875}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005876EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01005877
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005879 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5880 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005881 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005882void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005884 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005885 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005886 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5887
Jesper Juhlfa671642005-11-07 01:01:27 -08005888 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005889 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005890 kfree(chip->data_buf);
5891 kfree(chip->ecc.code_buf);
5892 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07005893
5894 /* Free bad block descriptor memory */
5895 if (chip->badblock_pattern && chip->badblock_pattern->options
5896 & NAND_BBT_DYNAMICSTRUCT)
5897 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005898
5899 /* Free manufacturer priv data. */
5900 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005901
5902 /* Free controller specific allocations after chip identification */
5903 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005904
5905 /* Free identification phase allocations */
5906 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907}
Miquel Raynal05b54c72018-07-19 01:05:46 +02005908
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005909EXPORT_SYMBOL_GPL(nand_cleanup);
5910
5911/**
5912 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5913 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02005914 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005915 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02005916void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005917{
Boris Brezillon59ac2762018-09-06 14:05:15 +02005918 mtd_device_unregister(nand_to_mtd(chip));
5919 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005920}
David Woodhousee0c7d762006-05-13 18:07:53 +01005921EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005922
David Woodhousee0c7d762006-05-13 18:07:53 +01005923MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005924MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5925MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005926MODULE_DESCRIPTION("Generic NAND flash driver code");