blob: 2a50afbdeeeb9736992be6749bd60ede3a43d193 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010042#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/interrupt.h>
44#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020045#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020047#include <linux/of.h>
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +020048#include <linux/gpio/consumer.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010049
Boris Brezillon348d56a2018-09-07 00:38:48 +020050#include "internals.h"
51
Boris Brezillon08136212018-11-11 08:55:03 +010052static int nand_get_device(struct nand_chip *chip, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Boris Brezillon08136212018-11-11 08:55:03 +010054static int nand_do_write_oob(struct nand_chip *chip, loff_t to,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020055 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
Boris Brezillon08136212018-11-11 08:55:03 +0100217static int check_offs_len(struct nand_chip *chip, loff_t ofs, uint64_t len)
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530218{
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530219 int ret = 0;
220
221 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300222 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700223 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530224 ret = -EINVAL;
225 }
226
227 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300228 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700229 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530230 ret = -EINVAL;
231 }
232
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530233 return ret;
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/**
237 * nand_release_device - [GENERIC] release chip
Boris Brezillon08136212018-11-11 08:55:03 +0100238 * @chip: NAND chip object
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000239 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800240 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
Boris Brezillon08136212018-11-11 08:55:03 +0100242static void nand_release_device(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200244 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200245 spin_lock(&chip->controller->lock);
246 chip->controller->active = NULL;
247 chip->state = FL_READY;
248 wake_up(&chip->controller->wq);
249 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Boris Brezillonc17556f2018-09-06 14:05:25 +0200254 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700255 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000257 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200259static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200261 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900262 int page, page_end, res;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900263 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Brian Norris5fb15492011-05-31 16:31:21 -0700265 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700266 ofs += mtd->erasesize - mtd->writesize;
267
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100268 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900269 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100270
Masahiro Yamadac120e752017-03-23 05:07:01 +0900271 for (; page < page_end; page++) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200272 res = chip->ecc.read_oob(chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530273 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900274 return res;
275
276 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000277
Brian Norriscdbec052012-01-13 18:11:48 -0800278 if (likely(chip->badblockbits == 8))
279 res = bad != 0xFF;
280 else
281 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900282 if (res)
283 return res;
284 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200285
Masahiro Yamadac120e752017-03-23 05:07:01 +0900286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700290 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200291 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700292 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700294 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700295 * specific driver. It provides the details for writing a bad block marker to a
296 * block.
297 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200298static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700299{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200300 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700301 struct mtd_oob_ops ops;
302 uint8_t buf[2] = { 0, 0 };
303 int ret = 0, res, i = 0;
304
Brian Norris0ec56dc2015-02-28 02:02:30 -0800305 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700306 ops.oobbuf = buf;
307 ops.ooboffs = chip->badblockpos;
308 if (chip->options & NAND_BUSWIDTH_16) {
309 ops.ooboffs &= ~0x01;
310 ops.len = ops.ooblen = 2;
311 } else {
312 ops.len = ops.ooblen = 1;
313 }
314 ops.mode = MTD_OPS_PLACE_OOB;
315
316 /* Write to first/last page(s) if necessary */
317 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
318 ofs += mtd->erasesize - mtd->writesize;
319 do {
Boris Brezillon08136212018-11-11 08:55:03 +0100320 res = nand_do_write_oob(chip, ofs, &ops);
Brian Norris5a0edb22013-07-30 17:52:58 -0700321 if (!ret)
322 ret = res;
323
324 i++;
325 ofs += mtd->writesize;
326 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
327
328 return ret;
329}
330
331/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200332 * nand_markbad_bbm - mark a block by updating the BBM
333 * @chip: NAND chip object
334 * @ofs: offset of the block to mark bad
335 */
336int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
337{
338 if (chip->legacy.block_markbad)
339 return chip->legacy.block_markbad(chip, ofs);
340
341 return nand_default_block_markbad(chip, ofs);
342}
343
344static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
345{
346 if (chip->legacy.block_bad)
347 return chip->legacy.block_bad(chip, ofs);
348
349 return nand_block_bad(chip, ofs);
350}
351
352/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700353 * nand_block_markbad_lowlevel - mark a block bad
Boris Brezillon08136212018-11-11 08:55:03 +0100354 * @chip: NAND chip object
Brian Norris5a0edb22013-07-30 17:52:58 -0700355 * @ofs: offset from device start
356 *
357 * This function performs the generic NAND bad block marking steps (i.e., bad
358 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200359 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700360 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700361 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300362 *
Brian Norrise2414f42012-02-06 13:44:00 -0800363 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700364 * (2) write bad block marker to OOB area of affected block (unless flag
365 * NAND_BBT_NO_OOB_BBM is present)
366 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300367 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700368 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800369 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370*/
Boris Brezillon08136212018-11-11 08:55:03 +0100371static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Boris Brezillon08136212018-11-11 08:55:03 +0100373 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisb32843b2013-07-30 17:52:59 -0700374 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000375
Brian Norrisb32843b2013-07-30 17:52:59 -0700376 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800377 struct erase_info einfo;
378
379 /* Attempt erase before marking OOB */
380 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800381 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300382 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200383 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800384
Brian Norrisb32843b2013-07-30 17:52:59 -0700385 /* Write bad block marker to OOB */
Boris Brezillon08136212018-11-11 08:55:03 +0100386 nand_get_device(chip, FL_WRITING);
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200387 ret = nand_markbad_bbm(chip, ofs);
Boris Brezillon08136212018-11-11 08:55:03 +0100388 nand_release_device(chip);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200389 }
Brian Norrise2414f42012-02-06 13:44:00 -0800390
Brian Norrisb32843b2013-07-30 17:52:59 -0700391 /* Mark block bad in BBT */
392 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200393 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800394 if (!ret)
395 ret = res;
396 }
397
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200398 if (!ret)
399 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300400
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200401 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000404/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * nand_check_wp - [GENERIC] check if the chip is write protected
Boris Brezillon08136212018-11-11 08:55:03 +0100406 * @chip: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700408 * Check, if the device is write protected. The function expects, that the
409 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Boris Brezillon08136212018-11-11 08:55:03 +0100411static int nand_check_wp(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Boris Brezillon97d90da2017-11-30 18:01:29 +0100413 u8 status;
414 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200415
Brian Norris8b6e50c2011-05-25 14:59:01 -0700416 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200417 if (chip->options & NAND_BROKEN_XD)
418 return 0;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100421 ret = nand_status_op(chip, &status);
422 if (ret)
423 return ret;
424
425 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800429 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700430 * @mtd: MTD device structure
431 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300432 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800433 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300434 */
435static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
436{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100437 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300438
439 if (!chip->bbt)
440 return 0;
441 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200442 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300443}
444
445/**
446 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Boris Brezillon08136212018-11-11 08:55:03 +0100447 * @chip: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300448 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700449 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 *
451 * Check, if the block is bad. Either by reading the bad block table or
452 * calling of the scan function.
453 */
Boris Brezillon08136212018-11-11 08:55:03 +0100454static int nand_block_checkbad(struct nand_chip *chip, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200457 if (chip->bbt)
458 return nand_isbad_bbt(chip, ofs, allowbbt);
459
460 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200463/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100464 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
465 * @chip: NAND chip structure
466 * @timeout_ms: Timeout in ms
467 *
468 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
469 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
470 * returned.
471 *
472 * This helper is intended to be used when the controller does not have access
473 * to the NAND R/B pin.
474 *
475 * Be aware that calling this helper from an ->exec_op() implementation means
476 * ->exec_op() must be re-entrant.
477 *
478 * Return 0 if the NAND chip is ready, a negative error otherwise.
479 */
480int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
481{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200482 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100483 u8 status = 0;
484 int ret;
485
486 if (!chip->exec_op)
487 return -ENOTSUPP;
488
Boris Brezillon3057fce2018-05-04 21:24:31 +0200489 /* Wait tWB before polling the STATUS reg. */
490 timings = nand_get_sdr_timings(&chip->data_interface);
491 ndelay(PSEC_TO_NSEC(timings->tWB_max));
492
Miquel Raynal8878b122017-11-09 14:16:45 +0100493 ret = nand_status_op(chip, NULL);
494 if (ret)
495 return ret;
496
497 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
498 do {
499 ret = nand_read_data_op(chip, &status, sizeof(status), true);
500 if (ret)
501 break;
502
503 if (status & NAND_STATUS_READY)
504 break;
505
506 /*
507 * Typical lowest execution time for a tR on most NANDs is 10us,
508 * use this as polling delay before doing something smarter (ie.
509 * deriving a delay from the timeout value, timeout_ms/ratio).
510 */
511 udelay(10);
512 } while (time_before(jiffies, timeout_ms));
513
514 /*
515 * We have to exit READ_STATUS mode in order to read real data on the
516 * bus in case the WAITRDY instruction is preceding a DATA_IN
517 * instruction.
518 */
519 nand_exit_status_op(chip);
520
521 if (ret)
522 return ret;
523
524 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
525};
526EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
527
528/**
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +0200529 * nand_gpio_waitrdy - Poll R/B GPIO pin until ready
530 * @chip: NAND chip structure
531 * @gpiod: GPIO descriptor of R/B pin
532 * @timeout_ms: Timeout in ms
533 *
534 * Poll the R/B GPIO pin until it becomes ready. If that does not happen
535 * whitin the specified timeout, -ETIMEDOUT is returned.
536 *
537 * This helper is intended to be used when the controller has access to the
538 * NAND R/B pin over GPIO.
539 *
540 * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise.
541 */
542int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
543 unsigned long timeout_ms)
544{
545 /* Wait until R/B pin indicates chip is ready or timeout occurs */
546 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
547 do {
548 if (gpiod_get_value_cansleep(gpiod))
549 return 0;
550
551 cond_resched();
552 } while (time_before(jiffies, timeout_ms));
553
554 return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT;
555};
556EXPORT_SYMBOL_GPL(nand_gpio_waitrdy);
557
558/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200559 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700560 * @chip: the nand chip descriptor
Brian Norris8b6e50c2011-05-25 14:59:01 -0700561 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200562 *
563 * Used when in panic, no locks are taken.
564 */
Boris Brezillon08136212018-11-11 08:55:03 +0100565static void panic_nand_get_device(struct nand_chip *chip, int new_state)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200566{
Brian Norris7854d3f2011-06-23 14:12:08 -0700567 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200568 chip->controller->active = chip;
569 chip->state = new_state;
570}
571
572/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 * nand_get_device - [GENERIC] Get chip for selected access
Boris Brezillon08136212018-11-11 08:55:03 +0100574 * @chip: NAND chip structure
Brian Norris8b6e50c2011-05-25 14:59:01 -0700575 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *
577 * Get the device and lock it for exclusive access
578 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200579static int
Boris Brezillon08136212018-11-11 08:55:03 +0100580nand_get_device(struct nand_chip *chip, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200582 spinlock_t *lock = &chip->controller->lock;
583 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100584 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200585retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100586 spin_lock(lock);
587
vimal singhb8b3ee92009-07-09 20:41:22 +0530588 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200589 if (!chip->controller->active)
590 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200591
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200592 if (chip->controller->active == chip && chip->state == FL_READY) {
593 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100594 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100595 return 0;
596 }
597 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800598 if (chip->controller->active->state == FL_PM_SUSPENDED) {
599 chip->state = FL_PM_SUSPENDED;
600 spin_unlock(lock);
601 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800602 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100603 }
604 set_current_state(TASK_UNINTERRUPTIBLE);
605 add_wait_queue(wq, &wait);
606 spin_unlock(lock);
607 schedule();
608 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 goto retry;
610}
611
612/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700613 * panic_nand_wait - [GENERIC] wait until the command is done
Brian Norris8b6e50c2011-05-25 14:59:01 -0700614 * @chip: NAND chip structure
615 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200616 *
617 * Wait for command done. This is a helper function for nand_wait used when
618 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400619 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200620 */
Boris Brezillon3d4af7c2018-09-07 00:38:49 +0200621void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200622{
623 int i;
624 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200625 if (chip->legacy.dev_ready) {
626 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200627 break;
628 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100629 int ret;
630 u8 status;
631
632 ret = nand_read_data_op(chip, &status, sizeof(status),
633 true);
634 if (ret)
635 return;
636
637 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200638 break;
639 }
640 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200641 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200642}
643
Miquel Raynal789157e2018-03-19 14:47:28 +0100644static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +0100645{
Miquel Raynal789157e2018-03-19 14:47:28 +0100646 return (chip->parameters.supports_set_get_features &&
647 test_bit(addr, chip->parameters.get_feature_list));
648}
649
650static bool nand_supports_set_features(struct nand_chip *chip, int addr)
651{
652 return (chip->parameters.supports_set_get_features &&
653 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +0100654}
655
656/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200657 * nand_reset_data_interface - Reset data interface and timings
658 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100659 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200660 *
661 * Reset the Data interface and timings to ONFI mode 0.
662 *
663 * Returns 0 for success or negative error code otherwise.
664 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100665static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200666{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200667 int ret;
668
669 if (!chip->setup_data_interface)
670 return 0;
671
672 /*
673 * The ONFI specification says:
674 * "
675 * To transition from NV-DDR or NV-DDR2 to the SDR data
676 * interface, the host shall use the Reset (FFh) command
677 * using SDR timing mode 0. A device in any timing mode is
678 * required to recognize Reset (FFh) command issued in SDR
679 * timing mode 0.
680 * "
681 *
682 * Configure the data interface in SDR mode and set the
683 * timings to timing mode 0.
684 */
685
Miquel Raynal17fa8042017-11-30 18:01:31 +0100686 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon858838b2018-09-06 14:05:33 +0200687 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200688 if (ret)
689 pr_err("Failed to configure data interface to SDR timing mode 0\n");
690
691 return ret;
692}
693
694/**
695 * nand_setup_data_interface - Setup the best data interface and timings
696 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100697 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200698 *
699 * Find and configure the best data interface and NAND timings supported by
700 * the chip and the driver.
701 * First tries to retrieve supported timing modes from ONFI information,
702 * and if the NAND chip does not support ONFI, relies on the
703 * ->onfi_timing_mode_default specified in the nand_ids table.
704 *
705 * Returns 0 for success or negative error code otherwise.
706 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100707static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200708{
Miquel Raynal97baea12018-03-19 14:47:20 +0100709 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
710 chip->onfi_timing_mode_default,
711 };
Boris Brezillond8e725d2016-09-15 10:32:50 +0200712 int ret;
713
Miquel Raynal17fa8042017-11-30 18:01:31 +0100714 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200715 return 0;
716
Miquel Raynal993447b2018-03-19 14:47:21 +0100717 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +0100718 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +0200719 chip->select_chip(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +0100720 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
721 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +0200722 chip->select_chip(chip, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200723 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +0100724 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +0200725 }
726
Miquel Raynal97baea12018-03-19 14:47:20 +0100727 /* Change the mode on the controller side */
Boris Brezillon858838b2018-09-06 14:05:33 +0200728 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +0100729 if (ret)
730 return ret;
731
732 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +0100733 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +0100734 return 0;
735
736 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon758b56f2018-09-06 14:05:24 +0200737 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100738 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
739 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +0200740 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +0100741 if (ret)
742 goto err_reset_chip;
743
744 if (tmode_param[0] != chip->onfi_timing_mode_default) {
745 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
746 chip->onfi_timing_mode_default);
747 goto err_reset_chip;
748 }
749
750 return 0;
751
752err_reset_chip:
753 /*
754 * Fallback to mode 0 if the chip explicitly did not ack the chosen
755 * timing mode.
756 */
757 nand_reset_data_interface(chip, chipnr);
Boris Brezillon758b56f2018-09-06 14:05:24 +0200758 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100759 nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +0200760 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +0100761
Boris Brezillond8e725d2016-09-15 10:32:50 +0200762 return ret;
763}
764
765/**
766 * nand_init_data_interface - find the best data interface and timings
767 * @chip: The NAND chip
768 *
769 * Find the best data interface and NAND timings supported by the chip
770 * and the driver.
771 * First tries to retrieve supported timing modes from ONFI information,
772 * and if the NAND chip does not support ONFI, relies on the
773 * ->onfi_timing_mode_default specified in the nand_ids table. After this
774 * function nand_chip->data_interface is initialized with the best timing mode
775 * available.
776 *
777 * Returns 0 for success or negative error code otherwise.
778 */
779static int nand_init_data_interface(struct nand_chip *chip)
780{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200781 int modes, mode, ret;
782
783 if (!chip->setup_data_interface)
784 return 0;
785
786 /*
787 * First try to identify the best timings from ONFI parameters and
788 * if the NAND does not support ONFI, fallback to the default ONFI
789 * timing mode.
790 */
Boris Brezillon462f35d2018-09-07 00:38:47 +0200791 if (chip->parameters.onfi) {
792 modes = chip->parameters.onfi->async_timing_mode;
793 } else {
Boris Brezillond8e725d2016-09-15 10:32:50 +0200794 if (!chip->onfi_timing_mode_default)
795 return 0;
796
797 modes = GENMASK(chip->onfi_timing_mode_default, 0);
798 }
799
Boris Brezillond8e725d2016-09-15 10:32:50 +0200800 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +0100801 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200802 if (ret)
803 continue;
804
Miquel Raynald787b8b2017-12-22 18:12:41 +0100805 /*
806 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
807 * controller supports the requested timings.
808 */
Boris Brezillon858838b2018-09-06 14:05:33 +0200809 ret = chip->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +0100810 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +0100811 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200812 if (!ret) {
813 chip->onfi_timing_mode_default = mode;
814 break;
815 }
816 }
817
818 return 0;
819}
820
Boris Brezillond8e725d2016-09-15 10:32:50 +0200821/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100822 * nand_fill_column_cycles - fill the column cycles of an address
823 * @chip: The NAND chip
824 * @addrs: Array of address cycles to fill
825 * @offset_in_page: The offset in the page
826 *
827 * Fills the first or the first two bytes of the @addrs field depending
828 * on the NAND bus width and the page size.
829 *
830 * Returns the number of cycles needed to encode the column, or a negative
831 * error code in case one of the arguments is invalid.
832 */
833static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
834 unsigned int offset_in_page)
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100835{
Miquel Raynal8878b122017-11-09 14:16:45 +0100836 struct mtd_info *mtd = nand_to_mtd(chip);
837
838 /* Make sure the offset is less than the actual page size. */
839 if (offset_in_page > mtd->writesize + mtd->oobsize)
840 return -EINVAL;
841
842 /*
843 * On small page NANDs, there's a dedicated command to access the OOB
844 * area, and the column address is relative to the start of the OOB
845 * area, not the start of the page. Asjust the address accordingly.
846 */
847 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
848 offset_in_page -= mtd->writesize;
849
850 /*
851 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
852 * wide, then it must be divided by 2.
853 */
854 if (chip->options & NAND_BUSWIDTH_16) {
855 if (WARN_ON(offset_in_page % 2))
856 return -EINVAL;
857
858 offset_in_page /= 2;
859 }
860
861 addrs[0] = offset_in_page;
862
863 /*
864 * Small page NANDs use 1 cycle for the columns, while large page NANDs
865 * need 2
866 */
867 if (mtd->writesize <= 512)
868 return 1;
869
870 addrs[1] = offset_in_page >> 8;
871
872 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Miquel Raynal8878b122017-11-09 14:16:45 +0100875static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
876 unsigned int offset_in_page, void *buf,
877 unsigned int len)
878{
879 struct mtd_info *mtd = nand_to_mtd(chip);
880 const struct nand_sdr_timings *sdr =
881 nand_get_sdr_timings(&chip->data_interface);
882 u8 addrs[4];
883 struct nand_op_instr instrs[] = {
884 NAND_OP_CMD(NAND_CMD_READ0, 0),
885 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
886 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
887 PSEC_TO_NSEC(sdr->tRR_min)),
888 NAND_OP_DATA_IN(len, buf, 0),
889 };
890 struct nand_operation op = NAND_OPERATION(instrs);
891 int ret;
892
893 /* Drop the DATA_IN instruction if len is set to 0. */
894 if (!len)
895 op.ninstrs--;
896
897 if (offset_in_page >= mtd->writesize)
898 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
899 else if (offset_in_page >= 256 &&
900 !(chip->options & NAND_BUSWIDTH_16))
901 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
902
903 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
904 if (ret < 0)
905 return ret;
906
907 addrs[1] = page;
908 addrs[2] = page >> 8;
909
910 if (chip->options & NAND_ROW_ADDR_3) {
911 addrs[3] = page >> 16;
912 instrs[1].ctx.addr.naddrs++;
913 }
914
915 return nand_exec_op(chip, &op);
916}
917
918static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
919 unsigned int offset_in_page, void *buf,
920 unsigned int len)
921{
922 const struct nand_sdr_timings *sdr =
923 nand_get_sdr_timings(&chip->data_interface);
924 u8 addrs[5];
925 struct nand_op_instr instrs[] = {
926 NAND_OP_CMD(NAND_CMD_READ0, 0),
927 NAND_OP_ADDR(4, addrs, 0),
928 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
929 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
930 PSEC_TO_NSEC(sdr->tRR_min)),
931 NAND_OP_DATA_IN(len, buf, 0),
932 };
933 struct nand_operation op = NAND_OPERATION(instrs);
934 int ret;
935
936 /* Drop the DATA_IN instruction if len is set to 0. */
937 if (!len)
938 op.ninstrs--;
939
940 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
941 if (ret < 0)
942 return ret;
943
944 addrs[2] = page;
945 addrs[3] = page >> 8;
946
947 if (chip->options & NAND_ROW_ADDR_3) {
948 addrs[4] = page >> 16;
949 instrs[1].ctx.addr.naddrs++;
950 }
951
952 return nand_exec_op(chip, &op);
953}
954
955/**
Boris Brezillon97d90da2017-11-30 18:01:29 +0100956 * nand_read_page_op - Do a READ PAGE operation
957 * @chip: The NAND chip
958 * @page: page to read
959 * @offset_in_page: offset within the page
960 * @buf: buffer used to store the data
961 * @len: length of the buffer
962 *
963 * This function issues a READ PAGE operation.
964 * This function does not select/unselect the CS line.
965 *
966 * Returns 0 on success, a negative error code otherwise.
967 */
968int nand_read_page_op(struct nand_chip *chip, unsigned int page,
969 unsigned int offset_in_page, void *buf, unsigned int len)
970{
971 struct mtd_info *mtd = nand_to_mtd(chip);
972
973 if (len && !buf)
974 return -EINVAL;
975
976 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
977 return -EINVAL;
978
Miquel Raynal8878b122017-11-09 14:16:45 +0100979 if (chip->exec_op) {
980 if (mtd->writesize > 512)
981 return nand_lp_exec_read_page_op(chip, page,
982 offset_in_page, buf,
983 len);
984
985 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
986 buf, len);
987 }
988
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200989 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100990 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +0200991 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100992
993 return 0;
994}
995EXPORT_SYMBOL_GPL(nand_read_page_op);
996
997/**
998 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
999 * @chip: The NAND chip
1000 * @page: parameter page to read
1001 * @buf: buffer used to store the data
1002 * @len: length of the buffer
1003 *
1004 * This function issues a READ PARAMETER PAGE operation.
1005 * This function does not select/unselect the CS line.
1006 *
1007 * Returns 0 on success, a negative error code otherwise.
1008 */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02001009int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1010 unsigned int len)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001011{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001012 unsigned int i;
1013 u8 *p = buf;
1014
1015 if (len && !buf)
1016 return -EINVAL;
1017
Miquel Raynal8878b122017-11-09 14:16:45 +01001018 if (chip->exec_op) {
1019 const struct nand_sdr_timings *sdr =
1020 nand_get_sdr_timings(&chip->data_interface);
1021 struct nand_op_instr instrs[] = {
1022 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1023 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1024 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1025 PSEC_TO_NSEC(sdr->tRR_min)),
1026 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1027 };
1028 struct nand_operation op = NAND_OPERATION(instrs);
1029
1030 /* Drop the DATA_IN instruction if len is set to 0. */
1031 if (!len)
1032 op.ninstrs--;
1033
1034 return nand_exec_op(chip, &op);
1035 }
1036
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001037 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001038 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001039 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001040
1041 return 0;
1042}
1043
1044/**
1045 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1046 * @chip: The NAND chip
1047 * @offset_in_page: offset within the page
1048 * @buf: buffer used to store the data
1049 * @len: length of the buffer
1050 * @force_8bit: force 8-bit bus access
1051 *
1052 * This function issues a CHANGE READ COLUMN operation.
1053 * This function does not select/unselect the CS line.
1054 *
1055 * Returns 0 on success, a negative error code otherwise.
1056 */
1057int nand_change_read_column_op(struct nand_chip *chip,
1058 unsigned int offset_in_page, void *buf,
1059 unsigned int len, bool force_8bit)
1060{
1061 struct mtd_info *mtd = nand_to_mtd(chip);
1062
1063 if (len && !buf)
1064 return -EINVAL;
1065
1066 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1067 return -EINVAL;
1068
Miquel Raynal8878b122017-11-09 14:16:45 +01001069 /* Small page NANDs do not support column change. */
1070 if (mtd->writesize <= 512)
1071 return -ENOTSUPP;
1072
1073 if (chip->exec_op) {
1074 const struct nand_sdr_timings *sdr =
1075 nand_get_sdr_timings(&chip->data_interface);
1076 u8 addrs[2] = {};
1077 struct nand_op_instr instrs[] = {
1078 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1079 NAND_OP_ADDR(2, addrs, 0),
1080 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1081 PSEC_TO_NSEC(sdr->tCCS_min)),
1082 NAND_OP_DATA_IN(len, buf, 0),
1083 };
1084 struct nand_operation op = NAND_OPERATION(instrs);
1085 int ret;
1086
1087 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1088 if (ret < 0)
1089 return ret;
1090
1091 /* Drop the DATA_IN instruction if len is set to 0. */
1092 if (!len)
1093 op.ninstrs--;
1094
1095 instrs[3].ctx.data.force_8bit = force_8bit;
1096
1097 return nand_exec_op(chip, &op);
1098 }
1099
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001100 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001101 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001102 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001103
1104 return 0;
1105}
1106EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1107
1108/**
1109 * nand_read_oob_op - Do a READ OOB operation
1110 * @chip: The NAND chip
1111 * @page: page to read
1112 * @offset_in_oob: offset within the OOB area
1113 * @buf: buffer used to store the data
1114 * @len: length of the buffer
1115 *
1116 * This function issues a READ OOB operation.
1117 * This function does not select/unselect the CS line.
1118 *
1119 * Returns 0 on success, a negative error code otherwise.
1120 */
1121int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1122 unsigned int offset_in_oob, void *buf, unsigned int len)
1123{
1124 struct mtd_info *mtd = nand_to_mtd(chip);
1125
1126 if (len && !buf)
1127 return -EINVAL;
1128
1129 if (offset_in_oob + len > mtd->oobsize)
1130 return -EINVAL;
1131
Miquel Raynal8878b122017-11-09 14:16:45 +01001132 if (chip->exec_op)
1133 return nand_read_page_op(chip, page,
1134 mtd->writesize + offset_in_oob,
1135 buf, len);
1136
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001137 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001138 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001139 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001140
1141 return 0;
1142}
1143EXPORT_SYMBOL_GPL(nand_read_oob_op);
1144
Miquel Raynal8878b122017-11-09 14:16:45 +01001145static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1146 unsigned int offset_in_page, const void *buf,
1147 unsigned int len, bool prog)
1148{
1149 struct mtd_info *mtd = nand_to_mtd(chip);
1150 const struct nand_sdr_timings *sdr =
1151 nand_get_sdr_timings(&chip->data_interface);
1152 u8 addrs[5] = {};
1153 struct nand_op_instr instrs[] = {
1154 /*
1155 * The first instruction will be dropped if we're dealing
1156 * with a large page NAND and adjusted if we're dealing
1157 * with a small page NAND and the page offset is > 255.
1158 */
1159 NAND_OP_CMD(NAND_CMD_READ0, 0),
1160 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1161 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1162 NAND_OP_DATA_OUT(len, buf, 0),
1163 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1164 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1165 };
1166 struct nand_operation op = NAND_OPERATION(instrs);
1167 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1168 int ret;
1169 u8 status;
1170
1171 if (naddrs < 0)
1172 return naddrs;
1173
1174 addrs[naddrs++] = page;
1175 addrs[naddrs++] = page >> 8;
1176 if (chip->options & NAND_ROW_ADDR_3)
1177 addrs[naddrs++] = page >> 16;
1178
1179 instrs[2].ctx.addr.naddrs = naddrs;
1180
1181 /* Drop the last two instructions if we're not programming the page. */
1182 if (!prog) {
1183 op.ninstrs -= 2;
1184 /* Also drop the DATA_OUT instruction if empty. */
1185 if (!len)
1186 op.ninstrs--;
1187 }
1188
1189 if (mtd->writesize <= 512) {
1190 /*
1191 * Small pages need some more tweaking: we have to adjust the
1192 * first instruction depending on the page offset we're trying
1193 * to access.
1194 */
1195 if (offset_in_page >= mtd->writesize)
1196 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1197 else if (offset_in_page >= 256 &&
1198 !(chip->options & NAND_BUSWIDTH_16))
1199 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1200 } else {
1201 /*
1202 * Drop the first command if we're dealing with a large page
1203 * NAND.
1204 */
1205 op.instrs++;
1206 op.ninstrs--;
1207 }
1208
1209 ret = nand_exec_op(chip, &op);
1210 if (!prog || ret)
1211 return ret;
1212
1213 ret = nand_status_op(chip, &status);
1214 if (ret)
1215 return ret;
1216
1217 return status;
1218}
1219
Boris Brezillon97d90da2017-11-30 18:01:29 +01001220/**
1221 * nand_prog_page_begin_op - starts a PROG PAGE operation
1222 * @chip: The NAND chip
1223 * @page: page to write
1224 * @offset_in_page: offset within the page
1225 * @buf: buffer containing the data to write to the page
1226 * @len: length of the buffer
1227 *
1228 * This function issues the first half of a PROG PAGE operation.
1229 * This function does not select/unselect the CS line.
1230 *
1231 * Returns 0 on success, a negative error code otherwise.
1232 */
1233int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1234 unsigned int offset_in_page, const void *buf,
1235 unsigned int len)
1236{
1237 struct mtd_info *mtd = nand_to_mtd(chip);
1238
1239 if (len && !buf)
1240 return -EINVAL;
1241
1242 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1243 return -EINVAL;
1244
Miquel Raynal8878b122017-11-09 14:16:45 +01001245 if (chip->exec_op)
1246 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1247 len, false);
1248
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001249 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001250
1251 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001252 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001253
1254 return 0;
1255}
1256EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1257
1258/**
1259 * nand_prog_page_end_op - ends a PROG PAGE operation
1260 * @chip: The NAND chip
1261 *
1262 * This function issues the second half of a PROG PAGE operation.
1263 * This function does not select/unselect the CS line.
1264 *
1265 * Returns 0 on success, a negative error code otherwise.
1266 */
1267int nand_prog_page_end_op(struct nand_chip *chip)
1268{
Miquel Raynal8878b122017-11-09 14:16:45 +01001269 int ret;
1270 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001271
Miquel Raynal8878b122017-11-09 14:16:45 +01001272 if (chip->exec_op) {
1273 const struct nand_sdr_timings *sdr =
1274 nand_get_sdr_timings(&chip->data_interface);
1275 struct nand_op_instr instrs[] = {
1276 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1277 PSEC_TO_NSEC(sdr->tWB_max)),
1278 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1279 };
1280 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001281
Miquel Raynal8878b122017-11-09 14:16:45 +01001282 ret = nand_exec_op(chip, &op);
1283 if (ret)
1284 return ret;
1285
1286 ret = nand_status_op(chip, &status);
1287 if (ret)
1288 return ret;
1289 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001290 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001291 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001292 if (ret < 0)
1293 return ret;
1294
1295 status = ret;
1296 }
1297
Boris Brezillon97d90da2017-11-30 18:01:29 +01001298 if (status & NAND_STATUS_FAIL)
1299 return -EIO;
1300
1301 return 0;
1302}
1303EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1304
1305/**
1306 * nand_prog_page_op - Do a full PROG PAGE operation
1307 * @chip: The NAND chip
1308 * @page: page to write
1309 * @offset_in_page: offset within the page
1310 * @buf: buffer containing the data to write to the page
1311 * @len: length of the buffer
1312 *
1313 * This function issues a full PROG PAGE operation.
1314 * This function does not select/unselect the CS line.
1315 *
1316 * Returns 0 on success, a negative error code otherwise.
1317 */
1318int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1319 unsigned int offset_in_page, const void *buf,
1320 unsigned int len)
1321{
1322 struct mtd_info *mtd = nand_to_mtd(chip);
1323 int status;
1324
1325 if (!len || !buf)
1326 return -EINVAL;
1327
1328 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1329 return -EINVAL;
1330
Miquel Raynal8878b122017-11-09 14:16:45 +01001331 if (chip->exec_op) {
1332 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1333 len, true);
1334 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001335 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1336 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001337 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001338 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001339 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001340 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001341
Boris Brezillon97d90da2017-11-30 18:01:29 +01001342 if (status & NAND_STATUS_FAIL)
1343 return -EIO;
1344
1345 return 0;
1346}
1347EXPORT_SYMBOL_GPL(nand_prog_page_op);
1348
1349/**
1350 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1351 * @chip: The NAND chip
1352 * @offset_in_page: offset within the page
1353 * @buf: buffer containing the data to send to the NAND
1354 * @len: length of the buffer
1355 * @force_8bit: force 8-bit bus access
1356 *
1357 * This function issues a CHANGE WRITE COLUMN operation.
1358 * This function does not select/unselect the CS line.
1359 *
1360 * Returns 0 on success, a negative error code otherwise.
1361 */
1362int nand_change_write_column_op(struct nand_chip *chip,
1363 unsigned int offset_in_page,
1364 const void *buf, unsigned int len,
1365 bool force_8bit)
1366{
1367 struct mtd_info *mtd = nand_to_mtd(chip);
1368
1369 if (len && !buf)
1370 return -EINVAL;
1371
1372 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1373 return -EINVAL;
1374
Miquel Raynal8878b122017-11-09 14:16:45 +01001375 /* Small page NANDs do not support column change. */
1376 if (mtd->writesize <= 512)
1377 return -ENOTSUPP;
1378
1379 if (chip->exec_op) {
1380 const struct nand_sdr_timings *sdr =
1381 nand_get_sdr_timings(&chip->data_interface);
1382 u8 addrs[2];
1383 struct nand_op_instr instrs[] = {
1384 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1385 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1386 NAND_OP_DATA_OUT(len, buf, 0),
1387 };
1388 struct nand_operation op = NAND_OPERATION(instrs);
1389 int ret;
1390
1391 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1392 if (ret < 0)
1393 return ret;
1394
1395 instrs[2].ctx.data.force_8bit = force_8bit;
1396
1397 /* Drop the DATA_OUT instruction if len is set to 0. */
1398 if (!len)
1399 op.ninstrs--;
1400
1401 return nand_exec_op(chip, &op);
1402 }
1403
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001404 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001405 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001406 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001407
1408 return 0;
1409}
1410EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1411
1412/**
1413 * nand_readid_op - Do a READID operation
1414 * @chip: The NAND chip
1415 * @addr: address cycle to pass after the READID command
1416 * @buf: buffer used to store the ID
1417 * @len: length of the buffer
1418 *
1419 * This function sends a READID command and reads back the ID returned by the
1420 * NAND.
1421 * This function does not select/unselect the CS line.
1422 *
1423 * Returns 0 on success, a negative error code otherwise.
1424 */
1425int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1426 unsigned int len)
1427{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001428 unsigned int i;
1429 u8 *id = buf;
1430
1431 if (len && !buf)
1432 return -EINVAL;
1433
Miquel Raynal8878b122017-11-09 14:16:45 +01001434 if (chip->exec_op) {
1435 const struct nand_sdr_timings *sdr =
1436 nand_get_sdr_timings(&chip->data_interface);
1437 struct nand_op_instr instrs[] = {
1438 NAND_OP_CMD(NAND_CMD_READID, 0),
1439 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1440 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1441 };
1442 struct nand_operation op = NAND_OPERATION(instrs);
1443
1444 /* Drop the DATA_IN instruction if len is set to 0. */
1445 if (!len)
1446 op.ninstrs--;
1447
1448 return nand_exec_op(chip, &op);
1449 }
1450
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001451 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001452
1453 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001454 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001455
1456 return 0;
1457}
1458EXPORT_SYMBOL_GPL(nand_readid_op);
1459
1460/**
1461 * nand_status_op - Do a STATUS operation
1462 * @chip: The NAND chip
1463 * @status: out variable to store the NAND status
1464 *
1465 * This function sends a STATUS command and reads back the status returned by
1466 * the NAND.
1467 * This function does not select/unselect the CS line.
1468 *
1469 * Returns 0 on success, a negative error code otherwise.
1470 */
1471int nand_status_op(struct nand_chip *chip, u8 *status)
1472{
Miquel Raynal8878b122017-11-09 14:16:45 +01001473 if (chip->exec_op) {
1474 const struct nand_sdr_timings *sdr =
1475 nand_get_sdr_timings(&chip->data_interface);
1476 struct nand_op_instr instrs[] = {
1477 NAND_OP_CMD(NAND_CMD_STATUS,
1478 PSEC_TO_NSEC(sdr->tADL_min)),
1479 NAND_OP_8BIT_DATA_IN(1, status, 0),
1480 };
1481 struct nand_operation op = NAND_OPERATION(instrs);
1482
1483 if (!status)
1484 op.ninstrs--;
1485
1486 return nand_exec_op(chip, &op);
1487 }
1488
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001489 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001490 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001491 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001492
1493 return 0;
1494}
1495EXPORT_SYMBOL_GPL(nand_status_op);
1496
1497/**
1498 * nand_exit_status_op - Exit a STATUS operation
1499 * @chip: The NAND chip
1500 *
1501 * This function sends a READ0 command to cancel the effect of the STATUS
1502 * command to avoid reading only the status until a new read command is sent.
1503 *
1504 * This function does not select/unselect the CS line.
1505 *
1506 * Returns 0 on success, a negative error code otherwise.
1507 */
1508int nand_exit_status_op(struct nand_chip *chip)
1509{
Miquel Raynal8878b122017-11-09 14:16:45 +01001510 if (chip->exec_op) {
1511 struct nand_op_instr instrs[] = {
1512 NAND_OP_CMD(NAND_CMD_READ0, 0),
1513 };
1514 struct nand_operation op = NAND_OPERATION(instrs);
1515
1516 return nand_exec_op(chip, &op);
1517 }
1518
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001519 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001520
1521 return 0;
1522}
Boris Brezillon97d90da2017-11-30 18:01:29 +01001523
1524/**
1525 * nand_erase_op - Do an erase operation
1526 * @chip: The NAND chip
1527 * @eraseblock: block to erase
1528 *
1529 * This function sends an ERASE command and waits for the NAND to be ready
1530 * before returning.
1531 * This function does not select/unselect the CS line.
1532 *
1533 * Returns 0 on success, a negative error code otherwise.
1534 */
1535int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1536{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001537 unsigned int page = eraseblock <<
1538 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01001539 int ret;
1540 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001541
Miquel Raynal8878b122017-11-09 14:16:45 +01001542 if (chip->exec_op) {
1543 const struct nand_sdr_timings *sdr =
1544 nand_get_sdr_timings(&chip->data_interface);
1545 u8 addrs[3] = { page, page >> 8, page >> 16 };
1546 struct nand_op_instr instrs[] = {
1547 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
1548 NAND_OP_ADDR(2, addrs, 0),
1549 NAND_OP_CMD(NAND_CMD_ERASE2,
1550 PSEC_TO_MSEC(sdr->tWB_max)),
1551 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
1552 };
1553 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001554
Miquel Raynal8878b122017-11-09 14:16:45 +01001555 if (chip->options & NAND_ROW_ADDR_3)
1556 instrs[1].ctx.addr.naddrs++;
1557
1558 ret = nand_exec_op(chip, &op);
1559 if (ret)
1560 return ret;
1561
1562 ret = nand_status_op(chip, &status);
1563 if (ret)
1564 return ret;
1565 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001566 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
1567 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01001568
Boris Brezillon8395b752018-09-07 00:38:37 +02001569 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001570 if (ret < 0)
1571 return ret;
1572
1573 status = ret;
1574 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001575
1576 if (status & NAND_STATUS_FAIL)
1577 return -EIO;
1578
1579 return 0;
1580}
1581EXPORT_SYMBOL_GPL(nand_erase_op);
1582
1583/**
1584 * nand_set_features_op - Do a SET FEATURES operation
1585 * @chip: The NAND chip
1586 * @feature: feature id
1587 * @data: 4 bytes of data
1588 *
1589 * This function sends a SET FEATURES command and waits for the NAND to be
1590 * ready before returning.
1591 * This function does not select/unselect the CS line.
1592 *
1593 * Returns 0 on success, a negative error code otherwise.
1594 */
1595static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1596 const void *data)
1597{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001598 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01001599 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001600
Miquel Raynal8878b122017-11-09 14:16:45 +01001601 if (chip->exec_op) {
1602 const struct nand_sdr_timings *sdr =
1603 nand_get_sdr_timings(&chip->data_interface);
1604 struct nand_op_instr instrs[] = {
1605 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
1606 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
1607 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
1608 PSEC_TO_NSEC(sdr->tWB_max)),
1609 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
1610 };
1611 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001612
Boris Brezillon782d1962018-05-11 14:44:07 +02001613 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01001614 }
1615
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001616 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02001617 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001618 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02001619
Boris Brezillon8395b752018-09-07 00:38:37 +02001620 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02001621 if (ret < 0)
1622 return ret;
1623
1624 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001625 return -EIO;
1626
1627 return 0;
1628}
1629
1630/**
1631 * nand_get_features_op - Do a GET FEATURES operation
1632 * @chip: The NAND chip
1633 * @feature: feature id
1634 * @data: 4 bytes of data
1635 *
1636 * This function sends a GET FEATURES command and waits for the NAND to be
1637 * ready before returning.
1638 * This function does not select/unselect the CS line.
1639 *
1640 * Returns 0 on success, a negative error code otherwise.
1641 */
1642static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1643 void *data)
1644{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001645 u8 *params = data;
1646 int i;
1647
Miquel Raynal8878b122017-11-09 14:16:45 +01001648 if (chip->exec_op) {
1649 const struct nand_sdr_timings *sdr =
1650 nand_get_sdr_timings(&chip->data_interface);
1651 struct nand_op_instr instrs[] = {
1652 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
1653 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
1654 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
1655 PSEC_TO_NSEC(sdr->tRR_min)),
1656 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
1657 data, 0),
1658 };
1659 struct nand_operation op = NAND_OPERATION(instrs);
1660
1661 return nand_exec_op(chip, &op);
1662 }
1663
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001664 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001665 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001666 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001667
1668 return 0;
1669}
1670
Boris Brezillon52f05b62018-07-27 09:44:18 +02001671static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
1672 unsigned int delay_ns)
1673{
1674 if (chip->exec_op) {
1675 struct nand_op_instr instrs[] = {
1676 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
1677 PSEC_TO_NSEC(delay_ns)),
1678 };
1679 struct nand_operation op = NAND_OPERATION(instrs);
1680
1681 return nand_exec_op(chip, &op);
1682 }
1683
1684 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02001685 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001686 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001687 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001688 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001689
1690 return 0;
1691}
1692
Boris Brezillon97d90da2017-11-30 18:01:29 +01001693/**
1694 * nand_reset_op - Do a reset operation
1695 * @chip: The NAND chip
1696 *
1697 * This function sends a RESET command and waits for the NAND to be ready
1698 * before returning.
1699 * This function does not select/unselect the CS line.
1700 *
1701 * Returns 0 on success, a negative error code otherwise.
1702 */
1703int nand_reset_op(struct nand_chip *chip)
1704{
Miquel Raynal8878b122017-11-09 14:16:45 +01001705 if (chip->exec_op) {
1706 const struct nand_sdr_timings *sdr =
1707 nand_get_sdr_timings(&chip->data_interface);
1708 struct nand_op_instr instrs[] = {
1709 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
1710 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
1711 };
1712 struct nand_operation op = NAND_OPERATION(instrs);
1713
1714 return nand_exec_op(chip, &op);
1715 }
1716
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001717 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001718
1719 return 0;
1720}
1721EXPORT_SYMBOL_GPL(nand_reset_op);
1722
1723/**
1724 * nand_read_data_op - Read data from the NAND
1725 * @chip: The NAND chip
1726 * @buf: buffer used to store the data
1727 * @len: length of the buffer
1728 * @force_8bit: force 8-bit bus access
1729 *
1730 * This function does a raw data read on the bus. Usually used after launching
1731 * another NAND operation like nand_read_page_op().
1732 * This function does not select/unselect the CS line.
1733 *
1734 * Returns 0 on success, a negative error code otherwise.
1735 */
1736int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1737 bool force_8bit)
1738{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001739 if (!len || !buf)
1740 return -EINVAL;
1741
Miquel Raynal8878b122017-11-09 14:16:45 +01001742 if (chip->exec_op) {
1743 struct nand_op_instr instrs[] = {
1744 NAND_OP_DATA_IN(len, buf, 0),
1745 };
1746 struct nand_operation op = NAND_OPERATION(instrs);
1747
1748 instrs[0].ctx.data.force_8bit = force_8bit;
1749
1750 return nand_exec_op(chip, &op);
1751 }
1752
Boris Brezillon97d90da2017-11-30 18:01:29 +01001753 if (force_8bit) {
1754 u8 *p = buf;
1755 unsigned int i;
1756
1757 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001758 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001759 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001760 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001761 }
1762
1763 return 0;
1764}
1765EXPORT_SYMBOL_GPL(nand_read_data_op);
1766
1767/**
1768 * nand_write_data_op - Write data from the NAND
1769 * @chip: The NAND chip
1770 * @buf: buffer containing the data to send on the bus
1771 * @len: length of the buffer
1772 * @force_8bit: force 8-bit bus access
1773 *
1774 * This function does a raw data write on the bus. Usually used after launching
1775 * another NAND operation like nand_write_page_begin_op().
1776 * This function does not select/unselect the CS line.
1777 *
1778 * Returns 0 on success, a negative error code otherwise.
1779 */
1780int nand_write_data_op(struct nand_chip *chip, const void *buf,
1781 unsigned int len, bool force_8bit)
1782{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001783 if (!len || !buf)
1784 return -EINVAL;
1785
Miquel Raynal8878b122017-11-09 14:16:45 +01001786 if (chip->exec_op) {
1787 struct nand_op_instr instrs[] = {
1788 NAND_OP_DATA_OUT(len, buf, 0),
1789 };
1790 struct nand_operation op = NAND_OPERATION(instrs);
1791
1792 instrs[0].ctx.data.force_8bit = force_8bit;
1793
1794 return nand_exec_op(chip, &op);
1795 }
1796
Boris Brezillon97d90da2017-11-30 18:01:29 +01001797 if (force_8bit) {
1798 const u8 *p = buf;
1799 unsigned int i;
1800
1801 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001802 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001803 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001804 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001805 }
1806
1807 return 0;
1808}
1809EXPORT_SYMBOL_GPL(nand_write_data_op);
1810
1811/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001812 * struct nand_op_parser_ctx - Context used by the parser
1813 * @instrs: array of all the instructions that must be addressed
1814 * @ninstrs: length of the @instrs array
1815 * @subop: Sub-operation to be passed to the NAND controller
1816 *
1817 * This structure is used by the core to split NAND operations into
1818 * sub-operations that can be handled by the NAND controller.
1819 */
1820struct nand_op_parser_ctx {
1821 const struct nand_op_instr *instrs;
1822 unsigned int ninstrs;
1823 struct nand_subop subop;
1824};
1825
1826/**
1827 * nand_op_parser_must_split_instr - Checks if an instruction must be split
1828 * @pat: the parser pattern element that matches @instr
1829 * @instr: pointer to the instruction to check
1830 * @start_offset: this is an in/out parameter. If @instr has already been
1831 * split, then @start_offset is the offset from which to start
1832 * (either an address cycle or an offset in the data buffer).
1833 * Conversely, if the function returns true (ie. instr must be
1834 * split), this parameter is updated to point to the first
1835 * data/address cycle that has not been taken care of.
1836 *
1837 * Some NAND controllers are limited and cannot send X address cycles with a
1838 * unique operation, or cannot read/write more than Y bytes at the same time.
1839 * In this case, split the instruction that does not fit in a single
1840 * controller-operation into two or more chunks.
1841 *
1842 * Returns true if the instruction must be split, false otherwise.
1843 * The @start_offset parameter is also updated to the offset at which the next
1844 * bundle of instruction must start (if an address or a data instruction).
1845 */
1846static bool
1847nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
1848 const struct nand_op_instr *instr,
1849 unsigned int *start_offset)
1850{
1851 switch (pat->type) {
1852 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001853 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01001854 break;
1855
1856 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001857 pat->ctx.addr.maxcycles) {
1858 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01001859 return true;
1860 }
1861 break;
1862
1863 case NAND_OP_DATA_IN_INSTR:
1864 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001865 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01001866 break;
1867
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001868 if (instr->ctx.data.len - *start_offset >
1869 pat->ctx.data.maxlen) {
1870 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01001871 return true;
1872 }
1873 break;
1874
1875 default:
1876 break;
1877 }
1878
1879 return false;
1880}
1881
1882/**
1883 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
1884 * remaining in the parser context
1885 * @pat: the pattern to test
1886 * @ctx: the parser context structure to match with the pattern @pat
1887 *
1888 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
1889 * Returns true if this is the case, false ortherwise. When true is returned,
1890 * @ctx->subop is updated with the set of instructions to be passed to the
1891 * controller driver.
1892 */
1893static bool
1894nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
1895 struct nand_op_parser_ctx *ctx)
1896{
1897 unsigned int instr_offset = ctx->subop.first_instr_start_off;
1898 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
1899 const struct nand_op_instr *instr = ctx->subop.instrs;
1900 unsigned int i, ninstrs;
1901
1902 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
1903 /*
1904 * The pattern instruction does not match the operation
1905 * instruction. If the instruction is marked optional in the
1906 * pattern definition, we skip the pattern element and continue
1907 * to the next one. If the element is mandatory, there's no
1908 * match and we can return false directly.
1909 */
1910 if (instr->type != pat->elems[i].type) {
1911 if (!pat->elems[i].optional)
1912 return false;
1913
1914 continue;
1915 }
1916
1917 /*
1918 * Now check the pattern element constraints. If the pattern is
1919 * not able to handle the whole instruction in a single step,
1920 * we have to split it.
1921 * The last_instr_end_off value comes back updated to point to
1922 * the position where we have to split the instruction (the
1923 * start of the next subop chunk).
1924 */
1925 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
1926 &instr_offset)) {
1927 ninstrs++;
1928 i++;
1929 break;
1930 }
1931
1932 instr++;
1933 ninstrs++;
1934 instr_offset = 0;
1935 }
1936
1937 /*
1938 * This can happen if all instructions of a pattern are optional.
1939 * Still, if there's not at least one instruction handled by this
1940 * pattern, this is not a match, and we should try the next one (if
1941 * any).
1942 */
1943 if (!ninstrs)
1944 return false;
1945
1946 /*
1947 * We had a match on the pattern head, but the pattern may be longer
1948 * than the instructions we're asked to execute. We need to make sure
1949 * there's no mandatory elements in the pattern tail.
1950 */
1951 for (; i < pat->nelems; i++) {
1952 if (!pat->elems[i].optional)
1953 return false;
1954 }
1955
1956 /*
1957 * We have a match: update the subop structure accordingly and return
1958 * true.
1959 */
1960 ctx->subop.ninstrs = ninstrs;
1961 ctx->subop.last_instr_end_off = instr_offset;
1962
1963 return true;
1964}
1965
1966#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
1967static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
1968{
1969 const struct nand_op_instr *instr;
1970 char *prefix = " ";
1971 unsigned int i;
1972
1973 pr_debug("executing subop:\n");
1974
1975 for (i = 0; i < ctx->ninstrs; i++) {
1976 instr = &ctx->instrs[i];
1977
1978 if (instr == &ctx->subop.instrs[0])
1979 prefix = " ->";
1980
1981 switch (instr->type) {
1982 case NAND_OP_CMD_INSTR:
1983 pr_debug("%sCMD [0x%02x]\n", prefix,
1984 instr->ctx.cmd.opcode);
1985 break;
1986 case NAND_OP_ADDR_INSTR:
1987 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
1988 instr->ctx.addr.naddrs,
1989 instr->ctx.addr.naddrs < 64 ?
1990 instr->ctx.addr.naddrs : 64,
1991 instr->ctx.addr.addrs);
1992 break;
1993 case NAND_OP_DATA_IN_INSTR:
1994 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
1995 instr->ctx.data.len,
1996 instr->ctx.data.force_8bit ?
1997 ", force 8-bit" : "");
1998 break;
1999 case NAND_OP_DATA_OUT_INSTR:
2000 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2001 instr->ctx.data.len,
2002 instr->ctx.data.force_8bit ?
2003 ", force 8-bit" : "");
2004 break;
2005 case NAND_OP_WAITRDY_INSTR:
2006 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2007 instr->ctx.waitrdy.timeout_ms);
2008 break;
2009 }
2010
2011 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2012 prefix = " ";
2013 }
2014}
2015#else
2016static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2017{
2018 /* NOP */
2019}
2020#endif
2021
2022/**
2023 * nand_op_parser_exec_op - exec_op parser
2024 * @chip: the NAND chip
2025 * @parser: patterns description provided by the controller driver
2026 * @op: the NAND operation to address
2027 * @check_only: when true, the function only checks if @op can be handled but
2028 * does not execute the operation
2029 *
2030 * Helper function designed to ease integration of NAND controller drivers that
2031 * only support a limited set of instruction sequences. The supported sequences
2032 * are described in @parser, and the framework takes care of splitting @op into
2033 * multiple sub-operations (if required) and pass them back to the ->exec()
2034 * callback of the matching pattern if @check_only is set to false.
2035 *
2036 * NAND controller drivers should call this function from their own ->exec_op()
2037 * implementation.
2038 *
2039 * Returns 0 on success, a negative error code otherwise. A failure can be
2040 * caused by an unsupported operation (none of the supported patterns is able
2041 * to handle the requested operation), or an error returned by one of the
2042 * matching pattern->exec() hook.
2043 */
2044int nand_op_parser_exec_op(struct nand_chip *chip,
2045 const struct nand_op_parser *parser,
2046 const struct nand_operation *op, bool check_only)
2047{
2048 struct nand_op_parser_ctx ctx = {
2049 .subop.instrs = op->instrs,
2050 .instrs = op->instrs,
2051 .ninstrs = op->ninstrs,
2052 };
2053 unsigned int i;
2054
2055 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2056 int ret;
2057
2058 for (i = 0; i < parser->npatterns; i++) {
2059 const struct nand_op_parser_pattern *pattern;
2060
2061 pattern = &parser->patterns[i];
2062 if (!nand_op_parser_match_pat(pattern, &ctx))
2063 continue;
2064
2065 nand_op_parser_trace(&ctx);
2066
2067 if (check_only)
2068 break;
2069
2070 ret = pattern->exec(chip, &ctx.subop);
2071 if (ret)
2072 return ret;
2073
2074 break;
2075 }
2076
2077 if (i == parser->npatterns) {
2078 pr_debug("->exec_op() parser: pattern not found!\n");
2079 return -ENOTSUPP;
2080 }
2081
2082 /*
2083 * Update the context structure by pointing to the start of the
2084 * next subop.
2085 */
2086 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2087 if (ctx.subop.last_instr_end_off)
2088 ctx.subop.instrs -= 1;
2089
2090 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2091 }
2092
2093 return 0;
2094}
2095EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2096
2097static bool nand_instr_is_data(const struct nand_op_instr *instr)
2098{
2099 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2100 instr->type == NAND_OP_DATA_OUT_INSTR);
2101}
2102
2103static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2104 unsigned int instr_idx)
2105{
2106 return subop && instr_idx < subop->ninstrs;
2107}
2108
Miquel Raynal760c4352018-07-19 00:09:12 +02002109static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2110 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002111{
2112 if (instr_idx)
2113 return 0;
2114
2115 return subop->first_instr_start_off;
2116}
2117
2118/**
2119 * nand_subop_get_addr_start_off - Get the start offset in an address array
2120 * @subop: The entire sub-operation
2121 * @instr_idx: Index of the instruction inside the sub-operation
2122 *
2123 * During driver development, one could be tempted to directly use the
2124 * ->addr.addrs field of address instructions. This is wrong as address
2125 * instructions might be split.
2126 *
2127 * Given an address instruction, returns the offset of the first cycle to issue.
2128 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002129unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2130 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002131{
Miquel Raynal760c4352018-07-19 00:09:12 +02002132 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2133 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2134 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002135
2136 return nand_subop_get_start_off(subop, instr_idx);
2137}
2138EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2139
2140/**
2141 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2142 * @subop: The entire sub-operation
2143 * @instr_idx: Index of the instruction inside the sub-operation
2144 *
2145 * During driver development, one could be tempted to directly use the
2146 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2147 * might be split.
2148 *
2149 * Given an address instruction, returns the number of address cycle to issue.
2150 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002151unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2152 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002153{
2154 int start_off, end_off;
2155
Miquel Raynal760c4352018-07-19 00:09:12 +02002156 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2157 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2158 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002159
2160 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2161
2162 if (instr_idx == subop->ninstrs - 1 &&
2163 subop->last_instr_end_off)
2164 end_off = subop->last_instr_end_off;
2165 else
2166 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2167
2168 return end_off - start_off;
2169}
2170EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2171
2172/**
2173 * nand_subop_get_data_start_off - Get the start offset in a data array
2174 * @subop: The entire sub-operation
2175 * @instr_idx: Index of the instruction inside the sub-operation
2176 *
2177 * During driver development, one could be tempted to directly use the
2178 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2179 * instructions might be split.
2180 *
2181 * Given a data instruction, returns the offset to start from.
2182 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002183unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2184 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002185{
Miquel Raynal760c4352018-07-19 00:09:12 +02002186 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2187 !nand_instr_is_data(&subop->instrs[instr_idx])))
2188 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002189
2190 return nand_subop_get_start_off(subop, instr_idx);
2191}
2192EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2193
2194/**
2195 * nand_subop_get_data_len - Get the number of bytes to retrieve
2196 * @subop: The entire sub-operation
2197 * @instr_idx: Index of the instruction inside the sub-operation
2198 *
2199 * During driver development, one could be tempted to directly use the
2200 * ->data->len field of a data instruction. This is wrong as data instructions
2201 * might be split.
2202 *
2203 * Returns the length of the chunk of data to send/receive.
2204 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002205unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2206 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002207{
2208 int start_off = 0, end_off;
2209
Miquel Raynal760c4352018-07-19 00:09:12 +02002210 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2211 !nand_instr_is_data(&subop->instrs[instr_idx])))
2212 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002213
2214 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2215
2216 if (instr_idx == subop->ninstrs - 1 &&
2217 subop->last_instr_end_off)
2218 end_off = subop->last_instr_end_off;
2219 else
2220 end_off = subop->instrs[instr_idx].ctx.data.len;
2221
2222 return end_off - start_off;
2223}
2224EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002227 * nand_reset - Reset and initialize a NAND device
2228 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002229 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002230 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002231 * Save the timings data structure, then apply SDR timings mode 0 (see
2232 * nand_reset_data_interface for details), do the reset operation, and
2233 * apply back the previous timings.
2234 *
2235 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002236 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002237int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002238{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002239 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002240 int ret;
2241
Boris Brezillon104e4422017-03-16 09:35:58 +01002242 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002243 if (ret)
2244 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002245
Boris Brezillon73f907f2016-10-24 16:46:20 +02002246 /*
2247 * The CS line has to be released before we can apply the new NAND
2248 * interface settings, hence this weird ->select_chip() dance.
2249 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02002250 chip->select_chip(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002251 ret = nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02002252 chip->select_chip(chip, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002253 if (ret)
2254 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002255
Miquel Raynal107b7d62018-03-19 14:47:25 +01002256 /*
2257 * A nand_reset_data_interface() put both the NAND chip and the NAND
2258 * controller in timings mode 0. If the default mode for this chip is
2259 * also 0, no need to proceed to the change again. Plus, at probe time,
2260 * nand_setup_data_interface() uses ->set/get_features() which would
2261 * fail anyway as the parameter page is not available yet.
2262 */
2263 if (!chip->onfi_timing_mode_default)
2264 return 0;
2265
Miquel Raynal17fa8042017-11-30 18:01:31 +01002266 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002267 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002268 if (ret)
2269 return ret;
2270
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002271 return 0;
2272}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002273EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002274
2275/**
Boris Brezillon45240362018-09-07 00:38:40 +02002276 * nand_get_features - wrapper to perform a GET_FEATURE
2277 * @chip: NAND chip info structure
2278 * @addr: feature address
2279 * @subfeature_param: the subfeature parameters, a four bytes array
2280 *
2281 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2282 * operation cannot be handled.
2283 */
2284int nand_get_features(struct nand_chip *chip, int addr,
2285 u8 *subfeature_param)
2286{
2287 if (!nand_supports_get_features(chip, addr))
2288 return -ENOTSUPP;
2289
2290 if (chip->legacy.get_features)
2291 return chip->legacy.get_features(chip, addr, subfeature_param);
2292
2293 return nand_get_features_op(chip, addr, subfeature_param);
2294}
Boris Brezillon45240362018-09-07 00:38:40 +02002295
2296/**
2297 * nand_set_features - wrapper to perform a SET_FEATURE
2298 * @chip: NAND chip info structure
2299 * @addr: feature address
2300 * @subfeature_param: the subfeature parameters, a four bytes array
2301 *
2302 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2303 * operation cannot be handled.
2304 */
2305int nand_set_features(struct nand_chip *chip, int addr,
2306 u8 *subfeature_param)
2307{
2308 if (!nand_supports_set_features(chip, addr))
2309 return -ENOTSUPP;
2310
2311 if (chip->legacy.set_features)
2312 return chip->legacy.set_features(chip, addr, subfeature_param);
2313
2314 return nand_set_features_op(chip, addr, subfeature_param);
2315}
Boris Brezillon45240362018-09-07 00:38:40 +02002316
2317/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002318 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2319 * @buf: buffer to test
2320 * @len: buffer length
2321 * @bitflips_threshold: maximum number of bitflips
2322 *
2323 * Check if a buffer contains only 0xff, which means the underlying region
2324 * has been erased and is ready to be programmed.
2325 * The bitflips_threshold specify the maximum number of bitflips before
2326 * considering the region is not erased.
2327 * Note: The logic of this function has been extracted from the memweight
2328 * implementation, except that nand_check_erased_buf function exit before
2329 * testing the whole buffer if the number of bitflips exceed the
2330 * bitflips_threshold value.
2331 *
2332 * Returns a positive number of bitflips less than or equal to
2333 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2334 * threshold.
2335 */
2336static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2337{
2338 const unsigned char *bitmap = buf;
2339 int bitflips = 0;
2340 int weight;
2341
2342 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2343 len--, bitmap++) {
2344 weight = hweight8(*bitmap);
2345 bitflips += BITS_PER_BYTE - weight;
2346 if (unlikely(bitflips > bitflips_threshold))
2347 return -EBADMSG;
2348 }
2349
2350 for (; len >= sizeof(long);
2351 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002352 unsigned long d = *((unsigned long *)bitmap);
2353 if (d == ~0UL)
2354 continue;
2355 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002356 bitflips += BITS_PER_LONG - weight;
2357 if (unlikely(bitflips > bitflips_threshold))
2358 return -EBADMSG;
2359 }
2360
2361 for (; len > 0; len--, bitmap++) {
2362 weight = hweight8(*bitmap);
2363 bitflips += BITS_PER_BYTE - weight;
2364 if (unlikely(bitflips > bitflips_threshold))
2365 return -EBADMSG;
2366 }
2367
2368 return bitflips;
2369}
2370
2371/**
2372 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2373 * 0xff data
2374 * @data: data buffer to test
2375 * @datalen: data length
2376 * @ecc: ECC buffer
2377 * @ecclen: ECC length
2378 * @extraoob: extra OOB buffer
2379 * @extraooblen: extra OOB length
2380 * @bitflips_threshold: maximum number of bitflips
2381 *
2382 * Check if a data buffer and its associated ECC and OOB data contains only
2383 * 0xff pattern, which means the underlying region has been erased and is
2384 * ready to be programmed.
2385 * The bitflips_threshold specify the maximum number of bitflips before
2386 * considering the region as not erased.
2387 *
2388 * Note:
2389 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2390 * different from the NAND page size. When fixing bitflips, ECC engines will
2391 * report the number of errors per chunk, and the NAND core infrastructure
2392 * expect you to return the maximum number of bitflips for the whole page.
2393 * This is why you should always use this function on a single chunk and
2394 * not on the whole page. After checking each chunk you should update your
2395 * max_bitflips value accordingly.
2396 * 2/ When checking for bitflips in erased pages you should not only check
2397 * the payload data but also their associated ECC data, because a user might
2398 * have programmed almost all bits to 1 but a few. In this case, we
2399 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2400 * this case.
2401 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2402 * data are protected by the ECC engine.
2403 * It could also be used if you support subpages and want to attach some
2404 * extra OOB data to an ECC chunk.
2405 *
2406 * Returns a positive number of bitflips less than or equal to
2407 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2408 * threshold. In case of success, the passed buffers are filled with 0xff.
2409 */
2410int nand_check_erased_ecc_chunk(void *data, int datalen,
2411 void *ecc, int ecclen,
2412 void *extraoob, int extraooblen,
2413 int bitflips_threshold)
2414{
2415 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2416
2417 data_bitflips = nand_check_erased_buf(data, datalen,
2418 bitflips_threshold);
2419 if (data_bitflips < 0)
2420 return data_bitflips;
2421
2422 bitflips_threshold -= data_bitflips;
2423
2424 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2425 if (ecc_bitflips < 0)
2426 return ecc_bitflips;
2427
2428 bitflips_threshold -= ecc_bitflips;
2429
2430 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2431 bitflips_threshold);
2432 if (extraoob_bitflips < 0)
2433 return extraoob_bitflips;
2434
2435 if (data_bitflips)
2436 memset(data, 0xff, datalen);
2437
2438 if (ecc_bitflips)
2439 memset(ecc, 0xff, ecclen);
2440
2441 if (extraoob_bitflips)
2442 memset(extraoob, 0xff, extraooblen);
2443
2444 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2445}
2446EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2447
2448/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002449 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002450 * @chip: nand chip info structure
2451 * @buf: buffer to store read data
2452 * @oob_required: caller requires OOB data read to chip->oob_poi
2453 * @page: page number to read
2454 *
2455 * Returns -ENOTSUPP unconditionally.
2456 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002457int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2458 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002459{
2460 return -ENOTSUPP;
2461}
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002462
2463/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002464 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002465 * @chip: nand chip info structure
2466 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002467 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002468 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002469 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002470 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002471 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002472int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2473 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002474{
Boris Brezillonb9761682018-09-06 14:05:20 +02002475 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002476 int ret;
2477
Boris Brezillon25f815f2017-11-30 18:01:30 +01002478 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002479 if (ret)
2480 return ret;
2481
2482 if (oob_required) {
2483 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2484 false);
2485 if (ret)
2486 return ret;
2487 }
2488
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002489 return 0;
2490}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002491EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002492
2493/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002494 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002495 * @chip: nand chip info structure
2496 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002497 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002498 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002499 *
2500 * We need a special oob layout and handling even when OOB isn't used.
2501 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002502static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07002503 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002504{
Boris Brezillonb9761682018-09-06 14:05:20 +02002505 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08002506 int eccsize = chip->ecc.size;
2507 int eccbytes = chip->ecc.bytes;
2508 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002509 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002510
Boris Brezillon25f815f2017-11-30 18:01:30 +01002511 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2512 if (ret)
2513 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002514
2515 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002516 ret = nand_read_data_op(chip, buf, eccsize, false);
2517 if (ret)
2518 return ret;
2519
David Brownell52ff49d2009-03-04 12:01:36 -08002520 buf += eccsize;
2521
2522 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002523 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2524 false);
2525 if (ret)
2526 return ret;
2527
David Brownell52ff49d2009-03-04 12:01:36 -08002528 oob += chip->ecc.prepad;
2529 }
2530
Boris Brezillon97d90da2017-11-30 18:01:29 +01002531 ret = nand_read_data_op(chip, oob, eccbytes, false);
2532 if (ret)
2533 return ret;
2534
David Brownell52ff49d2009-03-04 12:01:36 -08002535 oob += eccbytes;
2536
2537 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002538 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2539 false);
2540 if (ret)
2541 return ret;
2542
David Brownell52ff49d2009-03-04 12:01:36 -08002543 oob += chip->ecc.postpad;
2544 }
2545 }
2546
2547 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002548 if (size) {
2549 ret = nand_read_data_op(chip, oob, size, false);
2550 if (ret)
2551 return ret;
2552 }
David Brownell52ff49d2009-03-04 12:01:36 -08002553
2554 return 0;
2555}
2556
2557/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002558 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002559 * @chip: nand chip info structure
2560 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002561 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002562 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002563 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002564static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
2565 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Boris Brezillonb9761682018-09-06 14:05:20 +02002567 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002568 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002569 int eccbytes = chip->ecc.bytes;
2570 int eccsteps = chip->ecc.steps;
2571 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002572 uint8_t *ecc_calc = chip->ecc.calc_buf;
2573 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002574 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002575
Boris Brezillonb9761682018-09-06 14:05:20 +02002576 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002577
2578 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002579 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002580
Boris Brezillon846031d2016-02-03 20:11:00 +01002581 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2582 chip->ecc.total);
2583 if (ret)
2584 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002585
2586 eccsteps = chip->ecc.steps;
2587 p = buf;
2588
2589 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2590 int stat;
2591
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002592 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07002593 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002594 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002595 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002596 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002597 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2598 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002599 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002600 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01002601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302604 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002605 * @chip: nand chip info structure
2606 * @data_offs: offset of requested data within the page
2607 * @readlen: data length
2608 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08002609 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01002610 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002611static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
2612 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01002613{
Boris Brezillonb9761682018-09-06 14:05:20 +02002614 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002615 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002616 uint8_t *p;
2617 int data_col_addr, i, gaps = 0;
2618 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
2619 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01002620 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07002621 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01002622 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01002623
Brian Norris7854d3f2011-06-23 14:12:08 -07002624 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01002625 start_step = data_offs / chip->ecc.size;
2626 end_step = (data_offs + readlen - 1) / chip->ecc.size;
2627 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10302628 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01002629
Brian Norris8b6e50c2011-05-25 14:59:01 -07002630 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01002631 datafrag_len = num_steps * chip->ecc.size;
2632 eccfrag_len = num_steps * chip->ecc.bytes;
2633
2634 data_col_addr = start_step * chip->ecc.size;
2635 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01002636 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01002637 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002638 if (ret)
2639 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002640
Brian Norris8b6e50c2011-05-25 14:59:01 -07002641 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01002642 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002643 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01002644
Brian Norris8b6e50c2011-05-25 14:59:01 -07002645 /*
2646 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07002647 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07002648 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002649 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
2650 if (ret)
2651 return ret;
2652
2653 if (oobregion.length < eccfrag_len)
2654 gaps = 1;
2655
Alexey Korolev3d459552008-05-15 17:23:18 +01002656 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002657 ret = nand_change_read_column_op(chip, mtd->writesize,
2658 chip->oob_poi, mtd->oobsize,
2659 false);
2660 if (ret)
2661 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002662 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002663 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002664 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07002665 * about buswidth alignment in read_buf.
2666 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002667 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01002668 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01002669 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002670 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01002671 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
2672 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002673 aligned_len++;
2674
Boris Brezillon97d90da2017-11-30 18:01:29 +01002675 ret = nand_change_read_column_op(chip,
2676 mtd->writesize + aligned_pos,
2677 &chip->oob_poi[aligned_pos],
2678 aligned_len, false);
2679 if (ret)
2680 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002681 }
2682
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002683 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01002684 chip->oob_poi, index, eccfrag_len);
2685 if (ret)
2686 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002687
2688 p = bufpoi + data_col_addr;
2689 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
2690 int stat;
2691
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002692 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002693 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002694 if (stat == -EBADMSG &&
2695 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2696 /* check for empty pages with bitflips */
2697 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002698 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002699 chip->ecc.bytes,
2700 NULL, 0,
2701 chip->ecc.strength);
2702 }
2703
Mike Dunn3f91e942012-04-25 12:06:09 -07002704 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002705 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002706 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01002707 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002708 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2709 }
Alexey Korolev3d459552008-05-15 17:23:18 +01002710 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002711 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01002712}
2713
2714/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002715 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002716 * @chip: nand chip info structure
2717 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002718 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002719 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002720 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002721 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002722 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002723static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
2724 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002725{
Boris Brezillonb9761682018-09-06 14:05:20 +02002726 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002727 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002728 int eccbytes = chip->ecc.bytes;
2729 int eccsteps = chip->ecc.steps;
2730 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002731 uint8_t *ecc_calc = chip->ecc.calc_buf;
2732 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002733 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002734
Boris Brezillon25f815f2017-11-30 18:01:30 +01002735 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2736 if (ret)
2737 return ret;
2738
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002739 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02002740 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002741
2742 ret = nand_read_data_op(chip, p, eccsize, false);
2743 if (ret)
2744 return ret;
2745
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002746 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002747 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002748
2749 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2750 if (ret)
2751 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002752
Boris Brezillon846031d2016-02-03 20:11:00 +01002753 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2754 chip->ecc.total);
2755 if (ret)
2756 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002757
2758 eccsteps = chip->ecc.steps;
2759 p = buf;
2760
2761 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2762 int stat;
2763
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002764 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002765 if (stat == -EBADMSG &&
2766 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2767 /* check for empty pages with bitflips */
2768 stat = nand_check_erased_ecc_chunk(p, eccsize,
2769 &ecc_code[i], eccbytes,
2770 NULL, 0,
2771 chip->ecc.strength);
2772 }
2773
Mike Dunn3f91e942012-04-25 12:06:09 -07002774 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002775 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002776 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002777 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002778 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2779 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002780 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002781 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002782}
2783
2784/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002785 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07002786 * @chip: nand chip info structure
2787 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002788 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002789 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002790 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002791 * Hardware ECC for large page chips, require OOB to be read first. For this
2792 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2793 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2794 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2795 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002796 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002797static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
2798 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002799{
Boris Brezillonb9761682018-09-06 14:05:20 +02002800 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002801 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002802 int eccbytes = chip->ecc.bytes;
2803 int eccsteps = chip->ecc.steps;
2804 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002805 uint8_t *ecc_code = chip->ecc.code_buf;
2806 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002807 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002808
2809 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01002810 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2811 if (ret)
2812 return ret;
2813
2814 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2815 if (ret)
2816 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002817
Boris Brezillon846031d2016-02-03 20:11:00 +01002818 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2819 chip->ecc.total);
2820 if (ret)
2821 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002822
2823 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2824 int stat;
2825
Boris Brezillonec476362018-09-06 14:05:17 +02002826 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002827
2828 ret = nand_read_data_op(chip, p, eccsize, false);
2829 if (ret)
2830 return ret;
2831
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002832 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002833
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002834 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002835 if (stat == -EBADMSG &&
2836 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2837 /* check for empty pages with bitflips */
2838 stat = nand_check_erased_ecc_chunk(p, eccsize,
2839 &ecc_code[i], eccbytes,
2840 NULL, 0,
2841 chip->ecc.strength);
2842 }
2843
Mike Dunn3f91e942012-04-25 12:06:09 -07002844 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002845 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002846 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002847 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002848 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2849 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002850 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002851 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002852}
2853
2854/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002855 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07002856 * @chip: nand chip info structure
2857 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002858 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002859 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002860 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002861 * The hw generator calculates the error syndrome automatically. Therefore we
2862 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002863 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002864static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
2865 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002866{
Boris Brezillonb9761682018-09-06 14:05:20 +02002867 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002868 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002869 int eccbytes = chip->ecc.bytes;
2870 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002871 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002872 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002873 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07002874 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002875
Boris Brezillon25f815f2017-11-30 18:01:30 +01002876 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2877 if (ret)
2878 return ret;
2879
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002880 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2881 int stat;
2882
Boris Brezillonec476362018-09-06 14:05:17 +02002883 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002884
2885 ret = nand_read_data_op(chip, p, eccsize, false);
2886 if (ret)
2887 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002888
2889 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002890 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2891 false);
2892 if (ret)
2893 return ret;
2894
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002895 oob += chip->ecc.prepad;
2896 }
2897
Boris Brezillonec476362018-09-06 14:05:17 +02002898 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002899
2900 ret = nand_read_data_op(chip, oob, eccbytes, false);
2901 if (ret)
2902 return ret;
2903
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002904 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002905
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002906 oob += eccbytes;
2907
2908 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002909 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2910 false);
2911 if (ret)
2912 return ret;
2913
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002914 oob += chip->ecc.postpad;
2915 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002916
2917 if (stat == -EBADMSG &&
2918 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2919 /* check for empty pages with bitflips */
2920 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2921 oob - eccpadbytes,
2922 eccpadbytes,
2923 NULL, 0,
2924 chip->ecc.strength);
2925 }
2926
2927 if (stat < 0) {
2928 mtd->ecc_stats.failed++;
2929 } else {
2930 mtd->ecc_stats.corrected += stat;
2931 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2932 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002933 }
2934
2935 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002936 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002937 if (i) {
2938 ret = nand_read_data_op(chip, oob, i, false);
2939 if (ret)
2940 return ret;
2941 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002942
Mike Dunn3f91e942012-04-25 12:06:09 -07002943 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002944}
2945
2946/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002947 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon08136212018-11-11 08:55:03 +01002948 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07002949 * @oob: oob destination address
2950 * @ops: oob ops structure
2951 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002952 */
Boris Brezillon08136212018-11-11 08:55:03 +01002953static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03002954 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002955{
Boris Brezillon08136212018-11-11 08:55:03 +01002956 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002957 int ret;
2958
Florian Fainellif8ac0412010-09-07 13:23:43 +02002959 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002960
Brian Norris0612b9d2011-08-30 18:45:40 -07002961 case MTD_OPS_PLACE_OOB:
2962 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002963 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2964 return oob + len;
2965
Boris Brezillon846031d2016-02-03 20:11:00 +01002966 case MTD_OPS_AUTO_OOB:
2967 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
2968 ops->ooboffs, len);
2969 BUG_ON(ret);
2970 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002971
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002972 default:
2973 BUG();
2974 }
2975 return NULL;
2976}
2977
2978/**
Brian Norrisba84fb52014-01-03 15:13:33 -08002979 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02002980 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08002981 * @retry_mode: the retry mode to use
2982 *
2983 * Some vendors supply a special command to shift the Vt threshold, to be used
2984 * when there are too many bitflips in a page (i.e., ECC error). After setting
2985 * a new threshold, the host should retry reading the page.
2986 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02002987static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08002988{
Brian Norrisba84fb52014-01-03 15:13:33 -08002989 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2990
2991 if (retry_mode >= chip->read_retries)
2992 return -EINVAL;
2993
2994 if (!chip->setup_read_retry)
2995 return -EOPNOTSUPP;
2996
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02002997 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08002998}
2999
Boris Brezillon85e08e52018-07-27 09:44:17 +02003000static void nand_wait_readrdy(struct nand_chip *chip)
3001{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003002 const struct nand_sdr_timings *sdr;
3003
Boris Brezillon85e08e52018-07-27 09:44:17 +02003004 if (!(chip->options & NAND_NEED_READRDY))
3005 return;
3006
Boris Brezillon52f05b62018-07-27 09:44:18 +02003007 sdr = nand_get_sdr_timings(&chip->data_interface);
3008 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003009}
3010
Brian Norrisba84fb52014-01-03 15:13:33 -08003011/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003012 * nand_do_read_ops - [INTERN] Read data with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003013 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003014 * @from: offset to read from
3015 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003016 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003017 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003018 */
Boris Brezillon08136212018-11-11 08:55:03 +01003019static int nand_do_read_ops(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003020 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003021{
Brian Norrise47f3db2012-05-02 10:14:56 -07003022 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris Brezillon08136212018-11-11 08:55:03 +01003023 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003024 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003025 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003026 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003027 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003028
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003029 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003030 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003031 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003032 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003033 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003035 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003036 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003038 realpage = (int)(from >> chip->page_shift);
3039 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003041 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003043 buf = ops->datbuf;
3044 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003045 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003046
Florian Fainellif8ac0412010-09-07 13:23:43 +02003047 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003048 unsigned int ecc_failures = mtd->ecc_stats.failed;
3049
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003050 bytes = min(mtd->writesize - col, readlen);
3051 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003052
Kamal Dasu66507c72014-05-01 20:51:19 -04003053 if (!aligned)
3054 use_bufpoi = 1;
3055 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003056 use_bufpoi = !virt_addr_valid(buf) ||
3057 !IS_ALIGNED((unsigned long)buf,
3058 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003059 else
3060 use_bufpoi = 0;
3061
Brian Norris8b6e50c2011-05-25 14:59:01 -07003062 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003063 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003064 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003065
3066 if (use_bufpoi && aligned)
3067 pr_debug("%s: using read bounce buffer for buf@%p\n",
3068 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Brian Norrisba84fb52014-01-03 15:13:33 -08003070read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003071 /*
3072 * Now read the page into the buffer. Absent an error,
3073 * the read methods return max bitflips per ecc step.
3074 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003075 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003076 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003077 oob_required,
3078 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003079 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3080 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003081 ret = chip->ecc.read_subpage(chip, col, bytes,
3082 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003083 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003084 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003085 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003086 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003087 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003088 /* Invalidate page cache */
3089 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003090 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003091 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003092
3093 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003094 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003095 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003096 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003097 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003098 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003099 chip->pagebuf_bitflips = ret;
3100 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003101 /* Invalidate page cache */
3102 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003103 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003104 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003106
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003107 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003108 int toread = min(oobreadlen, max_oobsize);
3109
3110 if (toread) {
Boris Brezillon08136212018-11-11 08:55:03 +01003111 oob = nand_transfer_oob(chip, oob, ops,
3112 toread);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003113 oobreadlen -= toread;
3114 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003115 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003116
Boris Brezillon85e08e52018-07-27 09:44:17 +02003117 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003118
Brian Norrisba84fb52014-01-03 15:13:33 -08003119 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003120 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003121 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003122 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003123 retry_mode);
3124 if (ret < 0)
3125 break;
3126
3127 /* Reset failures; retry */
3128 mtd->ecc_stats.failed = ecc_failures;
3129 goto read_retry;
3130 } else {
3131 /* No more retry modes; real failure */
3132 ecc_fail = true;
3133 }
3134 }
3135
3136 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003137 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003138 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003139 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003140 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003141 max_bitflips = max_t(unsigned int, max_bitflips,
3142 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003145 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003146
Brian Norrisba84fb52014-01-03 15:13:33 -08003147 /* Reset to retry mode 0 */
3148 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003149 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003150 if (ret < 0)
3151 break;
3152 retry_mode = 0;
3153 }
3154
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003155 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003156 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Brian Norris8b6e50c2011-05-25 14:59:01 -07003158 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 col = 0;
3160 /* Increment page address */
3161 realpage++;
3162
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003163 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 /* Check, if we cross a chip boundary */
3165 if (!page) {
3166 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003167 chip->select_chip(chip, -1);
3168 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003171 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003173 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003174 if (oob)
3175 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Mike Dunn3f91e942012-04-25 12:06:09 -07003177 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003178 return ret;
3179
Brian Norrisb72f3df2013-12-03 11:04:14 -08003180 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003181 return -EBADMSG;
3182
Mike Dunnedbc45402012-04-25 12:06:11 -07003183 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003184}
3185
3186/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003187 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003188 * @chip: nand chip info structure
3189 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003190 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003191int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003192{
Boris Brezillonb9761682018-09-06 14:05:20 +02003193 struct mtd_info *mtd = nand_to_mtd(chip);
3194
Boris Brezillon97d90da2017-11-30 18:01:29 +01003195 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003196}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003197EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003198
3199/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003200 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003201 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003202 * @chip: nand chip info structure
3203 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003204 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003205static int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003206{
Boris Brezillonb9761682018-09-06 14:05:20 +02003207 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003208 int length = mtd->oobsize;
3209 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3210 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003211 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003212 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003213
Boris Brezillon97d90da2017-11-30 18:01:29 +01003214 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3215 if (ret)
3216 return ret;
3217
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003218 for (i = 0; i < chip->ecc.steps; i++) {
3219 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003220 int ret;
3221
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003222 pos = eccsize + i * (eccsize + chunk);
3223 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003224 ret = nand_change_read_column_op(chip, pos,
3225 NULL, 0,
3226 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003227 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003228 ret = nand_read_page_op(chip, page, pos, NULL,
3229 0);
3230
3231 if (ret)
3232 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003233 } else
3234 sndrnd = 1;
3235 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003236
3237 ret = nand_read_data_op(chip, bufpoi, toread, false);
3238 if (ret)
3239 return ret;
3240
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003241 bufpoi += toread;
3242 length -= toread;
3243 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003244 if (length > 0) {
3245 ret = nand_read_data_op(chip, bufpoi, length, false);
3246 if (ret)
3247 return ret;
3248 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003249
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003250 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003251}
3252
3253/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003254 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003255 * @chip: nand chip info structure
3256 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003257 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003258int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003259{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003260 struct mtd_info *mtd = nand_to_mtd(chip);
3261
Boris Brezillon97d90da2017-11-30 18:01:29 +01003262 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3263 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003264}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003265EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003266
3267/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003268 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003269 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003270 * @chip: nand chip info structure
3271 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003272 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003273static int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003274{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003275 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003276 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3277 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003278 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003279 const uint8_t *bufpoi = chip->oob_poi;
3280
3281 /*
3282 * data-ecc-data-ecc ... ecc-oob
3283 * or
3284 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3285 */
3286 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3287 pos = steps * (eccsize + chunk);
3288 steps = 0;
3289 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003290 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003291
Boris Brezillon97d90da2017-11-30 18:01:29 +01003292 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3293 if (ret)
3294 return ret;
3295
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003296 for (i = 0; i < steps; i++) {
3297 if (sndcmd) {
3298 if (mtd->writesize <= 512) {
3299 uint32_t fill = 0xFFFFFFFF;
3300
3301 len = eccsize;
3302 while (len > 0) {
3303 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003304
3305 ret = nand_write_data_op(chip, &fill,
3306 num, false);
3307 if (ret)
3308 return ret;
3309
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003310 len -= num;
3311 }
3312 } else {
3313 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003314 ret = nand_change_write_column_op(chip, pos,
3315 NULL, 0,
3316 false);
3317 if (ret)
3318 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003319 }
3320 } else
3321 sndcmd = 1;
3322 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003323
3324 ret = nand_write_data_op(chip, bufpoi, len, false);
3325 if (ret)
3326 return ret;
3327
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003328 bufpoi += len;
3329 length -= len;
3330 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003331 if (length > 0) {
3332 ret = nand_write_data_op(chip, bufpoi, length, false);
3333 if (ret)
3334 return ret;
3335 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003336
Boris Brezillon97d90da2017-11-30 18:01:29 +01003337 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003338}
3339
3340/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003341 * nand_do_read_oob - [INTERN] NAND read out-of-band
Boris Brezillon08136212018-11-11 08:55:03 +01003342 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003343 * @from: offset to read from
3344 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003346 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 */
Boris Brezillon08136212018-11-11 08:55:03 +01003348static int nand_do_read_oob(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003349 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
Boris Brezillon08136212018-11-11 08:55:03 +01003351 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003352 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003353 int page, realpage, chipnr;
Brian Norris041e4572011-06-23 16:45:24 -07003354 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003355 int readlen = ops->ooblen;
3356 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003357 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003358 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Brian Norris289c0522011-07-19 10:06:09 -07003360 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303361 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Brian Norris041e4572011-06-23 16:45:24 -07003363 stats = mtd->ecc_stats;
3364
Boris BREZILLON29f10582016-03-07 10:46:52 +01003365 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003366
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003367 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003368 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003370 /* Shift to get page */
3371 realpage = (int)(from >> chip->page_shift);
3372 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Florian Fainellif8ac0412010-09-07 13:23:43 +02003374 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003375 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003376 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003377 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003378 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003379
3380 if (ret < 0)
3381 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003382
3383 len = min(len, readlen);
Boris Brezillon08136212018-11-11 08:55:03 +01003384 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003385
Boris Brezillon85e08e52018-07-27 09:44:17 +02003386 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003387
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003388 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3389
Vitaly Wool70145682006-11-03 18:20:38 +03003390 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003391 if (!readlen)
3392 break;
3393
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003394 /* Increment page address */
3395 realpage++;
3396
3397 page = realpage & chip->pagemask;
3398 /* Check, if we cross a chip boundary */
3399 if (!page) {
3400 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003401 chip->select_chip(chip, -1);
3402 chip->select_chip(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003405 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003407 ops->oobretlen = ops->ooblen - readlen;
3408
3409 if (ret < 0)
3410 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003411
3412 if (mtd->ecc_stats.failed - stats.failed)
3413 return -EBADMSG;
3414
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003415 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
3418/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003419 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003420 * @mtd: MTD device structure
3421 * @from: offset to read from
3422 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003424 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003426static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3427 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428{
Boris Brezillon08136212018-11-11 08:55:03 +01003429 struct nand_chip *chip = mtd_to_nand(mtd);
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003430 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003431
3432 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003434 if (ops->mode != MTD_OPS_PLACE_OOB &&
3435 ops->mode != MTD_OPS_AUTO_OOB &&
3436 ops->mode != MTD_OPS_RAW)
3437 return -ENOTSUPP;
3438
Boris Brezillon08136212018-11-11 08:55:03 +01003439 nand_get_device(chip, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003441 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01003442 ret = nand_do_read_oob(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003443 else
Boris Brezillon08136212018-11-11 08:55:03 +01003444 ret = nand_do_read_ops(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003445
Boris Brezillon08136212018-11-11 08:55:03 +01003446 nand_release_device(chip);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003447 return ret;
3448}
3449
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003450/**
3451 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003452 * @chip: nand chip info structure
3453 * @buf: data buffer
3454 * @oob_required: must write chip->oob_poi to OOB
3455 * @page: page number to write
3456 *
3457 * Returns -ENOTSUPP unconditionally.
3458 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003459int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3460 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003461{
3462 return -ENOTSUPP;
3463}
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003464
3465/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003466 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003467 * @chip: nand chip info structure
3468 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003469 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003470 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003471 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003472 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003473 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003474int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3475 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003476{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003477 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003478 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003479
Boris Brezillon25f815f2017-11-30 18:01:30 +01003480 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003481 if (ret)
3482 return ret;
3483
3484 if (oob_required) {
3485 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3486 false);
3487 if (ret)
3488 return ret;
3489 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003490
Boris Brezillon25f815f2017-11-30 18:01:30 +01003491 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003493EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003495/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003496 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003497 * @chip: nand chip info structure
3498 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003499 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003500 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003501 *
3502 * We need a special oob layout and handling even when ECC isn't checked.
3503 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003504static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003505 const uint8_t *buf, int oob_required,
3506 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003507{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003508 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003509 int eccsize = chip->ecc.size;
3510 int eccbytes = chip->ecc.bytes;
3511 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003512 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003513
Boris Brezillon25f815f2017-11-30 18:01:30 +01003514 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3515 if (ret)
3516 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003517
3518 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003519 ret = nand_write_data_op(chip, buf, eccsize, false);
3520 if (ret)
3521 return ret;
3522
David Brownell52ff49d2009-03-04 12:01:36 -08003523 buf += eccsize;
3524
3525 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003526 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3527 false);
3528 if (ret)
3529 return ret;
3530
David Brownell52ff49d2009-03-04 12:01:36 -08003531 oob += chip->ecc.prepad;
3532 }
3533
Boris Brezillon97d90da2017-11-30 18:01:29 +01003534 ret = nand_write_data_op(chip, oob, eccbytes, false);
3535 if (ret)
3536 return ret;
3537
David Brownell52ff49d2009-03-04 12:01:36 -08003538 oob += eccbytes;
3539
3540 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003541 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3542 false);
3543 if (ret)
3544 return ret;
3545
David Brownell52ff49d2009-03-04 12:01:36 -08003546 oob += chip->ecc.postpad;
3547 }
3548 }
3549
3550 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003551 if (size) {
3552 ret = nand_write_data_op(chip, oob, size, false);
3553 if (ret)
3554 return ret;
3555 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003556
Boris Brezillon25f815f2017-11-30 18:01:30 +01003557 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003558}
3559/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003560 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003561 * @chip: nand chip info structure
3562 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003563 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003564 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003565 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003566static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
3567 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003568{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003569 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003570 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003571 int eccbytes = chip->ecc.bytes;
3572 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003573 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003574 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003575
Brian Norris7854d3f2011-06-23 14:12:08 -07003576 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003577 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003578 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003579
Boris Brezillon846031d2016-02-03 20:11:00 +01003580 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3581 chip->ecc.total);
3582 if (ret)
3583 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003584
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003585 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003586}
3587
3588/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003589 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003590 * @chip: nand chip info structure
3591 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003592 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003593 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003594 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003595static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
3596 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003597{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003598 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003599 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003600 int eccbytes = chip->ecc.bytes;
3601 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003602 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003603 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003604
Boris Brezillon25f815f2017-11-30 18:01:30 +01003605 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3606 if (ret)
3607 return ret;
3608
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003609 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003610 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003611
3612 ret = nand_write_data_op(chip, p, eccsize, false);
3613 if (ret)
3614 return ret;
3615
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003616 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003617 }
3618
Boris Brezillon846031d2016-02-03 20:11:00 +01003619 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3620 chip->ecc.total);
3621 if (ret)
3622 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003623
Boris Brezillon97d90da2017-11-30 18:01:29 +01003624 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3625 if (ret)
3626 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003627
Boris Brezillon25f815f2017-11-30 18:01:30 +01003628 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003629}
3630
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303631
3632/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08003633 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303634 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07003635 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303636 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07003637 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303638 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003639 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303640 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003641static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
3642 uint32_t data_len, const uint8_t *buf,
3643 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303644{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003645 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303646 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003647 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303648 int ecc_size = chip->ecc.size;
3649 int ecc_bytes = chip->ecc.bytes;
3650 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303651 uint32_t start_step = offset / ecc_size;
3652 uint32_t end_step = (offset + data_len - 1) / ecc_size;
3653 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01003654 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303655
Boris Brezillon25f815f2017-11-30 18:01:30 +01003656 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3657 if (ret)
3658 return ret;
3659
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303660 for (step = 0; step < ecc_steps; step++) {
3661 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02003662 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303663
3664 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003665 ret = nand_write_data_op(chip, buf, ecc_size, false);
3666 if (ret)
3667 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303668
3669 /* mask ECC of un-touched subpages by padding 0xFF */
3670 if ((step < start_step) || (step > end_step))
3671 memset(ecc_calc, 0xff, ecc_bytes);
3672 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003673 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303674
3675 /* mask OOB of un-touched subpages by padding 0xFF */
3676 /* if oob_required, preserve OOB metadata of written subpage */
3677 if (!oob_required || (step < start_step) || (step > end_step))
3678 memset(oob_buf, 0xff, oob_bytes);
3679
Brian Norrisd6a950802013-08-08 17:16:36 -07003680 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303681 ecc_calc += ecc_bytes;
3682 oob_buf += oob_bytes;
3683 }
3684
3685 /* copy calculated ECC for whole page to chip->buffer->oob */
3686 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003687 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01003688 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3689 chip->ecc.total);
3690 if (ret)
3691 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303692
3693 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003694 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3695 if (ret)
3696 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303697
Boris Brezillon25f815f2017-11-30 18:01:30 +01003698 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303699}
3700
3701
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003702/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003703 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003704 * @chip: nand chip info structure
3705 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003706 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003707 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003708 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003709 * The hw generator calculates the error syndrome automatically. Therefore we
3710 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003711 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003712static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
3713 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003714{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003715 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003716 int i, eccsize = chip->ecc.size;
3717 int eccbytes = chip->ecc.bytes;
3718 int eccsteps = chip->ecc.steps;
3719 const uint8_t *p = buf;
3720 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003721 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003722
Boris Brezillon25f815f2017-11-30 18:01:30 +01003723 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3724 if (ret)
3725 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003726
3727 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003728 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003729
3730 ret = nand_write_data_op(chip, p, eccsize, false);
3731 if (ret)
3732 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003733
3734 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003735 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3736 false);
3737 if (ret)
3738 return ret;
3739
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003740 oob += chip->ecc.prepad;
3741 }
3742
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003743 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003744
3745 ret = nand_write_data_op(chip, oob, eccbytes, false);
3746 if (ret)
3747 return ret;
3748
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003749 oob += eccbytes;
3750
3751 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003752 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3753 false);
3754 if (ret)
3755 return ret;
3756
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003757 oob += chip->ecc.postpad;
3758 }
3759 }
3760
3761 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003762 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003763 if (i) {
3764 ret = nand_write_data_op(chip, oob, i, false);
3765 if (ret)
3766 return ret;
3767 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003768
Boris Brezillon25f815f2017-11-30 18:01:30 +01003769 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003770}
3771
3772/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003773 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07003774 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303775 * @offset: address offset within the page
3776 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07003777 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07003778 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07003779 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003780 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003781 */
Boris Brezillon08136212018-11-11 08:55:03 +01003782static int nand_write_page(struct nand_chip *chip, uint32_t offset,
3783 int data_len, const uint8_t *buf, int oob_required,
3784 int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003785{
Boris Brezillon08136212018-11-11 08:55:03 +01003786 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303787 int status, subpage;
3788
3789 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3790 chip->ecc.write_subpage)
3791 subpage = offset || (data_len < mtd->writesize);
3792 else
3793 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003794
David Woodhouse956e9442006-09-25 17:12:39 +01003795 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003796 status = chip->ecc.write_page_raw(chip, buf, oob_required,
3797 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303798 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003799 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
3800 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003801 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003802 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08003803
3804 if (status < 0)
3805 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003806
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003807 return 0;
3808}
3809
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003810/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003811 * nand_fill_oob - [INTERN] Transfer client buffer to oob
Boris Brezillon08136212018-11-11 08:55:03 +01003812 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003813 * @oob: oob data buffer
3814 * @len: oob data write length
3815 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003816 */
Boris Brezillon08136212018-11-11 08:55:03 +01003817static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003818 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003819{
Boris Brezillon08136212018-11-11 08:55:03 +01003820 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003821 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003822
3823 /*
3824 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3825 * data from a previous OOB read.
3826 */
3827 memset(chip->oob_poi, 0xff, mtd->oobsize);
3828
Florian Fainellif8ac0412010-09-07 13:23:43 +02003829 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003830
Brian Norris0612b9d2011-08-30 18:45:40 -07003831 case MTD_OPS_PLACE_OOB:
3832 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003833 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3834 return oob + len;
3835
Boris Brezillon846031d2016-02-03 20:11:00 +01003836 case MTD_OPS_AUTO_OOB:
3837 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
3838 ops->ooboffs, len);
3839 BUG_ON(ret);
3840 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003841
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003842 default:
3843 BUG();
3844 }
3845 return NULL;
3846}
3847
Florian Fainellif8ac0412010-09-07 13:23:43 +02003848#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003849
3850/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003851 * nand_do_write_ops - [INTERN] NAND write with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003852 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003853 * @to: offset to write to
3854 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003855 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003856 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003857 */
Boris Brezillon08136212018-11-11 08:55:03 +01003858static int nand_do_write_ops(struct nand_chip *chip, loff_t to,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003859 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003860{
Boris Brezillon08136212018-11-11 08:55:03 +01003861 struct mtd_info *mtd = nand_to_mtd(chip);
Corentin Labbe73600b62017-09-02 10:49:38 +02003862 int chipnr, realpage, page, column;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003863 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02003864
3865 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003866 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003867
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003868 uint8_t *oob = ops->oobbuf;
3869 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303870 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07003871 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003872
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003873 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003874 if (!writelen)
3875 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003876
Brian Norris8b6e50c2011-05-25 14:59:01 -07003877 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003878 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003879 pr_notice("%s: attempt to write non page aligned data\n",
3880 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003881 return -EINVAL;
3882 }
3883
Thomas Gleixner29072b92006-09-28 15:38:36 +02003884 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003885
Thomas Gleixner6a930962006-06-28 00:11:45 +02003886 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003887 chip->select_chip(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02003888
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003889 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01003890 if (nand_check_wp(chip)) {
Huang Shijieb0bb6902012-11-19 14:43:29 +08003891 ret = -EIO;
3892 goto err_out;
3893 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003894
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003895 realpage = (int)(to >> chip->page_shift);
3896 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003897
3898 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07003899 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3900 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003901 chip->pagebuf = -1;
3902
Maxim Levitsky782ce792010-02-22 20:39:36 +02003903 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003904 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3905 ret = -EINVAL;
3906 goto err_out;
3907 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02003908
Florian Fainellif8ac0412010-09-07 13:23:43 +02003909 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003910 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003911 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003912 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02003913 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02003914
Kamal Dasu66507c72014-05-01 20:51:19 -04003915 if (part_pagewr)
3916 use_bufpoi = 1;
3917 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003918 use_bufpoi = !virt_addr_valid(buf) ||
3919 !IS_ALIGNED((unsigned long)buf,
3920 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003921 else
3922 use_bufpoi = 0;
3923
3924 /* Partial page write?, or need to use bounce buffer */
3925 if (use_bufpoi) {
3926 pr_debug("%s: using write bounce buffer for buf@%p\n",
3927 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04003928 if (part_pagewr)
3929 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02003930 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003931 memset(chip->data_buf, 0xff, mtd->writesize);
3932 memcpy(&chip->data_buf[column], buf, bytes);
3933 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003934 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003935
Maxim Levitsky782ce792010-02-22 20:39:36 +02003936 if (unlikely(oob)) {
3937 size_t len = min(oobwritelen, oobmaxlen);
Boris Brezillon08136212018-11-11 08:55:03 +01003938 oob = nand_fill_oob(chip, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003939 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003940 } else {
3941 /* We still need to erase leftover OOB data */
3942 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003943 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003944
Boris Brezillon08136212018-11-11 08:55:03 +01003945 ret = nand_write_page(chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02003946 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003947 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003948 if (ret)
3949 break;
3950
3951 writelen -= bytes;
3952 if (!writelen)
3953 break;
3954
Thomas Gleixner29072b92006-09-28 15:38:36 +02003955 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003956 buf += bytes;
3957 realpage++;
3958
3959 page = realpage & chip->pagemask;
3960 /* Check, if we cross a chip boundary */
3961 if (!page) {
3962 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003963 chip->select_chip(chip, -1);
3964 chip->select_chip(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003965 }
3966 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003967
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003968 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03003969 if (unlikely(oob))
3970 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003971
3972err_out:
Boris Brezillon758b56f2018-09-06 14:05:24 +02003973 chip->select_chip(chip, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003974 return ret;
3975}
3976
3977/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003978 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003979 * @mtd: MTD device structure
3980 * @to: offset to write to
3981 * @len: number of bytes to write
3982 * @retlen: pointer to variable to store the number of written bytes
3983 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003984 *
3985 * NAND write with ECC. Used when performing writes in interrupt context, this
3986 * may for example be called by mtdoops when writing an oops while in panic.
3987 */
3988static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3989 size_t *retlen, const uint8_t *buf)
3990{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003991 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05003992 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07003993 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003994 int ret;
3995
Brian Norris8b6e50c2011-05-25 14:59:01 -07003996 /* Grab the device */
Boris Brezillon08136212018-11-11 08:55:03 +01003997 panic_nand_get_device(chip, FL_WRITING);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003998
Boris Brezillon758b56f2018-09-06 14:05:24 +02003999 chip->select_chip(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004000
4001 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004002 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004003
Brian Norris0ec56dc2015-02-28 02:02:30 -08004004 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004005 ops.len = len;
4006 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004007 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004008
Boris Brezillon08136212018-11-11 08:55:03 +01004009 ret = nand_do_write_ops(chip, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004010
Brian Norris4a89ff82011-08-30 18:45:45 -07004011 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004012 return ret;
4013}
4014
4015/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004016 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Boris Brezillon08136212018-11-11 08:55:03 +01004017 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004018 * @to: offset to write to
4019 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004020 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004021 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004022 */
Boris Brezillon08136212018-11-11 08:55:03 +01004023static int nand_do_write_oob(struct nand_chip *chip, loff_t to,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004024 struct mtd_oob_ops *ops)
4025{
Boris Brezillon08136212018-11-11 08:55:03 +01004026 struct mtd_info *mtd = nand_to_mtd(chip);
Adrian Hunter03736152007-01-31 17:58:29 +02004027 int chipnr, page, status, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
Brian Norris289c0522011-07-19 10:06:09 -07004029 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304030 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
Boris BREZILLON29f10582016-03-07 10:46:52 +01004032 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004033
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004035 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004036 pr_debug("%s: attempt to write past end of page\n",
4037 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 return -EINVAL;
4039 }
4040
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004041 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004042
4043 /*
4044 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4045 * of my DiskOnChip 2000 test units) will clear the whole data page too
4046 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4047 * it in the doc2000 driver in August 1999. dwmw2.
4048 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004049 nand_reset(chip, chipnr);
4050
Boris Brezillon758b56f2018-09-06 14:05:24 +02004051 chip->select_chip(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004052
4053 /* Shift to get page */
4054 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
4056 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01004057 if (nand_check_wp(chip)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02004058 chip->select_chip(chip, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004059 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004060 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004063 if (page == chip->pagebuf)
4064 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
Boris Brezillon08136212018-11-11 08:55:03 +01004066 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004067
Brian Norris0612b9d2011-08-30 18:45:40 -07004068 if (ops->mode == MTD_OPS_RAW)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004069 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
Brian Norris9ce244b2011-08-30 18:45:37 -07004070 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004071 status = chip->ecc.write_oob(chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004072
Boris Brezillon758b56f2018-09-06 14:05:24 +02004073 chip->select_chip(chip, -1);
Huang Shijieb0bb6902012-11-19 14:43:29 +08004074
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004075 if (status)
4076 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Vitaly Wool70145682006-11-03 18:20:38 +03004078 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004080 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004083/**
4084 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004085 * @mtd: MTD device structure
4086 * @to: offset to write to
4087 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004088 */
4089static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4090 struct mtd_oob_ops *ops)
4091{
Boris Brezillon08136212018-11-11 08:55:03 +01004092 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004093 int ret = -ENOTSUPP;
4094
4095 ops->retlen = 0;
4096
Boris Brezillon08136212018-11-11 08:55:03 +01004097 nand_get_device(chip, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004098
Florian Fainellif8ac0412010-09-07 13:23:43 +02004099 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004100 case MTD_OPS_PLACE_OOB:
4101 case MTD_OPS_AUTO_OOB:
4102 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004103 break;
4104
4105 default:
4106 goto out;
4107 }
4108
4109 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01004110 ret = nand_do_write_oob(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004111 else
Boris Brezillon08136212018-11-11 08:55:03 +01004112 ret = nand_do_write_ops(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004113
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004114out:
Boris Brezillon08136212018-11-11 08:55:03 +01004115 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 return ret;
4117}
4118
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119/**
Brian Norris49c50b92014-05-06 16:02:19 -07004120 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004121 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004122 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 *
Brian Norris49c50b92014-05-06 16:02:19 -07004124 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004126static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004128 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004129
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004131 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004132
Boris Brezillon97d90da2017-11-30 18:01:29 +01004133 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134}
4135
4136/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004138 * @mtd: MTD device structure
4139 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004141 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004143static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004145 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004147
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004149 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004150 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004151 * @instr: erase instruction
4152 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004154 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004156int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004157 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158{
Adrian Hunter69423d92008-12-10 13:37:21 +00004159 int page, status, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004160 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Brian Norris289c0522011-07-19 10:06:09 -07004162 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4163 __func__, (unsigned long long)instr->addr,
4164 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Boris Brezillon08136212018-11-11 08:55:03 +01004166 if (check_offs_len(chip, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 /* Grab the lock and see if the device is available */
Boris Brezillon08136212018-11-11 08:55:03 +01004170 nand_get_device(chip, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004173 page = (int)(instr->addr >> chip->page_shift);
4174 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
4176 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004177 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
4179 /* Select the NAND device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004180 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01004183 if (nand_check_wp(chip)) {
Brian Norris289c0522011-07-19 10:06:09 -07004184 pr_debug("%s: device is write protected!\n",
4185 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004186 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 goto erase_exit;
4188 }
4189
4190 /* Loop through the pages */
4191 len = instr->len;
4192
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004194 /* Check if we have a bad block, we do not erase bad blocks! */
Boris Brezillon08136212018-11-11 08:55:03 +01004195 if (nand_block_checkbad(chip, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304196 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004197 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4198 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004199 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 goto erase_exit;
4201 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004202
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004203 /*
4204 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004205 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004206 */
4207 if (page <= chip->pagebuf && chip->pagebuf <
4208 (page + pages_per_block))
4209 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Boris Brezillonf9ebd1b2018-09-07 00:38:39 +02004211 if (chip->legacy.erase)
4212 status = chip->legacy.erase(chip,
4213 page & chip->pagemask);
4214 else
4215 status = single_erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
4217 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004218 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004219 pr_debug("%s: failed erase, page 0x%08x\n",
4220 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004221 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004222 instr->fail_addr =
4223 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 goto erase_exit;
4225 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004226
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004228 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 page += pages_per_block;
4230
4231 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004232 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004234 chip->select_chip(chip, -1);
4235 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 }
4237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004239 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004240erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004243 chip->select_chip(chip, -1);
Boris Brezillon08136212018-11-11 08:55:03 +01004244 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 /* Return more or less happy */
4247 return ret;
4248}
4249
4250/**
4251 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004252 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004254 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004256static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257{
Boris Brezillon08136212018-11-11 08:55:03 +01004258 struct nand_chip *chip = mtd_to_nand(mtd);
4259
Brian Norris289c0522011-07-19 10:06:09 -07004260 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
4262 /* Grab the lock and see if the device is available */
Boris Brezillon08136212018-11-11 08:55:03 +01004263 nand_get_device(chip, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 /* Release it and go back */
Boris Brezillon08136212018-11-11 08:55:03 +01004265 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266}
4267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004269 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004270 * @mtd: MTD device structure
4271 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004273static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304275 struct nand_chip *chip = mtd_to_nand(mtd);
4276 int chipnr = (int)(offs >> chip->chip_shift);
4277 int ret;
4278
4279 /* Select the NAND device */
Boris Brezillon08136212018-11-11 08:55:03 +01004280 nand_get_device(chip, FL_READING);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004281 chip->select_chip(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304282
Boris Brezillon08136212018-11-11 08:55:03 +01004283 ret = nand_block_checkbad(chip, offs, 0);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304284
Boris Brezillon758b56f2018-09-06 14:05:24 +02004285 chip->select_chip(chip, -1);
Boris Brezillon08136212018-11-11 08:55:03 +01004286 nand_release_device(chip);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304287
4288 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289}
4290
4291/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004292 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004293 * @mtd: MTD device structure
4294 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004296static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 int ret;
4299
Florian Fainellif8ac0412010-09-07 13:23:43 +02004300 ret = nand_block_isbad(mtd, ofs);
4301 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004302 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 if (ret > 0)
4304 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004305 return ret;
4306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
Boris Brezillon08136212018-11-11 08:55:03 +01004308 return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309}
4310
4311/**
Zach Brown56718422017-01-10 13:30:20 -06004312 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4313 * @mtd: MTD device structure
4314 * @ofs: offset relative to mtd start
4315 * @len: length of mtd
4316 */
4317static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4318{
4319 struct nand_chip *chip = mtd_to_nand(mtd);
4320 u32 part_start_block;
4321 u32 part_end_block;
4322 u32 part_start_die;
4323 u32 part_end_die;
4324
4325 /*
4326 * max_bb_per_die and blocks_per_die used to determine
4327 * the maximum bad block count.
4328 */
4329 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4330 return -ENOTSUPP;
4331
4332 /* Get the start and end of the partition in erase blocks. */
4333 part_start_block = mtd_div_by_eb(ofs, mtd);
4334 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4335
4336 /* Get the start and end LUNs of the partition. */
4337 part_start_die = part_start_block / chip->blocks_per_die;
4338 part_end_die = part_end_block / chip->blocks_per_die;
4339
4340 /*
4341 * Look up the bad blocks per unit and multiply by the number of units
4342 * that the partition spans.
4343 */
4344 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4345}
4346
4347/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004348 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004349 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004350 */
4351static int nand_suspend(struct mtd_info *mtd)
4352{
Boris Brezillon08136212018-11-11 08:55:03 +01004353 return nand_get_device(mtd_to_nand(mtd), FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004354}
4355
4356/**
4357 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004358 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004359 */
4360static void nand_resume(struct mtd_info *mtd)
4361{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004362 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004363
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004364 if (chip->state == FL_PM_SUSPENDED)
Boris Brezillon08136212018-11-11 08:55:03 +01004365 nand_release_device(chip);
Vitaly Wool962034f2005-09-15 14:58:53 +01004366 else
Brian Norrisd0370212011-07-19 10:06:08 -07004367 pr_err("%s called for a chip which is not in suspended state\n",
4368 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004369}
4370
Scott Branden72ea4032014-11-20 11:18:05 -08004371/**
4372 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4373 * prevent further operations
4374 * @mtd: MTD device structure
4375 */
4376static void nand_shutdown(struct mtd_info *mtd)
4377{
Boris Brezillon08136212018-11-11 08:55:03 +01004378 nand_get_device(mtd_to_nand(mtd), FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004379}
4380
Brian Norris8b6e50c2011-05-25 14:59:01 -07004381/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004382static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004383{
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004384 nand_legacy_set_defaults(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004385
4386 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004387 chip->controller = &chip->dummy_controller;
4388 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004389 }
4390
Masahiro Yamada477544c2017-03-30 17:15:05 +09004391 if (!chip->buf_align)
4392 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004393}
4394
Brian Norris8b6e50c2011-05-25 14:59:01 -07004395/* Sanitize ONFI strings so we can safely print them */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004396void sanitize_string(uint8_t *s, size_t len)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004397{
4398 ssize_t i;
4399
Brian Norris8b6e50c2011-05-25 14:59:01 -07004400 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004401 s[len - 1] = 0;
4402
Brian Norris8b6e50c2011-05-25 14:59:01 -07004403 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004404 for (i = 0; i < len - 1; i++) {
4405 if (s[i] < ' ' || s[i] > 127)
4406 s[i] = '?';
4407 }
4408
Brian Norris8b6e50c2011-05-25 14:59:01 -07004409 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004410 strim(s);
4411}
4412
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004413/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07004414 * nand_id_has_period - Check if an ID string has a given wraparound period
4415 * @id_data: the ID string
4416 * @arrlen: the length of the @id_data array
4417 * @period: the period of repitition
4418 *
4419 * Check if an ID string is repeated within a given sequence of bytes at
4420 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08004421 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07004422 * if the repetition has a period of @period; otherwise, returns zero.
4423 */
4424static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4425{
4426 int i, j;
4427 for (i = 0; i < period; i++)
4428 for (j = i + period; j < arrlen; j += period)
4429 if (id_data[i] != id_data[j])
4430 return 0;
4431 return 1;
4432}
4433
4434/*
4435 * nand_id_len - Get the length of an ID string returned by CMD_READID
4436 * @id_data: the ID string
4437 * @arrlen: the length of the @id_data array
4438
4439 * Returns the length of the ID string, according to known wraparound/trailing
4440 * zero patterns. If no pattern exists, returns the length of the array.
4441 */
4442static int nand_id_len(u8 *id_data, int arrlen)
4443{
4444 int last_nonzero, period;
4445
4446 /* Find last non-zero byte */
4447 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4448 if (id_data[last_nonzero])
4449 break;
4450
4451 /* All zeros */
4452 if (last_nonzero < 0)
4453 return 0;
4454
4455 /* Calculate wraparound period */
4456 for (period = 1; period < arrlen; period++)
4457 if (nand_id_has_period(id_data, arrlen, period))
4458 break;
4459
4460 /* There's a repeated pattern */
4461 if (period < arrlen)
4462 return period;
4463
4464 /* There are trailing zeros */
4465 if (last_nonzero < arrlen - 1)
4466 return last_nonzero + 1;
4467
4468 /* No pattern detected */
4469 return arrlen;
4470}
4471
Huang Shijie7db906b2013-09-25 14:58:11 +08004472/* Extract the bits of per cell from the 3rd byte of the extended ID */
4473static int nand_get_bits_per_cell(u8 cellinfo)
4474{
4475 int bits;
4476
4477 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4478 bits >>= NAND_CI_CELLTYPE_SHIFT;
4479 return bits + 1;
4480}
4481
Brian Norrise3b88bd2012-09-24 20:40:52 -07004482/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004483 * Many new NAND share similar device ID codes, which represent the size of the
4484 * chip. The rest of the parameters must be decoded according to generic or
4485 * manufacturer-specific "extended ID" decoding patterns.
4486 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004487void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004488{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004489 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02004490 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004491 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004492 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08004493 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004494 /* The 4th id byte is the important one */
4495 extid = id_data[3];
4496
Boris Brezillon01389b62016-06-08 10:30:18 +02004497 /* Calc pagesize */
4498 mtd->writesize = 1024 << (extid & 0x03);
4499 extid >>= 2;
4500 /* Calc oobsize */
4501 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4502 extid >>= 2;
4503 /* Calc blocksize. Blocksize is multiples of 64KiB */
4504 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4505 extid >>= 2;
4506 /* Get buswidth information */
4507 if (extid & 0x1)
4508 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004509}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004510EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004511
4512/*
Brian Norrisf23a4812012-09-24 20:40:51 -07004513 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4514 * decodes a matching ID table entry and assigns the MTD size parameters for
4515 * the chip.
4516 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004517static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07004518{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004519 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07004520
4521 mtd->erasesize = type->erasesize;
4522 mtd->writesize = type->pagesize;
4523 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07004524
Huang Shijie1c195e92013-09-25 14:58:12 +08004525 /* All legacy ID NAND are small-page, SLC */
4526 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07004527}
4528
4529/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004530 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4531 * heuristic patterns using various detected parameters (e.g., manufacturer,
4532 * page size, cell-type information).
4533 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004534static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07004535{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004536 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004537
4538 /* Set the bad block position */
4539 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4540 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4541 else
4542 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004543}
4544
Huang Shijieec6e87e2013-03-15 11:01:00 +08004545static inline bool is_full_id_nand(struct nand_flash_dev *type)
4546{
4547 return type->id_len;
4548}
4549
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004550static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02004551 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004552{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004553 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02004554 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004555
Huang Shijieec6e87e2013-03-15 11:01:00 +08004556 if (!strncmp(type->id, id_data, type->id_len)) {
4557 mtd->writesize = type->pagesize;
4558 mtd->erasesize = type->erasesize;
4559 mtd->oobsize = type->oobsize;
4560
Huang Shijie7db906b2013-09-25 14:58:11 +08004561 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004562 chip->chipsize = (uint64_t)type->chipsize << 20;
4563 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004564 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4565 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004566 chip->onfi_timing_mode_default =
4567 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004568
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004569 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4570 if (!chip->parameters.model)
4571 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004572
Huang Shijieec6e87e2013-03-15 11:01:00 +08004573 return true;
4574 }
4575 return false;
4576}
4577
Brian Norris7e74c2d2012-09-24 20:40:49 -07004578/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004579 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4580 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4581 * table.
4582 */
4583static void nand_manufacturer_detect(struct nand_chip *chip)
4584{
4585 /*
4586 * Try manufacturer detection if available and use
4587 * nand_decode_ext_id() otherwise.
4588 */
4589 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004590 chip->manufacturer.desc->ops->detect) {
4591 /* The 3rd id byte holds MLC / multichip data */
4592 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004593 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004594 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004595 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004596 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004597}
4598
4599/*
4600 * Manufacturer initialization. This function is called for all NANDs including
4601 * ONFI and JEDEC compliant ones.
4602 * Manufacturer drivers should put all their specific initialization code in
4603 * their ->init() hook.
4604 */
4605static int nand_manufacturer_init(struct nand_chip *chip)
4606{
4607 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4608 !chip->manufacturer.desc->ops->init)
4609 return 0;
4610
4611 return chip->manufacturer.desc->ops->init(chip);
4612}
4613
4614/*
4615 * Manufacturer cleanup. This function is called for all NANDs including
4616 * ONFI and JEDEC compliant ones.
4617 * Manufacturer drivers should put all their specific cleanup code in their
4618 * ->cleanup() hook.
4619 */
4620static void nand_manufacturer_cleanup(struct nand_chip *chip)
4621{
4622 /* Release manufacturer private data */
4623 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4624 chip->manufacturer.desc->ops->cleanup)
4625 chip->manufacturer.desc->ops->cleanup(chip);
4626}
4627
Boris Brezillon348d56a2018-09-07 00:38:48 +02004628static const char *
4629nand_manufacturer_name(const struct nand_manufacturer *manufacturer)
4630{
4631 return manufacturer ? manufacturer->name : "Unknown";
4632}
4633
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004634/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004635 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004636 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004637static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004638{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004639 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004640 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004641 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004642 u8 *id_data = chip->id.data;
4643 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Karl Beldanef89a882008-09-15 14:37:29 +02004645 /*
4646 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004647 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004648 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004649 ret = nand_reset(chip, 0);
4650 if (ret)
4651 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004652
4653 /* Select the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004654 chip->select_chip(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004657 ret = nand_readid_op(chip, 0, id_data, 2);
4658 if (ret)
4659 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
4661 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004662 maf_id = id_data[0];
4663 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Brian Norris8b6e50c2011-05-25 14:59:01 -07004665 /*
4666 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004667 * interface concerns can cause random data which looks like a
4668 * possibly credible NAND flash to appear. If the two results do
4669 * not match, ignore the device completely.
4670 */
4671
Brian Norris4aef9b72012-09-24 20:40:48 -07004672 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004673 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
4674 if (ret)
4675 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01004676
Boris Brezillon7f501f02016-05-24 19:20:05 +02004677 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004678 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004679 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004680 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004681 }
4682
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02004683 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02004684
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004685 /* Try to identify manufacturer */
4686 manufacturer = nand_get_manufacturer(maf_id);
4687 chip->manufacturer.desc = manufacturer;
4688
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004689 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004690 type = nand_flash_ids;
4691
Boris Brezillon29a198a2016-05-24 20:17:48 +02004692 /*
4693 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4694 * override it.
4695 * This is required to make sure initial NAND bus width set by the
4696 * NAND controller driver is coherent with the real NAND bus width
4697 * (extracted by auto-detection code).
4698 */
4699 busw = chip->options & NAND_BUSWIDTH_16;
4700
4701 /*
4702 * The flag is only set (never cleared), reset it to its default value
4703 * before starting auto-detection.
4704 */
4705 chip->options &= ~NAND_BUSWIDTH_16;
4706
Huang Shijieec6e87e2013-03-15 11:01:00 +08004707 for (; type->name != NULL; type++) {
4708 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004709 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004710 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004711 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004712 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004713 }
4714 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004715
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004716 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004717 /* Check if the chip is ONFI compliant */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004718 ret = nand_onfi_detect(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01004719 if (ret < 0)
4720 return ret;
4721 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004722 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004723
4724 /* Check if the chip is JEDEC compliant */
Boris Brezillon8ae3fbf2018-09-07 00:38:51 +02004725 ret = nand_jedec_detect(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01004726 if (ret < 0)
4727 return ret;
4728 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08004729 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004730 }
4731
David Woodhouse5e81e882010-02-26 18:32:56 +00004732 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004733 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004734
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004735 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4736 if (!chip->parameters.model)
4737 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004738
Adrian Hunter69423d92008-12-10 13:37:21 +00004739 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004740
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004741 if (!type->pagesize)
4742 nand_manufacturer_detect(chip);
4743 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004744 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004745
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004746 /* Get chip options */
4747 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004748
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004749ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01004750 if (!mtd->name)
4751 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004752
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004753 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004754 WARN_ON(busw & NAND_BUSWIDTH_16);
4755 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004756 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4757 /*
4758 * Check, if buswidth is correct. Hardware drivers should set
4759 * chip correct!
4760 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004761 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004762 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004763 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4764 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004765 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4766 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004767 ret = -EINVAL;
4768
4769 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004770 }
4771
Boris Brezillon7f501f02016-05-24 19:20:05 +02004772 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004773
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004774 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004775 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004776 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004777 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004778
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004779 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004780 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004781 if (chip->chipsize & 0xffffffff)
4782 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004783 else {
4784 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4785 chip->chip_shift += 32 - 1;
4786 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004787
Masahiro Yamada14157f82017-09-13 11:05:50 +09004788 if (chip->chip_shift - chip->page_shift > 16)
4789 chip->options |= NAND_ROW_ADDR_3;
4790
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004791 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004792
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004793 nand_legacy_adjust_cmdfunc(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004794
Ezequiel Garcia20171642013-11-25 08:30:31 -03004795 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004796 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01004797 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4798 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02004799 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004800 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004801 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004802 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004803
4804free_detect_allocation:
4805 kfree(chip->parameters.model);
4806
4807 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004808}
4809
Boris Brezillond48f62b2016-04-01 14:54:32 +02004810static const char * const nand_ecc_modes[] = {
4811 [NAND_ECC_NONE] = "none",
4812 [NAND_ECC_SOFT] = "soft",
4813 [NAND_ECC_HW] = "hw",
4814 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4815 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004816 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004817};
4818
4819static int of_get_nand_ecc_mode(struct device_node *np)
4820{
4821 const char *pm;
4822 int err, i;
4823
4824 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4825 if (err < 0)
4826 return err;
4827
4828 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4829 if (!strcasecmp(pm, nand_ecc_modes[i]))
4830 return i;
4831
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004832 /*
4833 * For backward compatibility we support few obsoleted values that don't
4834 * have their mappings into nand_ecc_modes_t anymore (they were merged
4835 * with other enums).
4836 */
4837 if (!strcasecmp(pm, "soft_bch"))
4838 return NAND_ECC_SOFT;
4839
Boris Brezillond48f62b2016-04-01 14:54:32 +02004840 return -ENODEV;
4841}
4842
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004843static const char * const nand_ecc_algos[] = {
4844 [NAND_ECC_HAMMING] = "hamming",
4845 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02004846 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004847};
4848
Boris Brezillond48f62b2016-04-01 14:54:32 +02004849static int of_get_nand_ecc_algo(struct device_node *np)
4850{
4851 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004852 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004853
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004854 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4855 if (!err) {
4856 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4857 if (!strcasecmp(pm, nand_ecc_algos[i]))
4858 return i;
4859 return -ENODEV;
4860 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004861
4862 /*
4863 * For backward compatibility we also read "nand-ecc-mode" checking
4864 * for some obsoleted values that were specifying ECC algorithm.
4865 */
4866 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4867 if (err < 0)
4868 return err;
4869
4870 if (!strcasecmp(pm, "soft"))
4871 return NAND_ECC_HAMMING;
4872 else if (!strcasecmp(pm, "soft_bch"))
4873 return NAND_ECC_BCH;
4874
4875 return -ENODEV;
4876}
4877
4878static int of_get_nand_ecc_step_size(struct device_node *np)
4879{
4880 int ret;
4881 u32 val;
4882
4883 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4884 return ret ? ret : val;
4885}
4886
4887static int of_get_nand_ecc_strength(struct device_node *np)
4888{
4889 int ret;
4890 u32 val;
4891
4892 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4893 return ret ? ret : val;
4894}
4895
4896static int of_get_nand_bus_width(struct device_node *np)
4897{
4898 u32 val;
4899
4900 if (of_property_read_u32(np, "nand-bus-width", &val))
4901 return 8;
4902
4903 switch (val) {
4904 case 8:
4905 case 16:
4906 return val;
4907 default:
4908 return -EIO;
4909 }
4910}
4911
4912static bool of_get_nand_on_flash_bbt(struct device_node *np)
4913{
4914 return of_property_read_bool(np, "nand-on-flash-bbt");
4915}
4916
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004917static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004918{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004919 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004920 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004921
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004922 if (!dn)
4923 return 0;
4924
Brian Norris5844fee2015-01-23 00:22:27 -08004925 if (of_get_nand_bus_width(dn) == 16)
4926 chip->options |= NAND_BUSWIDTH_16;
4927
Stefan Agnerf922bd72018-06-24 23:27:23 +02004928 if (of_property_read_bool(dn, "nand-is-boot-medium"))
4929 chip->options |= NAND_IS_BOOT_MEDIUM;
4930
Brian Norris5844fee2015-01-23 00:22:27 -08004931 if (of_get_nand_on_flash_bbt(dn))
4932 chip->bbt_options |= NAND_BBT_USE_FLASH;
4933
4934 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004935 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004936 ecc_strength = of_get_nand_ecc_strength(dn);
4937 ecc_step = of_get_nand_ecc_step_size(dn);
4938
Brian Norris5844fee2015-01-23 00:22:27 -08004939 if (ecc_mode >= 0)
4940 chip->ecc.mode = ecc_mode;
4941
Rafał Miłecki79082452016-03-23 11:19:02 +01004942 if (ecc_algo >= 0)
4943 chip->ecc.algo = ecc_algo;
4944
Brian Norris5844fee2015-01-23 00:22:27 -08004945 if (ecc_strength >= 0)
4946 chip->ecc.strength = ecc_strength;
4947
4948 if (ecc_step > 0)
4949 chip->ecc.size = ecc_step;
4950
Boris Brezillonba78ee02016-06-08 17:04:22 +02004951 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4952 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4953
Brian Norris5844fee2015-01-23 00:22:27 -08004954 return 0;
4955}
4956
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004957/**
Miquel Raynal98732da2018-07-25 15:31:50 +02004958 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02004959 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004960 * @maxchips: number of chips to scan for
4961 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004962 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004963 * This is the first phase of the normal nand_scan() function. It reads the
4964 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004965 *
Miquel Raynal98732da2018-07-25 15:31:50 +02004966 * This helper used to be called directly from controller drivers that needed
4967 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
4968 * prevented dynamic allocations during this phase which was unconvenient and
4969 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004970 */
Boris Brezillon871a4072018-08-04 22:59:22 +02004971static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02004972 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004973{
Boris Brezillon00ad3782018-09-06 14:05:14 +02004974 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02004975 int nand_maf_id, nand_dev_id;
4976 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08004977 int ret;
4978
Miquel Raynal17fa8042017-11-30 18:01:31 +01004979 /* Enforce the right timings for reset/detection */
4980 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
4981
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004982 ret = nand_dt_init(chip);
4983 if (ret)
4984 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004985
Brian Norrisf7a8e382016-01-05 10:39:45 -08004986 if (!mtd->name && mtd->dev.parent)
4987 mtd->name = dev_name(mtd->dev.parent);
4988
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004989 if (chip->exec_op && !chip->select_chip) {
4990 pr_err("->select_chip() is mandatory when implementing ->exec_op()\n");
4991 return -EINVAL;
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004992 }
Miquel Raynal8878b122017-11-09 14:16:45 +01004993
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004994 ret = nand_legacy_check_hooks(chip);
4995 if (ret)
4996 return ret;
4997
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004998 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004999 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005000
5001 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005002 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005003 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005004 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005005 pr_warn("No NAND device found\n");
Boris Brezillon758b56f2018-09-06 14:05:24 +02005006 chip->select_chip(chip, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005007 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 }
5009
Boris Brezillon7f501f02016-05-24 19:20:05 +02005010 nand_maf_id = chip->id.data[0];
5011 nand_dev_id = chip->id.data[1];
5012
Boris Brezillon758b56f2018-09-06 14:05:24 +02005013 chip->select_chip(chip, -1);
Huang Shijie07300162012-11-09 16:23:45 +08005014
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005015 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005016 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005017 u8 id[2];
5018
Karl Beldanef89a882008-09-15 14:37:29 +02005019 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005020 nand_reset(chip, i);
5021
Boris Brezillon758b56f2018-09-06 14:05:24 +02005022 chip->select_chip(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005024 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005026 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02005027 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 break;
Huang Shijie07300162012-11-09 16:23:45 +08005029 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02005030 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 }
5032 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005033 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005034
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005036 chip->numchips = i;
5037 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
David Woodhouse3b85c322006-09-25 17:06:53 +01005039 return 0;
5040}
5041
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005042static void nand_scan_ident_cleanup(struct nand_chip *chip)
5043{
5044 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005045 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005046}
5047
Boris Brezillon08136212018-11-11 08:55:03 +01005048static int nand_set_ecc_soft_ops(struct nand_chip *chip)
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005049{
Boris Brezillon08136212018-11-11 08:55:03 +01005050 struct mtd_info *mtd = nand_to_mtd(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005051 struct nand_ecc_ctrl *ecc = &chip->ecc;
5052
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005053 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005054 return -EINVAL;
5055
5056 switch (ecc->algo) {
5057 case NAND_ECC_HAMMING:
5058 ecc->calculate = nand_calculate_ecc;
5059 ecc->correct = nand_correct_data;
5060 ecc->read_page = nand_read_page_swecc;
5061 ecc->read_subpage = nand_read_subpage;
5062 ecc->write_page = nand_write_page_swecc;
5063 ecc->read_page_raw = nand_read_page_raw;
5064 ecc->write_page_raw = nand_write_page_raw;
5065 ecc->read_oob = nand_read_oob_std;
5066 ecc->write_oob = nand_write_oob_std;
5067 if (!ecc->size)
5068 ecc->size = 256;
5069 ecc->bytes = 3;
5070 ecc->strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +02005071
5072 if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC))
5073 ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
5074
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005075 return 0;
5076 case NAND_ECC_BCH:
5077 if (!mtd_nand_has_bch()) {
5078 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5079 return -EINVAL;
5080 }
5081 ecc->calculate = nand_bch_calculate_ecc;
5082 ecc->correct = nand_bch_correct_data;
5083 ecc->read_page = nand_read_page_swecc;
5084 ecc->read_subpage = nand_read_subpage;
5085 ecc->write_page = nand_write_page_swecc;
5086 ecc->read_page_raw = nand_read_page_raw;
5087 ecc->write_page_raw = nand_write_page_raw;
5088 ecc->read_oob = nand_read_oob_std;
5089 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005090
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005091 /*
5092 * Board driver should supply ecc.size and ecc.strength
5093 * values to select how many bits are correctable.
5094 * Otherwise, default to 4 bits for large page devices.
5095 */
5096 if (!ecc->size && (mtd->oobsize >= 64)) {
5097 ecc->size = 512;
5098 ecc->strength = 4;
5099 }
5100
5101 /*
5102 * if no ecc placement scheme was provided pickup the default
5103 * large page one.
5104 */
5105 if (!mtd->ooblayout) {
5106 /* handle large page devices only */
5107 if (mtd->oobsize < 64) {
5108 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5109 return -EINVAL;
5110 }
5111
5112 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005113
5114 }
5115
5116 /*
5117 * We can only maximize ECC config when the default layout is
5118 * used, otherwise we don't know how many bytes can really be
5119 * used.
5120 */
5121 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5122 ecc->options & NAND_ECC_MAXIMIZE) {
5123 int steps, bytes;
5124
5125 /* Always prefer 1k blocks over 512bytes ones */
5126 ecc->size = 1024;
5127 steps = mtd->writesize / ecc->size;
5128
5129 /* Reserve 2 bytes for the BBM */
5130 bytes = (mtd->oobsize - 2) / steps;
5131 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005132 }
5133
5134 /* See nand_bch_init() for details. */
5135 ecc->bytes = 0;
5136 ecc->priv = nand_bch_init(mtd);
5137 if (!ecc->priv) {
5138 WARN(1, "BCH ECC initialization failed!\n");
5139 return -EINVAL;
5140 }
5141 return 0;
5142 default:
5143 WARN(1, "Unsupported ECC algorithm!\n");
5144 return -EINVAL;
5145 }
5146}
5147
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005148/**
5149 * nand_check_ecc_caps - check the sanity of preset ECC settings
5150 * @chip: nand chip info structure
5151 * @caps: ECC caps info structure
5152 * @oobavail: OOB size that the ECC engine can use
5153 *
5154 * When ECC step size and strength are already set, check if they are supported
5155 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5156 * On success, the calculated ECC bytes is set.
5157 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305158static int
5159nand_check_ecc_caps(struct nand_chip *chip,
5160 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005161{
5162 struct mtd_info *mtd = nand_to_mtd(chip);
5163 const struct nand_ecc_step_info *stepinfo;
5164 int preset_step = chip->ecc.size;
5165 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305166 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005167 int i, j;
5168
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005169 for (i = 0; i < caps->nstepinfos; i++) {
5170 stepinfo = &caps->stepinfos[i];
5171
5172 if (stepinfo->stepsize != preset_step)
5173 continue;
5174
5175 for (j = 0; j < stepinfo->nstrengths; j++) {
5176 if (stepinfo->strengths[j] != preset_strength)
5177 continue;
5178
5179 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5180 preset_strength);
5181 if (WARN_ON_ONCE(ecc_bytes < 0))
5182 return ecc_bytes;
5183
5184 if (ecc_bytes * nsteps > oobavail) {
5185 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5186 preset_step, preset_strength);
5187 return -ENOSPC;
5188 }
5189
5190 chip->ecc.bytes = ecc_bytes;
5191
5192 return 0;
5193 }
5194 }
5195
5196 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5197 preset_step, preset_strength);
5198
5199 return -ENOTSUPP;
5200}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005201
5202/**
5203 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5204 * @chip: nand chip info structure
5205 * @caps: ECC engine caps info structure
5206 * @oobavail: OOB size that the ECC engine can use
5207 *
5208 * If a chip's ECC requirement is provided, try to meet it with the least
5209 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5210 * On success, the chosen ECC settings are set.
5211 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305212static int
5213nand_match_ecc_req(struct nand_chip *chip,
5214 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005215{
5216 struct mtd_info *mtd = nand_to_mtd(chip);
5217 const struct nand_ecc_step_info *stepinfo;
5218 int req_step = chip->ecc_step_ds;
5219 int req_strength = chip->ecc_strength_ds;
5220 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5221 int best_step, best_strength, best_ecc_bytes;
5222 int best_ecc_bytes_total = INT_MAX;
5223 int i, j;
5224
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005225 /* No information provided by the NAND chip */
5226 if (!req_step || !req_strength)
5227 return -ENOTSUPP;
5228
5229 /* number of correctable bits the chip requires in a page */
5230 req_corr = mtd->writesize / req_step * req_strength;
5231
5232 for (i = 0; i < caps->nstepinfos; i++) {
5233 stepinfo = &caps->stepinfos[i];
5234 step_size = stepinfo->stepsize;
5235
5236 for (j = 0; j < stepinfo->nstrengths; j++) {
5237 strength = stepinfo->strengths[j];
5238
5239 /*
5240 * If both step size and strength are smaller than the
5241 * chip's requirement, it is not easy to compare the
5242 * resulted reliability.
5243 */
5244 if (step_size < req_step && strength < req_strength)
5245 continue;
5246
5247 if (mtd->writesize % step_size)
5248 continue;
5249
5250 nsteps = mtd->writesize / step_size;
5251
5252 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5253 if (WARN_ON_ONCE(ecc_bytes < 0))
5254 continue;
5255 ecc_bytes_total = ecc_bytes * nsteps;
5256
5257 if (ecc_bytes_total > oobavail ||
5258 strength * nsteps < req_corr)
5259 continue;
5260
5261 /*
5262 * We assume the best is to meet the chip's requrement
5263 * with the least number of ECC bytes.
5264 */
5265 if (ecc_bytes_total < best_ecc_bytes_total) {
5266 best_ecc_bytes_total = ecc_bytes_total;
5267 best_step = step_size;
5268 best_strength = strength;
5269 best_ecc_bytes = ecc_bytes;
5270 }
5271 }
5272 }
5273
5274 if (best_ecc_bytes_total == INT_MAX)
5275 return -ENOTSUPP;
5276
5277 chip->ecc.size = best_step;
5278 chip->ecc.strength = best_strength;
5279 chip->ecc.bytes = best_ecc_bytes;
5280
5281 return 0;
5282}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005283
5284/**
5285 * nand_maximize_ecc - choose the max ECC strength available
5286 * @chip: nand chip info structure
5287 * @caps: ECC engine caps info structure
5288 * @oobavail: OOB size that the ECC engine can use
5289 *
5290 * Choose the max ECC strength that is supported on the controller, and can fit
5291 * within the chip's OOB. On success, the chosen ECC settings are set.
5292 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305293static int
5294nand_maximize_ecc(struct nand_chip *chip,
5295 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005296{
5297 struct mtd_info *mtd = nand_to_mtd(chip);
5298 const struct nand_ecc_step_info *stepinfo;
5299 int step_size, strength, nsteps, ecc_bytes, corr;
5300 int best_corr = 0;
5301 int best_step = 0;
5302 int best_strength, best_ecc_bytes;
5303 int i, j;
5304
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005305 for (i = 0; i < caps->nstepinfos; i++) {
5306 stepinfo = &caps->stepinfos[i];
5307 step_size = stepinfo->stepsize;
5308
5309 /* If chip->ecc.size is already set, respect it */
5310 if (chip->ecc.size && step_size != chip->ecc.size)
5311 continue;
5312
5313 for (j = 0; j < stepinfo->nstrengths; j++) {
5314 strength = stepinfo->strengths[j];
5315
5316 if (mtd->writesize % step_size)
5317 continue;
5318
5319 nsteps = mtd->writesize / step_size;
5320
5321 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5322 if (WARN_ON_ONCE(ecc_bytes < 0))
5323 continue;
5324
5325 if (ecc_bytes * nsteps > oobavail)
5326 continue;
5327
5328 corr = strength * nsteps;
5329
5330 /*
5331 * If the number of correctable bits is the same,
5332 * bigger step_size has more reliability.
5333 */
5334 if (corr > best_corr ||
5335 (corr == best_corr && step_size > best_step)) {
5336 best_corr = corr;
5337 best_step = step_size;
5338 best_strength = strength;
5339 best_ecc_bytes = ecc_bytes;
5340 }
5341 }
5342 }
5343
5344 if (!best_corr)
5345 return -ENOTSUPP;
5346
5347 chip->ecc.size = best_step;
5348 chip->ecc.strength = best_strength;
5349 chip->ecc.bytes = best_ecc_bytes;
5350
5351 return 0;
5352}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005353
Abhishek Sahu181ace92018-06-20 12:57:28 +05305354/**
5355 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
5356 * @chip: nand chip info structure
5357 * @caps: ECC engine caps info structure
5358 * @oobavail: OOB size that the ECC engine can use
5359 *
5360 * Choose the ECC configuration according to following logic
5361 *
5362 * 1. If both ECC step size and ECC strength are already set (usually by DT)
5363 * then check if it is supported by this controller.
5364 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
5365 * 3. Otherwise, try to match the ECC step size and ECC strength closest
5366 * to the chip's requirement. If available OOB size can't fit the chip
5367 * requirement then fallback to the maximum ECC step size and ECC strength.
5368 *
5369 * On success, the chosen ECC settings are set.
5370 */
5371int nand_ecc_choose_conf(struct nand_chip *chip,
5372 const struct nand_ecc_caps *caps, int oobavail)
5373{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305374 struct mtd_info *mtd = nand_to_mtd(chip);
5375
5376 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
5377 return -EINVAL;
5378
Abhishek Sahu181ace92018-06-20 12:57:28 +05305379 if (chip->ecc.size && chip->ecc.strength)
5380 return nand_check_ecc_caps(chip, caps, oobavail);
5381
5382 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
5383 return nand_maximize_ecc(chip, caps, oobavail);
5384
5385 if (!nand_match_ecc_req(chip, caps, oobavail))
5386 return 0;
5387
5388 return nand_maximize_ecc(chip, caps, oobavail);
5389}
5390EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
5391
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005392/*
5393 * Check if the chip configuration meet the datasheet requirements.
5394
5395 * If our configuration corrects A bits per B bytes and the minimum
5396 * required correction level is X bits per Y bytes, then we must ensure
5397 * both of the following are true:
5398 *
5399 * (1) A / B >= X / Y
5400 * (2) A >= X
5401 *
5402 * Requirement (1) ensures we can correct for the required bitflip density.
5403 * Requirement (2) ensures we can correct even when all bitflips are clumped
5404 * in the same sector.
5405 */
Boris Brezillon08136212018-11-11 08:55:03 +01005406static bool nand_ecc_strength_good(struct nand_chip *chip)
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005407{
Boris Brezillon08136212018-11-11 08:55:03 +01005408 struct mtd_info *mtd = nand_to_mtd(chip);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005409 struct nand_ecc_ctrl *ecc = &chip->ecc;
5410 int corr, ds_corr;
5411
5412 if (ecc->size == 0 || chip->ecc_step_ds == 0)
5413 /* Not enough information */
5414 return true;
5415
5416 /*
5417 * We get the number of corrected bits per page to compare
5418 * the correction density.
5419 */
5420 corr = (mtd->writesize * ecc->strength) / ecc->size;
5421 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
5422
5423 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
5424}
David Woodhouse3b85c322006-09-25 17:06:53 +01005425
5426/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005427 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005428 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01005429 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005430 * This is the second phase of the normal nand_scan() function. It fills out
5431 * all the uninitialized function pointers with the defaults and scans for a
5432 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01005433 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005434static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01005435{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005436 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08005437 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005438 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01005439
Brian Norrise2414f42012-02-06 13:44:00 -08005440 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005441 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07005442 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005443 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07005444 }
Brian Norrise2414f42012-02-06 13:44:00 -08005445
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005446 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005447 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02005448 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005449
Boris Brezillonf84674b2017-06-02 12:18:24 +02005450 /*
5451 * FIXME: some NAND manufacturer drivers expect the first die to be
5452 * selected when manufacturer->init() is called. They should be fixed
5453 * to explictly select the relevant die when interacting with the NAND
5454 * chip.
5455 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02005456 chip->select_chip(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005457 ret = nand_manufacturer_init(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02005458 chip->select_chip(chip, -1);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005459 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005460 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005461
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01005462 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005463 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005464
5465 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005466 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005467 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005468 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005469 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005470 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01005473 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 break;
5475 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005476 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02005477 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02005480 /*
5481 * Expose the whole OOB area to users if ECC_NONE
5482 * is passed. We could do that for all kind of
5483 * ->oobsize, but we must keep the old large/small
5484 * page with ECC layout when ->oobsize <= 128 for
5485 * compatibility reasons.
5486 */
5487 if (ecc->mode == NAND_ECC_NONE) {
5488 mtd_set_ooblayout(mtd,
5489 &nand_ooblayout_lp_ops);
5490 break;
5491 }
5492
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005493 WARN(1, "No oob scheme defined for oobsize %d\n",
5494 mtd->oobsize);
5495 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005496 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 }
5498 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005499
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005500 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005501 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005502 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01005503 */
David Woodhouse956e9442006-09-25 17:12:39 +01005504
Huang Shijie97de79e02013-10-18 14:20:53 +08005505 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005506 case NAND_ECC_HW_OOB_FIRST:
5507 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08005508 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005509 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5510 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005511 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005512 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005513 if (!ecc->read_page)
5514 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005515
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005516 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07005517 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005518 if (!ecc->read_page)
5519 ecc->read_page = nand_read_page_hwecc;
5520 if (!ecc->write_page)
5521 ecc->write_page = nand_write_page_hwecc;
5522 if (!ecc->read_page_raw)
5523 ecc->read_page_raw = nand_read_page_raw;
5524 if (!ecc->write_page_raw)
5525 ecc->write_page_raw = nand_write_page_raw;
5526 if (!ecc->read_oob)
5527 ecc->read_oob = nand_read_oob_std;
5528 if (!ecc->write_oob)
5529 ecc->write_oob = nand_write_oob_std;
5530 if (!ecc->read_subpage)
5531 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02005532 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08005533 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005534
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005535 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08005536 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5537 (!ecc->read_page ||
5538 ecc->read_page == nand_read_page_hwecc ||
5539 !ecc->write_page ||
5540 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005541 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5542 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005543 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005544 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07005545 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005546 if (!ecc->read_page)
5547 ecc->read_page = nand_read_page_syndrome;
5548 if (!ecc->write_page)
5549 ecc->write_page = nand_write_page_syndrome;
5550 if (!ecc->read_page_raw)
5551 ecc->read_page_raw = nand_read_page_raw_syndrome;
5552 if (!ecc->write_page_raw)
5553 ecc->write_page_raw = nand_write_page_raw_syndrome;
5554 if (!ecc->read_oob)
5555 ecc->read_oob = nand_read_oob_syndrome;
5556 if (!ecc->write_oob)
5557 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005558
Huang Shijie97de79e02013-10-18 14:20:53 +08005559 if (mtd->writesize >= ecc->size) {
5560 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005561 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
5562 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005563 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07005564 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005565 break;
Mike Dunne2788c92012-04-25 12:06:10 -07005566 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005567 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5568 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08005569 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02005570 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005572 case NAND_ECC_SOFT:
Boris Brezillon08136212018-11-11 08:55:03 +01005573 ret = nand_set_ecc_soft_ops(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005574 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005575 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005576 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01005577 }
5578 break;
5579
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005580 case NAND_ECC_ON_DIE:
5581 if (!ecc->read_page || !ecc->write_page) {
5582 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
5583 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005584 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005585 }
5586 if (!ecc->read_oob)
5587 ecc->read_oob = nand_read_oob_std;
5588 if (!ecc->write_oob)
5589 ecc->write_oob = nand_write_oob_std;
5590 break;
5591
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005592 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005593 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08005594 ecc->read_page = nand_read_page_raw;
5595 ecc->write_page = nand_write_page_raw;
5596 ecc->read_oob = nand_read_oob_std;
5597 ecc->read_page_raw = nand_read_page_raw;
5598 ecc->write_page_raw = nand_write_page_raw;
5599 ecc->write_oob = nand_write_oob_std;
5600 ecc->size = mtd->writesize;
5601 ecc->bytes = 0;
5602 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005606 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5607 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005608 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005611 if (ecc->correct || ecc->calculate) {
5612 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5613 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5614 if (!ecc->calc_buf || !ecc->code_buf) {
5615 ret = -ENOMEM;
5616 goto err_nand_manuf_cleanup;
5617 }
5618 }
5619
Brian Norris9ce244b2011-08-30 18:45:37 -07005620 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005621 if (!ecc->read_oob_raw)
5622 ecc->read_oob_raw = ecc->read_oob;
5623 if (!ecc->write_oob_raw)
5624 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005625
Boris Brezillon846031d2016-02-03 20:11:00 +01005626 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005627 mtd->ecc_strength = ecc->strength;
5628 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005629
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005630 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005631 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005632 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005633 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005634 ecc->steps = mtd->writesize / ecc->size;
5635 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005636 WARN(1, "Invalid ECC parameters\n");
5637 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005638 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005640 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005641 if (ecc->total > mtd->oobsize) {
5642 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5643 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005644 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005645 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005646
Boris Brezillon846031d2016-02-03 20:11:00 +01005647 /*
5648 * The number of bytes available for a client to place data into
5649 * the out of band area.
5650 */
5651 ret = mtd_ooblayout_count_freebytes(mtd);
5652 if (ret < 0)
5653 ret = 0;
5654
5655 mtd->oobavail = ret;
5656
5657 /* ECC sanity check: warn if it's too weak */
Boris Brezillon08136212018-11-11 08:55:03 +01005658 if (!nand_ecc_strength_good(chip))
Boris Brezillon846031d2016-02-03 20:11:00 +01005659 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5660 mtd->name);
5661
Brian Norris8b6e50c2011-05-25 14:59:01 -07005662 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005663 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005664 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005665 case 2:
5666 mtd->subpage_sft = 1;
5667 break;
5668 case 4:
5669 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005670 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005671 mtd->subpage_sft = 2;
5672 break;
5673 }
5674 }
5675 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5676
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02005677 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005678 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005681 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005683 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305684 switch (ecc->mode) {
5685 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305686 if (chip->page_shift > 9)
5687 chip->options |= NAND_SUBPAGE_READ;
5688 break;
5689
5690 default:
5691 break;
5692 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005693
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08005695 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02005696 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5697 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005698 mtd->_erase = nand_erase;
5699 mtd->_point = NULL;
5700 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005701 mtd->_panic_write = panic_nand_write;
5702 mtd->_read_oob = nand_read_oob;
5703 mtd->_write_oob = nand_write_oob;
5704 mtd->_sync = nand_sync;
5705 mtd->_lock = NULL;
5706 mtd->_unlock = NULL;
5707 mtd->_suspend = nand_suspend;
5708 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005709 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005710 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005711 mtd->_block_isbad = nand_block_isbad;
5712 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06005713 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01005714 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005716 /*
5717 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5718 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5719 * properly set.
5720 */
5721 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005722 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
Boris Brezillonf84674b2017-06-02 12:18:24 +02005724 /* Initialize the ->data_interface field. */
5725 ret = nand_init_data_interface(chip);
5726 if (ret)
5727 goto err_nand_manuf_cleanup;
5728
5729 /* Enter fastest possible mode on all dies. */
5730 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005731 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005732 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005733 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005734 }
5735
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005736 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005737 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
5740 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02005741 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07005742 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005743 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005744
Brian Norris44d41822017-05-01 17:04:50 -07005745 return 0;
5746
Boris Brezillonf84674b2017-06-02 12:18:24 +02005747
5748err_nand_manuf_cleanup:
5749 nand_manufacturer_cleanup(chip);
5750
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005751err_free_buf:
5752 kfree(chip->data_buf);
5753 kfree(ecc->code_buf);
5754 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07005755
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005756 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757}
5758
Miquel Raynal05b54c72018-07-19 01:05:46 +02005759static int nand_attach(struct nand_chip *chip)
5760{
5761 if (chip->controller->ops && chip->controller->ops->attach_chip)
5762 return chip->controller->ops->attach_chip(chip);
5763
5764 return 0;
5765}
5766
5767static void nand_detach(struct nand_chip *chip)
5768{
5769 if (chip->controller->ops && chip->controller->ops->detach_chip)
5770 chip->controller->ops->detach_chip(chip);
5771}
5772
David Woodhouse3b85c322006-09-25 17:06:53 +01005773/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005774 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005775 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02005776 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005777 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01005778 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005779 * This fills out all the uninitialized function pointers with the defaults.
5780 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005781 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005782 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005783int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005784 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01005785{
5786 int ret;
5787
Boris Brezillon800342d2018-08-04 22:59:23 +02005788 if (!maxchips)
5789 return -EINVAL;
5790
5791 ret = nand_scan_ident(chip, maxchips, ids);
5792 if (ret)
5793 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005794
5795 ret = nand_attach(chip);
5796 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005797 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005798
Boris Brezillon00ad3782018-09-06 14:05:14 +02005799 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005800 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005801 goto detach_chip;
5802
5803 return 0;
5804
5805detach_chip:
5806 nand_detach(chip);
5807cleanup_ident:
5808 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005809
David Woodhouse3b85c322006-09-25 17:06:53 +01005810 return ret;
5811}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005812EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01005813
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005815 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5816 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005817 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005818void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005820 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005821 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005822 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5823
Jesper Juhlfa671642005-11-07 01:01:27 -08005824 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005825 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005826 kfree(chip->data_buf);
5827 kfree(chip->ecc.code_buf);
5828 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07005829
5830 /* Free bad block descriptor memory */
5831 if (chip->badblock_pattern && chip->badblock_pattern->options
5832 & NAND_BBT_DYNAMICSTRUCT)
5833 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005834
5835 /* Free manufacturer priv data. */
5836 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005837
5838 /* Free controller specific allocations after chip identification */
5839 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005840
5841 /* Free identification phase allocations */
5842 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843}
Miquel Raynal05b54c72018-07-19 01:05:46 +02005844
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005845EXPORT_SYMBOL_GPL(nand_cleanup);
5846
5847/**
5848 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5849 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02005850 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005851 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02005852void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005853{
Boris Brezillon59ac2762018-09-06 14:05:15 +02005854 mtd_device_unregister(nand_to_mtd(chip));
5855 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005856}
David Woodhousee0c7d762006-05-13 18:07:53 +01005857EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005858
David Woodhousee0c7d762006-05-13 18:07:53 +01005859MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005860MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5861MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005862MODULE_DESCRIPTION("Generic NAND flash driver code");