blob: 4bc89c959f232eba10f9cb71cdb3ad50e087471a [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
Huang Shijie6a8214a2012-11-19 14:43:30 +080052static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020054static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
55 struct mtd_oob_ops *ops);
56
Boris Brezillon41b207a2016-02-03 19:06:15 +010057/* Define default oob placement schemes for large and small page devices */
58static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
59 struct mtd_oob_region *oobregion)
60{
61 struct nand_chip *chip = mtd_to_nand(mtd);
62 struct nand_ecc_ctrl *ecc = &chip->ecc;
63
64 if (section > 1)
65 return -ERANGE;
66
67 if (!section) {
68 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020069 if (mtd->oobsize == 16)
70 oobregion->length = 4;
71 else
72 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010073 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020074 if (mtd->oobsize == 8)
75 return -ERANGE;
76
Boris Brezillon41b207a2016-02-03 19:06:15 +010077 oobregion->offset = 6;
78 oobregion->length = ecc->total - 4;
79 }
80
81 return 0;
82}
83
84static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
85 struct mtd_oob_region *oobregion)
86{
87 if (section > 1)
88 return -ERANGE;
89
90 if (mtd->oobsize == 16) {
91 if (section)
92 return -ERANGE;
93
94 oobregion->length = 8;
95 oobregion->offset = 8;
96 } else {
97 oobregion->length = 2;
98 if (!section)
99 oobregion->offset = 3;
100 else
101 oobregion->offset = 6;
102 }
103
104 return 0;
105}
106
107const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
108 .ecc = nand_ooblayout_ecc_sp,
109 .free = nand_ooblayout_free_sp,
110};
111EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
112
113static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
114 struct mtd_oob_region *oobregion)
115{
116 struct nand_chip *chip = mtd_to_nand(mtd);
117 struct nand_ecc_ctrl *ecc = &chip->ecc;
118
Miquel Raynal882fd152017-08-26 17:19:15 +0200119 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100120 return -ERANGE;
121
122 oobregion->length = ecc->total;
123 oobregion->offset = mtd->oobsize - oobregion->length;
124
125 return 0;
126}
127
128static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
129 struct mtd_oob_region *oobregion)
130{
131 struct nand_chip *chip = mtd_to_nand(mtd);
132 struct nand_ecc_ctrl *ecc = &chip->ecc;
133
134 if (section)
135 return -ERANGE;
136
137 oobregion->length = mtd->oobsize - ecc->total - 2;
138 oobregion->offset = 2;
139
140 return 0;
141}
142
143const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
144 .ecc = nand_ooblayout_ecc_lp,
145 .free = nand_ooblayout_free_lp,
146};
147EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200148
Alexander Couzens6a623e02017-05-02 12:19:00 +0200149/*
150 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
151 * are placed at a fixed offset.
152 */
153static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
154 struct mtd_oob_region *oobregion)
155{
156 struct nand_chip *chip = mtd_to_nand(mtd);
157 struct nand_ecc_ctrl *ecc = &chip->ecc;
158
159 if (section)
160 return -ERANGE;
161
162 switch (mtd->oobsize) {
163 case 64:
164 oobregion->offset = 40;
165 break;
166 case 128:
167 oobregion->offset = 80;
168 break;
169 default:
170 return -EINVAL;
171 }
172
173 oobregion->length = ecc->total;
174 if (oobregion->offset + oobregion->length > mtd->oobsize)
175 return -ERANGE;
176
177 return 0;
178}
179
180static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
181 struct mtd_oob_region *oobregion)
182{
183 struct nand_chip *chip = mtd_to_nand(mtd);
184 struct nand_ecc_ctrl *ecc = &chip->ecc;
185 int ecc_offset = 0;
186
187 if (section < 0 || section > 1)
188 return -ERANGE;
189
190 switch (mtd->oobsize) {
191 case 64:
192 ecc_offset = 40;
193 break;
194 case 128:
195 ecc_offset = 80;
196 break;
197 default:
198 return -EINVAL;
199 }
200
201 if (section == 0) {
202 oobregion->offset = 2;
203 oobregion->length = ecc_offset - 2;
204 } else {
205 oobregion->offset = ecc_offset + ecc->total;
206 oobregion->length = mtd->oobsize - oobregion->offset;
207 }
208
209 return 0;
210}
211
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100212static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200213 .ecc = nand_ooblayout_ecc_lp_hamming,
214 .free = nand_ooblayout_free_lp_hamming,
215};
216
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530217static int check_offs_len(struct mtd_info *mtd,
218 loff_t ofs, uint64_t len)
219{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100220 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530221 int ret = 0;
222
223 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300224 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700225 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530226 ret = -EINVAL;
227 }
228
229 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300230 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700231 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530232 ret = -EINVAL;
233 }
234
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530235 return ret;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/**
239 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700240 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000241 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800242 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100244static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100246 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200248 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200249 spin_lock(&chip->controller->lock);
250 chip->controller->active = NULL;
251 chip->state = FL_READY;
252 wake_up(&chip->controller->wq);
253 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Boris Brezillonc17556f2018-09-06 14:05:25 +0200258 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700259 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000261 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200263static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200265 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900266 int page, page_end, res;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900267 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Brian Norris5fb15492011-05-31 16:31:21 -0700269 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700270 ofs += mtd->erasesize - mtd->writesize;
271
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100272 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900273 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100274
Masahiro Yamadac120e752017-03-23 05:07:01 +0900275 for (; page < page_end; page++) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200276 res = chip->ecc.read_oob(chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530277 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900278 return res;
279
280 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000281
Brian Norriscdbec052012-01-13 18:11:48 -0800282 if (likely(chip->badblockbits == 8))
283 res = bad != 0xFF;
284 else
285 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900286 if (res)
287 return res;
288 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200289
Masahiro Yamadac120e752017-03-23 05:07:01 +0900290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700294 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200295 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700296 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700298 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700299 * specific driver. It provides the details for writing a bad block marker to a
300 * block.
301 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200302static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700303{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200304 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700305 struct mtd_oob_ops ops;
306 uint8_t buf[2] = { 0, 0 };
307 int ret = 0, res, i = 0;
308
Brian Norris0ec56dc2015-02-28 02:02:30 -0800309 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700310 ops.oobbuf = buf;
311 ops.ooboffs = chip->badblockpos;
312 if (chip->options & NAND_BUSWIDTH_16) {
313 ops.ooboffs &= ~0x01;
314 ops.len = ops.ooblen = 2;
315 } else {
316 ops.len = ops.ooblen = 1;
317 }
318 ops.mode = MTD_OPS_PLACE_OOB;
319
320 /* Write to first/last page(s) if necessary */
321 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
322 ofs += mtd->erasesize - mtd->writesize;
323 do {
324 res = nand_do_write_oob(mtd, ofs, &ops);
325 if (!ret)
326 ret = res;
327
328 i++;
329 ofs += mtd->writesize;
330 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
331
332 return ret;
333}
334
335/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200336 * nand_markbad_bbm - mark a block by updating the BBM
337 * @chip: NAND chip object
338 * @ofs: offset of the block to mark bad
339 */
340int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
341{
342 if (chip->legacy.block_markbad)
343 return chip->legacy.block_markbad(chip, ofs);
344
345 return nand_default_block_markbad(chip, ofs);
346}
347
348static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
349{
350 if (chip->legacy.block_bad)
351 return chip->legacy.block_bad(chip, ofs);
352
353 return nand_block_bad(chip, ofs);
354}
355
356/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700357 * nand_block_markbad_lowlevel - mark a block bad
358 * @mtd: MTD device structure
359 * @ofs: offset from device start
360 *
361 * This function performs the generic NAND bad block marking steps (i.e., bad
362 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200363 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700364 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700365 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300366 *
Brian Norrise2414f42012-02-06 13:44:00 -0800367 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700368 * (2) write bad block marker to OOB area of affected block (unless flag
369 * NAND_BBT_NO_OOB_BBM is present)
370 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300371 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700372 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800373 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700375static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100377 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700378 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000379
Brian Norrisb32843b2013-07-30 17:52:59 -0700380 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800381 struct erase_info einfo;
382
383 /* Attempt erase before marking OOB */
384 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800385 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300386 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200387 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800388
Brian Norrisb32843b2013-07-30 17:52:59 -0700389 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800390 nand_get_device(mtd, FL_WRITING);
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200391 ret = nand_markbad_bbm(chip, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300392 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200393 }
Brian Norrise2414f42012-02-06 13:44:00 -0800394
Brian Norrisb32843b2013-07-30 17:52:59 -0700395 /* Mark block bad in BBT */
396 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200397 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800398 if (!ret)
399 ret = res;
400 }
401
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200402 if (!ret)
403 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300404
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200405 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000408/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700410 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700412 * Check, if the device is write protected. The function expects, that the
413 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100415static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100417 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100418 u8 status;
419 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200420
Brian Norris8b6e50c2011-05-25 14:59:01 -0700421 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200422 if (chip->options & NAND_BROKEN_XD)
423 return 0;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100426 ret = nand_status_op(chip, &status);
427 if (ret)
428 return ret;
429
430 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800434 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700435 * @mtd: MTD device structure
436 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300437 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800438 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300439 */
440static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
441{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100442 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300443
444 if (!chip->bbt)
445 return 0;
446 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200447 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300448}
449
450/**
451 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
452 * @mtd: MTD device structure
453 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700454 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *
456 * Check, if the block is bad. Either by reading the bad block table or
457 * calling of the scan function.
458 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530459static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100461 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200464 if (chip->bbt)
465 return nand_isbad_bbt(chip, ofs, allowbbt);
466
467 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200470/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100471 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
472 * @chip: NAND chip structure
473 * @timeout_ms: Timeout in ms
474 *
475 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
476 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
477 * returned.
478 *
479 * This helper is intended to be used when the controller does not have access
480 * to the NAND R/B pin.
481 *
482 * Be aware that calling this helper from an ->exec_op() implementation means
483 * ->exec_op() must be re-entrant.
484 *
485 * Return 0 if the NAND chip is ready, a negative error otherwise.
486 */
487int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
488{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200489 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100490 u8 status = 0;
491 int ret;
492
493 if (!chip->exec_op)
494 return -ENOTSUPP;
495
Boris Brezillon3057fce2018-05-04 21:24:31 +0200496 /* Wait tWB before polling the STATUS reg. */
497 timings = nand_get_sdr_timings(&chip->data_interface);
498 ndelay(PSEC_TO_NSEC(timings->tWB_max));
499
Miquel Raynal8878b122017-11-09 14:16:45 +0100500 ret = nand_status_op(chip, NULL);
501 if (ret)
502 return ret;
503
504 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
505 do {
506 ret = nand_read_data_op(chip, &status, sizeof(status), true);
507 if (ret)
508 break;
509
510 if (status & NAND_STATUS_READY)
511 break;
512
513 /*
514 * Typical lowest execution time for a tR on most NANDs is 10us,
515 * use this as polling delay before doing something smarter (ie.
516 * deriving a delay from the timeout value, timeout_ms/ratio).
517 */
518 udelay(10);
519 } while (time_before(jiffies, timeout_ms));
520
521 /*
522 * We have to exit READ_STATUS mode in order to read real data on the
523 * bus in case the WAITRDY instruction is preceding a DATA_IN
524 * instruction.
525 */
526 nand_exit_status_op(chip);
527
528 if (ret)
529 return ret;
530
531 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
532};
533EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
534
535/**
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +0200536 * nand_gpio_waitrdy - Poll R/B GPIO pin until ready
537 * @chip: NAND chip structure
538 * @gpiod: GPIO descriptor of R/B pin
539 * @timeout_ms: Timeout in ms
540 *
541 * Poll the R/B GPIO pin until it becomes ready. If that does not happen
542 * whitin the specified timeout, -ETIMEDOUT is returned.
543 *
544 * This helper is intended to be used when the controller has access to the
545 * NAND R/B pin over GPIO.
546 *
547 * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise.
548 */
549int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
550 unsigned long timeout_ms)
551{
552 /* Wait until R/B pin indicates chip is ready or timeout occurs */
553 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
554 do {
555 if (gpiod_get_value_cansleep(gpiod))
556 return 0;
557
558 cond_resched();
559 } while (time_before(jiffies, timeout_ms));
560
561 return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT;
562};
563EXPORT_SYMBOL_GPL(nand_gpio_waitrdy);
564
565/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200566 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700567 * @chip: the nand chip descriptor
568 * @mtd: MTD device structure
569 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200570 *
571 * Used when in panic, no locks are taken.
572 */
573static void panic_nand_get_device(struct nand_chip *chip,
574 struct mtd_info *mtd, int new_state)
575{
Brian Norris7854d3f2011-06-23 14:12:08 -0700576 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200577 chip->controller->active = chip;
578 chip->state = new_state;
579}
580
581/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700583 * @mtd: MTD device structure
584 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
586 * Get the device and lock it for exclusive access
587 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200588static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800589nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100591 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200592 spinlock_t *lock = &chip->controller->lock;
593 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100594 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200595retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100596 spin_lock(lock);
597
vimal singhb8b3ee92009-07-09 20:41:22 +0530598 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200599 if (!chip->controller->active)
600 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200601
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200602 if (chip->controller->active == chip && chip->state == FL_READY) {
603 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100604 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100605 return 0;
606 }
607 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800608 if (chip->controller->active->state == FL_PM_SUSPENDED) {
609 chip->state = FL_PM_SUSPENDED;
610 spin_unlock(lock);
611 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800612 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100613 }
614 set_current_state(TASK_UNINTERRUPTIBLE);
615 add_wait_queue(wq, &wait);
616 spin_unlock(lock);
617 schedule();
618 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 goto retry;
620}
621
622/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700623 * panic_nand_wait - [GENERIC] wait until the command is done
Brian Norris8b6e50c2011-05-25 14:59:01 -0700624 * @chip: NAND chip structure
625 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200626 *
627 * Wait for command done. This is a helper function for nand_wait used when
628 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400629 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200630 */
Boris Brezillon3d4af7c2018-09-07 00:38:49 +0200631void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200632{
633 int i;
634 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200635 if (chip->legacy.dev_ready) {
636 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200637 break;
638 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100639 int ret;
640 u8 status;
641
642 ret = nand_read_data_op(chip, &status, sizeof(status),
643 true);
644 if (ret)
645 return;
646
647 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200648 break;
649 }
650 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200651 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200652}
653
Miquel Raynal789157e2018-03-19 14:47:28 +0100654static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +0100655{
Miquel Raynal789157e2018-03-19 14:47:28 +0100656 return (chip->parameters.supports_set_get_features &&
657 test_bit(addr, chip->parameters.get_feature_list));
658}
659
660static bool nand_supports_set_features(struct nand_chip *chip, int addr)
661{
662 return (chip->parameters.supports_set_get_features &&
663 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +0100664}
665
666/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200667 * nand_reset_data_interface - Reset data interface and timings
668 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100669 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200670 *
671 * Reset the Data interface and timings to ONFI mode 0.
672 *
673 * Returns 0 for success or negative error code otherwise.
674 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100675static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200676{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200677 int ret;
678
679 if (!chip->setup_data_interface)
680 return 0;
681
682 /*
683 * The ONFI specification says:
684 * "
685 * To transition from NV-DDR or NV-DDR2 to the SDR data
686 * interface, the host shall use the Reset (FFh) command
687 * using SDR timing mode 0. A device in any timing mode is
688 * required to recognize Reset (FFh) command issued in SDR
689 * timing mode 0.
690 * "
691 *
692 * Configure the data interface in SDR mode and set the
693 * timings to timing mode 0.
694 */
695
Miquel Raynal17fa8042017-11-30 18:01:31 +0100696 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon858838b2018-09-06 14:05:33 +0200697 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200698 if (ret)
699 pr_err("Failed to configure data interface to SDR timing mode 0\n");
700
701 return ret;
702}
703
704/**
705 * nand_setup_data_interface - Setup the best data interface and timings
706 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100707 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200708 *
709 * Find and configure the best data interface and NAND timings supported by
710 * the chip and the driver.
711 * First tries to retrieve supported timing modes from ONFI information,
712 * and if the NAND chip does not support ONFI, relies on the
713 * ->onfi_timing_mode_default specified in the nand_ids table.
714 *
715 * Returns 0 for success or negative error code otherwise.
716 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100717static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200718{
Miquel Raynal97baea12018-03-19 14:47:20 +0100719 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
720 chip->onfi_timing_mode_default,
721 };
Boris Brezillond8e725d2016-09-15 10:32:50 +0200722 int ret;
723
Miquel Raynal17fa8042017-11-30 18:01:31 +0100724 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200725 return 0;
726
Miquel Raynal993447b2018-03-19 14:47:21 +0100727 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +0100728 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +0200729 chip->select_chip(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +0100730 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
731 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +0200732 chip->select_chip(chip, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200733 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +0100734 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +0200735 }
736
Miquel Raynal97baea12018-03-19 14:47:20 +0100737 /* Change the mode on the controller side */
Boris Brezillon858838b2018-09-06 14:05:33 +0200738 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +0100739 if (ret)
740 return ret;
741
742 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +0100743 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +0100744 return 0;
745
746 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon758b56f2018-09-06 14:05:24 +0200747 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100748 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
749 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +0200750 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +0100751 if (ret)
752 goto err_reset_chip;
753
754 if (tmode_param[0] != chip->onfi_timing_mode_default) {
755 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
756 chip->onfi_timing_mode_default);
757 goto err_reset_chip;
758 }
759
760 return 0;
761
762err_reset_chip:
763 /*
764 * Fallback to mode 0 if the chip explicitly did not ack the chosen
765 * timing mode.
766 */
767 nand_reset_data_interface(chip, chipnr);
Boris Brezillon758b56f2018-09-06 14:05:24 +0200768 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100769 nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +0200770 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +0100771
Boris Brezillond8e725d2016-09-15 10:32:50 +0200772 return ret;
773}
774
775/**
776 * nand_init_data_interface - find the best data interface and timings
777 * @chip: The NAND chip
778 *
779 * Find the best data interface and NAND timings supported by the chip
780 * and the driver.
781 * First tries to retrieve supported timing modes from ONFI information,
782 * and if the NAND chip does not support ONFI, relies on the
783 * ->onfi_timing_mode_default specified in the nand_ids table. After this
784 * function nand_chip->data_interface is initialized with the best timing mode
785 * available.
786 *
787 * Returns 0 for success or negative error code otherwise.
788 */
789static int nand_init_data_interface(struct nand_chip *chip)
790{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200791 int modes, mode, ret;
792
793 if (!chip->setup_data_interface)
794 return 0;
795
796 /*
797 * First try to identify the best timings from ONFI parameters and
798 * if the NAND does not support ONFI, fallback to the default ONFI
799 * timing mode.
800 */
Boris Brezillon462f35d2018-09-07 00:38:47 +0200801 if (chip->parameters.onfi) {
802 modes = chip->parameters.onfi->async_timing_mode;
803 } else {
Boris Brezillond8e725d2016-09-15 10:32:50 +0200804 if (!chip->onfi_timing_mode_default)
805 return 0;
806
807 modes = GENMASK(chip->onfi_timing_mode_default, 0);
808 }
809
Boris Brezillond8e725d2016-09-15 10:32:50 +0200810 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +0100811 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200812 if (ret)
813 continue;
814
Miquel Raynald787b8b2017-12-22 18:12:41 +0100815 /*
816 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
817 * controller supports the requested timings.
818 */
Boris Brezillon858838b2018-09-06 14:05:33 +0200819 ret = chip->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +0100820 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +0100821 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200822 if (!ret) {
823 chip->onfi_timing_mode_default = mode;
824 break;
825 }
826 }
827
828 return 0;
829}
830
Boris Brezillond8e725d2016-09-15 10:32:50 +0200831/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100832 * nand_fill_column_cycles - fill the column cycles of an address
833 * @chip: The NAND chip
834 * @addrs: Array of address cycles to fill
835 * @offset_in_page: The offset in the page
836 *
837 * Fills the first or the first two bytes of the @addrs field depending
838 * on the NAND bus width and the page size.
839 *
840 * Returns the number of cycles needed to encode the column, or a negative
841 * error code in case one of the arguments is invalid.
842 */
843static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
844 unsigned int offset_in_page)
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100845{
Miquel Raynal8878b122017-11-09 14:16:45 +0100846 struct mtd_info *mtd = nand_to_mtd(chip);
847
848 /* Make sure the offset is less than the actual page size. */
849 if (offset_in_page > mtd->writesize + mtd->oobsize)
850 return -EINVAL;
851
852 /*
853 * On small page NANDs, there's a dedicated command to access the OOB
854 * area, and the column address is relative to the start of the OOB
855 * area, not the start of the page. Asjust the address accordingly.
856 */
857 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
858 offset_in_page -= mtd->writesize;
859
860 /*
861 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
862 * wide, then it must be divided by 2.
863 */
864 if (chip->options & NAND_BUSWIDTH_16) {
865 if (WARN_ON(offset_in_page % 2))
866 return -EINVAL;
867
868 offset_in_page /= 2;
869 }
870
871 addrs[0] = offset_in_page;
872
873 /*
874 * Small page NANDs use 1 cycle for the columns, while large page NANDs
875 * need 2
876 */
877 if (mtd->writesize <= 512)
878 return 1;
879
880 addrs[1] = offset_in_page >> 8;
881
882 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Miquel Raynal8878b122017-11-09 14:16:45 +0100885static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
886 unsigned int offset_in_page, void *buf,
887 unsigned int len)
888{
889 struct mtd_info *mtd = nand_to_mtd(chip);
890 const struct nand_sdr_timings *sdr =
891 nand_get_sdr_timings(&chip->data_interface);
892 u8 addrs[4];
893 struct nand_op_instr instrs[] = {
894 NAND_OP_CMD(NAND_CMD_READ0, 0),
895 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
896 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
897 PSEC_TO_NSEC(sdr->tRR_min)),
898 NAND_OP_DATA_IN(len, buf, 0),
899 };
900 struct nand_operation op = NAND_OPERATION(instrs);
901 int ret;
902
903 /* Drop the DATA_IN instruction if len is set to 0. */
904 if (!len)
905 op.ninstrs--;
906
907 if (offset_in_page >= mtd->writesize)
908 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
909 else if (offset_in_page >= 256 &&
910 !(chip->options & NAND_BUSWIDTH_16))
911 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
912
913 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
914 if (ret < 0)
915 return ret;
916
917 addrs[1] = page;
918 addrs[2] = page >> 8;
919
920 if (chip->options & NAND_ROW_ADDR_3) {
921 addrs[3] = page >> 16;
922 instrs[1].ctx.addr.naddrs++;
923 }
924
925 return nand_exec_op(chip, &op);
926}
927
928static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
929 unsigned int offset_in_page, void *buf,
930 unsigned int len)
931{
932 const struct nand_sdr_timings *sdr =
933 nand_get_sdr_timings(&chip->data_interface);
934 u8 addrs[5];
935 struct nand_op_instr instrs[] = {
936 NAND_OP_CMD(NAND_CMD_READ0, 0),
937 NAND_OP_ADDR(4, addrs, 0),
938 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
939 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
940 PSEC_TO_NSEC(sdr->tRR_min)),
941 NAND_OP_DATA_IN(len, buf, 0),
942 };
943 struct nand_operation op = NAND_OPERATION(instrs);
944 int ret;
945
946 /* Drop the DATA_IN instruction if len is set to 0. */
947 if (!len)
948 op.ninstrs--;
949
950 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
951 if (ret < 0)
952 return ret;
953
954 addrs[2] = page;
955 addrs[3] = page >> 8;
956
957 if (chip->options & NAND_ROW_ADDR_3) {
958 addrs[4] = page >> 16;
959 instrs[1].ctx.addr.naddrs++;
960 }
961
962 return nand_exec_op(chip, &op);
963}
964
965/**
Boris Brezillon97d90da2017-11-30 18:01:29 +0100966 * nand_read_page_op - Do a READ PAGE operation
967 * @chip: The NAND chip
968 * @page: page to read
969 * @offset_in_page: offset within the page
970 * @buf: buffer used to store the data
971 * @len: length of the buffer
972 *
973 * This function issues a READ PAGE operation.
974 * This function does not select/unselect the CS line.
975 *
976 * Returns 0 on success, a negative error code otherwise.
977 */
978int nand_read_page_op(struct nand_chip *chip, unsigned int page,
979 unsigned int offset_in_page, void *buf, unsigned int len)
980{
981 struct mtd_info *mtd = nand_to_mtd(chip);
982
983 if (len && !buf)
984 return -EINVAL;
985
986 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
987 return -EINVAL;
988
Miquel Raynal8878b122017-11-09 14:16:45 +0100989 if (chip->exec_op) {
990 if (mtd->writesize > 512)
991 return nand_lp_exec_read_page_op(chip, page,
992 offset_in_page, buf,
993 len);
994
995 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
996 buf, len);
997 }
998
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200999 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001000 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001001 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001002
1003 return 0;
1004}
1005EXPORT_SYMBOL_GPL(nand_read_page_op);
1006
1007/**
1008 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1009 * @chip: The NAND chip
1010 * @page: parameter page to read
1011 * @buf: buffer used to store the data
1012 * @len: length of the buffer
1013 *
1014 * This function issues a READ PARAMETER PAGE operation.
1015 * This function does not select/unselect the CS line.
1016 *
1017 * Returns 0 on success, a negative error code otherwise.
1018 */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02001019int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1020 unsigned int len)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001021{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001022 unsigned int i;
1023 u8 *p = buf;
1024
1025 if (len && !buf)
1026 return -EINVAL;
1027
Miquel Raynal8878b122017-11-09 14:16:45 +01001028 if (chip->exec_op) {
1029 const struct nand_sdr_timings *sdr =
1030 nand_get_sdr_timings(&chip->data_interface);
1031 struct nand_op_instr instrs[] = {
1032 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1033 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1034 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1035 PSEC_TO_NSEC(sdr->tRR_min)),
1036 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1037 };
1038 struct nand_operation op = NAND_OPERATION(instrs);
1039
1040 /* Drop the DATA_IN instruction if len is set to 0. */
1041 if (!len)
1042 op.ninstrs--;
1043
1044 return nand_exec_op(chip, &op);
1045 }
1046
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001047 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001048 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001049 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001050
1051 return 0;
1052}
1053
1054/**
1055 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1056 * @chip: The NAND chip
1057 * @offset_in_page: offset within the page
1058 * @buf: buffer used to store the data
1059 * @len: length of the buffer
1060 * @force_8bit: force 8-bit bus access
1061 *
1062 * This function issues a CHANGE READ COLUMN operation.
1063 * This function does not select/unselect the CS line.
1064 *
1065 * Returns 0 on success, a negative error code otherwise.
1066 */
1067int nand_change_read_column_op(struct nand_chip *chip,
1068 unsigned int offset_in_page, void *buf,
1069 unsigned int len, bool force_8bit)
1070{
1071 struct mtd_info *mtd = nand_to_mtd(chip);
1072
1073 if (len && !buf)
1074 return -EINVAL;
1075
1076 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1077 return -EINVAL;
1078
Miquel Raynal8878b122017-11-09 14:16:45 +01001079 /* Small page NANDs do not support column change. */
1080 if (mtd->writesize <= 512)
1081 return -ENOTSUPP;
1082
1083 if (chip->exec_op) {
1084 const struct nand_sdr_timings *sdr =
1085 nand_get_sdr_timings(&chip->data_interface);
1086 u8 addrs[2] = {};
1087 struct nand_op_instr instrs[] = {
1088 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1089 NAND_OP_ADDR(2, addrs, 0),
1090 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1091 PSEC_TO_NSEC(sdr->tCCS_min)),
1092 NAND_OP_DATA_IN(len, buf, 0),
1093 };
1094 struct nand_operation op = NAND_OPERATION(instrs);
1095 int ret;
1096
1097 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1098 if (ret < 0)
1099 return ret;
1100
1101 /* Drop the DATA_IN instruction if len is set to 0. */
1102 if (!len)
1103 op.ninstrs--;
1104
1105 instrs[3].ctx.data.force_8bit = force_8bit;
1106
1107 return nand_exec_op(chip, &op);
1108 }
1109
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001110 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001111 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001112 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001113
1114 return 0;
1115}
1116EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1117
1118/**
1119 * nand_read_oob_op - Do a READ OOB operation
1120 * @chip: The NAND chip
1121 * @page: page to read
1122 * @offset_in_oob: offset within the OOB area
1123 * @buf: buffer used to store the data
1124 * @len: length of the buffer
1125 *
1126 * This function issues a READ OOB operation.
1127 * This function does not select/unselect the CS line.
1128 *
1129 * Returns 0 on success, a negative error code otherwise.
1130 */
1131int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1132 unsigned int offset_in_oob, void *buf, unsigned int len)
1133{
1134 struct mtd_info *mtd = nand_to_mtd(chip);
1135
1136 if (len && !buf)
1137 return -EINVAL;
1138
1139 if (offset_in_oob + len > mtd->oobsize)
1140 return -EINVAL;
1141
Miquel Raynal8878b122017-11-09 14:16:45 +01001142 if (chip->exec_op)
1143 return nand_read_page_op(chip, page,
1144 mtd->writesize + offset_in_oob,
1145 buf, len);
1146
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001147 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001148 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001149 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001150
1151 return 0;
1152}
1153EXPORT_SYMBOL_GPL(nand_read_oob_op);
1154
Miquel Raynal8878b122017-11-09 14:16:45 +01001155static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1156 unsigned int offset_in_page, const void *buf,
1157 unsigned int len, bool prog)
1158{
1159 struct mtd_info *mtd = nand_to_mtd(chip);
1160 const struct nand_sdr_timings *sdr =
1161 nand_get_sdr_timings(&chip->data_interface);
1162 u8 addrs[5] = {};
1163 struct nand_op_instr instrs[] = {
1164 /*
1165 * The first instruction will be dropped if we're dealing
1166 * with a large page NAND and adjusted if we're dealing
1167 * with a small page NAND and the page offset is > 255.
1168 */
1169 NAND_OP_CMD(NAND_CMD_READ0, 0),
1170 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1171 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1172 NAND_OP_DATA_OUT(len, buf, 0),
1173 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1174 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1175 };
1176 struct nand_operation op = NAND_OPERATION(instrs);
1177 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1178 int ret;
1179 u8 status;
1180
1181 if (naddrs < 0)
1182 return naddrs;
1183
1184 addrs[naddrs++] = page;
1185 addrs[naddrs++] = page >> 8;
1186 if (chip->options & NAND_ROW_ADDR_3)
1187 addrs[naddrs++] = page >> 16;
1188
1189 instrs[2].ctx.addr.naddrs = naddrs;
1190
1191 /* Drop the last two instructions if we're not programming the page. */
1192 if (!prog) {
1193 op.ninstrs -= 2;
1194 /* Also drop the DATA_OUT instruction if empty. */
1195 if (!len)
1196 op.ninstrs--;
1197 }
1198
1199 if (mtd->writesize <= 512) {
1200 /*
1201 * Small pages need some more tweaking: we have to adjust the
1202 * first instruction depending on the page offset we're trying
1203 * to access.
1204 */
1205 if (offset_in_page >= mtd->writesize)
1206 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1207 else if (offset_in_page >= 256 &&
1208 !(chip->options & NAND_BUSWIDTH_16))
1209 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1210 } else {
1211 /*
1212 * Drop the first command if we're dealing with a large page
1213 * NAND.
1214 */
1215 op.instrs++;
1216 op.ninstrs--;
1217 }
1218
1219 ret = nand_exec_op(chip, &op);
1220 if (!prog || ret)
1221 return ret;
1222
1223 ret = nand_status_op(chip, &status);
1224 if (ret)
1225 return ret;
1226
1227 return status;
1228}
1229
Boris Brezillon97d90da2017-11-30 18:01:29 +01001230/**
1231 * nand_prog_page_begin_op - starts a PROG PAGE operation
1232 * @chip: The NAND chip
1233 * @page: page to write
1234 * @offset_in_page: offset within the page
1235 * @buf: buffer containing the data to write to the page
1236 * @len: length of the buffer
1237 *
1238 * This function issues the first half of a PROG PAGE operation.
1239 * This function does not select/unselect the CS line.
1240 *
1241 * Returns 0 on success, a negative error code otherwise.
1242 */
1243int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1244 unsigned int offset_in_page, const void *buf,
1245 unsigned int len)
1246{
1247 struct mtd_info *mtd = nand_to_mtd(chip);
1248
1249 if (len && !buf)
1250 return -EINVAL;
1251
1252 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1253 return -EINVAL;
1254
Miquel Raynal8878b122017-11-09 14:16:45 +01001255 if (chip->exec_op)
1256 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1257 len, false);
1258
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001259 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001260
1261 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001262 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001263
1264 return 0;
1265}
1266EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1267
1268/**
1269 * nand_prog_page_end_op - ends a PROG PAGE operation
1270 * @chip: The NAND chip
1271 *
1272 * This function issues the second half of a PROG PAGE operation.
1273 * This function does not select/unselect the CS line.
1274 *
1275 * Returns 0 on success, a negative error code otherwise.
1276 */
1277int nand_prog_page_end_op(struct nand_chip *chip)
1278{
Miquel Raynal8878b122017-11-09 14:16:45 +01001279 int ret;
1280 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001281
Miquel Raynal8878b122017-11-09 14:16:45 +01001282 if (chip->exec_op) {
1283 const struct nand_sdr_timings *sdr =
1284 nand_get_sdr_timings(&chip->data_interface);
1285 struct nand_op_instr instrs[] = {
1286 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1287 PSEC_TO_NSEC(sdr->tWB_max)),
1288 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1289 };
1290 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001291
Miquel Raynal8878b122017-11-09 14:16:45 +01001292 ret = nand_exec_op(chip, &op);
1293 if (ret)
1294 return ret;
1295
1296 ret = nand_status_op(chip, &status);
1297 if (ret)
1298 return ret;
1299 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001300 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001301 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001302 if (ret < 0)
1303 return ret;
1304
1305 status = ret;
1306 }
1307
Boris Brezillon97d90da2017-11-30 18:01:29 +01001308 if (status & NAND_STATUS_FAIL)
1309 return -EIO;
1310
1311 return 0;
1312}
1313EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1314
1315/**
1316 * nand_prog_page_op - Do a full PROG PAGE operation
1317 * @chip: The NAND chip
1318 * @page: page to write
1319 * @offset_in_page: offset within the page
1320 * @buf: buffer containing the data to write to the page
1321 * @len: length of the buffer
1322 *
1323 * This function issues a full PROG PAGE operation.
1324 * This function does not select/unselect the CS line.
1325 *
1326 * Returns 0 on success, a negative error code otherwise.
1327 */
1328int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1329 unsigned int offset_in_page, const void *buf,
1330 unsigned int len)
1331{
1332 struct mtd_info *mtd = nand_to_mtd(chip);
1333 int status;
1334
1335 if (!len || !buf)
1336 return -EINVAL;
1337
1338 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1339 return -EINVAL;
1340
Miquel Raynal8878b122017-11-09 14:16:45 +01001341 if (chip->exec_op) {
1342 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1343 len, true);
1344 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001345 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1346 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001347 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001348 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001349 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001350 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001351
Boris Brezillon97d90da2017-11-30 18:01:29 +01001352 if (status & NAND_STATUS_FAIL)
1353 return -EIO;
1354
1355 return 0;
1356}
1357EXPORT_SYMBOL_GPL(nand_prog_page_op);
1358
1359/**
1360 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1361 * @chip: The NAND chip
1362 * @offset_in_page: offset within the page
1363 * @buf: buffer containing the data to send to the NAND
1364 * @len: length of the buffer
1365 * @force_8bit: force 8-bit bus access
1366 *
1367 * This function issues a CHANGE WRITE COLUMN operation.
1368 * This function does not select/unselect the CS line.
1369 *
1370 * Returns 0 on success, a negative error code otherwise.
1371 */
1372int nand_change_write_column_op(struct nand_chip *chip,
1373 unsigned int offset_in_page,
1374 const void *buf, unsigned int len,
1375 bool force_8bit)
1376{
1377 struct mtd_info *mtd = nand_to_mtd(chip);
1378
1379 if (len && !buf)
1380 return -EINVAL;
1381
1382 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1383 return -EINVAL;
1384
Miquel Raynal8878b122017-11-09 14:16:45 +01001385 /* Small page NANDs do not support column change. */
1386 if (mtd->writesize <= 512)
1387 return -ENOTSUPP;
1388
1389 if (chip->exec_op) {
1390 const struct nand_sdr_timings *sdr =
1391 nand_get_sdr_timings(&chip->data_interface);
1392 u8 addrs[2];
1393 struct nand_op_instr instrs[] = {
1394 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1395 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1396 NAND_OP_DATA_OUT(len, buf, 0),
1397 };
1398 struct nand_operation op = NAND_OPERATION(instrs);
1399 int ret;
1400
1401 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1402 if (ret < 0)
1403 return ret;
1404
1405 instrs[2].ctx.data.force_8bit = force_8bit;
1406
1407 /* Drop the DATA_OUT instruction if len is set to 0. */
1408 if (!len)
1409 op.ninstrs--;
1410
1411 return nand_exec_op(chip, &op);
1412 }
1413
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001414 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001415 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001416 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001417
1418 return 0;
1419}
1420EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1421
1422/**
1423 * nand_readid_op - Do a READID operation
1424 * @chip: The NAND chip
1425 * @addr: address cycle to pass after the READID command
1426 * @buf: buffer used to store the ID
1427 * @len: length of the buffer
1428 *
1429 * This function sends a READID command and reads back the ID returned by the
1430 * NAND.
1431 * This function does not select/unselect the CS line.
1432 *
1433 * Returns 0 on success, a negative error code otherwise.
1434 */
1435int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1436 unsigned int len)
1437{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001438 unsigned int i;
1439 u8 *id = buf;
1440
1441 if (len && !buf)
1442 return -EINVAL;
1443
Miquel Raynal8878b122017-11-09 14:16:45 +01001444 if (chip->exec_op) {
1445 const struct nand_sdr_timings *sdr =
1446 nand_get_sdr_timings(&chip->data_interface);
1447 struct nand_op_instr instrs[] = {
1448 NAND_OP_CMD(NAND_CMD_READID, 0),
1449 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1450 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1451 };
1452 struct nand_operation op = NAND_OPERATION(instrs);
1453
1454 /* Drop the DATA_IN instruction if len is set to 0. */
1455 if (!len)
1456 op.ninstrs--;
1457
1458 return nand_exec_op(chip, &op);
1459 }
1460
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001461 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001462
1463 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001464 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001465
1466 return 0;
1467}
1468EXPORT_SYMBOL_GPL(nand_readid_op);
1469
1470/**
1471 * nand_status_op - Do a STATUS operation
1472 * @chip: The NAND chip
1473 * @status: out variable to store the NAND status
1474 *
1475 * This function sends a STATUS command and reads back the status returned by
1476 * the NAND.
1477 * This function does not select/unselect the CS line.
1478 *
1479 * Returns 0 on success, a negative error code otherwise.
1480 */
1481int nand_status_op(struct nand_chip *chip, u8 *status)
1482{
Miquel Raynal8878b122017-11-09 14:16:45 +01001483 if (chip->exec_op) {
1484 const struct nand_sdr_timings *sdr =
1485 nand_get_sdr_timings(&chip->data_interface);
1486 struct nand_op_instr instrs[] = {
1487 NAND_OP_CMD(NAND_CMD_STATUS,
1488 PSEC_TO_NSEC(sdr->tADL_min)),
1489 NAND_OP_8BIT_DATA_IN(1, status, 0),
1490 };
1491 struct nand_operation op = NAND_OPERATION(instrs);
1492
1493 if (!status)
1494 op.ninstrs--;
1495
1496 return nand_exec_op(chip, &op);
1497 }
1498
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001499 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001500 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001501 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001502
1503 return 0;
1504}
1505EXPORT_SYMBOL_GPL(nand_status_op);
1506
1507/**
1508 * nand_exit_status_op - Exit a STATUS operation
1509 * @chip: The NAND chip
1510 *
1511 * This function sends a READ0 command to cancel the effect of the STATUS
1512 * command to avoid reading only the status until a new read command is sent.
1513 *
1514 * This function does not select/unselect the CS line.
1515 *
1516 * Returns 0 on success, a negative error code otherwise.
1517 */
1518int nand_exit_status_op(struct nand_chip *chip)
1519{
Miquel Raynal8878b122017-11-09 14:16:45 +01001520 if (chip->exec_op) {
1521 struct nand_op_instr instrs[] = {
1522 NAND_OP_CMD(NAND_CMD_READ0, 0),
1523 };
1524 struct nand_operation op = NAND_OPERATION(instrs);
1525
1526 return nand_exec_op(chip, &op);
1527 }
1528
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001529 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001530
1531 return 0;
1532}
Boris Brezillon97d90da2017-11-30 18:01:29 +01001533
1534/**
1535 * nand_erase_op - Do an erase operation
1536 * @chip: The NAND chip
1537 * @eraseblock: block to erase
1538 *
1539 * This function sends an ERASE command and waits for the NAND to be ready
1540 * before returning.
1541 * This function does not select/unselect the CS line.
1542 *
1543 * Returns 0 on success, a negative error code otherwise.
1544 */
1545int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1546{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001547 unsigned int page = eraseblock <<
1548 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01001549 int ret;
1550 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001551
Miquel Raynal8878b122017-11-09 14:16:45 +01001552 if (chip->exec_op) {
1553 const struct nand_sdr_timings *sdr =
1554 nand_get_sdr_timings(&chip->data_interface);
1555 u8 addrs[3] = { page, page >> 8, page >> 16 };
1556 struct nand_op_instr instrs[] = {
1557 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
1558 NAND_OP_ADDR(2, addrs, 0),
1559 NAND_OP_CMD(NAND_CMD_ERASE2,
1560 PSEC_TO_MSEC(sdr->tWB_max)),
1561 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
1562 };
1563 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001564
Miquel Raynal8878b122017-11-09 14:16:45 +01001565 if (chip->options & NAND_ROW_ADDR_3)
1566 instrs[1].ctx.addr.naddrs++;
1567
1568 ret = nand_exec_op(chip, &op);
1569 if (ret)
1570 return ret;
1571
1572 ret = nand_status_op(chip, &status);
1573 if (ret)
1574 return ret;
1575 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001576 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
1577 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01001578
Boris Brezillon8395b752018-09-07 00:38:37 +02001579 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001580 if (ret < 0)
1581 return ret;
1582
1583 status = ret;
1584 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001585
1586 if (status & NAND_STATUS_FAIL)
1587 return -EIO;
1588
1589 return 0;
1590}
1591EXPORT_SYMBOL_GPL(nand_erase_op);
1592
1593/**
1594 * nand_set_features_op - Do a SET FEATURES operation
1595 * @chip: The NAND chip
1596 * @feature: feature id
1597 * @data: 4 bytes of data
1598 *
1599 * This function sends a SET FEATURES command and waits for the NAND to be
1600 * ready before returning.
1601 * This function does not select/unselect the CS line.
1602 *
1603 * Returns 0 on success, a negative error code otherwise.
1604 */
1605static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1606 const void *data)
1607{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001608 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01001609 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001610
Miquel Raynal8878b122017-11-09 14:16:45 +01001611 if (chip->exec_op) {
1612 const struct nand_sdr_timings *sdr =
1613 nand_get_sdr_timings(&chip->data_interface);
1614 struct nand_op_instr instrs[] = {
1615 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
1616 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
1617 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
1618 PSEC_TO_NSEC(sdr->tWB_max)),
1619 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
1620 };
1621 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001622
Boris Brezillon782d1962018-05-11 14:44:07 +02001623 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01001624 }
1625
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001626 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02001627 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001628 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02001629
Boris Brezillon8395b752018-09-07 00:38:37 +02001630 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02001631 if (ret < 0)
1632 return ret;
1633
1634 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001635 return -EIO;
1636
1637 return 0;
1638}
1639
1640/**
1641 * nand_get_features_op - Do a GET FEATURES operation
1642 * @chip: The NAND chip
1643 * @feature: feature id
1644 * @data: 4 bytes of data
1645 *
1646 * This function sends a GET FEATURES command and waits for the NAND to be
1647 * ready before returning.
1648 * This function does not select/unselect the CS line.
1649 *
1650 * Returns 0 on success, a negative error code otherwise.
1651 */
1652static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1653 void *data)
1654{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001655 u8 *params = data;
1656 int i;
1657
Miquel Raynal8878b122017-11-09 14:16:45 +01001658 if (chip->exec_op) {
1659 const struct nand_sdr_timings *sdr =
1660 nand_get_sdr_timings(&chip->data_interface);
1661 struct nand_op_instr instrs[] = {
1662 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
1663 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
1664 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
1665 PSEC_TO_NSEC(sdr->tRR_min)),
1666 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
1667 data, 0),
1668 };
1669 struct nand_operation op = NAND_OPERATION(instrs);
1670
1671 return nand_exec_op(chip, &op);
1672 }
1673
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001674 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001675 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001676 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001677
1678 return 0;
1679}
1680
Boris Brezillon52f05b62018-07-27 09:44:18 +02001681static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
1682 unsigned int delay_ns)
1683{
1684 if (chip->exec_op) {
1685 struct nand_op_instr instrs[] = {
1686 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
1687 PSEC_TO_NSEC(delay_ns)),
1688 };
1689 struct nand_operation op = NAND_OPERATION(instrs);
1690
1691 return nand_exec_op(chip, &op);
1692 }
1693
1694 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02001695 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001696 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001697 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001698 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001699
1700 return 0;
1701}
1702
Boris Brezillon97d90da2017-11-30 18:01:29 +01001703/**
1704 * nand_reset_op - Do a reset operation
1705 * @chip: The NAND chip
1706 *
1707 * This function sends a RESET command and waits for the NAND to be ready
1708 * before returning.
1709 * This function does not select/unselect the CS line.
1710 *
1711 * Returns 0 on success, a negative error code otherwise.
1712 */
1713int nand_reset_op(struct nand_chip *chip)
1714{
Miquel Raynal8878b122017-11-09 14:16:45 +01001715 if (chip->exec_op) {
1716 const struct nand_sdr_timings *sdr =
1717 nand_get_sdr_timings(&chip->data_interface);
1718 struct nand_op_instr instrs[] = {
1719 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
1720 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
1721 };
1722 struct nand_operation op = NAND_OPERATION(instrs);
1723
1724 return nand_exec_op(chip, &op);
1725 }
1726
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001727 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001728
1729 return 0;
1730}
1731EXPORT_SYMBOL_GPL(nand_reset_op);
1732
1733/**
1734 * nand_read_data_op - Read data from the NAND
1735 * @chip: The NAND chip
1736 * @buf: buffer used to store the data
1737 * @len: length of the buffer
1738 * @force_8bit: force 8-bit bus access
1739 *
1740 * This function does a raw data read on the bus. Usually used after launching
1741 * another NAND operation like nand_read_page_op().
1742 * This function does not select/unselect the CS line.
1743 *
1744 * Returns 0 on success, a negative error code otherwise.
1745 */
1746int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1747 bool force_8bit)
1748{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001749 if (!len || !buf)
1750 return -EINVAL;
1751
Miquel Raynal8878b122017-11-09 14:16:45 +01001752 if (chip->exec_op) {
1753 struct nand_op_instr instrs[] = {
1754 NAND_OP_DATA_IN(len, buf, 0),
1755 };
1756 struct nand_operation op = NAND_OPERATION(instrs);
1757
1758 instrs[0].ctx.data.force_8bit = force_8bit;
1759
1760 return nand_exec_op(chip, &op);
1761 }
1762
Boris Brezillon97d90da2017-11-30 18:01:29 +01001763 if (force_8bit) {
1764 u8 *p = buf;
1765 unsigned int i;
1766
1767 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001768 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001769 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001770 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001771 }
1772
1773 return 0;
1774}
1775EXPORT_SYMBOL_GPL(nand_read_data_op);
1776
1777/**
1778 * nand_write_data_op - Write data from the NAND
1779 * @chip: The NAND chip
1780 * @buf: buffer containing the data to send on the bus
1781 * @len: length of the buffer
1782 * @force_8bit: force 8-bit bus access
1783 *
1784 * This function does a raw data write on the bus. Usually used after launching
1785 * another NAND operation like nand_write_page_begin_op().
1786 * This function does not select/unselect the CS line.
1787 *
1788 * Returns 0 on success, a negative error code otherwise.
1789 */
1790int nand_write_data_op(struct nand_chip *chip, const void *buf,
1791 unsigned int len, bool force_8bit)
1792{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001793 if (!len || !buf)
1794 return -EINVAL;
1795
Miquel Raynal8878b122017-11-09 14:16:45 +01001796 if (chip->exec_op) {
1797 struct nand_op_instr instrs[] = {
1798 NAND_OP_DATA_OUT(len, buf, 0),
1799 };
1800 struct nand_operation op = NAND_OPERATION(instrs);
1801
1802 instrs[0].ctx.data.force_8bit = force_8bit;
1803
1804 return nand_exec_op(chip, &op);
1805 }
1806
Boris Brezillon97d90da2017-11-30 18:01:29 +01001807 if (force_8bit) {
1808 const u8 *p = buf;
1809 unsigned int i;
1810
1811 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001812 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001813 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001814 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001815 }
1816
1817 return 0;
1818}
1819EXPORT_SYMBOL_GPL(nand_write_data_op);
1820
1821/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001822 * struct nand_op_parser_ctx - Context used by the parser
1823 * @instrs: array of all the instructions that must be addressed
1824 * @ninstrs: length of the @instrs array
1825 * @subop: Sub-operation to be passed to the NAND controller
1826 *
1827 * This structure is used by the core to split NAND operations into
1828 * sub-operations that can be handled by the NAND controller.
1829 */
1830struct nand_op_parser_ctx {
1831 const struct nand_op_instr *instrs;
1832 unsigned int ninstrs;
1833 struct nand_subop subop;
1834};
1835
1836/**
1837 * nand_op_parser_must_split_instr - Checks if an instruction must be split
1838 * @pat: the parser pattern element that matches @instr
1839 * @instr: pointer to the instruction to check
1840 * @start_offset: this is an in/out parameter. If @instr has already been
1841 * split, then @start_offset is the offset from which to start
1842 * (either an address cycle or an offset in the data buffer).
1843 * Conversely, if the function returns true (ie. instr must be
1844 * split), this parameter is updated to point to the first
1845 * data/address cycle that has not been taken care of.
1846 *
1847 * Some NAND controllers are limited and cannot send X address cycles with a
1848 * unique operation, or cannot read/write more than Y bytes at the same time.
1849 * In this case, split the instruction that does not fit in a single
1850 * controller-operation into two or more chunks.
1851 *
1852 * Returns true if the instruction must be split, false otherwise.
1853 * The @start_offset parameter is also updated to the offset at which the next
1854 * bundle of instruction must start (if an address or a data instruction).
1855 */
1856static bool
1857nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
1858 const struct nand_op_instr *instr,
1859 unsigned int *start_offset)
1860{
1861 switch (pat->type) {
1862 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001863 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01001864 break;
1865
1866 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001867 pat->ctx.addr.maxcycles) {
1868 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01001869 return true;
1870 }
1871 break;
1872
1873 case NAND_OP_DATA_IN_INSTR:
1874 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001875 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01001876 break;
1877
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001878 if (instr->ctx.data.len - *start_offset >
1879 pat->ctx.data.maxlen) {
1880 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01001881 return true;
1882 }
1883 break;
1884
1885 default:
1886 break;
1887 }
1888
1889 return false;
1890}
1891
1892/**
1893 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
1894 * remaining in the parser context
1895 * @pat: the pattern to test
1896 * @ctx: the parser context structure to match with the pattern @pat
1897 *
1898 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
1899 * Returns true if this is the case, false ortherwise. When true is returned,
1900 * @ctx->subop is updated with the set of instructions to be passed to the
1901 * controller driver.
1902 */
1903static bool
1904nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
1905 struct nand_op_parser_ctx *ctx)
1906{
1907 unsigned int instr_offset = ctx->subop.first_instr_start_off;
1908 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
1909 const struct nand_op_instr *instr = ctx->subop.instrs;
1910 unsigned int i, ninstrs;
1911
1912 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
1913 /*
1914 * The pattern instruction does not match the operation
1915 * instruction. If the instruction is marked optional in the
1916 * pattern definition, we skip the pattern element and continue
1917 * to the next one. If the element is mandatory, there's no
1918 * match and we can return false directly.
1919 */
1920 if (instr->type != pat->elems[i].type) {
1921 if (!pat->elems[i].optional)
1922 return false;
1923
1924 continue;
1925 }
1926
1927 /*
1928 * Now check the pattern element constraints. If the pattern is
1929 * not able to handle the whole instruction in a single step,
1930 * we have to split it.
1931 * The last_instr_end_off value comes back updated to point to
1932 * the position where we have to split the instruction (the
1933 * start of the next subop chunk).
1934 */
1935 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
1936 &instr_offset)) {
1937 ninstrs++;
1938 i++;
1939 break;
1940 }
1941
1942 instr++;
1943 ninstrs++;
1944 instr_offset = 0;
1945 }
1946
1947 /*
1948 * This can happen if all instructions of a pattern are optional.
1949 * Still, if there's not at least one instruction handled by this
1950 * pattern, this is not a match, and we should try the next one (if
1951 * any).
1952 */
1953 if (!ninstrs)
1954 return false;
1955
1956 /*
1957 * We had a match on the pattern head, but the pattern may be longer
1958 * than the instructions we're asked to execute. We need to make sure
1959 * there's no mandatory elements in the pattern tail.
1960 */
1961 for (; i < pat->nelems; i++) {
1962 if (!pat->elems[i].optional)
1963 return false;
1964 }
1965
1966 /*
1967 * We have a match: update the subop structure accordingly and return
1968 * true.
1969 */
1970 ctx->subop.ninstrs = ninstrs;
1971 ctx->subop.last_instr_end_off = instr_offset;
1972
1973 return true;
1974}
1975
1976#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
1977static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
1978{
1979 const struct nand_op_instr *instr;
1980 char *prefix = " ";
1981 unsigned int i;
1982
1983 pr_debug("executing subop:\n");
1984
1985 for (i = 0; i < ctx->ninstrs; i++) {
1986 instr = &ctx->instrs[i];
1987
1988 if (instr == &ctx->subop.instrs[0])
1989 prefix = " ->";
1990
1991 switch (instr->type) {
1992 case NAND_OP_CMD_INSTR:
1993 pr_debug("%sCMD [0x%02x]\n", prefix,
1994 instr->ctx.cmd.opcode);
1995 break;
1996 case NAND_OP_ADDR_INSTR:
1997 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
1998 instr->ctx.addr.naddrs,
1999 instr->ctx.addr.naddrs < 64 ?
2000 instr->ctx.addr.naddrs : 64,
2001 instr->ctx.addr.addrs);
2002 break;
2003 case NAND_OP_DATA_IN_INSTR:
2004 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2005 instr->ctx.data.len,
2006 instr->ctx.data.force_8bit ?
2007 ", force 8-bit" : "");
2008 break;
2009 case NAND_OP_DATA_OUT_INSTR:
2010 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2011 instr->ctx.data.len,
2012 instr->ctx.data.force_8bit ?
2013 ", force 8-bit" : "");
2014 break;
2015 case NAND_OP_WAITRDY_INSTR:
2016 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2017 instr->ctx.waitrdy.timeout_ms);
2018 break;
2019 }
2020
2021 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2022 prefix = " ";
2023 }
2024}
2025#else
2026static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2027{
2028 /* NOP */
2029}
2030#endif
2031
2032/**
2033 * nand_op_parser_exec_op - exec_op parser
2034 * @chip: the NAND chip
2035 * @parser: patterns description provided by the controller driver
2036 * @op: the NAND operation to address
2037 * @check_only: when true, the function only checks if @op can be handled but
2038 * does not execute the operation
2039 *
2040 * Helper function designed to ease integration of NAND controller drivers that
2041 * only support a limited set of instruction sequences. The supported sequences
2042 * are described in @parser, and the framework takes care of splitting @op into
2043 * multiple sub-operations (if required) and pass them back to the ->exec()
2044 * callback of the matching pattern if @check_only is set to false.
2045 *
2046 * NAND controller drivers should call this function from their own ->exec_op()
2047 * implementation.
2048 *
2049 * Returns 0 on success, a negative error code otherwise. A failure can be
2050 * caused by an unsupported operation (none of the supported patterns is able
2051 * to handle the requested operation), or an error returned by one of the
2052 * matching pattern->exec() hook.
2053 */
2054int nand_op_parser_exec_op(struct nand_chip *chip,
2055 const struct nand_op_parser *parser,
2056 const struct nand_operation *op, bool check_only)
2057{
2058 struct nand_op_parser_ctx ctx = {
2059 .subop.instrs = op->instrs,
2060 .instrs = op->instrs,
2061 .ninstrs = op->ninstrs,
2062 };
2063 unsigned int i;
2064
2065 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2066 int ret;
2067
2068 for (i = 0; i < parser->npatterns; i++) {
2069 const struct nand_op_parser_pattern *pattern;
2070
2071 pattern = &parser->patterns[i];
2072 if (!nand_op_parser_match_pat(pattern, &ctx))
2073 continue;
2074
2075 nand_op_parser_trace(&ctx);
2076
2077 if (check_only)
2078 break;
2079
2080 ret = pattern->exec(chip, &ctx.subop);
2081 if (ret)
2082 return ret;
2083
2084 break;
2085 }
2086
2087 if (i == parser->npatterns) {
2088 pr_debug("->exec_op() parser: pattern not found!\n");
2089 return -ENOTSUPP;
2090 }
2091
2092 /*
2093 * Update the context structure by pointing to the start of the
2094 * next subop.
2095 */
2096 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2097 if (ctx.subop.last_instr_end_off)
2098 ctx.subop.instrs -= 1;
2099
2100 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2101 }
2102
2103 return 0;
2104}
2105EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2106
2107static bool nand_instr_is_data(const struct nand_op_instr *instr)
2108{
2109 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2110 instr->type == NAND_OP_DATA_OUT_INSTR);
2111}
2112
2113static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2114 unsigned int instr_idx)
2115{
2116 return subop && instr_idx < subop->ninstrs;
2117}
2118
Miquel Raynal760c4352018-07-19 00:09:12 +02002119static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2120 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002121{
2122 if (instr_idx)
2123 return 0;
2124
2125 return subop->first_instr_start_off;
2126}
2127
2128/**
2129 * nand_subop_get_addr_start_off - Get the start offset in an address array
2130 * @subop: The entire sub-operation
2131 * @instr_idx: Index of the instruction inside the sub-operation
2132 *
2133 * During driver development, one could be tempted to directly use the
2134 * ->addr.addrs field of address instructions. This is wrong as address
2135 * instructions might be split.
2136 *
2137 * Given an address instruction, returns the offset of the first cycle to issue.
2138 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002139unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2140 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002141{
Miquel Raynal760c4352018-07-19 00:09:12 +02002142 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2143 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2144 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002145
2146 return nand_subop_get_start_off(subop, instr_idx);
2147}
2148EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2149
2150/**
2151 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2152 * @subop: The entire sub-operation
2153 * @instr_idx: Index of the instruction inside the sub-operation
2154 *
2155 * During driver development, one could be tempted to directly use the
2156 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2157 * might be split.
2158 *
2159 * Given an address instruction, returns the number of address cycle to issue.
2160 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002161unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2162 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002163{
2164 int start_off, end_off;
2165
Miquel Raynal760c4352018-07-19 00:09:12 +02002166 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2167 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2168 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002169
2170 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2171
2172 if (instr_idx == subop->ninstrs - 1 &&
2173 subop->last_instr_end_off)
2174 end_off = subop->last_instr_end_off;
2175 else
2176 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2177
2178 return end_off - start_off;
2179}
2180EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2181
2182/**
2183 * nand_subop_get_data_start_off - Get the start offset in a data array
2184 * @subop: The entire sub-operation
2185 * @instr_idx: Index of the instruction inside the sub-operation
2186 *
2187 * During driver development, one could be tempted to directly use the
2188 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2189 * instructions might be split.
2190 *
2191 * Given a data instruction, returns the offset to start from.
2192 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002193unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2194 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002195{
Miquel Raynal760c4352018-07-19 00:09:12 +02002196 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2197 !nand_instr_is_data(&subop->instrs[instr_idx])))
2198 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002199
2200 return nand_subop_get_start_off(subop, instr_idx);
2201}
2202EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2203
2204/**
2205 * nand_subop_get_data_len - Get the number of bytes to retrieve
2206 * @subop: The entire sub-operation
2207 * @instr_idx: Index of the instruction inside the sub-operation
2208 *
2209 * During driver development, one could be tempted to directly use the
2210 * ->data->len field of a data instruction. This is wrong as data instructions
2211 * might be split.
2212 *
2213 * Returns the length of the chunk of data to send/receive.
2214 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002215unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2216 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002217{
2218 int start_off = 0, end_off;
2219
Miquel Raynal760c4352018-07-19 00:09:12 +02002220 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2221 !nand_instr_is_data(&subop->instrs[instr_idx])))
2222 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002223
2224 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2225
2226 if (instr_idx == subop->ninstrs - 1 &&
2227 subop->last_instr_end_off)
2228 end_off = subop->last_instr_end_off;
2229 else
2230 end_off = subop->instrs[instr_idx].ctx.data.len;
2231
2232 return end_off - start_off;
2233}
2234EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002237 * nand_reset - Reset and initialize a NAND device
2238 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002239 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002240 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002241 * Save the timings data structure, then apply SDR timings mode 0 (see
2242 * nand_reset_data_interface for details), do the reset operation, and
2243 * apply back the previous timings.
2244 *
2245 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002246 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002247int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002248{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002249 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002250 int ret;
2251
Boris Brezillon104e4422017-03-16 09:35:58 +01002252 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002253 if (ret)
2254 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002255
Boris Brezillon73f907f2016-10-24 16:46:20 +02002256 /*
2257 * The CS line has to be released before we can apply the new NAND
2258 * interface settings, hence this weird ->select_chip() dance.
2259 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02002260 chip->select_chip(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002261 ret = nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02002262 chip->select_chip(chip, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002263 if (ret)
2264 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002265
Miquel Raynal107b7d62018-03-19 14:47:25 +01002266 /*
2267 * A nand_reset_data_interface() put both the NAND chip and the NAND
2268 * controller in timings mode 0. If the default mode for this chip is
2269 * also 0, no need to proceed to the change again. Plus, at probe time,
2270 * nand_setup_data_interface() uses ->set/get_features() which would
2271 * fail anyway as the parameter page is not available yet.
2272 */
2273 if (!chip->onfi_timing_mode_default)
2274 return 0;
2275
Miquel Raynal17fa8042017-11-30 18:01:31 +01002276 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002277 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002278 if (ret)
2279 return ret;
2280
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002281 return 0;
2282}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002283EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002284
2285/**
Boris Brezillon45240362018-09-07 00:38:40 +02002286 * nand_get_features - wrapper to perform a GET_FEATURE
2287 * @chip: NAND chip info structure
2288 * @addr: feature address
2289 * @subfeature_param: the subfeature parameters, a four bytes array
2290 *
2291 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2292 * operation cannot be handled.
2293 */
2294int nand_get_features(struct nand_chip *chip, int addr,
2295 u8 *subfeature_param)
2296{
2297 if (!nand_supports_get_features(chip, addr))
2298 return -ENOTSUPP;
2299
2300 if (chip->legacy.get_features)
2301 return chip->legacy.get_features(chip, addr, subfeature_param);
2302
2303 return nand_get_features_op(chip, addr, subfeature_param);
2304}
Boris Brezillon45240362018-09-07 00:38:40 +02002305
2306/**
2307 * nand_set_features - wrapper to perform a SET_FEATURE
2308 * @chip: NAND chip info structure
2309 * @addr: feature address
2310 * @subfeature_param: the subfeature parameters, a four bytes array
2311 *
2312 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2313 * operation cannot be handled.
2314 */
2315int nand_set_features(struct nand_chip *chip, int addr,
2316 u8 *subfeature_param)
2317{
2318 if (!nand_supports_set_features(chip, addr))
2319 return -ENOTSUPP;
2320
2321 if (chip->legacy.set_features)
2322 return chip->legacy.set_features(chip, addr, subfeature_param);
2323
2324 return nand_set_features_op(chip, addr, subfeature_param);
2325}
Boris Brezillon45240362018-09-07 00:38:40 +02002326
2327/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002328 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2329 * @buf: buffer to test
2330 * @len: buffer length
2331 * @bitflips_threshold: maximum number of bitflips
2332 *
2333 * Check if a buffer contains only 0xff, which means the underlying region
2334 * has been erased and is ready to be programmed.
2335 * The bitflips_threshold specify the maximum number of bitflips before
2336 * considering the region is not erased.
2337 * Note: The logic of this function has been extracted from the memweight
2338 * implementation, except that nand_check_erased_buf function exit before
2339 * testing the whole buffer if the number of bitflips exceed the
2340 * bitflips_threshold value.
2341 *
2342 * Returns a positive number of bitflips less than or equal to
2343 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2344 * threshold.
2345 */
2346static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2347{
2348 const unsigned char *bitmap = buf;
2349 int bitflips = 0;
2350 int weight;
2351
2352 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2353 len--, bitmap++) {
2354 weight = hweight8(*bitmap);
2355 bitflips += BITS_PER_BYTE - weight;
2356 if (unlikely(bitflips > bitflips_threshold))
2357 return -EBADMSG;
2358 }
2359
2360 for (; len >= sizeof(long);
2361 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002362 unsigned long d = *((unsigned long *)bitmap);
2363 if (d == ~0UL)
2364 continue;
2365 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002366 bitflips += BITS_PER_LONG - weight;
2367 if (unlikely(bitflips > bitflips_threshold))
2368 return -EBADMSG;
2369 }
2370
2371 for (; len > 0; len--, bitmap++) {
2372 weight = hweight8(*bitmap);
2373 bitflips += BITS_PER_BYTE - weight;
2374 if (unlikely(bitflips > bitflips_threshold))
2375 return -EBADMSG;
2376 }
2377
2378 return bitflips;
2379}
2380
2381/**
2382 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2383 * 0xff data
2384 * @data: data buffer to test
2385 * @datalen: data length
2386 * @ecc: ECC buffer
2387 * @ecclen: ECC length
2388 * @extraoob: extra OOB buffer
2389 * @extraooblen: extra OOB length
2390 * @bitflips_threshold: maximum number of bitflips
2391 *
2392 * Check if a data buffer and its associated ECC and OOB data contains only
2393 * 0xff pattern, which means the underlying region has been erased and is
2394 * ready to be programmed.
2395 * The bitflips_threshold specify the maximum number of bitflips before
2396 * considering the region as not erased.
2397 *
2398 * Note:
2399 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2400 * different from the NAND page size. When fixing bitflips, ECC engines will
2401 * report the number of errors per chunk, and the NAND core infrastructure
2402 * expect you to return the maximum number of bitflips for the whole page.
2403 * This is why you should always use this function on a single chunk and
2404 * not on the whole page. After checking each chunk you should update your
2405 * max_bitflips value accordingly.
2406 * 2/ When checking for bitflips in erased pages you should not only check
2407 * the payload data but also their associated ECC data, because a user might
2408 * have programmed almost all bits to 1 but a few. In this case, we
2409 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2410 * this case.
2411 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2412 * data are protected by the ECC engine.
2413 * It could also be used if you support subpages and want to attach some
2414 * extra OOB data to an ECC chunk.
2415 *
2416 * Returns a positive number of bitflips less than or equal to
2417 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2418 * threshold. In case of success, the passed buffers are filled with 0xff.
2419 */
2420int nand_check_erased_ecc_chunk(void *data, int datalen,
2421 void *ecc, int ecclen,
2422 void *extraoob, int extraooblen,
2423 int bitflips_threshold)
2424{
2425 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2426
2427 data_bitflips = nand_check_erased_buf(data, datalen,
2428 bitflips_threshold);
2429 if (data_bitflips < 0)
2430 return data_bitflips;
2431
2432 bitflips_threshold -= data_bitflips;
2433
2434 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2435 if (ecc_bitflips < 0)
2436 return ecc_bitflips;
2437
2438 bitflips_threshold -= ecc_bitflips;
2439
2440 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2441 bitflips_threshold);
2442 if (extraoob_bitflips < 0)
2443 return extraoob_bitflips;
2444
2445 if (data_bitflips)
2446 memset(data, 0xff, datalen);
2447
2448 if (ecc_bitflips)
2449 memset(ecc, 0xff, ecclen);
2450
2451 if (extraoob_bitflips)
2452 memset(extraoob, 0xff, extraooblen);
2453
2454 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2455}
2456EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2457
2458/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002459 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002460 * @chip: nand chip info structure
2461 * @buf: buffer to store read data
2462 * @oob_required: caller requires OOB data read to chip->oob_poi
2463 * @page: page number to read
2464 *
2465 * Returns -ENOTSUPP unconditionally.
2466 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002467int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2468 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002469{
2470 return -ENOTSUPP;
2471}
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002472
2473/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002474 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002475 * @chip: nand chip info structure
2476 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002477 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002478 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002479 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002480 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002481 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002482int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2483 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002484{
Boris Brezillonb9761682018-09-06 14:05:20 +02002485 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002486 int ret;
2487
Boris Brezillon25f815f2017-11-30 18:01:30 +01002488 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002489 if (ret)
2490 return ret;
2491
2492 if (oob_required) {
2493 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2494 false);
2495 if (ret)
2496 return ret;
2497 }
2498
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002499 return 0;
2500}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002501EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002502
2503/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002504 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002505 * @chip: nand chip info structure
2506 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002507 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002508 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002509 *
2510 * We need a special oob layout and handling even when OOB isn't used.
2511 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002512static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07002513 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002514{
Boris Brezillonb9761682018-09-06 14:05:20 +02002515 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08002516 int eccsize = chip->ecc.size;
2517 int eccbytes = chip->ecc.bytes;
2518 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002519 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002520
Boris Brezillon25f815f2017-11-30 18:01:30 +01002521 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2522 if (ret)
2523 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002524
2525 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002526 ret = nand_read_data_op(chip, buf, eccsize, false);
2527 if (ret)
2528 return ret;
2529
David Brownell52ff49d2009-03-04 12:01:36 -08002530 buf += eccsize;
2531
2532 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002533 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2534 false);
2535 if (ret)
2536 return ret;
2537
David Brownell52ff49d2009-03-04 12:01:36 -08002538 oob += chip->ecc.prepad;
2539 }
2540
Boris Brezillon97d90da2017-11-30 18:01:29 +01002541 ret = nand_read_data_op(chip, oob, eccbytes, false);
2542 if (ret)
2543 return ret;
2544
David Brownell52ff49d2009-03-04 12:01:36 -08002545 oob += eccbytes;
2546
2547 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002548 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2549 false);
2550 if (ret)
2551 return ret;
2552
David Brownell52ff49d2009-03-04 12:01:36 -08002553 oob += chip->ecc.postpad;
2554 }
2555 }
2556
2557 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002558 if (size) {
2559 ret = nand_read_data_op(chip, oob, size, false);
2560 if (ret)
2561 return ret;
2562 }
David Brownell52ff49d2009-03-04 12:01:36 -08002563
2564 return 0;
2565}
2566
2567/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002568 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002569 * @chip: nand chip info structure
2570 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002571 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002572 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002573 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002574static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
2575 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Boris Brezillonb9761682018-09-06 14:05:20 +02002577 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002578 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002579 int eccbytes = chip->ecc.bytes;
2580 int eccsteps = chip->ecc.steps;
2581 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002582 uint8_t *ecc_calc = chip->ecc.calc_buf;
2583 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002584 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002585
Boris Brezillonb9761682018-09-06 14:05:20 +02002586 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002587
2588 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002589 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002590
Boris Brezillon846031d2016-02-03 20:11:00 +01002591 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2592 chip->ecc.total);
2593 if (ret)
2594 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002595
2596 eccsteps = chip->ecc.steps;
2597 p = buf;
2598
2599 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2600 int stat;
2601
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002602 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07002603 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002604 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002605 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002606 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002607 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2608 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002609 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002610 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01002611}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302614 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002615 * @chip: nand chip info structure
2616 * @data_offs: offset of requested data within the page
2617 * @readlen: data length
2618 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08002619 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01002620 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002621static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
2622 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01002623{
Boris Brezillonb9761682018-09-06 14:05:20 +02002624 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002625 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002626 uint8_t *p;
2627 int data_col_addr, i, gaps = 0;
2628 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
2629 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01002630 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07002631 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01002632 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01002633
Brian Norris7854d3f2011-06-23 14:12:08 -07002634 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01002635 start_step = data_offs / chip->ecc.size;
2636 end_step = (data_offs + readlen - 1) / chip->ecc.size;
2637 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10302638 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01002639
Brian Norris8b6e50c2011-05-25 14:59:01 -07002640 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01002641 datafrag_len = num_steps * chip->ecc.size;
2642 eccfrag_len = num_steps * chip->ecc.bytes;
2643
2644 data_col_addr = start_step * chip->ecc.size;
2645 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01002646 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01002647 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002648 if (ret)
2649 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002650
Brian Norris8b6e50c2011-05-25 14:59:01 -07002651 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01002652 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002653 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01002654
Brian Norris8b6e50c2011-05-25 14:59:01 -07002655 /*
2656 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07002657 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07002658 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002659 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
2660 if (ret)
2661 return ret;
2662
2663 if (oobregion.length < eccfrag_len)
2664 gaps = 1;
2665
Alexey Korolev3d459552008-05-15 17:23:18 +01002666 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002667 ret = nand_change_read_column_op(chip, mtd->writesize,
2668 chip->oob_poi, mtd->oobsize,
2669 false);
2670 if (ret)
2671 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002672 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002673 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002674 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07002675 * about buswidth alignment in read_buf.
2676 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002677 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01002678 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01002679 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002680 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01002681 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
2682 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002683 aligned_len++;
2684
Boris Brezillon97d90da2017-11-30 18:01:29 +01002685 ret = nand_change_read_column_op(chip,
2686 mtd->writesize + aligned_pos,
2687 &chip->oob_poi[aligned_pos],
2688 aligned_len, false);
2689 if (ret)
2690 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002691 }
2692
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002693 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01002694 chip->oob_poi, index, eccfrag_len);
2695 if (ret)
2696 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002697
2698 p = bufpoi + data_col_addr;
2699 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
2700 int stat;
2701
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002702 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002703 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002704 if (stat == -EBADMSG &&
2705 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2706 /* check for empty pages with bitflips */
2707 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002708 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002709 chip->ecc.bytes,
2710 NULL, 0,
2711 chip->ecc.strength);
2712 }
2713
Mike Dunn3f91e942012-04-25 12:06:09 -07002714 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002715 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002716 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01002717 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002718 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2719 }
Alexey Korolev3d459552008-05-15 17:23:18 +01002720 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002721 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01002722}
2723
2724/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002725 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002726 * @chip: nand chip info structure
2727 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002728 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002729 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002730 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002731 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002732 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002733static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
2734 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002735{
Boris Brezillonb9761682018-09-06 14:05:20 +02002736 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002737 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002738 int eccbytes = chip->ecc.bytes;
2739 int eccsteps = chip->ecc.steps;
2740 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002741 uint8_t *ecc_calc = chip->ecc.calc_buf;
2742 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002743 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002744
Boris Brezillon25f815f2017-11-30 18:01:30 +01002745 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2746 if (ret)
2747 return ret;
2748
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002749 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02002750 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002751
2752 ret = nand_read_data_op(chip, p, eccsize, false);
2753 if (ret)
2754 return ret;
2755
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002756 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002757 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002758
2759 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2760 if (ret)
2761 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002762
Boris Brezillon846031d2016-02-03 20:11:00 +01002763 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2764 chip->ecc.total);
2765 if (ret)
2766 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002767
2768 eccsteps = chip->ecc.steps;
2769 p = buf;
2770
2771 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2772 int stat;
2773
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002774 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002775 if (stat == -EBADMSG &&
2776 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2777 /* check for empty pages with bitflips */
2778 stat = nand_check_erased_ecc_chunk(p, eccsize,
2779 &ecc_code[i], eccbytes,
2780 NULL, 0,
2781 chip->ecc.strength);
2782 }
2783
Mike Dunn3f91e942012-04-25 12:06:09 -07002784 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002785 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002786 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002787 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002788 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2789 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002790 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002791 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002792}
2793
2794/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002795 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07002796 * @chip: nand chip info structure
2797 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002798 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002799 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002800 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002801 * Hardware ECC for large page chips, require OOB to be read first. For this
2802 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2803 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2804 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2805 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002806 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002807static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
2808 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002809{
Boris Brezillonb9761682018-09-06 14:05:20 +02002810 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002811 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002812 int eccbytes = chip->ecc.bytes;
2813 int eccsteps = chip->ecc.steps;
2814 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002815 uint8_t *ecc_code = chip->ecc.code_buf;
2816 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002817 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002818
2819 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01002820 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2821 if (ret)
2822 return ret;
2823
2824 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2825 if (ret)
2826 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002827
Boris Brezillon846031d2016-02-03 20:11:00 +01002828 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2829 chip->ecc.total);
2830 if (ret)
2831 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002832
2833 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2834 int stat;
2835
Boris Brezillonec476362018-09-06 14:05:17 +02002836 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002837
2838 ret = nand_read_data_op(chip, p, eccsize, false);
2839 if (ret)
2840 return ret;
2841
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002842 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002843
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002844 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002845 if (stat == -EBADMSG &&
2846 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2847 /* check for empty pages with bitflips */
2848 stat = nand_check_erased_ecc_chunk(p, eccsize,
2849 &ecc_code[i], eccbytes,
2850 NULL, 0,
2851 chip->ecc.strength);
2852 }
2853
Mike Dunn3f91e942012-04-25 12:06:09 -07002854 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002855 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002856 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002857 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002858 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2859 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002860 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002861 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002862}
2863
2864/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002865 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07002866 * @chip: nand chip info structure
2867 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002868 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002869 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002870 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002871 * The hw generator calculates the error syndrome automatically. Therefore we
2872 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002873 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002874static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
2875 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002876{
Boris Brezillonb9761682018-09-06 14:05:20 +02002877 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002878 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002879 int eccbytes = chip->ecc.bytes;
2880 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002881 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002882 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002883 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07002884 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002885
Boris Brezillon25f815f2017-11-30 18:01:30 +01002886 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2887 if (ret)
2888 return ret;
2889
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002890 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2891 int stat;
2892
Boris Brezillonec476362018-09-06 14:05:17 +02002893 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002894
2895 ret = nand_read_data_op(chip, p, eccsize, false);
2896 if (ret)
2897 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002898
2899 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002900 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2901 false);
2902 if (ret)
2903 return ret;
2904
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002905 oob += chip->ecc.prepad;
2906 }
2907
Boris Brezillonec476362018-09-06 14:05:17 +02002908 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002909
2910 ret = nand_read_data_op(chip, oob, eccbytes, false);
2911 if (ret)
2912 return ret;
2913
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002914 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002915
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002916 oob += eccbytes;
2917
2918 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002919 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2920 false);
2921 if (ret)
2922 return ret;
2923
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002924 oob += chip->ecc.postpad;
2925 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002926
2927 if (stat == -EBADMSG &&
2928 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2929 /* check for empty pages with bitflips */
2930 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2931 oob - eccpadbytes,
2932 eccpadbytes,
2933 NULL, 0,
2934 chip->ecc.strength);
2935 }
2936
2937 if (stat < 0) {
2938 mtd->ecc_stats.failed++;
2939 } else {
2940 mtd->ecc_stats.corrected += stat;
2941 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2942 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002943 }
2944
2945 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002946 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002947 if (i) {
2948 ret = nand_read_data_op(chip, oob, i, false);
2949 if (ret)
2950 return ret;
2951 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002952
Mike Dunn3f91e942012-04-25 12:06:09 -07002953 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002954}
2955
2956/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002957 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01002958 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002959 * @oob: oob destination address
2960 * @ops: oob ops structure
2961 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002962 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002963static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03002964 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002965{
Boris Brezillon846031d2016-02-03 20:11:00 +01002966 struct nand_chip *chip = mtd_to_nand(mtd);
2967 int ret;
2968
Florian Fainellif8ac0412010-09-07 13:23:43 +02002969 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002970
Brian Norris0612b9d2011-08-30 18:45:40 -07002971 case MTD_OPS_PLACE_OOB:
2972 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002973 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2974 return oob + len;
2975
Boris Brezillon846031d2016-02-03 20:11:00 +01002976 case MTD_OPS_AUTO_OOB:
2977 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
2978 ops->ooboffs, len);
2979 BUG_ON(ret);
2980 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002981
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002982 default:
2983 BUG();
2984 }
2985 return NULL;
2986}
2987
2988/**
Brian Norrisba84fb52014-01-03 15:13:33 -08002989 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02002990 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08002991 * @retry_mode: the retry mode to use
2992 *
2993 * Some vendors supply a special command to shift the Vt threshold, to be used
2994 * when there are too many bitflips in a page (i.e., ECC error). After setting
2995 * a new threshold, the host should retry reading the page.
2996 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02002997static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08002998{
Brian Norrisba84fb52014-01-03 15:13:33 -08002999 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3000
3001 if (retry_mode >= chip->read_retries)
3002 return -EINVAL;
3003
3004 if (!chip->setup_read_retry)
3005 return -EOPNOTSUPP;
3006
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003007 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003008}
3009
Boris Brezillon85e08e52018-07-27 09:44:17 +02003010static void nand_wait_readrdy(struct nand_chip *chip)
3011{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003012 const struct nand_sdr_timings *sdr;
3013
Boris Brezillon85e08e52018-07-27 09:44:17 +02003014 if (!(chip->options & NAND_NEED_READRDY))
3015 return;
3016
Boris Brezillon52f05b62018-07-27 09:44:18 +02003017 sdr = nand_get_sdr_timings(&chip->data_interface);
3018 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003019}
3020
Brian Norrisba84fb52014-01-03 15:13:33 -08003021/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003022 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003023 * @mtd: MTD device structure
3024 * @from: offset to read from
3025 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003026 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003027 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003028 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003029static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3030 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003031{
Brian Norrise47f3db2012-05-02 10:14:56 -07003032 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003033 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003034 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003035 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003036 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003037 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003038
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003039 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003040 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003041 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003042 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003043 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003045 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003046 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003048 realpage = (int)(from >> chip->page_shift);
3049 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003051 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003053 buf = ops->datbuf;
3054 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003055 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003056
Florian Fainellif8ac0412010-09-07 13:23:43 +02003057 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003058 unsigned int ecc_failures = mtd->ecc_stats.failed;
3059
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003060 bytes = min(mtd->writesize - col, readlen);
3061 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003062
Kamal Dasu66507c72014-05-01 20:51:19 -04003063 if (!aligned)
3064 use_bufpoi = 1;
3065 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003066 use_bufpoi = !virt_addr_valid(buf) ||
3067 !IS_ALIGNED((unsigned long)buf,
3068 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003069 else
3070 use_bufpoi = 0;
3071
Brian Norris8b6e50c2011-05-25 14:59:01 -07003072 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003073 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003074 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003075
3076 if (use_bufpoi && aligned)
3077 pr_debug("%s: using read bounce buffer for buf@%p\n",
3078 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Brian Norrisba84fb52014-01-03 15:13:33 -08003080read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003081 /*
3082 * Now read the page into the buffer. Absent an error,
3083 * the read methods return max bitflips per ecc step.
3084 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003085 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003086 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003087 oob_required,
3088 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003089 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3090 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003091 ret = chip->ecc.read_subpage(chip, col, bytes,
3092 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003093 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003094 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003095 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003096 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003097 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003098 /* Invalidate page cache */
3099 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003100 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003101 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003102
3103 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003104 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003105 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003106 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003107 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003108 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003109 chip->pagebuf_bitflips = ret;
3110 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003111 /* Invalidate page cache */
3112 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003113 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003114 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003116
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003117 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003118 int toread = min(oobreadlen, max_oobsize);
3119
3120 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003121 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003122 oob, ops, toread);
3123 oobreadlen -= toread;
3124 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003125 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003126
Boris Brezillon85e08e52018-07-27 09:44:17 +02003127 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003128
Brian Norrisba84fb52014-01-03 15:13:33 -08003129 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003130 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003131 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003132 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003133 retry_mode);
3134 if (ret < 0)
3135 break;
3136
3137 /* Reset failures; retry */
3138 mtd->ecc_stats.failed = ecc_failures;
3139 goto read_retry;
3140 } else {
3141 /* No more retry modes; real failure */
3142 ecc_fail = true;
3143 }
3144 }
3145
3146 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003147 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003148 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003149 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003150 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003151 max_bitflips = max_t(unsigned int, max_bitflips,
3152 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003155 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003156
Brian Norrisba84fb52014-01-03 15:13:33 -08003157 /* Reset to retry mode 0 */
3158 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003159 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003160 if (ret < 0)
3161 break;
3162 retry_mode = 0;
3163 }
3164
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003165 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Brian Norris8b6e50c2011-05-25 14:59:01 -07003168 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 col = 0;
3170 /* Increment page address */
3171 realpage++;
3172
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003173 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 /* Check, if we cross a chip boundary */
3175 if (!page) {
3176 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003177 chip->select_chip(chip, -1);
3178 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003181 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003183 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003184 if (oob)
3185 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Mike Dunn3f91e942012-04-25 12:06:09 -07003187 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003188 return ret;
3189
Brian Norrisb72f3df2013-12-03 11:04:14 -08003190 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003191 return -EBADMSG;
3192
Mike Dunnedbc45402012-04-25 12:06:11 -07003193 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003194}
3195
3196/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003197 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003198 * @chip: nand chip info structure
3199 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003200 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003201int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003202{
Boris Brezillonb9761682018-09-06 14:05:20 +02003203 struct mtd_info *mtd = nand_to_mtd(chip);
3204
Boris Brezillon97d90da2017-11-30 18:01:29 +01003205 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003206}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003207EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003208
3209/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003210 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003211 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003212 * @chip: nand chip info structure
3213 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003214 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003215static int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003216{
Boris Brezillonb9761682018-09-06 14:05:20 +02003217 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003218 int length = mtd->oobsize;
3219 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3220 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003221 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003222 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003223
Boris Brezillon97d90da2017-11-30 18:01:29 +01003224 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3225 if (ret)
3226 return ret;
3227
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003228 for (i = 0; i < chip->ecc.steps; i++) {
3229 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003230 int ret;
3231
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003232 pos = eccsize + i * (eccsize + chunk);
3233 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003234 ret = nand_change_read_column_op(chip, pos,
3235 NULL, 0,
3236 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003237 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003238 ret = nand_read_page_op(chip, page, pos, NULL,
3239 0);
3240
3241 if (ret)
3242 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003243 } else
3244 sndrnd = 1;
3245 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003246
3247 ret = nand_read_data_op(chip, bufpoi, toread, false);
3248 if (ret)
3249 return ret;
3250
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003251 bufpoi += toread;
3252 length -= toread;
3253 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003254 if (length > 0) {
3255 ret = nand_read_data_op(chip, bufpoi, length, false);
3256 if (ret)
3257 return ret;
3258 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003259
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003260 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003261}
3262
3263/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003264 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003265 * @chip: nand chip info structure
3266 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003267 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003268int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003269{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003270 struct mtd_info *mtd = nand_to_mtd(chip);
3271
Boris Brezillon97d90da2017-11-30 18:01:29 +01003272 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3273 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003274}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003275EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003276
3277/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003278 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003279 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003280 * @chip: nand chip info structure
3281 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003282 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003283static int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003284{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003285 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003286 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3287 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003288 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003289 const uint8_t *bufpoi = chip->oob_poi;
3290
3291 /*
3292 * data-ecc-data-ecc ... ecc-oob
3293 * or
3294 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3295 */
3296 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3297 pos = steps * (eccsize + chunk);
3298 steps = 0;
3299 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003300 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003301
Boris Brezillon97d90da2017-11-30 18:01:29 +01003302 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3303 if (ret)
3304 return ret;
3305
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003306 for (i = 0; i < steps; i++) {
3307 if (sndcmd) {
3308 if (mtd->writesize <= 512) {
3309 uint32_t fill = 0xFFFFFFFF;
3310
3311 len = eccsize;
3312 while (len > 0) {
3313 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003314
3315 ret = nand_write_data_op(chip, &fill,
3316 num, false);
3317 if (ret)
3318 return ret;
3319
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003320 len -= num;
3321 }
3322 } else {
3323 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003324 ret = nand_change_write_column_op(chip, pos,
3325 NULL, 0,
3326 false);
3327 if (ret)
3328 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003329 }
3330 } else
3331 sndcmd = 1;
3332 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003333
3334 ret = nand_write_data_op(chip, bufpoi, len, false);
3335 if (ret)
3336 return ret;
3337
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003338 bufpoi += len;
3339 length -= len;
3340 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003341 if (length > 0) {
3342 ret = nand_write_data_op(chip, bufpoi, length, false);
3343 if (ret)
3344 return ret;
3345 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003346
Boris Brezillon97d90da2017-11-30 18:01:29 +01003347 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003348}
3349
3350/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003351 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003352 * @mtd: MTD device structure
3353 * @from: offset to read from
3354 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003356 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003358static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3359 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003361 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003362 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003363 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003364 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003365 int readlen = ops->ooblen;
3366 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003367 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003368 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Brian Norris289c0522011-07-19 10:06:09 -07003370 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303371 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Brian Norris041e4572011-06-23 16:45:24 -07003373 stats = mtd->ecc_stats;
3374
Boris BREZILLON29f10582016-03-07 10:46:52 +01003375 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003376
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003377 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003378 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003380 /* Shift to get page */
3381 realpage = (int)(from >> chip->page_shift);
3382 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Florian Fainellif8ac0412010-09-07 13:23:43 +02003384 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003385 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003386 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003387 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003388 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003389
3390 if (ret < 0)
3391 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003392
3393 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003394 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003395
Boris Brezillon85e08e52018-07-27 09:44:17 +02003396 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003397
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003398 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3399
Vitaly Wool70145682006-11-03 18:20:38 +03003400 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003401 if (!readlen)
3402 break;
3403
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003404 /* Increment page address */
3405 realpage++;
3406
3407 page = realpage & chip->pagemask;
3408 /* Check, if we cross a chip boundary */
3409 if (!page) {
3410 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003411 chip->select_chip(chip, -1);
3412 chip->select_chip(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003415 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003417 ops->oobretlen = ops->ooblen - readlen;
3418
3419 if (ret < 0)
3420 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003421
3422 if (mtd->ecc_stats.failed - stats.failed)
3423 return -EBADMSG;
3424
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003425 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
3428/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003429 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003430 * @mtd: MTD device structure
3431 * @from: offset to read from
3432 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003434 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003436static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3437 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003439 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003440
3441 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003443 if (ops->mode != MTD_OPS_PLACE_OOB &&
3444 ops->mode != MTD_OPS_AUTO_OOB &&
3445 ops->mode != MTD_OPS_RAW)
3446 return -ENOTSUPP;
3447
Huang Shijie6a8214a2012-11-19 14:43:30 +08003448 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003450 if (!ops->datbuf)
3451 ret = nand_do_read_oob(mtd, from, ops);
3452 else
3453 ret = nand_do_read_ops(mtd, from, ops);
3454
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003456 return ret;
3457}
3458
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003459/**
3460 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003461 * @chip: nand chip info structure
3462 * @buf: data buffer
3463 * @oob_required: must write chip->oob_poi to OOB
3464 * @page: page number to write
3465 *
3466 * Returns -ENOTSUPP unconditionally.
3467 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003468int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3469 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003470{
3471 return -ENOTSUPP;
3472}
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003473
3474/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003475 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003476 * @chip: nand chip info structure
3477 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003478 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003479 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003480 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003481 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003482 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003483int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3484 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003485{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003486 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003487 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003488
Boris Brezillon25f815f2017-11-30 18:01:30 +01003489 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003490 if (ret)
3491 return ret;
3492
3493 if (oob_required) {
3494 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3495 false);
3496 if (ret)
3497 return ret;
3498 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003499
Boris Brezillon25f815f2017-11-30 18:01:30 +01003500 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003502EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003504/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003505 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003506 * @chip: nand chip info structure
3507 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003508 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003509 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003510 *
3511 * We need a special oob layout and handling even when ECC isn't checked.
3512 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003513static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003514 const uint8_t *buf, int oob_required,
3515 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003516{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003517 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003518 int eccsize = chip->ecc.size;
3519 int eccbytes = chip->ecc.bytes;
3520 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003521 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003522
Boris Brezillon25f815f2017-11-30 18:01:30 +01003523 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3524 if (ret)
3525 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003526
3527 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003528 ret = nand_write_data_op(chip, buf, eccsize, false);
3529 if (ret)
3530 return ret;
3531
David Brownell52ff49d2009-03-04 12:01:36 -08003532 buf += eccsize;
3533
3534 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003535 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3536 false);
3537 if (ret)
3538 return ret;
3539
David Brownell52ff49d2009-03-04 12:01:36 -08003540 oob += chip->ecc.prepad;
3541 }
3542
Boris Brezillon97d90da2017-11-30 18:01:29 +01003543 ret = nand_write_data_op(chip, oob, eccbytes, false);
3544 if (ret)
3545 return ret;
3546
David Brownell52ff49d2009-03-04 12:01:36 -08003547 oob += eccbytes;
3548
3549 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003550 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3551 false);
3552 if (ret)
3553 return ret;
3554
David Brownell52ff49d2009-03-04 12:01:36 -08003555 oob += chip->ecc.postpad;
3556 }
3557 }
3558
3559 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003560 if (size) {
3561 ret = nand_write_data_op(chip, oob, size, false);
3562 if (ret)
3563 return ret;
3564 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003565
Boris Brezillon25f815f2017-11-30 18:01:30 +01003566 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003567}
3568/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003569 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003570 * @chip: nand chip info structure
3571 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003572 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003573 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003574 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003575static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
3576 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003577{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003578 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003579 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003580 int eccbytes = chip->ecc.bytes;
3581 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003582 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003583 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003584
Brian Norris7854d3f2011-06-23 14:12:08 -07003585 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003586 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003587 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003588
Boris Brezillon846031d2016-02-03 20:11:00 +01003589 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3590 chip->ecc.total);
3591 if (ret)
3592 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003593
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003594 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003595}
3596
3597/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003598 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003599 * @chip: nand chip info structure
3600 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003601 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003602 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003603 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003604static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
3605 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003606{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003607 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003608 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003609 int eccbytes = chip->ecc.bytes;
3610 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003611 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003612 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003613
Boris Brezillon25f815f2017-11-30 18:01:30 +01003614 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3615 if (ret)
3616 return ret;
3617
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003618 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003619 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003620
3621 ret = nand_write_data_op(chip, p, eccsize, false);
3622 if (ret)
3623 return ret;
3624
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003625 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003626 }
3627
Boris Brezillon846031d2016-02-03 20:11:00 +01003628 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3629 chip->ecc.total);
3630 if (ret)
3631 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003632
Boris Brezillon97d90da2017-11-30 18:01:29 +01003633 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3634 if (ret)
3635 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003636
Boris Brezillon25f815f2017-11-30 18:01:30 +01003637 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003638}
3639
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303640
3641/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08003642 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303643 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07003644 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303645 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07003646 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303647 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003648 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303649 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003650static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
3651 uint32_t data_len, const uint8_t *buf,
3652 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303653{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003654 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303655 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003656 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303657 int ecc_size = chip->ecc.size;
3658 int ecc_bytes = chip->ecc.bytes;
3659 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303660 uint32_t start_step = offset / ecc_size;
3661 uint32_t end_step = (offset + data_len - 1) / ecc_size;
3662 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01003663 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303664
Boris Brezillon25f815f2017-11-30 18:01:30 +01003665 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3666 if (ret)
3667 return ret;
3668
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303669 for (step = 0; step < ecc_steps; step++) {
3670 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02003671 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303672
3673 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003674 ret = nand_write_data_op(chip, buf, ecc_size, false);
3675 if (ret)
3676 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303677
3678 /* mask ECC of un-touched subpages by padding 0xFF */
3679 if ((step < start_step) || (step > end_step))
3680 memset(ecc_calc, 0xff, ecc_bytes);
3681 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003682 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303683
3684 /* mask OOB of un-touched subpages by padding 0xFF */
3685 /* if oob_required, preserve OOB metadata of written subpage */
3686 if (!oob_required || (step < start_step) || (step > end_step))
3687 memset(oob_buf, 0xff, oob_bytes);
3688
Brian Norrisd6a950802013-08-08 17:16:36 -07003689 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303690 ecc_calc += ecc_bytes;
3691 oob_buf += oob_bytes;
3692 }
3693
3694 /* copy calculated ECC for whole page to chip->buffer->oob */
3695 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003696 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01003697 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3698 chip->ecc.total);
3699 if (ret)
3700 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303701
3702 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003703 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3704 if (ret)
3705 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303706
Boris Brezillon25f815f2017-11-30 18:01:30 +01003707 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303708}
3709
3710
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003711/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003712 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003713 * @chip: nand chip info structure
3714 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003715 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003716 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003717 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003718 * The hw generator calculates the error syndrome automatically. Therefore we
3719 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003720 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003721static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
3722 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003723{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003724 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003725 int i, eccsize = chip->ecc.size;
3726 int eccbytes = chip->ecc.bytes;
3727 int eccsteps = chip->ecc.steps;
3728 const uint8_t *p = buf;
3729 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003730 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003731
Boris Brezillon25f815f2017-11-30 18:01:30 +01003732 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3733 if (ret)
3734 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003735
3736 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003737 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003738
3739 ret = nand_write_data_op(chip, p, eccsize, false);
3740 if (ret)
3741 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003742
3743 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003744 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3745 false);
3746 if (ret)
3747 return ret;
3748
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003749 oob += chip->ecc.prepad;
3750 }
3751
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003752 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003753
3754 ret = nand_write_data_op(chip, oob, eccbytes, false);
3755 if (ret)
3756 return ret;
3757
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003758 oob += eccbytes;
3759
3760 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003761 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3762 false);
3763 if (ret)
3764 return ret;
3765
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003766 oob += chip->ecc.postpad;
3767 }
3768 }
3769
3770 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003771 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003772 if (i) {
3773 ret = nand_write_data_op(chip, oob, i, false);
3774 if (ret)
3775 return ret;
3776 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003777
Boris Brezillon25f815f2017-11-30 18:01:30 +01003778 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003779}
3780
3781/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003782 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07003783 * @mtd: MTD device structure
3784 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303785 * @offset: address offset within the page
3786 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07003787 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07003788 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07003789 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003790 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003791 */
3792static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303793 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02003794 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003795{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303796 int status, subpage;
3797
3798 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3799 chip->ecc.write_subpage)
3800 subpage = offset || (data_len < mtd->writesize);
3801 else
3802 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003803
David Woodhouse956e9442006-09-25 17:12:39 +01003804 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003805 status = chip->ecc.write_page_raw(chip, buf, oob_required,
3806 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303807 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003808 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
3809 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003810 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003811 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08003812
3813 if (status < 0)
3814 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003815
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003816 return 0;
3817}
3818
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003819/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003820 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003821 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003822 * @oob: oob data buffer
3823 * @len: oob data write length
3824 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003825 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003826static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3827 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003828{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003829 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01003830 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003831
3832 /*
3833 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3834 * data from a previous OOB read.
3835 */
3836 memset(chip->oob_poi, 0xff, mtd->oobsize);
3837
Florian Fainellif8ac0412010-09-07 13:23:43 +02003838 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003839
Brian Norris0612b9d2011-08-30 18:45:40 -07003840 case MTD_OPS_PLACE_OOB:
3841 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003842 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3843 return oob + len;
3844
Boris Brezillon846031d2016-02-03 20:11:00 +01003845 case MTD_OPS_AUTO_OOB:
3846 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
3847 ops->ooboffs, len);
3848 BUG_ON(ret);
3849 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003850
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003851 default:
3852 BUG();
3853 }
3854 return NULL;
3855}
3856
Florian Fainellif8ac0412010-09-07 13:23:43 +02003857#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003858
3859/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003860 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003861 * @mtd: MTD device structure
3862 * @to: offset to write to
3863 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003864 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003865 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003866 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003867static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3868 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003869{
Corentin Labbe73600b62017-09-02 10:49:38 +02003870 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003871 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003872 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02003873
3874 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003875 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003876
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003877 uint8_t *oob = ops->oobbuf;
3878 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303879 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07003880 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003881
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003882 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003883 if (!writelen)
3884 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003885
Brian Norris8b6e50c2011-05-25 14:59:01 -07003886 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003887 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003888 pr_notice("%s: attempt to write non page aligned data\n",
3889 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003890 return -EINVAL;
3891 }
3892
Thomas Gleixner29072b92006-09-28 15:38:36 +02003893 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003894
Thomas Gleixner6a930962006-06-28 00:11:45 +02003895 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003896 chip->select_chip(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02003897
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003898 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003899 if (nand_check_wp(mtd)) {
3900 ret = -EIO;
3901 goto err_out;
3902 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003903
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003904 realpage = (int)(to >> chip->page_shift);
3905 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003906
3907 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07003908 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3909 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003910 chip->pagebuf = -1;
3911
Maxim Levitsky782ce792010-02-22 20:39:36 +02003912 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003913 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3914 ret = -EINVAL;
3915 goto err_out;
3916 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02003917
Florian Fainellif8ac0412010-09-07 13:23:43 +02003918 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003919 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003920 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003921 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02003922 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02003923
Kamal Dasu66507c72014-05-01 20:51:19 -04003924 if (part_pagewr)
3925 use_bufpoi = 1;
3926 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003927 use_bufpoi = !virt_addr_valid(buf) ||
3928 !IS_ALIGNED((unsigned long)buf,
3929 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003930 else
3931 use_bufpoi = 0;
3932
3933 /* Partial page write?, or need to use bounce buffer */
3934 if (use_bufpoi) {
3935 pr_debug("%s: using write bounce buffer for buf@%p\n",
3936 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04003937 if (part_pagewr)
3938 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02003939 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003940 memset(chip->data_buf, 0xff, mtd->writesize);
3941 memcpy(&chip->data_buf[column], buf, bytes);
3942 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003943 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003944
Maxim Levitsky782ce792010-02-22 20:39:36 +02003945 if (unlikely(oob)) {
3946 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003947 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003948 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003949 } else {
3950 /* We still need to erase leftover OOB data */
3951 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003952 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003953
3954 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02003955 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003956 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003957 if (ret)
3958 break;
3959
3960 writelen -= bytes;
3961 if (!writelen)
3962 break;
3963
Thomas Gleixner29072b92006-09-28 15:38:36 +02003964 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003965 buf += bytes;
3966 realpage++;
3967
3968 page = realpage & chip->pagemask;
3969 /* Check, if we cross a chip boundary */
3970 if (!page) {
3971 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003972 chip->select_chip(chip, -1);
3973 chip->select_chip(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003974 }
3975 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003976
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003977 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03003978 if (unlikely(oob))
3979 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003980
3981err_out:
Boris Brezillon758b56f2018-09-06 14:05:24 +02003982 chip->select_chip(chip, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003983 return ret;
3984}
3985
3986/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003987 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003988 * @mtd: MTD device structure
3989 * @to: offset to write to
3990 * @len: number of bytes to write
3991 * @retlen: pointer to variable to store the number of written bytes
3992 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003993 *
3994 * NAND write with ECC. Used when performing writes in interrupt context, this
3995 * may for example be called by mtdoops when writing an oops while in panic.
3996 */
3997static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3998 size_t *retlen, const uint8_t *buf)
3999{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004000 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004001 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004002 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004003 int ret;
4004
Brian Norris8b6e50c2011-05-25 14:59:01 -07004005 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004006 panic_nand_get_device(chip, mtd, FL_WRITING);
4007
Boris Brezillon758b56f2018-09-06 14:05:24 +02004008 chip->select_chip(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004009
4010 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004011 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004012
Brian Norris0ec56dc2015-02-28 02:02:30 -08004013 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004014 ops.len = len;
4015 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004016 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004017
Brian Norris4a89ff82011-08-30 18:45:45 -07004018 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004019
Brian Norris4a89ff82011-08-30 18:45:45 -07004020 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004021 return ret;
4022}
4023
4024/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004025 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004026 * @mtd: MTD device structure
4027 * @to: offset to write to
4028 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004029 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004030 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004031 */
4032static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4033 struct mtd_oob_ops *ops)
4034{
Adrian Hunter03736152007-01-31 17:58:29 +02004035 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004036 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Brian Norris289c0522011-07-19 10:06:09 -07004038 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304039 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Boris BREZILLON29f10582016-03-07 10:46:52 +01004041 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004044 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004045 pr_debug("%s: attempt to write past end of page\n",
4046 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 return -EINVAL;
4048 }
4049
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004050 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004051
4052 /*
4053 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4054 * of my DiskOnChip 2000 test units) will clear the whole data page too
4055 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4056 * it in the doc2000 driver in August 1999. dwmw2.
4057 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004058 nand_reset(chip, chipnr);
4059
Boris Brezillon758b56f2018-09-06 14:05:24 +02004060 chip->select_chip(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004061
4062 /* Shift to get page */
4063 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
4065 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004066 if (nand_check_wp(mtd)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02004067 chip->select_chip(chip, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004068 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004069 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004072 if (page == chip->pagebuf)
4073 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004075 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004076
Brian Norris0612b9d2011-08-30 18:45:40 -07004077 if (ops->mode == MTD_OPS_RAW)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004078 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
Brian Norris9ce244b2011-08-30 18:45:37 -07004079 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004080 status = chip->ecc.write_oob(chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004081
Boris Brezillon758b56f2018-09-06 14:05:24 +02004082 chip->select_chip(chip, -1);
Huang Shijieb0bb6902012-11-19 14:43:29 +08004083
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004084 if (status)
4085 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Vitaly Wool70145682006-11-03 18:20:38 +03004087 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004089 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004090}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004092/**
4093 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004094 * @mtd: MTD device structure
4095 * @to: offset to write to
4096 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004097 */
4098static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4099 struct mtd_oob_ops *ops)
4100{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004101 int ret = -ENOTSUPP;
4102
4103 ops->retlen = 0;
4104
Huang Shijie6a8214a2012-11-19 14:43:30 +08004105 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004106
Florian Fainellif8ac0412010-09-07 13:23:43 +02004107 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004108 case MTD_OPS_PLACE_OOB:
4109 case MTD_OPS_AUTO_OOB:
4110 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004111 break;
4112
4113 default:
4114 goto out;
4115 }
4116
4117 if (!ops->datbuf)
4118 ret = nand_do_write_oob(mtd, to, ops);
4119 else
4120 ret = nand_do_write_ops(mtd, to, ops);
4121
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004122out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004123 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 return ret;
4125}
4126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127/**
Brian Norris49c50b92014-05-06 16:02:19 -07004128 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004129 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004130 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 *
Brian Norris49c50b92014-05-06 16:02:19 -07004132 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004134static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004136 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004139 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004140
Boris Brezillon97d90da2017-11-30 18:01:29 +01004141 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142}
4143
4144/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004146 * @mtd: MTD device structure
4147 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004149 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004151static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004153 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004155
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004157 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004158 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004159 * @instr: erase instruction
4160 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004162 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004164int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004165 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004167 struct mtd_info *mtd = nand_to_mtd(chip);
Adrian Hunter69423d92008-12-10 13:37:21 +00004168 int page, status, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004169 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Brian Norris289c0522011-07-19 10:06:09 -07004171 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4172 __func__, (unsigned long long)instr->addr,
4173 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304175 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004179 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
4181 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004182 page = (int)(instr->addr >> chip->page_shift);
4183 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
4185 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004186 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
4188 /* Select the NAND device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004189 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 /* Check, if it is write protected */
4192 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004193 pr_debug("%s: device is write protected!\n",
4194 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004195 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 goto erase_exit;
4197 }
4198
4199 /* Loop through the pages */
4200 len = instr->len;
4201
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004203 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004204 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304205 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004206 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4207 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004208 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 goto erase_exit;
4210 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004211
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004212 /*
4213 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004214 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004215 */
4216 if (page <= chip->pagebuf && chip->pagebuf <
4217 (page + pages_per_block))
4218 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
Boris Brezillonf9ebd1b2018-09-07 00:38:39 +02004220 if (chip->legacy.erase)
4221 status = chip->legacy.erase(chip,
4222 page & chip->pagemask);
4223 else
4224 status = single_erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
4226 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004227 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004228 pr_debug("%s: failed erase, page 0x%08x\n",
4229 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004230 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004231 instr->fail_addr =
4232 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 goto erase_exit;
4234 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004237 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 page += pages_per_block;
4239
4240 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004241 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004243 chip->select_chip(chip, -1);
4244 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 }
4246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004248 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004249erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004252 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 nand_release_device(mtd);
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 /* Return more or less happy */
4256 return ret;
4257}
4258
4259/**
4260 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004261 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004263 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004265static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266{
Brian Norris289c0522011-07-19 10:06:09 -07004267 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
4269 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004270 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004272 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273}
4274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004276 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004277 * @mtd: MTD device structure
4278 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004280static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304282 struct nand_chip *chip = mtd_to_nand(mtd);
4283 int chipnr = (int)(offs >> chip->chip_shift);
4284 int ret;
4285
4286 /* Select the NAND device */
4287 nand_get_device(mtd, FL_READING);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004288 chip->select_chip(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304289
4290 ret = nand_block_checkbad(mtd, offs, 0);
4291
Boris Brezillon758b56f2018-09-06 14:05:24 +02004292 chip->select_chip(chip, -1);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304293 nand_release_device(mtd);
4294
4295 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
4298/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004299 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004300 * @mtd: MTD device structure
4301 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004303static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 int ret;
4306
Florian Fainellif8ac0412010-09-07 13:23:43 +02004307 ret = nand_block_isbad(mtd, ofs);
4308 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004309 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 if (ret > 0)
4311 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004312 return ret;
4313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
Brian Norris5a0edb22013-07-30 17:52:58 -07004315 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
4317
4318/**
Zach Brown56718422017-01-10 13:30:20 -06004319 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4320 * @mtd: MTD device structure
4321 * @ofs: offset relative to mtd start
4322 * @len: length of mtd
4323 */
4324static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4325{
4326 struct nand_chip *chip = mtd_to_nand(mtd);
4327 u32 part_start_block;
4328 u32 part_end_block;
4329 u32 part_start_die;
4330 u32 part_end_die;
4331
4332 /*
4333 * max_bb_per_die and blocks_per_die used to determine
4334 * the maximum bad block count.
4335 */
4336 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4337 return -ENOTSUPP;
4338
4339 /* Get the start and end of the partition in erase blocks. */
4340 part_start_block = mtd_div_by_eb(ofs, mtd);
4341 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4342
4343 /* Get the start and end LUNs of the partition. */
4344 part_start_die = part_start_block / chip->blocks_per_die;
4345 part_end_die = part_end_block / chip->blocks_per_die;
4346
4347 /*
4348 * Look up the bad blocks per unit and multiply by the number of units
4349 * that the partition spans.
4350 */
4351 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4352}
4353
4354/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004355 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004356 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004357 */
4358static int nand_suspend(struct mtd_info *mtd)
4359{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004360 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004361}
4362
4363/**
4364 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004365 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004366 */
4367static void nand_resume(struct mtd_info *mtd)
4368{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004369 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004370
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004371 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004372 nand_release_device(mtd);
4373 else
Brian Norrisd0370212011-07-19 10:06:08 -07004374 pr_err("%s called for a chip which is not in suspended state\n",
4375 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004376}
4377
Scott Branden72ea4032014-11-20 11:18:05 -08004378/**
4379 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4380 * prevent further operations
4381 * @mtd: MTD device structure
4382 */
4383static void nand_shutdown(struct mtd_info *mtd)
4384{
Brian Norris9ca641b2015-11-09 16:37:28 -08004385 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004386}
4387
Brian Norris8b6e50c2011-05-25 14:59:01 -07004388/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004389static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004390{
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004391 nand_legacy_set_defaults(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004392
4393 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004394 chip->controller = &chip->dummy_controller;
4395 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004396 }
4397
Masahiro Yamada477544c2017-03-30 17:15:05 +09004398 if (!chip->buf_align)
4399 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004400}
4401
Brian Norris8b6e50c2011-05-25 14:59:01 -07004402/* Sanitize ONFI strings so we can safely print them */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004403void sanitize_string(uint8_t *s, size_t len)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004404{
4405 ssize_t i;
4406
Brian Norris8b6e50c2011-05-25 14:59:01 -07004407 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004408 s[len - 1] = 0;
4409
Brian Norris8b6e50c2011-05-25 14:59:01 -07004410 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004411 for (i = 0; i < len - 1; i++) {
4412 if (s[i] < ' ' || s[i] > 127)
4413 s[i] = '?';
4414 }
4415
Brian Norris8b6e50c2011-05-25 14:59:01 -07004416 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004417 strim(s);
4418}
4419
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004420/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07004421 * nand_id_has_period - Check if an ID string has a given wraparound period
4422 * @id_data: the ID string
4423 * @arrlen: the length of the @id_data array
4424 * @period: the period of repitition
4425 *
4426 * Check if an ID string is repeated within a given sequence of bytes at
4427 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08004428 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07004429 * if the repetition has a period of @period; otherwise, returns zero.
4430 */
4431static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4432{
4433 int i, j;
4434 for (i = 0; i < period; i++)
4435 for (j = i + period; j < arrlen; j += period)
4436 if (id_data[i] != id_data[j])
4437 return 0;
4438 return 1;
4439}
4440
4441/*
4442 * nand_id_len - Get the length of an ID string returned by CMD_READID
4443 * @id_data: the ID string
4444 * @arrlen: the length of the @id_data array
4445
4446 * Returns the length of the ID string, according to known wraparound/trailing
4447 * zero patterns. If no pattern exists, returns the length of the array.
4448 */
4449static int nand_id_len(u8 *id_data, int arrlen)
4450{
4451 int last_nonzero, period;
4452
4453 /* Find last non-zero byte */
4454 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4455 if (id_data[last_nonzero])
4456 break;
4457
4458 /* All zeros */
4459 if (last_nonzero < 0)
4460 return 0;
4461
4462 /* Calculate wraparound period */
4463 for (period = 1; period < arrlen; period++)
4464 if (nand_id_has_period(id_data, arrlen, period))
4465 break;
4466
4467 /* There's a repeated pattern */
4468 if (period < arrlen)
4469 return period;
4470
4471 /* There are trailing zeros */
4472 if (last_nonzero < arrlen - 1)
4473 return last_nonzero + 1;
4474
4475 /* No pattern detected */
4476 return arrlen;
4477}
4478
Huang Shijie7db906b2013-09-25 14:58:11 +08004479/* Extract the bits of per cell from the 3rd byte of the extended ID */
4480static int nand_get_bits_per_cell(u8 cellinfo)
4481{
4482 int bits;
4483
4484 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4485 bits >>= NAND_CI_CELLTYPE_SHIFT;
4486 return bits + 1;
4487}
4488
Brian Norrise3b88bd2012-09-24 20:40:52 -07004489/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004490 * Many new NAND share similar device ID codes, which represent the size of the
4491 * chip. The rest of the parameters must be decoded according to generic or
4492 * manufacturer-specific "extended ID" decoding patterns.
4493 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004494void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004495{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004496 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02004497 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004498 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004499 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08004500 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004501 /* The 4th id byte is the important one */
4502 extid = id_data[3];
4503
Boris Brezillon01389b62016-06-08 10:30:18 +02004504 /* Calc pagesize */
4505 mtd->writesize = 1024 << (extid & 0x03);
4506 extid >>= 2;
4507 /* Calc oobsize */
4508 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4509 extid >>= 2;
4510 /* Calc blocksize. Blocksize is multiples of 64KiB */
4511 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4512 extid >>= 2;
4513 /* Get buswidth information */
4514 if (extid & 0x1)
4515 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004516}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004517EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004518
4519/*
Brian Norrisf23a4812012-09-24 20:40:51 -07004520 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4521 * decodes a matching ID table entry and assigns the MTD size parameters for
4522 * the chip.
4523 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004524static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07004525{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004526 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07004527
4528 mtd->erasesize = type->erasesize;
4529 mtd->writesize = type->pagesize;
4530 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07004531
Huang Shijie1c195e92013-09-25 14:58:12 +08004532 /* All legacy ID NAND are small-page, SLC */
4533 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07004534}
4535
4536/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004537 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4538 * heuristic patterns using various detected parameters (e.g., manufacturer,
4539 * page size, cell-type information).
4540 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004541static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07004542{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004543 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004544
4545 /* Set the bad block position */
4546 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4547 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4548 else
4549 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004550}
4551
Huang Shijieec6e87e2013-03-15 11:01:00 +08004552static inline bool is_full_id_nand(struct nand_flash_dev *type)
4553{
4554 return type->id_len;
4555}
4556
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004557static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02004558 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004559{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004560 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02004561 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004562
Huang Shijieec6e87e2013-03-15 11:01:00 +08004563 if (!strncmp(type->id, id_data, type->id_len)) {
4564 mtd->writesize = type->pagesize;
4565 mtd->erasesize = type->erasesize;
4566 mtd->oobsize = type->oobsize;
4567
Huang Shijie7db906b2013-09-25 14:58:11 +08004568 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004569 chip->chipsize = (uint64_t)type->chipsize << 20;
4570 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004571 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4572 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004573 chip->onfi_timing_mode_default =
4574 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004575
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004576 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4577 if (!chip->parameters.model)
4578 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004579
Huang Shijieec6e87e2013-03-15 11:01:00 +08004580 return true;
4581 }
4582 return false;
4583}
4584
Brian Norris7e74c2d2012-09-24 20:40:49 -07004585/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004586 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4587 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4588 * table.
4589 */
4590static void nand_manufacturer_detect(struct nand_chip *chip)
4591{
4592 /*
4593 * Try manufacturer detection if available and use
4594 * nand_decode_ext_id() otherwise.
4595 */
4596 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004597 chip->manufacturer.desc->ops->detect) {
4598 /* The 3rd id byte holds MLC / multichip data */
4599 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004600 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004601 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004602 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004603 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004604}
4605
4606/*
4607 * Manufacturer initialization. This function is called for all NANDs including
4608 * ONFI and JEDEC compliant ones.
4609 * Manufacturer drivers should put all their specific initialization code in
4610 * their ->init() hook.
4611 */
4612static int nand_manufacturer_init(struct nand_chip *chip)
4613{
4614 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4615 !chip->manufacturer.desc->ops->init)
4616 return 0;
4617
4618 return chip->manufacturer.desc->ops->init(chip);
4619}
4620
4621/*
4622 * Manufacturer cleanup. This function is called for all NANDs including
4623 * ONFI and JEDEC compliant ones.
4624 * Manufacturer drivers should put all their specific cleanup code in their
4625 * ->cleanup() hook.
4626 */
4627static void nand_manufacturer_cleanup(struct nand_chip *chip)
4628{
4629 /* Release manufacturer private data */
4630 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4631 chip->manufacturer.desc->ops->cleanup)
4632 chip->manufacturer.desc->ops->cleanup(chip);
4633}
4634
Boris Brezillon348d56a2018-09-07 00:38:48 +02004635static const char *
4636nand_manufacturer_name(const struct nand_manufacturer *manufacturer)
4637{
4638 return manufacturer ? manufacturer->name : "Unknown";
4639}
4640
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004641/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004642 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004643 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004644static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004645{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004646 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004647 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004648 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004649 u8 *id_data = chip->id.data;
4650 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Karl Beldanef89a882008-09-15 14:37:29 +02004652 /*
4653 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004654 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004655 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004656 ret = nand_reset(chip, 0);
4657 if (ret)
4658 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004659
4660 /* Select the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004661 chip->select_chip(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004664 ret = nand_readid_op(chip, 0, id_data, 2);
4665 if (ret)
4666 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
4668 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004669 maf_id = id_data[0];
4670 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Brian Norris8b6e50c2011-05-25 14:59:01 -07004672 /*
4673 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004674 * interface concerns can cause random data which looks like a
4675 * possibly credible NAND flash to appear. If the two results do
4676 * not match, ignore the device completely.
4677 */
4678
Brian Norris4aef9b72012-09-24 20:40:48 -07004679 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004680 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
4681 if (ret)
4682 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01004683
Boris Brezillon7f501f02016-05-24 19:20:05 +02004684 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004685 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004686 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004687 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004688 }
4689
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02004690 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02004691
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004692 /* Try to identify manufacturer */
4693 manufacturer = nand_get_manufacturer(maf_id);
4694 chip->manufacturer.desc = manufacturer;
4695
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004696 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004697 type = nand_flash_ids;
4698
Boris Brezillon29a198a2016-05-24 20:17:48 +02004699 /*
4700 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4701 * override it.
4702 * This is required to make sure initial NAND bus width set by the
4703 * NAND controller driver is coherent with the real NAND bus width
4704 * (extracted by auto-detection code).
4705 */
4706 busw = chip->options & NAND_BUSWIDTH_16;
4707
4708 /*
4709 * The flag is only set (never cleared), reset it to its default value
4710 * before starting auto-detection.
4711 */
4712 chip->options &= ~NAND_BUSWIDTH_16;
4713
Huang Shijieec6e87e2013-03-15 11:01:00 +08004714 for (; type->name != NULL; type++) {
4715 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004716 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004717 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004718 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004719 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004720 }
4721 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004722
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004723 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004724 /* Check if the chip is ONFI compliant */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004725 ret = nand_onfi_detect(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01004726 if (ret < 0)
4727 return ret;
4728 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004729 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004730
4731 /* Check if the chip is JEDEC compliant */
Boris Brezillon8ae3fbf2018-09-07 00:38:51 +02004732 ret = nand_jedec_detect(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01004733 if (ret < 0)
4734 return ret;
4735 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08004736 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004737 }
4738
David Woodhouse5e81e882010-02-26 18:32:56 +00004739 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004740 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004741
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004742 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4743 if (!chip->parameters.model)
4744 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004745
Adrian Hunter69423d92008-12-10 13:37:21 +00004746 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004747
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004748 if (!type->pagesize)
4749 nand_manufacturer_detect(chip);
4750 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004751 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004752
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004753 /* Get chip options */
4754 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004755
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004756ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01004757 if (!mtd->name)
4758 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004759
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004760 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004761 WARN_ON(busw & NAND_BUSWIDTH_16);
4762 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004763 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4764 /*
4765 * Check, if buswidth is correct. Hardware drivers should set
4766 * chip correct!
4767 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004768 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004769 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004770 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4771 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004772 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4773 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004774 ret = -EINVAL;
4775
4776 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004777 }
4778
Boris Brezillon7f501f02016-05-24 19:20:05 +02004779 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004780
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004781 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004782 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004783 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004784 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004785
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004786 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004787 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004788 if (chip->chipsize & 0xffffffff)
4789 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004790 else {
4791 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4792 chip->chip_shift += 32 - 1;
4793 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004794
Masahiro Yamada14157f82017-09-13 11:05:50 +09004795 if (chip->chip_shift - chip->page_shift > 16)
4796 chip->options |= NAND_ROW_ADDR_3;
4797
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004798 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004799
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004800 nand_legacy_adjust_cmdfunc(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004801
Ezequiel Garcia20171642013-11-25 08:30:31 -03004802 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004803 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01004804 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4805 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02004806 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004807 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004808 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004809 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004810
4811free_detect_allocation:
4812 kfree(chip->parameters.model);
4813
4814 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004815}
4816
Boris Brezillond48f62b2016-04-01 14:54:32 +02004817static const char * const nand_ecc_modes[] = {
4818 [NAND_ECC_NONE] = "none",
4819 [NAND_ECC_SOFT] = "soft",
4820 [NAND_ECC_HW] = "hw",
4821 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4822 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004823 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004824};
4825
4826static int of_get_nand_ecc_mode(struct device_node *np)
4827{
4828 const char *pm;
4829 int err, i;
4830
4831 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4832 if (err < 0)
4833 return err;
4834
4835 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4836 if (!strcasecmp(pm, nand_ecc_modes[i]))
4837 return i;
4838
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004839 /*
4840 * For backward compatibility we support few obsoleted values that don't
4841 * have their mappings into nand_ecc_modes_t anymore (they were merged
4842 * with other enums).
4843 */
4844 if (!strcasecmp(pm, "soft_bch"))
4845 return NAND_ECC_SOFT;
4846
Boris Brezillond48f62b2016-04-01 14:54:32 +02004847 return -ENODEV;
4848}
4849
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004850static const char * const nand_ecc_algos[] = {
4851 [NAND_ECC_HAMMING] = "hamming",
4852 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02004853 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004854};
4855
Boris Brezillond48f62b2016-04-01 14:54:32 +02004856static int of_get_nand_ecc_algo(struct device_node *np)
4857{
4858 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004859 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004860
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004861 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4862 if (!err) {
4863 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4864 if (!strcasecmp(pm, nand_ecc_algos[i]))
4865 return i;
4866 return -ENODEV;
4867 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004868
4869 /*
4870 * For backward compatibility we also read "nand-ecc-mode" checking
4871 * for some obsoleted values that were specifying ECC algorithm.
4872 */
4873 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4874 if (err < 0)
4875 return err;
4876
4877 if (!strcasecmp(pm, "soft"))
4878 return NAND_ECC_HAMMING;
4879 else if (!strcasecmp(pm, "soft_bch"))
4880 return NAND_ECC_BCH;
4881
4882 return -ENODEV;
4883}
4884
4885static int of_get_nand_ecc_step_size(struct device_node *np)
4886{
4887 int ret;
4888 u32 val;
4889
4890 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4891 return ret ? ret : val;
4892}
4893
4894static int of_get_nand_ecc_strength(struct device_node *np)
4895{
4896 int ret;
4897 u32 val;
4898
4899 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4900 return ret ? ret : val;
4901}
4902
4903static int of_get_nand_bus_width(struct device_node *np)
4904{
4905 u32 val;
4906
4907 if (of_property_read_u32(np, "nand-bus-width", &val))
4908 return 8;
4909
4910 switch (val) {
4911 case 8:
4912 case 16:
4913 return val;
4914 default:
4915 return -EIO;
4916 }
4917}
4918
4919static bool of_get_nand_on_flash_bbt(struct device_node *np)
4920{
4921 return of_property_read_bool(np, "nand-on-flash-bbt");
4922}
4923
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004924static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004925{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004926 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004927 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004928
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004929 if (!dn)
4930 return 0;
4931
Brian Norris5844fee2015-01-23 00:22:27 -08004932 if (of_get_nand_bus_width(dn) == 16)
4933 chip->options |= NAND_BUSWIDTH_16;
4934
Stefan Agnerf922bd72018-06-24 23:27:23 +02004935 if (of_property_read_bool(dn, "nand-is-boot-medium"))
4936 chip->options |= NAND_IS_BOOT_MEDIUM;
4937
Brian Norris5844fee2015-01-23 00:22:27 -08004938 if (of_get_nand_on_flash_bbt(dn))
4939 chip->bbt_options |= NAND_BBT_USE_FLASH;
4940
4941 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004942 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004943 ecc_strength = of_get_nand_ecc_strength(dn);
4944 ecc_step = of_get_nand_ecc_step_size(dn);
4945
Brian Norris5844fee2015-01-23 00:22:27 -08004946 if (ecc_mode >= 0)
4947 chip->ecc.mode = ecc_mode;
4948
Rafał Miłecki79082452016-03-23 11:19:02 +01004949 if (ecc_algo >= 0)
4950 chip->ecc.algo = ecc_algo;
4951
Brian Norris5844fee2015-01-23 00:22:27 -08004952 if (ecc_strength >= 0)
4953 chip->ecc.strength = ecc_strength;
4954
4955 if (ecc_step > 0)
4956 chip->ecc.size = ecc_step;
4957
Boris Brezillonba78ee02016-06-08 17:04:22 +02004958 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4959 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4960
Brian Norris5844fee2015-01-23 00:22:27 -08004961 return 0;
4962}
4963
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004964/**
Miquel Raynal98732da2018-07-25 15:31:50 +02004965 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02004966 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004967 * @maxchips: number of chips to scan for
4968 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004969 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004970 * This is the first phase of the normal nand_scan() function. It reads the
4971 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004972 *
Miquel Raynal98732da2018-07-25 15:31:50 +02004973 * This helper used to be called directly from controller drivers that needed
4974 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
4975 * prevented dynamic allocations during this phase which was unconvenient and
4976 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004977 */
Boris Brezillon871a4072018-08-04 22:59:22 +02004978static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02004979 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004980{
Boris Brezillon00ad3782018-09-06 14:05:14 +02004981 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02004982 int nand_maf_id, nand_dev_id;
4983 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08004984 int ret;
4985
Miquel Raynal17fa8042017-11-30 18:01:31 +01004986 /* Enforce the right timings for reset/detection */
4987 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
4988
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004989 ret = nand_dt_init(chip);
4990 if (ret)
4991 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004992
Brian Norrisf7a8e382016-01-05 10:39:45 -08004993 if (!mtd->name && mtd->dev.parent)
4994 mtd->name = dev_name(mtd->dev.parent);
4995
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004996 if (chip->exec_op && !chip->select_chip) {
4997 pr_err("->select_chip() is mandatory when implementing ->exec_op()\n");
4998 return -EINVAL;
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004999 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005000
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02005001 ret = nand_legacy_check_hooks(chip);
5002 if (ret)
5003 return ret;
5004
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005005 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005006 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005007
5008 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005009 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005010 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005011 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005012 pr_warn("No NAND device found\n");
Boris Brezillon758b56f2018-09-06 14:05:24 +02005013 chip->select_chip(chip, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005014 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 }
5016
Boris Brezillon7f501f02016-05-24 19:20:05 +02005017 nand_maf_id = chip->id.data[0];
5018 nand_dev_id = chip->id.data[1];
5019
Boris Brezillon758b56f2018-09-06 14:05:24 +02005020 chip->select_chip(chip, -1);
Huang Shijie07300162012-11-09 16:23:45 +08005021
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005022 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005023 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005024 u8 id[2];
5025
Karl Beldanef89a882008-09-15 14:37:29 +02005026 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005027 nand_reset(chip, i);
5028
Boris Brezillon758b56f2018-09-06 14:05:24 +02005029 chip->select_chip(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005031 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005033 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02005034 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 break;
Huang Shijie07300162012-11-09 16:23:45 +08005036 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02005037 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 }
5039 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005040 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005041
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005043 chip->numchips = i;
5044 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
David Woodhouse3b85c322006-09-25 17:06:53 +01005046 return 0;
5047}
5048
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005049static void nand_scan_ident_cleanup(struct nand_chip *chip)
5050{
5051 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005052 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005053}
5054
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005055static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
5056{
5057 struct nand_chip *chip = mtd_to_nand(mtd);
5058 struct nand_ecc_ctrl *ecc = &chip->ecc;
5059
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005060 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005061 return -EINVAL;
5062
5063 switch (ecc->algo) {
5064 case NAND_ECC_HAMMING:
5065 ecc->calculate = nand_calculate_ecc;
5066 ecc->correct = nand_correct_data;
5067 ecc->read_page = nand_read_page_swecc;
5068 ecc->read_subpage = nand_read_subpage;
5069 ecc->write_page = nand_write_page_swecc;
5070 ecc->read_page_raw = nand_read_page_raw;
5071 ecc->write_page_raw = nand_write_page_raw;
5072 ecc->read_oob = nand_read_oob_std;
5073 ecc->write_oob = nand_write_oob_std;
5074 if (!ecc->size)
5075 ecc->size = 256;
5076 ecc->bytes = 3;
5077 ecc->strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +02005078
5079 if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC))
5080 ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
5081
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005082 return 0;
5083 case NAND_ECC_BCH:
5084 if (!mtd_nand_has_bch()) {
5085 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5086 return -EINVAL;
5087 }
5088 ecc->calculate = nand_bch_calculate_ecc;
5089 ecc->correct = nand_bch_correct_data;
5090 ecc->read_page = nand_read_page_swecc;
5091 ecc->read_subpage = nand_read_subpage;
5092 ecc->write_page = nand_write_page_swecc;
5093 ecc->read_page_raw = nand_read_page_raw;
5094 ecc->write_page_raw = nand_write_page_raw;
5095 ecc->read_oob = nand_read_oob_std;
5096 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005097
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005098 /*
5099 * Board driver should supply ecc.size and ecc.strength
5100 * values to select how many bits are correctable.
5101 * Otherwise, default to 4 bits for large page devices.
5102 */
5103 if (!ecc->size && (mtd->oobsize >= 64)) {
5104 ecc->size = 512;
5105 ecc->strength = 4;
5106 }
5107
5108 /*
5109 * if no ecc placement scheme was provided pickup the default
5110 * large page one.
5111 */
5112 if (!mtd->ooblayout) {
5113 /* handle large page devices only */
5114 if (mtd->oobsize < 64) {
5115 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5116 return -EINVAL;
5117 }
5118
5119 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005120
5121 }
5122
5123 /*
5124 * We can only maximize ECC config when the default layout is
5125 * used, otherwise we don't know how many bytes can really be
5126 * used.
5127 */
5128 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5129 ecc->options & NAND_ECC_MAXIMIZE) {
5130 int steps, bytes;
5131
5132 /* Always prefer 1k blocks over 512bytes ones */
5133 ecc->size = 1024;
5134 steps = mtd->writesize / ecc->size;
5135
5136 /* Reserve 2 bytes for the BBM */
5137 bytes = (mtd->oobsize - 2) / steps;
5138 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005139 }
5140
5141 /* See nand_bch_init() for details. */
5142 ecc->bytes = 0;
5143 ecc->priv = nand_bch_init(mtd);
5144 if (!ecc->priv) {
5145 WARN(1, "BCH ECC initialization failed!\n");
5146 return -EINVAL;
5147 }
5148 return 0;
5149 default:
5150 WARN(1, "Unsupported ECC algorithm!\n");
5151 return -EINVAL;
5152 }
5153}
5154
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005155/**
5156 * nand_check_ecc_caps - check the sanity of preset ECC settings
5157 * @chip: nand chip info structure
5158 * @caps: ECC caps info structure
5159 * @oobavail: OOB size that the ECC engine can use
5160 *
5161 * When ECC step size and strength are already set, check if they are supported
5162 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5163 * On success, the calculated ECC bytes is set.
5164 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305165static int
5166nand_check_ecc_caps(struct nand_chip *chip,
5167 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005168{
5169 struct mtd_info *mtd = nand_to_mtd(chip);
5170 const struct nand_ecc_step_info *stepinfo;
5171 int preset_step = chip->ecc.size;
5172 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305173 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005174 int i, j;
5175
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005176 for (i = 0; i < caps->nstepinfos; i++) {
5177 stepinfo = &caps->stepinfos[i];
5178
5179 if (stepinfo->stepsize != preset_step)
5180 continue;
5181
5182 for (j = 0; j < stepinfo->nstrengths; j++) {
5183 if (stepinfo->strengths[j] != preset_strength)
5184 continue;
5185
5186 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5187 preset_strength);
5188 if (WARN_ON_ONCE(ecc_bytes < 0))
5189 return ecc_bytes;
5190
5191 if (ecc_bytes * nsteps > oobavail) {
5192 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5193 preset_step, preset_strength);
5194 return -ENOSPC;
5195 }
5196
5197 chip->ecc.bytes = ecc_bytes;
5198
5199 return 0;
5200 }
5201 }
5202
5203 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5204 preset_step, preset_strength);
5205
5206 return -ENOTSUPP;
5207}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005208
5209/**
5210 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5211 * @chip: nand chip info structure
5212 * @caps: ECC engine caps info structure
5213 * @oobavail: OOB size that the ECC engine can use
5214 *
5215 * If a chip's ECC requirement is provided, try to meet it with the least
5216 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5217 * On success, the chosen ECC settings are set.
5218 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305219static int
5220nand_match_ecc_req(struct nand_chip *chip,
5221 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005222{
5223 struct mtd_info *mtd = nand_to_mtd(chip);
5224 const struct nand_ecc_step_info *stepinfo;
5225 int req_step = chip->ecc_step_ds;
5226 int req_strength = chip->ecc_strength_ds;
5227 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5228 int best_step, best_strength, best_ecc_bytes;
5229 int best_ecc_bytes_total = INT_MAX;
5230 int i, j;
5231
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005232 /* No information provided by the NAND chip */
5233 if (!req_step || !req_strength)
5234 return -ENOTSUPP;
5235
5236 /* number of correctable bits the chip requires in a page */
5237 req_corr = mtd->writesize / req_step * req_strength;
5238
5239 for (i = 0; i < caps->nstepinfos; i++) {
5240 stepinfo = &caps->stepinfos[i];
5241 step_size = stepinfo->stepsize;
5242
5243 for (j = 0; j < stepinfo->nstrengths; j++) {
5244 strength = stepinfo->strengths[j];
5245
5246 /*
5247 * If both step size and strength are smaller than the
5248 * chip's requirement, it is not easy to compare the
5249 * resulted reliability.
5250 */
5251 if (step_size < req_step && strength < req_strength)
5252 continue;
5253
5254 if (mtd->writesize % step_size)
5255 continue;
5256
5257 nsteps = mtd->writesize / step_size;
5258
5259 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5260 if (WARN_ON_ONCE(ecc_bytes < 0))
5261 continue;
5262 ecc_bytes_total = ecc_bytes * nsteps;
5263
5264 if (ecc_bytes_total > oobavail ||
5265 strength * nsteps < req_corr)
5266 continue;
5267
5268 /*
5269 * We assume the best is to meet the chip's requrement
5270 * with the least number of ECC bytes.
5271 */
5272 if (ecc_bytes_total < best_ecc_bytes_total) {
5273 best_ecc_bytes_total = ecc_bytes_total;
5274 best_step = step_size;
5275 best_strength = strength;
5276 best_ecc_bytes = ecc_bytes;
5277 }
5278 }
5279 }
5280
5281 if (best_ecc_bytes_total == INT_MAX)
5282 return -ENOTSUPP;
5283
5284 chip->ecc.size = best_step;
5285 chip->ecc.strength = best_strength;
5286 chip->ecc.bytes = best_ecc_bytes;
5287
5288 return 0;
5289}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005290
5291/**
5292 * nand_maximize_ecc - choose the max ECC strength available
5293 * @chip: nand chip info structure
5294 * @caps: ECC engine caps info structure
5295 * @oobavail: OOB size that the ECC engine can use
5296 *
5297 * Choose the max ECC strength that is supported on the controller, and can fit
5298 * within the chip's OOB. On success, the chosen ECC settings are set.
5299 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305300static int
5301nand_maximize_ecc(struct nand_chip *chip,
5302 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005303{
5304 struct mtd_info *mtd = nand_to_mtd(chip);
5305 const struct nand_ecc_step_info *stepinfo;
5306 int step_size, strength, nsteps, ecc_bytes, corr;
5307 int best_corr = 0;
5308 int best_step = 0;
5309 int best_strength, best_ecc_bytes;
5310 int i, j;
5311
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005312 for (i = 0; i < caps->nstepinfos; i++) {
5313 stepinfo = &caps->stepinfos[i];
5314 step_size = stepinfo->stepsize;
5315
5316 /* If chip->ecc.size is already set, respect it */
5317 if (chip->ecc.size && step_size != chip->ecc.size)
5318 continue;
5319
5320 for (j = 0; j < stepinfo->nstrengths; j++) {
5321 strength = stepinfo->strengths[j];
5322
5323 if (mtd->writesize % step_size)
5324 continue;
5325
5326 nsteps = mtd->writesize / step_size;
5327
5328 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5329 if (WARN_ON_ONCE(ecc_bytes < 0))
5330 continue;
5331
5332 if (ecc_bytes * nsteps > oobavail)
5333 continue;
5334
5335 corr = strength * nsteps;
5336
5337 /*
5338 * If the number of correctable bits is the same,
5339 * bigger step_size has more reliability.
5340 */
5341 if (corr > best_corr ||
5342 (corr == best_corr && step_size > best_step)) {
5343 best_corr = corr;
5344 best_step = step_size;
5345 best_strength = strength;
5346 best_ecc_bytes = ecc_bytes;
5347 }
5348 }
5349 }
5350
5351 if (!best_corr)
5352 return -ENOTSUPP;
5353
5354 chip->ecc.size = best_step;
5355 chip->ecc.strength = best_strength;
5356 chip->ecc.bytes = best_ecc_bytes;
5357
5358 return 0;
5359}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005360
Abhishek Sahu181ace92018-06-20 12:57:28 +05305361/**
5362 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
5363 * @chip: nand chip info structure
5364 * @caps: ECC engine caps info structure
5365 * @oobavail: OOB size that the ECC engine can use
5366 *
5367 * Choose the ECC configuration according to following logic
5368 *
5369 * 1. If both ECC step size and ECC strength are already set (usually by DT)
5370 * then check if it is supported by this controller.
5371 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
5372 * 3. Otherwise, try to match the ECC step size and ECC strength closest
5373 * to the chip's requirement. If available OOB size can't fit the chip
5374 * requirement then fallback to the maximum ECC step size and ECC strength.
5375 *
5376 * On success, the chosen ECC settings are set.
5377 */
5378int nand_ecc_choose_conf(struct nand_chip *chip,
5379 const struct nand_ecc_caps *caps, int oobavail)
5380{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305381 struct mtd_info *mtd = nand_to_mtd(chip);
5382
5383 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
5384 return -EINVAL;
5385
Abhishek Sahu181ace92018-06-20 12:57:28 +05305386 if (chip->ecc.size && chip->ecc.strength)
5387 return nand_check_ecc_caps(chip, caps, oobavail);
5388
5389 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
5390 return nand_maximize_ecc(chip, caps, oobavail);
5391
5392 if (!nand_match_ecc_req(chip, caps, oobavail))
5393 return 0;
5394
5395 return nand_maximize_ecc(chip, caps, oobavail);
5396}
5397EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
5398
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005399/*
5400 * Check if the chip configuration meet the datasheet requirements.
5401
5402 * If our configuration corrects A bits per B bytes and the minimum
5403 * required correction level is X bits per Y bytes, then we must ensure
5404 * both of the following are true:
5405 *
5406 * (1) A / B >= X / Y
5407 * (2) A >= X
5408 *
5409 * Requirement (1) ensures we can correct for the required bitflip density.
5410 * Requirement (2) ensures we can correct even when all bitflips are clumped
5411 * in the same sector.
5412 */
5413static bool nand_ecc_strength_good(struct mtd_info *mtd)
5414{
Boris BREZILLON862eba52015-12-01 12:03:03 +01005415 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005416 struct nand_ecc_ctrl *ecc = &chip->ecc;
5417 int corr, ds_corr;
5418
5419 if (ecc->size == 0 || chip->ecc_step_ds == 0)
5420 /* Not enough information */
5421 return true;
5422
5423 /*
5424 * We get the number of corrected bits per page to compare
5425 * the correction density.
5426 */
5427 corr = (mtd->writesize * ecc->strength) / ecc->size;
5428 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
5429
5430 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
5431}
David Woodhouse3b85c322006-09-25 17:06:53 +01005432
5433/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005434 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005435 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01005436 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005437 * This is the second phase of the normal nand_scan() function. It fills out
5438 * all the uninitialized function pointers with the defaults and scans for a
5439 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01005440 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005441static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01005442{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005443 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08005444 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005445 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01005446
Brian Norrise2414f42012-02-06 13:44:00 -08005447 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005448 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07005449 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005450 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07005451 }
Brian Norrise2414f42012-02-06 13:44:00 -08005452
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005453 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005454 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02005455 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005456
Boris Brezillonf84674b2017-06-02 12:18:24 +02005457 /*
5458 * FIXME: some NAND manufacturer drivers expect the first die to be
5459 * selected when manufacturer->init() is called. They should be fixed
5460 * to explictly select the relevant die when interacting with the NAND
5461 * chip.
5462 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02005463 chip->select_chip(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005464 ret = nand_manufacturer_init(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02005465 chip->select_chip(chip, -1);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005466 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005467 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005468
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01005469 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005470 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005471
5472 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005473 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005474 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005475 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005476 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005477 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01005480 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 break;
5482 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005483 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02005484 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02005487 /*
5488 * Expose the whole OOB area to users if ECC_NONE
5489 * is passed. We could do that for all kind of
5490 * ->oobsize, but we must keep the old large/small
5491 * page with ECC layout when ->oobsize <= 128 for
5492 * compatibility reasons.
5493 */
5494 if (ecc->mode == NAND_ECC_NONE) {
5495 mtd_set_ooblayout(mtd,
5496 &nand_ooblayout_lp_ops);
5497 break;
5498 }
5499
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005500 WARN(1, "No oob scheme defined for oobsize %d\n",
5501 mtd->oobsize);
5502 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005503 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 }
5505 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005506
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005507 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005508 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005509 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01005510 */
David Woodhouse956e9442006-09-25 17:12:39 +01005511
Huang Shijie97de79e02013-10-18 14:20:53 +08005512 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005513 case NAND_ECC_HW_OOB_FIRST:
5514 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08005515 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005516 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5517 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005518 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005519 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005520 if (!ecc->read_page)
5521 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005522
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005523 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07005524 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005525 if (!ecc->read_page)
5526 ecc->read_page = nand_read_page_hwecc;
5527 if (!ecc->write_page)
5528 ecc->write_page = nand_write_page_hwecc;
5529 if (!ecc->read_page_raw)
5530 ecc->read_page_raw = nand_read_page_raw;
5531 if (!ecc->write_page_raw)
5532 ecc->write_page_raw = nand_write_page_raw;
5533 if (!ecc->read_oob)
5534 ecc->read_oob = nand_read_oob_std;
5535 if (!ecc->write_oob)
5536 ecc->write_oob = nand_write_oob_std;
5537 if (!ecc->read_subpage)
5538 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02005539 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08005540 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005541
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005542 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08005543 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5544 (!ecc->read_page ||
5545 ecc->read_page == nand_read_page_hwecc ||
5546 !ecc->write_page ||
5547 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005548 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5549 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005550 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005551 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07005552 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005553 if (!ecc->read_page)
5554 ecc->read_page = nand_read_page_syndrome;
5555 if (!ecc->write_page)
5556 ecc->write_page = nand_write_page_syndrome;
5557 if (!ecc->read_page_raw)
5558 ecc->read_page_raw = nand_read_page_raw_syndrome;
5559 if (!ecc->write_page_raw)
5560 ecc->write_page_raw = nand_write_page_raw_syndrome;
5561 if (!ecc->read_oob)
5562 ecc->read_oob = nand_read_oob_syndrome;
5563 if (!ecc->write_oob)
5564 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005565
Huang Shijie97de79e02013-10-18 14:20:53 +08005566 if (mtd->writesize >= ecc->size) {
5567 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005568 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
5569 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005570 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07005571 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005572 break;
Mike Dunne2788c92012-04-25 12:06:10 -07005573 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005574 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5575 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08005576 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02005577 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005579 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005580 ret = nand_set_ecc_soft_ops(mtd);
5581 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005582 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005583 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01005584 }
5585 break;
5586
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005587 case NAND_ECC_ON_DIE:
5588 if (!ecc->read_page || !ecc->write_page) {
5589 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
5590 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005591 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005592 }
5593 if (!ecc->read_oob)
5594 ecc->read_oob = nand_read_oob_std;
5595 if (!ecc->write_oob)
5596 ecc->write_oob = nand_write_oob_std;
5597 break;
5598
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005599 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005600 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08005601 ecc->read_page = nand_read_page_raw;
5602 ecc->write_page = nand_write_page_raw;
5603 ecc->read_oob = nand_read_oob_std;
5604 ecc->read_page_raw = nand_read_page_raw;
5605 ecc->write_page_raw = nand_write_page_raw;
5606 ecc->write_oob = nand_write_oob_std;
5607 ecc->size = mtd->writesize;
5608 ecc->bytes = 0;
5609 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005611
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005613 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5614 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005615 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005618 if (ecc->correct || ecc->calculate) {
5619 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5620 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5621 if (!ecc->calc_buf || !ecc->code_buf) {
5622 ret = -ENOMEM;
5623 goto err_nand_manuf_cleanup;
5624 }
5625 }
5626
Brian Norris9ce244b2011-08-30 18:45:37 -07005627 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005628 if (!ecc->read_oob_raw)
5629 ecc->read_oob_raw = ecc->read_oob;
5630 if (!ecc->write_oob_raw)
5631 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005632
Boris Brezillon846031d2016-02-03 20:11:00 +01005633 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005634 mtd->ecc_strength = ecc->strength;
5635 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005636
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005637 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005638 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005639 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005640 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005641 ecc->steps = mtd->writesize / ecc->size;
5642 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005643 WARN(1, "Invalid ECC parameters\n");
5644 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005645 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005647 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005648 if (ecc->total > mtd->oobsize) {
5649 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5650 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005651 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005652 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005653
Boris Brezillon846031d2016-02-03 20:11:00 +01005654 /*
5655 * The number of bytes available for a client to place data into
5656 * the out of band area.
5657 */
5658 ret = mtd_ooblayout_count_freebytes(mtd);
5659 if (ret < 0)
5660 ret = 0;
5661
5662 mtd->oobavail = ret;
5663
5664 /* ECC sanity check: warn if it's too weak */
5665 if (!nand_ecc_strength_good(mtd))
5666 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5667 mtd->name);
5668
Brian Norris8b6e50c2011-05-25 14:59:01 -07005669 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005670 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005671 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005672 case 2:
5673 mtd->subpage_sft = 1;
5674 break;
5675 case 4:
5676 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005677 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005678 mtd->subpage_sft = 2;
5679 break;
5680 }
5681 }
5682 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5683
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02005684 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005685 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005688 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005690 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305691 switch (ecc->mode) {
5692 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305693 if (chip->page_shift > 9)
5694 chip->options |= NAND_SUBPAGE_READ;
5695 break;
5696
5697 default:
5698 break;
5699 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005700
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08005702 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02005703 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5704 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005705 mtd->_erase = nand_erase;
5706 mtd->_point = NULL;
5707 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005708 mtd->_panic_write = panic_nand_write;
5709 mtd->_read_oob = nand_read_oob;
5710 mtd->_write_oob = nand_write_oob;
5711 mtd->_sync = nand_sync;
5712 mtd->_lock = NULL;
5713 mtd->_unlock = NULL;
5714 mtd->_suspend = nand_suspend;
5715 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005716 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005717 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005718 mtd->_block_isbad = nand_block_isbad;
5719 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06005720 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01005721 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005723 /*
5724 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5725 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5726 * properly set.
5727 */
5728 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005729 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730
Boris Brezillonf84674b2017-06-02 12:18:24 +02005731 /* Initialize the ->data_interface field. */
5732 ret = nand_init_data_interface(chip);
5733 if (ret)
5734 goto err_nand_manuf_cleanup;
5735
5736 /* Enter fastest possible mode on all dies. */
5737 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005738 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005739 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005740 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005741 }
5742
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005743 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005744 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
5747 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02005748 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07005749 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005750 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005751
Brian Norris44d41822017-05-01 17:04:50 -07005752 return 0;
5753
Boris Brezillonf84674b2017-06-02 12:18:24 +02005754
5755err_nand_manuf_cleanup:
5756 nand_manufacturer_cleanup(chip);
5757
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005758err_free_buf:
5759 kfree(chip->data_buf);
5760 kfree(ecc->code_buf);
5761 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07005762
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005763 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764}
5765
Miquel Raynal05b54c72018-07-19 01:05:46 +02005766static int nand_attach(struct nand_chip *chip)
5767{
5768 if (chip->controller->ops && chip->controller->ops->attach_chip)
5769 return chip->controller->ops->attach_chip(chip);
5770
5771 return 0;
5772}
5773
5774static void nand_detach(struct nand_chip *chip)
5775{
5776 if (chip->controller->ops && chip->controller->ops->detach_chip)
5777 chip->controller->ops->detach_chip(chip);
5778}
5779
David Woodhouse3b85c322006-09-25 17:06:53 +01005780/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005781 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005782 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02005783 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005784 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01005785 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005786 * This fills out all the uninitialized function pointers with the defaults.
5787 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005788 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005789 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005790int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005791 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01005792{
5793 int ret;
5794
Boris Brezillon800342d2018-08-04 22:59:23 +02005795 if (!maxchips)
5796 return -EINVAL;
5797
5798 ret = nand_scan_ident(chip, maxchips, ids);
5799 if (ret)
5800 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005801
5802 ret = nand_attach(chip);
5803 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005804 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005805
Boris Brezillon00ad3782018-09-06 14:05:14 +02005806 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005807 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005808 goto detach_chip;
5809
5810 return 0;
5811
5812detach_chip:
5813 nand_detach(chip);
5814cleanup_ident:
5815 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005816
David Woodhouse3b85c322006-09-25 17:06:53 +01005817 return ret;
5818}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005819EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01005820
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005822 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5823 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005824 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005825void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005827 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005828 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005829 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5830
Jesper Juhlfa671642005-11-07 01:01:27 -08005831 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005832 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005833 kfree(chip->data_buf);
5834 kfree(chip->ecc.code_buf);
5835 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07005836
5837 /* Free bad block descriptor memory */
5838 if (chip->badblock_pattern && chip->badblock_pattern->options
5839 & NAND_BBT_DYNAMICSTRUCT)
5840 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005841
5842 /* Free manufacturer priv data. */
5843 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005844
5845 /* Free controller specific allocations after chip identification */
5846 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005847
5848 /* Free identification phase allocations */
5849 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850}
Miquel Raynal05b54c72018-07-19 01:05:46 +02005851
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005852EXPORT_SYMBOL_GPL(nand_cleanup);
5853
5854/**
5855 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5856 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02005857 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005858 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02005859void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005860{
Boris Brezillon59ac2762018-09-06 14:05:15 +02005861 mtd_device_unregister(nand_to_mtd(chip));
5862 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005863}
David Woodhousee0c7d762006-05-13 18:07:53 +01005864EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005865
David Woodhousee0c7d762006-05-13 18:07:53 +01005866MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005867MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5868MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005869MODULE_DESCRIPTION("Generic NAND flash driver code");