blob: 2cf71060d6f8459c7377ee2c29303efd5c23ec45 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010042#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/interrupt.h>
44#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020045#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020047#include <linux/of.h>
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +020048#include <linux/gpio/consumer.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010049
Boris Brezillon348d56a2018-09-07 00:38:48 +020050#include "internals.h"
51
Boris Brezillon41b207a2016-02-03 19:06:15 +010052/* Define default oob placement schemes for large and small page devices */
53static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
54 struct mtd_oob_region *oobregion)
55{
56 struct nand_chip *chip = mtd_to_nand(mtd);
57 struct nand_ecc_ctrl *ecc = &chip->ecc;
58
59 if (section > 1)
60 return -ERANGE;
61
62 if (!section) {
63 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020064 if (mtd->oobsize == 16)
65 oobregion->length = 4;
66 else
67 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010068 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020069 if (mtd->oobsize == 8)
70 return -ERANGE;
71
Boris Brezillon41b207a2016-02-03 19:06:15 +010072 oobregion->offset = 6;
73 oobregion->length = ecc->total - 4;
74 }
75
76 return 0;
77}
78
79static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
80 struct mtd_oob_region *oobregion)
81{
82 if (section > 1)
83 return -ERANGE;
84
85 if (mtd->oobsize == 16) {
86 if (section)
87 return -ERANGE;
88
89 oobregion->length = 8;
90 oobregion->offset = 8;
91 } else {
92 oobregion->length = 2;
93 if (!section)
94 oobregion->offset = 3;
95 else
96 oobregion->offset = 6;
97 }
98
99 return 0;
100}
101
102const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
103 .ecc = nand_ooblayout_ecc_sp,
104 .free = nand_ooblayout_free_sp,
105};
106EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
107
108static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
109 struct mtd_oob_region *oobregion)
110{
111 struct nand_chip *chip = mtd_to_nand(mtd);
112 struct nand_ecc_ctrl *ecc = &chip->ecc;
113
Miquel Raynal882fd152017-08-26 17:19:15 +0200114 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100115 return -ERANGE;
116
117 oobregion->length = ecc->total;
118 oobregion->offset = mtd->oobsize - oobregion->length;
119
120 return 0;
121}
122
123static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
124 struct mtd_oob_region *oobregion)
125{
126 struct nand_chip *chip = mtd_to_nand(mtd);
127 struct nand_ecc_ctrl *ecc = &chip->ecc;
128
129 if (section)
130 return -ERANGE;
131
132 oobregion->length = mtd->oobsize - ecc->total - 2;
133 oobregion->offset = 2;
134
135 return 0;
136}
137
138const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
139 .ecc = nand_ooblayout_ecc_lp,
140 .free = nand_ooblayout_free_lp,
141};
142EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200143
Alexander Couzens6a623e02017-05-02 12:19:00 +0200144/*
145 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
146 * are placed at a fixed offset.
147 */
148static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
149 struct mtd_oob_region *oobregion)
150{
151 struct nand_chip *chip = mtd_to_nand(mtd);
152 struct nand_ecc_ctrl *ecc = &chip->ecc;
153
154 if (section)
155 return -ERANGE;
156
157 switch (mtd->oobsize) {
158 case 64:
159 oobregion->offset = 40;
160 break;
161 case 128:
162 oobregion->offset = 80;
163 break;
164 default:
165 return -EINVAL;
166 }
167
168 oobregion->length = ecc->total;
169 if (oobregion->offset + oobregion->length > mtd->oobsize)
170 return -ERANGE;
171
172 return 0;
173}
174
175static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
176 struct mtd_oob_region *oobregion)
177{
178 struct nand_chip *chip = mtd_to_nand(mtd);
179 struct nand_ecc_ctrl *ecc = &chip->ecc;
180 int ecc_offset = 0;
181
182 if (section < 0 || section > 1)
183 return -ERANGE;
184
185 switch (mtd->oobsize) {
186 case 64:
187 ecc_offset = 40;
188 break;
189 case 128:
190 ecc_offset = 80;
191 break;
192 default:
193 return -EINVAL;
194 }
195
196 if (section == 0) {
197 oobregion->offset = 2;
198 oobregion->length = ecc_offset - 2;
199 } else {
200 oobregion->offset = ecc_offset + ecc->total;
201 oobregion->length = mtd->oobsize - oobregion->offset;
202 }
203
204 return 0;
205}
206
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100207static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200208 .ecc = nand_ooblayout_ecc_lp_hamming,
209 .free = nand_ooblayout_free_lp_hamming,
210};
211
Boris Brezillon08136212018-11-11 08:55:03 +0100212static int check_offs_len(struct nand_chip *chip, loff_t ofs, uint64_t len)
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530213{
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530214 int ret = 0;
215
216 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300217 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700218 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530219 ret = -EINVAL;
220 }
221
222 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530228 return ret;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/**
Boris Brezillon1d017852018-11-11 08:55:14 +0100232 * nand_select_target() - Select a NAND target (A.K.A. die)
233 * @chip: NAND chip object
234 * @cs: the CS line to select. Note that this CS id is always from the chip
235 * PoV, not the controller one
236 *
237 * Select a NAND target so that further operations executed on @chip go to the
238 * selected NAND target.
239 */
240void nand_select_target(struct nand_chip *chip, unsigned int cs)
241{
242 /*
Boris Brezillon32813e22018-10-29 11:58:29 +0100243 * cs should always lie between 0 and nanddev_ntargets(), when that's
244 * not the case it's a bug and the caller should be fixed.
Boris Brezillon1d017852018-11-11 08:55:14 +0100245 */
Boris Brezillon32813e22018-10-29 11:58:29 +0100246 if (WARN_ON(cs > nanddev_ntargets(&chip->base)))
Boris Brezillon1d017852018-11-11 08:55:14 +0100247 return;
248
Boris Brezillonae2294b2018-11-11 08:55:15 +0100249 chip->cur_cs = cs;
Boris Brezillon02b4a522018-11-11 08:55:16 +0100250
Boris Brezillon7d6c37e2018-11-11 08:55:22 +0100251 if (chip->legacy.select_chip)
252 chip->legacy.select_chip(chip, cs);
Boris Brezillon1d017852018-11-11 08:55:14 +0100253}
254EXPORT_SYMBOL_GPL(nand_select_target);
255
256/**
257 * nand_deselect_target() - Deselect the currently selected target
258 * @chip: NAND chip object
259 *
260 * Deselect the currently selected NAND target. The result of operations
261 * executed on @chip after the target has been deselected is undefined.
262 */
263void nand_deselect_target(struct nand_chip *chip)
264{
Boris Brezillon7d6c37e2018-11-11 08:55:22 +0100265 if (chip->legacy.select_chip)
266 chip->legacy.select_chip(chip, -1);
Boris Brezillon02b4a522018-11-11 08:55:16 +0100267
Boris Brezillonae2294b2018-11-11 08:55:15 +0100268 chip->cur_cs = -1;
Boris Brezillon1d017852018-11-11 08:55:14 +0100269}
270EXPORT_SYMBOL_GPL(nand_deselect_target);
271
272/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * nand_release_device - [GENERIC] release chip
Boris Brezillon08136212018-11-11 08:55:03 +0100274 * @chip: NAND chip object
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000275 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800276 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Boris Brezillon08136212018-11-11 08:55:03 +0100278static void nand_release_device(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200280 /* Release the controller and the chip */
Boris Brezillon013e6292018-11-20 11:57:20 +0100281 mutex_unlock(&chip->controller->lock);
282 mutex_unlock(&chip->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/**
Frieder Schrempff90da782019-04-17 12:36:37 +0000286 * nand_bbm_get_next_page - Get the next page for bad block markers
287 * @chip: NAND chip object
288 * @page: First page to start checking for bad block marker usage
289 *
290 * Returns an integer that corresponds to the page offset within a block, for
291 * a page that is used to store bad block markers. If no more pages are
292 * available, -EINVAL is returned.
293 */
294int nand_bbm_get_next_page(struct nand_chip *chip, int page)
295{
296 struct mtd_info *mtd = nand_to_mtd(chip);
297 int last_page = ((mtd->erasesize - mtd->writesize) >>
298 chip->page_shift) & chip->pagemask;
299
300 if (page == 0 && chip->options & NAND_BBM_FIRSTPAGE)
301 return 0;
302 else if (page <= 1 && chip->options & NAND_BBM_SECONDPAGE)
303 return 1;
304 else if (page <= last_page && chip->options & NAND_BBM_LASTPAGE)
305 return last_page;
306
307 return -EINVAL;
308}
309
310/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Boris Brezillonc17556f2018-09-06 14:05:25 +0200312 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700313 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000315 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200317static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Frieder Schrempff90da782019-04-17 12:36:37 +0000319 int first_page, page_offset;
320 int res;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900321 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Frieder Schrempff90da782019-04-17 12:36:37 +0000323 first_page = (int)(ofs >> chip->page_shift) & chip->pagemask;
324 page_offset = nand_bbm_get_next_page(chip, 0);
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700325
Frieder Schrempff90da782019-04-17 12:36:37 +0000326 while (page_offset >= 0) {
327 res = chip->ecc.read_oob(chip, first_page + page_offset);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530328 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900329 return res;
330
331 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000332
Brian Norriscdbec052012-01-13 18:11:48 -0800333 if (likely(chip->badblockbits == 8))
334 res = bad != 0xFF;
335 else
336 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900337 if (res)
338 return res;
Frieder Schrempff90da782019-04-17 12:36:37 +0000339
340 page_offset = nand_bbm_get_next_page(chip, page_offset + 1);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900341 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200342
Masahiro Yamadac120e752017-03-23 05:07:01 +0900343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Boris Brezillon99f33512018-11-11 08:55:04 +0100346static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
347{
348 if (chip->legacy.block_bad)
349 return chip->legacy.block_bad(chip, ofs);
350
351 return nand_block_bad(chip, ofs);
352}
353
354/**
Boris Brezillon99f33512018-11-11 08:55:04 +0100355 * nand_get_device - [GENERIC] Get chip for selected access
356 * @chip: NAND chip structure
Boris Brezillon99f33512018-11-11 08:55:04 +0100357 *
Boris Brezillon013e6292018-11-20 11:57:20 +0100358 * Lock the device and its controller for exclusive access
359 *
360 * Return: -EBUSY if the chip has been suspended, 0 otherwise
Boris Brezillon99f33512018-11-11 08:55:04 +0100361 */
Boris Brezillon013e6292018-11-20 11:57:20 +0100362static int nand_get_device(struct nand_chip *chip)
Boris Brezillon99f33512018-11-11 08:55:04 +0100363{
Boris Brezillon013e6292018-11-20 11:57:20 +0100364 mutex_lock(&chip->lock);
365 if (chip->suspended) {
366 mutex_unlock(&chip->lock);
367 return -EBUSY;
Boris Brezillon99f33512018-11-11 08:55:04 +0100368 }
Boris Brezillon013e6292018-11-20 11:57:20 +0100369 mutex_lock(&chip->controller->lock);
370
371 return 0;
Boris Brezillon99f33512018-11-11 08:55:04 +0100372}
373
374/**
375 * nand_check_wp - [GENERIC] check if the chip is write protected
376 * @chip: NAND chip object
377 *
378 * Check, if the device is write protected. The function expects, that the
379 * device is already selected.
380 */
381static int nand_check_wp(struct nand_chip *chip)
382{
383 u8 status;
384 int ret;
385
386 /* Broken xD cards report WP despite being writable */
387 if (chip->options & NAND_BROKEN_XD)
388 return 0;
389
390 /* Check the WP bit */
391 ret = nand_status_op(chip, &status);
392 if (ret)
393 return ret;
394
395 return status & NAND_STATUS_WP ? 0 : 1;
396}
397
398/**
399 * nand_fill_oob - [INTERN] Transfer client buffer to oob
Randy Dunlap455e7b32019-01-27 18:21:42 -0800400 * @chip: NAND chip object
Boris Brezillon99f33512018-11-11 08:55:04 +0100401 * @oob: oob data buffer
402 * @len: oob data write length
403 * @ops: oob ops structure
404 */
405static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
406 struct mtd_oob_ops *ops)
407{
408 struct mtd_info *mtd = nand_to_mtd(chip);
409 int ret;
410
411 /*
412 * Initialise to all 0xFF, to avoid the possibility of left over OOB
413 * data from a previous OOB read.
414 */
415 memset(chip->oob_poi, 0xff, mtd->oobsize);
416
417 switch (ops->mode) {
418
419 case MTD_OPS_PLACE_OOB:
420 case MTD_OPS_RAW:
421 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
422 return oob + len;
423
424 case MTD_OPS_AUTO_OOB:
425 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
426 ops->ooboffs, len);
427 BUG_ON(ret);
428 return oob + len;
429
430 default:
431 BUG();
432 }
433 return NULL;
434}
435
436/**
437 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
438 * @chip: NAND chip object
439 * @to: offset to write to
440 * @ops: oob operation description structure
441 *
442 * NAND write out-of-band.
443 */
444static int nand_do_write_oob(struct nand_chip *chip, loff_t to,
445 struct mtd_oob_ops *ops)
446{
447 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +0900448 int chipnr, page, status, len, ret;
Boris Brezillon99f33512018-11-11 08:55:04 +0100449
450 pr_debug("%s: to = 0x%08x, len = %i\n",
451 __func__, (unsigned int)to, (int)ops->ooblen);
452
453 len = mtd_oobavail(mtd, ops);
454
455 /* Do not allow write past end of page */
456 if ((ops->ooboffs + ops->ooblen) > len) {
457 pr_debug("%s: attempt to write past end of page\n",
458 __func__);
459 return -EINVAL;
460 }
461
462 chipnr = (int)(to >> chip->chip_shift);
463
464 /*
465 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
466 * of my DiskOnChip 2000 test units) will clear the whole data page too
467 * if we don't do this. I have no clue why, but I seem to have 'fixed'
468 * it in the doc2000 driver in August 1999. dwmw2.
469 */
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +0900470 ret = nand_reset(chip, chipnr);
471 if (ret)
472 return ret;
Boris Brezillon99f33512018-11-11 08:55:04 +0100473
Boris Brezillon1d017852018-11-11 08:55:14 +0100474 nand_select_target(chip, chipnr);
Boris Brezillon99f33512018-11-11 08:55:04 +0100475
476 /* Shift to get page */
477 page = (int)(to >> chip->page_shift);
478
479 /* Check, if it is write protected */
480 if (nand_check_wp(chip)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100481 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100482 return -EROFS;
483 }
484
485 /* Invalidate the page cache, if we write to the cached page */
Boris Brezillond9745412018-10-28 16:12:45 +0100486 if (page == chip->pagecache.page)
487 chip->pagecache.page = -1;
Boris Brezillon99f33512018-11-11 08:55:04 +0100488
489 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
490
491 if (ops->mode == MTD_OPS_RAW)
492 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
493 else
494 status = chip->ecc.write_oob(chip, page & chip->pagemask);
495
Boris Brezillon1d017852018-11-11 08:55:14 +0100496 nand_deselect_target(chip);
Boris Brezillon99f33512018-11-11 08:55:04 +0100497
498 if (status)
499 return status;
500
501 ops->oobretlen = ops->ooblen;
502
503 return 0;
504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700507 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200508 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700509 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700511 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700512 * specific driver. It provides the details for writing a bad block marker to a
513 * block.
514 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200515static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700516{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200517 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700518 struct mtd_oob_ops ops;
519 uint8_t buf[2] = { 0, 0 };
Frieder Schrempff90da782019-04-17 12:36:37 +0000520 int ret = 0, res, page_offset;
Brian Norris5a0edb22013-07-30 17:52:58 -0700521
Brian Norris0ec56dc2015-02-28 02:02:30 -0800522 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700523 ops.oobbuf = buf;
524 ops.ooboffs = chip->badblockpos;
525 if (chip->options & NAND_BUSWIDTH_16) {
526 ops.ooboffs &= ~0x01;
527 ops.len = ops.ooblen = 2;
528 } else {
529 ops.len = ops.ooblen = 1;
530 }
531 ops.mode = MTD_OPS_PLACE_OOB;
532
Frieder Schrempff90da782019-04-17 12:36:37 +0000533 page_offset = nand_bbm_get_next_page(chip, 0);
534
535 while (page_offset >= 0) {
536 res = nand_do_write_oob(chip,
537 ofs + (page_offset * mtd->writesize),
538 &ops);
539
Brian Norris5a0edb22013-07-30 17:52:58 -0700540 if (!ret)
541 ret = res;
542
Frieder Schrempff90da782019-04-17 12:36:37 +0000543 page_offset = nand_bbm_get_next_page(chip, page_offset + 1);
544 }
Brian Norris5a0edb22013-07-30 17:52:58 -0700545
546 return ret;
547}
548
549/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200550 * nand_markbad_bbm - mark a block by updating the BBM
551 * @chip: NAND chip object
552 * @ofs: offset of the block to mark bad
553 */
554int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
555{
556 if (chip->legacy.block_markbad)
557 return chip->legacy.block_markbad(chip, ofs);
558
559 return nand_default_block_markbad(chip, ofs);
560}
561
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200562/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700563 * nand_block_markbad_lowlevel - mark a block bad
Boris Brezillon08136212018-11-11 08:55:03 +0100564 * @chip: NAND chip object
Brian Norris5a0edb22013-07-30 17:52:58 -0700565 * @ofs: offset from device start
566 *
567 * This function performs the generic NAND bad block marking steps (i.e., bad
568 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200569 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700570 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700571 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300572 *
Brian Norrise2414f42012-02-06 13:44:00 -0800573 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700574 * (2) write bad block marker to OOB area of affected block (unless flag
575 * NAND_BBT_NO_OOB_BBM is present)
576 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300577 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700578 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800579 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580*/
Boris Brezillon08136212018-11-11 08:55:03 +0100581static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Boris Brezillon08136212018-11-11 08:55:03 +0100583 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisb32843b2013-07-30 17:52:59 -0700584 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000585
Brian Norrisb32843b2013-07-30 17:52:59 -0700586 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800587 struct erase_info einfo;
588
589 /* Attempt erase before marking OOB */
590 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800591 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300592 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200593 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800594
Brian Norrisb32843b2013-07-30 17:52:59 -0700595 /* Write bad block marker to OOB */
Boris Brezillon013e6292018-11-20 11:57:20 +0100596 ret = nand_get_device(chip);
597 if (ret)
598 return ret;
599
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200600 ret = nand_markbad_bbm(chip, ofs);
Boris Brezillon08136212018-11-11 08:55:03 +0100601 nand_release_device(chip);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200602 }
Brian Norrise2414f42012-02-06 13:44:00 -0800603
Brian Norrisb32843b2013-07-30 17:52:59 -0700604 /* Mark block bad in BBT */
605 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200606 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800607 if (!ret)
608 ret = res;
609 }
610
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200611 if (!ret)
612 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300613
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200614 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000617/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800618 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700619 * @mtd: MTD device structure
620 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300621 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800622 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300623 */
624static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
625{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100626 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300627
628 if (!chip->bbt)
629 return 0;
630 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200631 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300632}
633
634/**
635 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Boris Brezillon08136212018-11-11 08:55:03 +0100636 * @chip: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300637 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700638 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 *
640 * Check, if the block is bad. Either by reading the bad block table or
641 * calling of the scan function.
642 */
Boris Brezillon08136212018-11-11 08:55:03 +0100643static int nand_block_checkbad(struct nand_chip *chip, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200646 if (chip->bbt)
647 return nand_isbad_bbt(chip, ofs, allowbbt);
648
649 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200652/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100653 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
654 * @chip: NAND chip structure
655 * @timeout_ms: Timeout in ms
656 *
657 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
658 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
659 * returned.
660 *
661 * This helper is intended to be used when the controller does not have access
662 * to the NAND R/B pin.
663 *
664 * Be aware that calling this helper from an ->exec_op() implementation means
665 * ->exec_op() must be re-entrant.
666 *
667 * Return 0 if the NAND chip is ready, a negative error otherwise.
668 */
669int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
670{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200671 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100672 u8 status = 0;
673 int ret;
674
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100675 if (!nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +0100676 return -ENOTSUPP;
677
Boris Brezillon3057fce2018-05-04 21:24:31 +0200678 /* Wait tWB before polling the STATUS reg. */
679 timings = nand_get_sdr_timings(&chip->data_interface);
680 ndelay(PSEC_TO_NSEC(timings->tWB_max));
681
Miquel Raynal8878b122017-11-09 14:16:45 +0100682 ret = nand_status_op(chip, NULL);
683 if (ret)
684 return ret;
685
686 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
687 do {
688 ret = nand_read_data_op(chip, &status, sizeof(status), true);
689 if (ret)
690 break;
691
692 if (status & NAND_STATUS_READY)
693 break;
694
695 /*
696 * Typical lowest execution time for a tR on most NANDs is 10us,
697 * use this as polling delay before doing something smarter (ie.
698 * deriving a delay from the timeout value, timeout_ms/ratio).
699 */
700 udelay(10);
701 } while (time_before(jiffies, timeout_ms));
702
703 /*
704 * We have to exit READ_STATUS mode in order to read real data on the
705 * bus in case the WAITRDY instruction is preceding a DATA_IN
706 * instruction.
707 */
708 nand_exit_status_op(chip);
709
710 if (ret)
711 return ret;
712
713 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
714};
715EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
716
717/**
Janusz Krzysztofikb0e137a2018-10-15 21:41:28 +0200718 * nand_gpio_waitrdy - Poll R/B GPIO pin until ready
719 * @chip: NAND chip structure
720 * @gpiod: GPIO descriptor of R/B pin
721 * @timeout_ms: Timeout in ms
722 *
723 * Poll the R/B GPIO pin until it becomes ready. If that does not happen
724 * whitin the specified timeout, -ETIMEDOUT is returned.
725 *
726 * This helper is intended to be used when the controller has access to the
727 * NAND R/B pin over GPIO.
728 *
729 * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise.
730 */
731int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
732 unsigned long timeout_ms)
733{
734 /* Wait until R/B pin indicates chip is ready or timeout occurs */
735 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
736 do {
737 if (gpiod_get_value_cansleep(gpiod))
738 return 0;
739
740 cond_resched();
741 } while (time_before(jiffies, timeout_ms));
742
743 return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT;
744};
745EXPORT_SYMBOL_GPL(nand_gpio_waitrdy);
746
747/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700748 * panic_nand_wait - [GENERIC] wait until the command is done
Brian Norris8b6e50c2011-05-25 14:59:01 -0700749 * @chip: NAND chip structure
750 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200751 *
752 * Wait for command done. This is a helper function for nand_wait used when
753 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400754 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200755 */
Boris Brezillon3d4af7c2018-09-07 00:38:49 +0200756void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200757{
758 int i;
759 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200760 if (chip->legacy.dev_ready) {
761 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200762 break;
763 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100764 int ret;
765 u8 status;
766
767 ret = nand_read_data_op(chip, &status, sizeof(status),
768 true);
769 if (ret)
770 return;
771
772 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200773 break;
774 }
775 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200776 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200777}
778
Miquel Raynal789157e2018-03-19 14:47:28 +0100779static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +0100780{
Miquel Raynal789157e2018-03-19 14:47:28 +0100781 return (chip->parameters.supports_set_get_features &&
782 test_bit(addr, chip->parameters.get_feature_list));
783}
784
785static bool nand_supports_set_features(struct nand_chip *chip, int addr)
786{
787 return (chip->parameters.supports_set_get_features &&
788 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +0100789}
790
791/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200792 * nand_reset_data_interface - Reset data interface and timings
793 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100794 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200795 *
796 * Reset the Data interface and timings to ONFI mode 0.
797 *
798 * Returns 0 for success or negative error code otherwise.
799 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100800static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200801{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200802 int ret;
803
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100804 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200805 return 0;
806
807 /*
808 * The ONFI specification says:
809 * "
810 * To transition from NV-DDR or NV-DDR2 to the SDR data
811 * interface, the host shall use the Reset (FFh) command
812 * using SDR timing mode 0. A device in any timing mode is
813 * required to recognize Reset (FFh) command issued in SDR
814 * timing mode 0.
815 * "
816 *
817 * Configure the data interface in SDR mode and set the
818 * timings to timing mode 0.
819 */
820
Miquel Raynal17fa8042017-11-30 18:01:31 +0100821 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100822 ret = chip->controller->ops->setup_data_interface(chip, chipnr,
823 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200824 if (ret)
825 pr_err("Failed to configure data interface to SDR timing mode 0\n");
826
827 return ret;
828}
829
830/**
831 * nand_setup_data_interface - Setup the best data interface and timings
832 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +0100833 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +0200834 *
835 * Find and configure the best data interface and NAND timings supported by
836 * the chip and the driver.
837 * First tries to retrieve supported timing modes from ONFI information,
838 * and if the NAND chip does not support ONFI, relies on the
839 * ->onfi_timing_mode_default specified in the nand_ids table.
840 *
841 * Returns 0 for success or negative error code otherwise.
842 */
Boris Brezillon104e4422017-03-16 09:35:58 +0100843static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +0200844{
Miquel Raynal97baea12018-03-19 14:47:20 +0100845 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
846 chip->onfi_timing_mode_default,
847 };
Boris Brezillond8e725d2016-09-15 10:32:50 +0200848 int ret;
849
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100850 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200851 return 0;
852
Miquel Raynal993447b2018-03-19 14:47:21 +0100853 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +0100854 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon1d017852018-11-11 08:55:14 +0100855 nand_select_target(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +0100856 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
857 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100858 nand_deselect_target(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200859 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +0100860 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +0200861 }
862
Miquel Raynal97baea12018-03-19 14:47:20 +0100863 /* Change the mode on the controller side */
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100864 ret = chip->controller->ops->setup_data_interface(chip, chipnr,
865 &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +0100866 if (ret)
867 return ret;
868
869 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +0100870 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +0100871 return 0;
872
873 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon1d017852018-11-11 08:55:14 +0100874 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100875 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
876 tmode_param);
Boris Brezillon1d017852018-11-11 08:55:14 +0100877 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100878 if (ret)
879 goto err_reset_chip;
880
881 if (tmode_param[0] != chip->onfi_timing_mode_default) {
882 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
883 chip->onfi_timing_mode_default);
884 goto err_reset_chip;
885 }
886
887 return 0;
888
889err_reset_chip:
890 /*
891 * Fallback to mode 0 if the chip explicitly did not ack the chosen
892 * timing mode.
893 */
894 nand_reset_data_interface(chip, chipnr);
Boris Brezillon1d017852018-11-11 08:55:14 +0100895 nand_select_target(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +0100896 nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +0100897 nand_deselect_target(chip);
Miquel Raynal415ae782018-03-19 14:47:24 +0100898
Boris Brezillond8e725d2016-09-15 10:32:50 +0200899 return ret;
900}
901
902/**
903 * nand_init_data_interface - find the best data interface and timings
904 * @chip: The NAND chip
905 *
906 * Find the best data interface and NAND timings supported by the chip
907 * and the driver.
908 * First tries to retrieve supported timing modes from ONFI information,
909 * and if the NAND chip does not support ONFI, relies on the
910 * ->onfi_timing_mode_default specified in the nand_ids table. After this
911 * function nand_chip->data_interface is initialized with the best timing mode
912 * available.
913 *
914 * Returns 0 for success or negative error code otherwise.
915 */
916static int nand_init_data_interface(struct nand_chip *chip)
917{
Boris Brezillond8e725d2016-09-15 10:32:50 +0200918 int modes, mode, ret;
919
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100920 if (!nand_has_setup_data_iface(chip))
Boris Brezillond8e725d2016-09-15 10:32:50 +0200921 return 0;
922
923 /*
924 * First try to identify the best timings from ONFI parameters and
925 * if the NAND does not support ONFI, fallback to the default ONFI
926 * timing mode.
927 */
Boris Brezillon462f35d2018-09-07 00:38:47 +0200928 if (chip->parameters.onfi) {
929 modes = chip->parameters.onfi->async_timing_mode;
930 } else {
Boris Brezillond8e725d2016-09-15 10:32:50 +0200931 if (!chip->onfi_timing_mode_default)
932 return 0;
933
934 modes = GENMASK(chip->onfi_timing_mode_default, 0);
935 }
936
Boris Brezillond8e725d2016-09-15 10:32:50 +0200937 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +0100938 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200939 if (ret)
940 continue;
941
Miquel Raynald787b8b2017-12-22 18:12:41 +0100942 /*
943 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
944 * controller supports the requested timings.
945 */
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100946 ret = chip->controller->ops->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +0100947 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +0100948 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +0200949 if (!ret) {
950 chip->onfi_timing_mode_default = mode;
951 break;
952 }
953 }
954
955 return 0;
956}
957
Boris Brezillond8e725d2016-09-15 10:32:50 +0200958/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100959 * nand_fill_column_cycles - fill the column cycles of an address
960 * @chip: The NAND chip
961 * @addrs: Array of address cycles to fill
962 * @offset_in_page: The offset in the page
963 *
964 * Fills the first or the first two bytes of the @addrs field depending
965 * on the NAND bus width and the page size.
966 *
967 * Returns the number of cycles needed to encode the column, or a negative
968 * error code in case one of the arguments is invalid.
969 */
970static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
971 unsigned int offset_in_page)
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100972{
Miquel Raynal8878b122017-11-09 14:16:45 +0100973 struct mtd_info *mtd = nand_to_mtd(chip);
974
975 /* Make sure the offset is less than the actual page size. */
976 if (offset_in_page > mtd->writesize + mtd->oobsize)
977 return -EINVAL;
978
979 /*
980 * On small page NANDs, there's a dedicated command to access the OOB
981 * area, and the column address is relative to the start of the OOB
982 * area, not the start of the page. Asjust the address accordingly.
983 */
984 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
985 offset_in_page -= mtd->writesize;
986
987 /*
988 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
989 * wide, then it must be divided by 2.
990 */
991 if (chip->options & NAND_BUSWIDTH_16) {
992 if (WARN_ON(offset_in_page % 2))
993 return -EINVAL;
994
995 offset_in_page /= 2;
996 }
997
998 addrs[0] = offset_in_page;
999
1000 /*
1001 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1002 * need 2
1003 */
1004 if (mtd->writesize <= 512)
1005 return 1;
1006
1007 addrs[1] = offset_in_page >> 8;
1008
1009 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Miquel Raynal8878b122017-11-09 14:16:45 +01001012static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1013 unsigned int offset_in_page, void *buf,
1014 unsigned int len)
1015{
1016 struct mtd_info *mtd = nand_to_mtd(chip);
1017 const struct nand_sdr_timings *sdr =
1018 nand_get_sdr_timings(&chip->data_interface);
1019 u8 addrs[4];
1020 struct nand_op_instr instrs[] = {
1021 NAND_OP_CMD(NAND_CMD_READ0, 0),
1022 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1023 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1024 PSEC_TO_NSEC(sdr->tRR_min)),
1025 NAND_OP_DATA_IN(len, buf, 0),
1026 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001027 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001028 int ret;
1029
1030 /* Drop the DATA_IN instruction if len is set to 0. */
1031 if (!len)
1032 op.ninstrs--;
1033
1034 if (offset_in_page >= mtd->writesize)
1035 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1036 else if (offset_in_page >= 256 &&
1037 !(chip->options & NAND_BUSWIDTH_16))
1038 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1039
1040 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1041 if (ret < 0)
1042 return ret;
1043
1044 addrs[1] = page;
1045 addrs[2] = page >> 8;
1046
1047 if (chip->options & NAND_ROW_ADDR_3) {
1048 addrs[3] = page >> 16;
1049 instrs[1].ctx.addr.naddrs++;
1050 }
1051
1052 return nand_exec_op(chip, &op);
1053}
1054
1055static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1056 unsigned int offset_in_page, void *buf,
1057 unsigned int len)
1058{
1059 const struct nand_sdr_timings *sdr =
1060 nand_get_sdr_timings(&chip->data_interface);
1061 u8 addrs[5];
1062 struct nand_op_instr instrs[] = {
1063 NAND_OP_CMD(NAND_CMD_READ0, 0),
1064 NAND_OP_ADDR(4, addrs, 0),
1065 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1066 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1067 PSEC_TO_NSEC(sdr->tRR_min)),
1068 NAND_OP_DATA_IN(len, buf, 0),
1069 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001070 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001071 int ret;
1072
1073 /* Drop the DATA_IN instruction if len is set to 0. */
1074 if (!len)
1075 op.ninstrs--;
1076
1077 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1078 if (ret < 0)
1079 return ret;
1080
1081 addrs[2] = page;
1082 addrs[3] = page >> 8;
1083
1084 if (chip->options & NAND_ROW_ADDR_3) {
1085 addrs[4] = page >> 16;
1086 instrs[1].ctx.addr.naddrs++;
1087 }
1088
1089 return nand_exec_op(chip, &op);
1090}
1091
1092/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001093 * nand_read_page_op - Do a READ PAGE operation
1094 * @chip: The NAND chip
1095 * @page: page to read
1096 * @offset_in_page: offset within the page
1097 * @buf: buffer used to store the data
1098 * @len: length of the buffer
1099 *
1100 * This function issues a READ PAGE operation.
1101 * This function does not select/unselect the CS line.
1102 *
1103 * Returns 0 on success, a negative error code otherwise.
1104 */
1105int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1106 unsigned int offset_in_page, void *buf, unsigned int len)
1107{
1108 struct mtd_info *mtd = nand_to_mtd(chip);
1109
1110 if (len && !buf)
1111 return -EINVAL;
1112
1113 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1114 return -EINVAL;
1115
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001116 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001117 if (mtd->writesize > 512)
1118 return nand_lp_exec_read_page_op(chip, page,
1119 offset_in_page, buf,
1120 len);
1121
1122 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1123 buf, len);
1124 }
1125
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001126 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001127 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001128 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001129
1130 return 0;
1131}
1132EXPORT_SYMBOL_GPL(nand_read_page_op);
1133
1134/**
1135 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1136 * @chip: The NAND chip
1137 * @page: parameter page to read
1138 * @buf: buffer used to store the data
1139 * @len: length of the buffer
1140 *
1141 * This function issues a READ PARAMETER PAGE operation.
1142 * This function does not select/unselect the CS line.
1143 *
1144 * Returns 0 on success, a negative error code otherwise.
1145 */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02001146int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1147 unsigned int len)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001148{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001149 unsigned int i;
1150 u8 *p = buf;
1151
1152 if (len && !buf)
1153 return -EINVAL;
1154
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001155 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001156 const struct nand_sdr_timings *sdr =
1157 nand_get_sdr_timings(&chip->data_interface);
1158 struct nand_op_instr instrs[] = {
1159 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1160 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1161 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1162 PSEC_TO_NSEC(sdr->tRR_min)),
1163 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1164 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001165 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001166
1167 /* Drop the DATA_IN instruction if len is set to 0. */
1168 if (!len)
1169 op.ninstrs--;
1170
1171 return nand_exec_op(chip, &op);
1172 }
1173
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001174 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001175 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001176 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001177
1178 return 0;
1179}
1180
1181/**
1182 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1183 * @chip: The NAND chip
1184 * @offset_in_page: offset within the page
1185 * @buf: buffer used to store the data
1186 * @len: length of the buffer
1187 * @force_8bit: force 8-bit bus access
1188 *
1189 * This function issues a CHANGE READ COLUMN operation.
1190 * This function does not select/unselect the CS line.
1191 *
1192 * Returns 0 on success, a negative error code otherwise.
1193 */
1194int nand_change_read_column_op(struct nand_chip *chip,
1195 unsigned int offset_in_page, void *buf,
1196 unsigned int len, bool force_8bit)
1197{
1198 struct mtd_info *mtd = nand_to_mtd(chip);
1199
1200 if (len && !buf)
1201 return -EINVAL;
1202
1203 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1204 return -EINVAL;
1205
Miquel Raynal8878b122017-11-09 14:16:45 +01001206 /* Small page NANDs do not support column change. */
1207 if (mtd->writesize <= 512)
1208 return -ENOTSUPP;
1209
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001210 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001211 const struct nand_sdr_timings *sdr =
1212 nand_get_sdr_timings(&chip->data_interface);
1213 u8 addrs[2] = {};
1214 struct nand_op_instr instrs[] = {
1215 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1216 NAND_OP_ADDR(2, addrs, 0),
1217 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1218 PSEC_TO_NSEC(sdr->tCCS_min)),
1219 NAND_OP_DATA_IN(len, buf, 0),
1220 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001221 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001222 int ret;
1223
1224 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1225 if (ret < 0)
1226 return ret;
1227
1228 /* Drop the DATA_IN instruction if len is set to 0. */
1229 if (!len)
1230 op.ninstrs--;
1231
1232 instrs[3].ctx.data.force_8bit = force_8bit;
1233
1234 return nand_exec_op(chip, &op);
1235 }
1236
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001237 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001238 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001239 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001240
1241 return 0;
1242}
1243EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1244
1245/**
1246 * nand_read_oob_op - Do a READ OOB operation
1247 * @chip: The NAND chip
1248 * @page: page to read
1249 * @offset_in_oob: offset within the OOB area
1250 * @buf: buffer used to store the data
1251 * @len: length of the buffer
1252 *
1253 * This function issues a READ OOB operation.
1254 * This function does not select/unselect the CS line.
1255 *
1256 * Returns 0 on success, a negative error code otherwise.
1257 */
1258int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1259 unsigned int offset_in_oob, void *buf, unsigned int len)
1260{
1261 struct mtd_info *mtd = nand_to_mtd(chip);
1262
1263 if (len && !buf)
1264 return -EINVAL;
1265
1266 if (offset_in_oob + len > mtd->oobsize)
1267 return -EINVAL;
1268
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001269 if (nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +01001270 return nand_read_page_op(chip, page,
1271 mtd->writesize + offset_in_oob,
1272 buf, len);
1273
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001274 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001275 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001276 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001277
1278 return 0;
1279}
1280EXPORT_SYMBOL_GPL(nand_read_oob_op);
1281
Miquel Raynal8878b122017-11-09 14:16:45 +01001282static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1283 unsigned int offset_in_page, const void *buf,
1284 unsigned int len, bool prog)
1285{
1286 struct mtd_info *mtd = nand_to_mtd(chip);
1287 const struct nand_sdr_timings *sdr =
1288 nand_get_sdr_timings(&chip->data_interface);
1289 u8 addrs[5] = {};
1290 struct nand_op_instr instrs[] = {
1291 /*
1292 * The first instruction will be dropped if we're dealing
1293 * with a large page NAND and adjusted if we're dealing
1294 * with a small page NAND and the page offset is > 255.
1295 */
1296 NAND_OP_CMD(NAND_CMD_READ0, 0),
1297 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1298 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1299 NAND_OP_DATA_OUT(len, buf, 0),
1300 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1301 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1302 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001303 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001304 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1305 int ret;
1306 u8 status;
1307
1308 if (naddrs < 0)
1309 return naddrs;
1310
1311 addrs[naddrs++] = page;
1312 addrs[naddrs++] = page >> 8;
1313 if (chip->options & NAND_ROW_ADDR_3)
1314 addrs[naddrs++] = page >> 16;
1315
1316 instrs[2].ctx.addr.naddrs = naddrs;
1317
1318 /* Drop the last two instructions if we're not programming the page. */
1319 if (!prog) {
1320 op.ninstrs -= 2;
1321 /* Also drop the DATA_OUT instruction if empty. */
1322 if (!len)
1323 op.ninstrs--;
1324 }
1325
1326 if (mtd->writesize <= 512) {
1327 /*
1328 * Small pages need some more tweaking: we have to adjust the
1329 * first instruction depending on the page offset we're trying
1330 * to access.
1331 */
1332 if (offset_in_page >= mtd->writesize)
1333 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1334 else if (offset_in_page >= 256 &&
1335 !(chip->options & NAND_BUSWIDTH_16))
1336 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1337 } else {
1338 /*
1339 * Drop the first command if we're dealing with a large page
1340 * NAND.
1341 */
1342 op.instrs++;
1343 op.ninstrs--;
1344 }
1345
1346 ret = nand_exec_op(chip, &op);
1347 if (!prog || ret)
1348 return ret;
1349
1350 ret = nand_status_op(chip, &status);
1351 if (ret)
1352 return ret;
1353
1354 return status;
1355}
1356
Boris Brezillon97d90da2017-11-30 18:01:29 +01001357/**
1358 * nand_prog_page_begin_op - starts a PROG PAGE operation
1359 * @chip: The NAND chip
1360 * @page: page to write
1361 * @offset_in_page: offset within the page
1362 * @buf: buffer containing the data to write to the page
1363 * @len: length of the buffer
1364 *
1365 * This function issues the first half of a PROG PAGE operation.
1366 * This function does not select/unselect the CS line.
1367 *
1368 * Returns 0 on success, a negative error code otherwise.
1369 */
1370int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1371 unsigned int offset_in_page, const void *buf,
1372 unsigned int len)
1373{
1374 struct mtd_info *mtd = nand_to_mtd(chip);
1375
1376 if (len && !buf)
1377 return -EINVAL;
1378
1379 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1380 return -EINVAL;
1381
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001382 if (nand_has_exec_op(chip))
Miquel Raynal8878b122017-11-09 14:16:45 +01001383 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1384 len, false);
1385
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001386 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001387
1388 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001389 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001390
1391 return 0;
1392}
1393EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1394
1395/**
1396 * nand_prog_page_end_op - ends a PROG PAGE operation
1397 * @chip: The NAND chip
1398 *
1399 * This function issues the second half of a PROG PAGE operation.
1400 * This function does not select/unselect the CS line.
1401 *
1402 * Returns 0 on success, a negative error code otherwise.
1403 */
1404int nand_prog_page_end_op(struct nand_chip *chip)
1405{
Miquel Raynal8878b122017-11-09 14:16:45 +01001406 int ret;
1407 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001408
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001409 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001410 const struct nand_sdr_timings *sdr =
1411 nand_get_sdr_timings(&chip->data_interface);
1412 struct nand_op_instr instrs[] = {
1413 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1414 PSEC_TO_NSEC(sdr->tWB_max)),
1415 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1416 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001417 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001418
Miquel Raynal8878b122017-11-09 14:16:45 +01001419 ret = nand_exec_op(chip, &op);
1420 if (ret)
1421 return ret;
1422
1423 ret = nand_status_op(chip, &status);
1424 if (ret)
1425 return ret;
1426 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001427 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001428 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001429 if (ret < 0)
1430 return ret;
1431
1432 status = ret;
1433 }
1434
Boris Brezillon97d90da2017-11-30 18:01:29 +01001435 if (status & NAND_STATUS_FAIL)
1436 return -EIO;
1437
1438 return 0;
1439}
1440EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1441
1442/**
1443 * nand_prog_page_op - Do a full PROG PAGE operation
1444 * @chip: The NAND chip
1445 * @page: page to write
1446 * @offset_in_page: offset within the page
1447 * @buf: buffer containing the data to write to the page
1448 * @len: length of the buffer
1449 *
1450 * This function issues a full PROG PAGE operation.
1451 * This function does not select/unselect the CS line.
1452 *
1453 * Returns 0 on success, a negative error code otherwise.
1454 */
1455int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1456 unsigned int offset_in_page, const void *buf,
1457 unsigned int len)
1458{
1459 struct mtd_info *mtd = nand_to_mtd(chip);
1460 int status;
1461
1462 if (!len || !buf)
1463 return -EINVAL;
1464
1465 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1466 return -EINVAL;
1467
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001468 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001469 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1470 len, true);
1471 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001472 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1473 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001474 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001475 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001476 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001477 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001478
Boris Brezillon97d90da2017-11-30 18:01:29 +01001479 if (status & NAND_STATUS_FAIL)
1480 return -EIO;
1481
1482 return 0;
1483}
1484EXPORT_SYMBOL_GPL(nand_prog_page_op);
1485
1486/**
1487 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1488 * @chip: The NAND chip
1489 * @offset_in_page: offset within the page
1490 * @buf: buffer containing the data to send to the NAND
1491 * @len: length of the buffer
1492 * @force_8bit: force 8-bit bus access
1493 *
1494 * This function issues a CHANGE WRITE COLUMN operation.
1495 * This function does not select/unselect the CS line.
1496 *
1497 * Returns 0 on success, a negative error code otherwise.
1498 */
1499int nand_change_write_column_op(struct nand_chip *chip,
1500 unsigned int offset_in_page,
1501 const void *buf, unsigned int len,
1502 bool force_8bit)
1503{
1504 struct mtd_info *mtd = nand_to_mtd(chip);
1505
1506 if (len && !buf)
1507 return -EINVAL;
1508
1509 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1510 return -EINVAL;
1511
Miquel Raynal8878b122017-11-09 14:16:45 +01001512 /* Small page NANDs do not support column change. */
1513 if (mtd->writesize <= 512)
1514 return -ENOTSUPP;
1515
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001516 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001517 const struct nand_sdr_timings *sdr =
1518 nand_get_sdr_timings(&chip->data_interface);
1519 u8 addrs[2];
1520 struct nand_op_instr instrs[] = {
1521 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1522 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1523 NAND_OP_DATA_OUT(len, buf, 0),
1524 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001525 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001526 int ret;
1527
1528 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1529 if (ret < 0)
1530 return ret;
1531
1532 instrs[2].ctx.data.force_8bit = force_8bit;
1533
1534 /* Drop the DATA_OUT instruction if len is set to 0. */
1535 if (!len)
1536 op.ninstrs--;
1537
1538 return nand_exec_op(chip, &op);
1539 }
1540
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001541 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001542 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001543 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001544
1545 return 0;
1546}
1547EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1548
1549/**
1550 * nand_readid_op - Do a READID operation
1551 * @chip: The NAND chip
1552 * @addr: address cycle to pass after the READID command
1553 * @buf: buffer used to store the ID
1554 * @len: length of the buffer
1555 *
1556 * This function sends a READID command and reads back the ID returned by the
1557 * NAND.
1558 * This function does not select/unselect the CS line.
1559 *
1560 * Returns 0 on success, a negative error code otherwise.
1561 */
1562int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1563 unsigned int len)
1564{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001565 unsigned int i;
1566 u8 *id = buf;
1567
1568 if (len && !buf)
1569 return -EINVAL;
1570
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001571 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001572 const struct nand_sdr_timings *sdr =
1573 nand_get_sdr_timings(&chip->data_interface);
1574 struct nand_op_instr instrs[] = {
1575 NAND_OP_CMD(NAND_CMD_READID, 0),
1576 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1577 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1578 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001579 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001580
1581 /* Drop the DATA_IN instruction if len is set to 0. */
1582 if (!len)
1583 op.ninstrs--;
1584
1585 return nand_exec_op(chip, &op);
1586 }
1587
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001588 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001589
1590 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001591 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001592
1593 return 0;
1594}
1595EXPORT_SYMBOL_GPL(nand_readid_op);
1596
1597/**
1598 * nand_status_op - Do a STATUS operation
1599 * @chip: The NAND chip
1600 * @status: out variable to store the NAND status
1601 *
1602 * This function sends a STATUS command and reads back the status returned by
1603 * the NAND.
1604 * This function does not select/unselect the CS line.
1605 *
1606 * Returns 0 on success, a negative error code otherwise.
1607 */
1608int nand_status_op(struct nand_chip *chip, u8 *status)
1609{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001610 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001611 const struct nand_sdr_timings *sdr =
1612 nand_get_sdr_timings(&chip->data_interface);
1613 struct nand_op_instr instrs[] = {
1614 NAND_OP_CMD(NAND_CMD_STATUS,
1615 PSEC_TO_NSEC(sdr->tADL_min)),
1616 NAND_OP_8BIT_DATA_IN(1, status, 0),
1617 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001618 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001619
1620 if (!status)
1621 op.ninstrs--;
1622
1623 return nand_exec_op(chip, &op);
1624 }
1625
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001626 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001627 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001628 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001629
1630 return 0;
1631}
1632EXPORT_SYMBOL_GPL(nand_status_op);
1633
1634/**
1635 * nand_exit_status_op - Exit a STATUS operation
1636 * @chip: The NAND chip
1637 *
1638 * This function sends a READ0 command to cancel the effect of the STATUS
1639 * command to avoid reading only the status until a new read command is sent.
1640 *
1641 * This function does not select/unselect the CS line.
1642 *
1643 * Returns 0 on success, a negative error code otherwise.
1644 */
1645int nand_exit_status_op(struct nand_chip *chip)
1646{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001647 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001648 struct nand_op_instr instrs[] = {
1649 NAND_OP_CMD(NAND_CMD_READ0, 0),
1650 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001651 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001652
1653 return nand_exec_op(chip, &op);
1654 }
1655
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001656 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001657
1658 return 0;
1659}
Boris Brezillon97d90da2017-11-30 18:01:29 +01001660
1661/**
1662 * nand_erase_op - Do an erase operation
1663 * @chip: The NAND chip
1664 * @eraseblock: block to erase
1665 *
1666 * This function sends an ERASE command and waits for the NAND to be ready
1667 * before returning.
1668 * This function does not select/unselect the CS line.
1669 *
1670 * Returns 0 on success, a negative error code otherwise.
1671 */
1672int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1673{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001674 unsigned int page = eraseblock <<
1675 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01001676 int ret;
1677 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001678
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001679 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001680 const struct nand_sdr_timings *sdr =
1681 nand_get_sdr_timings(&chip->data_interface);
1682 u8 addrs[3] = { page, page >> 8, page >> 16 };
1683 struct nand_op_instr instrs[] = {
1684 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
1685 NAND_OP_ADDR(2, addrs, 0),
1686 NAND_OP_CMD(NAND_CMD_ERASE2,
1687 PSEC_TO_MSEC(sdr->tWB_max)),
1688 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
1689 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001690 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001691
Miquel Raynal8878b122017-11-09 14:16:45 +01001692 if (chip->options & NAND_ROW_ADDR_3)
1693 instrs[1].ctx.addr.naddrs++;
1694
1695 ret = nand_exec_op(chip, &op);
1696 if (ret)
1697 return ret;
1698
1699 ret = nand_status_op(chip, &status);
1700 if (ret)
1701 return ret;
1702 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001703 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
1704 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01001705
Boris Brezillon8395b752018-09-07 00:38:37 +02001706 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001707 if (ret < 0)
1708 return ret;
1709
1710 status = ret;
1711 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001712
1713 if (status & NAND_STATUS_FAIL)
1714 return -EIO;
1715
1716 return 0;
1717}
1718EXPORT_SYMBOL_GPL(nand_erase_op);
1719
1720/**
1721 * nand_set_features_op - Do a SET FEATURES operation
1722 * @chip: The NAND chip
1723 * @feature: feature id
1724 * @data: 4 bytes of data
1725 *
1726 * This function sends a SET FEATURES command and waits for the NAND to be
1727 * ready before returning.
1728 * This function does not select/unselect the CS line.
1729 *
1730 * Returns 0 on success, a negative error code otherwise.
1731 */
1732static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1733 const void *data)
1734{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001735 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01001736 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001737
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001738 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001739 const struct nand_sdr_timings *sdr =
1740 nand_get_sdr_timings(&chip->data_interface);
1741 struct nand_op_instr instrs[] = {
1742 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
1743 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
1744 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
1745 PSEC_TO_NSEC(sdr->tWB_max)),
1746 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
1747 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001748 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001749
Boris Brezillon782d1962018-05-11 14:44:07 +02001750 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01001751 }
1752
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001753 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02001754 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001755 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02001756
Boris Brezillon8395b752018-09-07 00:38:37 +02001757 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02001758 if (ret < 0)
1759 return ret;
1760
1761 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01001762 return -EIO;
1763
1764 return 0;
1765}
1766
1767/**
1768 * nand_get_features_op - Do a GET FEATURES operation
1769 * @chip: The NAND chip
1770 * @feature: feature id
1771 * @data: 4 bytes of data
1772 *
1773 * This function sends a GET FEATURES command and waits for the NAND to be
1774 * ready before returning.
1775 * This function does not select/unselect the CS line.
1776 *
1777 * Returns 0 on success, a negative error code otherwise.
1778 */
1779static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1780 void *data)
1781{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001782 u8 *params = data;
1783 int i;
1784
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001785 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001786 const struct nand_sdr_timings *sdr =
1787 nand_get_sdr_timings(&chip->data_interface);
1788 struct nand_op_instr instrs[] = {
1789 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
1790 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
1791 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
1792 PSEC_TO_NSEC(sdr->tRR_min)),
1793 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
1794 data, 0),
1795 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001796 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001797
1798 return nand_exec_op(chip, &op);
1799 }
1800
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001801 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001802 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001803 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001804
1805 return 0;
1806}
1807
Boris Brezillon52f05b62018-07-27 09:44:18 +02001808static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
1809 unsigned int delay_ns)
1810{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001811 if (nand_has_exec_op(chip)) {
Boris Brezillon52f05b62018-07-27 09:44:18 +02001812 struct nand_op_instr instrs[] = {
1813 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
1814 PSEC_TO_NSEC(delay_ns)),
1815 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001816 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001817
1818 return nand_exec_op(chip, &op);
1819 }
1820
1821 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02001822 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001823 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001824 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001825 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02001826
1827 return 0;
1828}
1829
Boris Brezillon97d90da2017-11-30 18:01:29 +01001830/**
1831 * nand_reset_op - Do a reset operation
1832 * @chip: The NAND chip
1833 *
1834 * This function sends a RESET command and waits for the NAND to be ready
1835 * before returning.
1836 * This function does not select/unselect the CS line.
1837 *
1838 * Returns 0 on success, a negative error code otherwise.
1839 */
1840int nand_reset_op(struct nand_chip *chip)
1841{
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001842 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001843 const struct nand_sdr_timings *sdr =
1844 nand_get_sdr_timings(&chip->data_interface);
1845 struct nand_op_instr instrs[] = {
1846 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
1847 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
1848 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001849 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001850
1851 return nand_exec_op(chip, &op);
1852 }
1853
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001854 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001855
1856 return 0;
1857}
1858EXPORT_SYMBOL_GPL(nand_reset_op);
1859
1860/**
1861 * nand_read_data_op - Read data from the NAND
1862 * @chip: The NAND chip
1863 * @buf: buffer used to store the data
1864 * @len: length of the buffer
1865 * @force_8bit: force 8-bit bus access
1866 *
1867 * This function does a raw data read on the bus. Usually used after launching
1868 * another NAND operation like nand_read_page_op().
1869 * This function does not select/unselect the CS line.
1870 *
1871 * Returns 0 on success, a negative error code otherwise.
1872 */
1873int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1874 bool force_8bit)
1875{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001876 if (!len || !buf)
1877 return -EINVAL;
1878
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001879 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001880 struct nand_op_instr instrs[] = {
1881 NAND_OP_DATA_IN(len, buf, 0),
1882 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001883 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001884
1885 instrs[0].ctx.data.force_8bit = force_8bit;
1886
1887 return nand_exec_op(chip, &op);
1888 }
1889
Boris Brezillon97d90da2017-11-30 18:01:29 +01001890 if (force_8bit) {
1891 u8 *p = buf;
1892 unsigned int i;
1893
1894 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001895 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001896 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001897 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001898 }
1899
1900 return 0;
1901}
1902EXPORT_SYMBOL_GPL(nand_read_data_op);
1903
1904/**
1905 * nand_write_data_op - Write data from the NAND
1906 * @chip: The NAND chip
1907 * @buf: buffer containing the data to send on the bus
1908 * @len: length of the buffer
1909 * @force_8bit: force 8-bit bus access
1910 *
1911 * This function does a raw data write on the bus. Usually used after launching
1912 * another NAND operation like nand_write_page_begin_op().
1913 * This function does not select/unselect the CS line.
1914 *
1915 * Returns 0 on success, a negative error code otherwise.
1916 */
1917int nand_write_data_op(struct nand_chip *chip, const void *buf,
1918 unsigned int len, bool force_8bit)
1919{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001920 if (!len || !buf)
1921 return -EINVAL;
1922
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01001923 if (nand_has_exec_op(chip)) {
Miquel Raynal8878b122017-11-09 14:16:45 +01001924 struct nand_op_instr instrs[] = {
1925 NAND_OP_DATA_OUT(len, buf, 0),
1926 };
Boris Brezillonae2294b2018-11-11 08:55:15 +01001927 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
Miquel Raynal8878b122017-11-09 14:16:45 +01001928
1929 instrs[0].ctx.data.force_8bit = force_8bit;
1930
1931 return nand_exec_op(chip, &op);
1932 }
1933
Boris Brezillon97d90da2017-11-30 18:01:29 +01001934 if (force_8bit) {
1935 const u8 *p = buf;
1936 unsigned int i;
1937
1938 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001939 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001940 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02001941 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001942 }
1943
1944 return 0;
1945}
1946EXPORT_SYMBOL_GPL(nand_write_data_op);
1947
1948/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001949 * struct nand_op_parser_ctx - Context used by the parser
1950 * @instrs: array of all the instructions that must be addressed
1951 * @ninstrs: length of the @instrs array
1952 * @subop: Sub-operation to be passed to the NAND controller
1953 *
1954 * This structure is used by the core to split NAND operations into
1955 * sub-operations that can be handled by the NAND controller.
1956 */
1957struct nand_op_parser_ctx {
1958 const struct nand_op_instr *instrs;
1959 unsigned int ninstrs;
1960 struct nand_subop subop;
1961};
1962
1963/**
1964 * nand_op_parser_must_split_instr - Checks if an instruction must be split
1965 * @pat: the parser pattern element that matches @instr
1966 * @instr: pointer to the instruction to check
1967 * @start_offset: this is an in/out parameter. If @instr has already been
1968 * split, then @start_offset is the offset from which to start
1969 * (either an address cycle or an offset in the data buffer).
1970 * Conversely, if the function returns true (ie. instr must be
1971 * split), this parameter is updated to point to the first
1972 * data/address cycle that has not been taken care of.
1973 *
1974 * Some NAND controllers are limited and cannot send X address cycles with a
1975 * unique operation, or cannot read/write more than Y bytes at the same time.
1976 * In this case, split the instruction that does not fit in a single
1977 * controller-operation into two or more chunks.
1978 *
1979 * Returns true if the instruction must be split, false otherwise.
1980 * The @start_offset parameter is also updated to the offset at which the next
1981 * bundle of instruction must start (if an address or a data instruction).
1982 */
1983static bool
1984nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
1985 const struct nand_op_instr *instr,
1986 unsigned int *start_offset)
1987{
1988 switch (pat->type) {
1989 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001990 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01001991 break;
1992
1993 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01001994 pat->ctx.addr.maxcycles) {
1995 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01001996 return true;
1997 }
1998 break;
1999
2000 case NAND_OP_DATA_IN_INSTR:
2001 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002002 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002003 break;
2004
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002005 if (instr->ctx.data.len - *start_offset >
2006 pat->ctx.data.maxlen) {
2007 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002008 return true;
2009 }
2010 break;
2011
2012 default:
2013 break;
2014 }
2015
2016 return false;
2017}
2018
2019/**
2020 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2021 * remaining in the parser context
2022 * @pat: the pattern to test
2023 * @ctx: the parser context structure to match with the pattern @pat
2024 *
2025 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2026 * Returns true if this is the case, false ortherwise. When true is returned,
2027 * @ctx->subop is updated with the set of instructions to be passed to the
2028 * controller driver.
2029 */
2030static bool
2031nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2032 struct nand_op_parser_ctx *ctx)
2033{
2034 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2035 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2036 const struct nand_op_instr *instr = ctx->subop.instrs;
2037 unsigned int i, ninstrs;
2038
2039 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2040 /*
2041 * The pattern instruction does not match the operation
2042 * instruction. If the instruction is marked optional in the
2043 * pattern definition, we skip the pattern element and continue
2044 * to the next one. If the element is mandatory, there's no
2045 * match and we can return false directly.
2046 */
2047 if (instr->type != pat->elems[i].type) {
2048 if (!pat->elems[i].optional)
2049 return false;
2050
2051 continue;
2052 }
2053
2054 /*
2055 * Now check the pattern element constraints. If the pattern is
2056 * not able to handle the whole instruction in a single step,
2057 * we have to split it.
2058 * The last_instr_end_off value comes back updated to point to
2059 * the position where we have to split the instruction (the
2060 * start of the next subop chunk).
2061 */
2062 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2063 &instr_offset)) {
2064 ninstrs++;
2065 i++;
2066 break;
2067 }
2068
2069 instr++;
2070 ninstrs++;
2071 instr_offset = 0;
2072 }
2073
2074 /*
2075 * This can happen if all instructions of a pattern are optional.
2076 * Still, if there's not at least one instruction handled by this
2077 * pattern, this is not a match, and we should try the next one (if
2078 * any).
2079 */
2080 if (!ninstrs)
2081 return false;
2082
2083 /*
2084 * We had a match on the pattern head, but the pattern may be longer
2085 * than the instructions we're asked to execute. We need to make sure
2086 * there's no mandatory elements in the pattern tail.
2087 */
2088 for (; i < pat->nelems; i++) {
2089 if (!pat->elems[i].optional)
2090 return false;
2091 }
2092
2093 /*
2094 * We have a match: update the subop structure accordingly and return
2095 * true.
2096 */
2097 ctx->subop.ninstrs = ninstrs;
2098 ctx->subop.last_instr_end_off = instr_offset;
2099
2100 return true;
2101}
2102
2103#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2104static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2105{
2106 const struct nand_op_instr *instr;
2107 char *prefix = " ";
2108 unsigned int i;
2109
2110 pr_debug("executing subop:\n");
2111
2112 for (i = 0; i < ctx->ninstrs; i++) {
2113 instr = &ctx->instrs[i];
2114
2115 if (instr == &ctx->subop.instrs[0])
2116 prefix = " ->";
2117
2118 switch (instr->type) {
2119 case NAND_OP_CMD_INSTR:
2120 pr_debug("%sCMD [0x%02x]\n", prefix,
2121 instr->ctx.cmd.opcode);
2122 break;
2123 case NAND_OP_ADDR_INSTR:
2124 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2125 instr->ctx.addr.naddrs,
2126 instr->ctx.addr.naddrs < 64 ?
2127 instr->ctx.addr.naddrs : 64,
2128 instr->ctx.addr.addrs);
2129 break;
2130 case NAND_OP_DATA_IN_INSTR:
2131 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2132 instr->ctx.data.len,
2133 instr->ctx.data.force_8bit ?
2134 ", force 8-bit" : "");
2135 break;
2136 case NAND_OP_DATA_OUT_INSTR:
2137 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2138 instr->ctx.data.len,
2139 instr->ctx.data.force_8bit ?
2140 ", force 8-bit" : "");
2141 break;
2142 case NAND_OP_WAITRDY_INSTR:
2143 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2144 instr->ctx.waitrdy.timeout_ms);
2145 break;
2146 }
2147
2148 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2149 prefix = " ";
2150 }
2151}
2152#else
2153static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2154{
2155 /* NOP */
2156}
2157#endif
2158
2159/**
2160 * nand_op_parser_exec_op - exec_op parser
2161 * @chip: the NAND chip
2162 * @parser: patterns description provided by the controller driver
2163 * @op: the NAND operation to address
2164 * @check_only: when true, the function only checks if @op can be handled but
2165 * does not execute the operation
2166 *
2167 * Helper function designed to ease integration of NAND controller drivers that
2168 * only support a limited set of instruction sequences. The supported sequences
2169 * are described in @parser, and the framework takes care of splitting @op into
2170 * multiple sub-operations (if required) and pass them back to the ->exec()
2171 * callback of the matching pattern if @check_only is set to false.
2172 *
2173 * NAND controller drivers should call this function from their own ->exec_op()
2174 * implementation.
2175 *
2176 * Returns 0 on success, a negative error code otherwise. A failure can be
2177 * caused by an unsupported operation (none of the supported patterns is able
2178 * to handle the requested operation), or an error returned by one of the
2179 * matching pattern->exec() hook.
2180 */
2181int nand_op_parser_exec_op(struct nand_chip *chip,
2182 const struct nand_op_parser *parser,
2183 const struct nand_operation *op, bool check_only)
2184{
2185 struct nand_op_parser_ctx ctx = {
2186 .subop.instrs = op->instrs,
2187 .instrs = op->instrs,
2188 .ninstrs = op->ninstrs,
2189 };
2190 unsigned int i;
2191
2192 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2193 int ret;
2194
2195 for (i = 0; i < parser->npatterns; i++) {
2196 const struct nand_op_parser_pattern *pattern;
2197
2198 pattern = &parser->patterns[i];
2199 if (!nand_op_parser_match_pat(pattern, &ctx))
2200 continue;
2201
2202 nand_op_parser_trace(&ctx);
2203
2204 if (check_only)
2205 break;
2206
2207 ret = pattern->exec(chip, &ctx.subop);
2208 if (ret)
2209 return ret;
2210
2211 break;
2212 }
2213
2214 if (i == parser->npatterns) {
2215 pr_debug("->exec_op() parser: pattern not found!\n");
2216 return -ENOTSUPP;
2217 }
2218
2219 /*
2220 * Update the context structure by pointing to the start of the
2221 * next subop.
2222 */
2223 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2224 if (ctx.subop.last_instr_end_off)
2225 ctx.subop.instrs -= 1;
2226
2227 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2228 }
2229
2230 return 0;
2231}
2232EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2233
2234static bool nand_instr_is_data(const struct nand_op_instr *instr)
2235{
2236 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2237 instr->type == NAND_OP_DATA_OUT_INSTR);
2238}
2239
2240static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2241 unsigned int instr_idx)
2242{
2243 return subop && instr_idx < subop->ninstrs;
2244}
2245
Miquel Raynal760c4352018-07-19 00:09:12 +02002246static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2247 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002248{
2249 if (instr_idx)
2250 return 0;
2251
2252 return subop->first_instr_start_off;
2253}
2254
2255/**
2256 * nand_subop_get_addr_start_off - Get the start offset in an address array
2257 * @subop: The entire sub-operation
2258 * @instr_idx: Index of the instruction inside the sub-operation
2259 *
2260 * During driver development, one could be tempted to directly use the
2261 * ->addr.addrs field of address instructions. This is wrong as address
2262 * instructions might be split.
2263 *
2264 * Given an address instruction, returns the offset of the first cycle to issue.
2265 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002266unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2267 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002268{
Miquel Raynal760c4352018-07-19 00:09:12 +02002269 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2270 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2271 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002272
2273 return nand_subop_get_start_off(subop, instr_idx);
2274}
2275EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2276
2277/**
2278 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2279 * @subop: The entire sub-operation
2280 * @instr_idx: Index of the instruction inside the sub-operation
2281 *
2282 * During driver development, one could be tempted to directly use the
2283 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2284 * might be split.
2285 *
2286 * Given an address instruction, returns the number of address cycle to issue.
2287 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002288unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2289 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002290{
2291 int start_off, end_off;
2292
Miquel Raynal760c4352018-07-19 00:09:12 +02002293 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2294 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2295 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002296
2297 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2298
2299 if (instr_idx == subop->ninstrs - 1 &&
2300 subop->last_instr_end_off)
2301 end_off = subop->last_instr_end_off;
2302 else
2303 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2304
2305 return end_off - start_off;
2306}
2307EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2308
2309/**
2310 * nand_subop_get_data_start_off - Get the start offset in a data array
2311 * @subop: The entire sub-operation
2312 * @instr_idx: Index of the instruction inside the sub-operation
2313 *
2314 * During driver development, one could be tempted to directly use the
2315 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2316 * instructions might be split.
2317 *
2318 * Given a data instruction, returns the offset to start from.
2319 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002320unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2321 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002322{
Miquel Raynal760c4352018-07-19 00:09:12 +02002323 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2324 !nand_instr_is_data(&subop->instrs[instr_idx])))
2325 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002326
2327 return nand_subop_get_start_off(subop, instr_idx);
2328}
2329EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2330
2331/**
2332 * nand_subop_get_data_len - Get the number of bytes to retrieve
2333 * @subop: The entire sub-operation
2334 * @instr_idx: Index of the instruction inside the sub-operation
2335 *
2336 * During driver development, one could be tempted to directly use the
2337 * ->data->len field of a data instruction. This is wrong as data instructions
2338 * might be split.
2339 *
2340 * Returns the length of the chunk of data to send/receive.
2341 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002342unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2343 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002344{
2345 int start_off = 0, end_off;
2346
Miquel Raynal760c4352018-07-19 00:09:12 +02002347 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2348 !nand_instr_is_data(&subop->instrs[instr_idx])))
2349 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002350
2351 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2352
2353 if (instr_idx == subop->ninstrs - 1 &&
2354 subop->last_instr_end_off)
2355 end_off = subop->last_instr_end_off;
2356 else
2357 end_off = subop->instrs[instr_idx].ctx.data.len;
2358
2359 return end_off - start_off;
2360}
2361EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002364 * nand_reset - Reset and initialize a NAND device
2365 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002366 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002367 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002368 * Save the timings data structure, then apply SDR timings mode 0 (see
2369 * nand_reset_data_interface for details), do the reset operation, and
2370 * apply back the previous timings.
2371 *
2372 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002373 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002374int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002375{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002376 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002377 int ret;
2378
Boris Brezillon104e4422017-03-16 09:35:58 +01002379 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002380 if (ret)
2381 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002382
Boris Brezillon73f907f2016-10-24 16:46:20 +02002383 /*
2384 * The CS line has to be released before we can apply the new NAND
Boris Brezillon1d017852018-11-11 08:55:14 +01002385 * interface settings, hence this weird nand_select_target()
2386 * nand_deselect_target() dance.
Boris Brezillon73f907f2016-10-24 16:46:20 +02002387 */
Boris Brezillon1d017852018-11-11 08:55:14 +01002388 nand_select_target(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002389 ret = nand_reset_op(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01002390 nand_deselect_target(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002391 if (ret)
2392 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002393
Miquel Raynal107b7d62018-03-19 14:47:25 +01002394 /*
2395 * A nand_reset_data_interface() put both the NAND chip and the NAND
2396 * controller in timings mode 0. If the default mode for this chip is
2397 * also 0, no need to proceed to the change again. Plus, at probe time,
2398 * nand_setup_data_interface() uses ->set/get_features() which would
2399 * fail anyway as the parameter page is not available yet.
2400 */
2401 if (!chip->onfi_timing_mode_default)
2402 return 0;
2403
Miquel Raynal17fa8042017-11-30 18:01:31 +01002404 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002405 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002406 if (ret)
2407 return ret;
2408
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002409 return 0;
2410}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002411EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002412
2413/**
Boris Brezillon45240362018-09-07 00:38:40 +02002414 * nand_get_features - wrapper to perform a GET_FEATURE
2415 * @chip: NAND chip info structure
2416 * @addr: feature address
2417 * @subfeature_param: the subfeature parameters, a four bytes array
2418 *
2419 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2420 * operation cannot be handled.
2421 */
2422int nand_get_features(struct nand_chip *chip, int addr,
2423 u8 *subfeature_param)
2424{
2425 if (!nand_supports_get_features(chip, addr))
2426 return -ENOTSUPP;
2427
2428 if (chip->legacy.get_features)
2429 return chip->legacy.get_features(chip, addr, subfeature_param);
2430
2431 return nand_get_features_op(chip, addr, subfeature_param);
2432}
Boris Brezillon45240362018-09-07 00:38:40 +02002433
2434/**
2435 * nand_set_features - wrapper to perform a SET_FEATURE
2436 * @chip: NAND chip info structure
2437 * @addr: feature address
2438 * @subfeature_param: the subfeature parameters, a four bytes array
2439 *
2440 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2441 * operation cannot be handled.
2442 */
2443int nand_set_features(struct nand_chip *chip, int addr,
2444 u8 *subfeature_param)
2445{
2446 if (!nand_supports_set_features(chip, addr))
2447 return -ENOTSUPP;
2448
2449 if (chip->legacy.set_features)
2450 return chip->legacy.set_features(chip, addr, subfeature_param);
2451
2452 return nand_set_features_op(chip, addr, subfeature_param);
2453}
Boris Brezillon45240362018-09-07 00:38:40 +02002454
2455/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002456 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2457 * @buf: buffer to test
2458 * @len: buffer length
2459 * @bitflips_threshold: maximum number of bitflips
2460 *
2461 * Check if a buffer contains only 0xff, which means the underlying region
2462 * has been erased and is ready to be programmed.
2463 * The bitflips_threshold specify the maximum number of bitflips before
2464 * considering the region is not erased.
2465 * Note: The logic of this function has been extracted from the memweight
2466 * implementation, except that nand_check_erased_buf function exit before
2467 * testing the whole buffer if the number of bitflips exceed the
2468 * bitflips_threshold value.
2469 *
2470 * Returns a positive number of bitflips less than or equal to
2471 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2472 * threshold.
2473 */
2474static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2475{
2476 const unsigned char *bitmap = buf;
2477 int bitflips = 0;
2478 int weight;
2479
2480 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2481 len--, bitmap++) {
2482 weight = hweight8(*bitmap);
2483 bitflips += BITS_PER_BYTE - weight;
2484 if (unlikely(bitflips > bitflips_threshold))
2485 return -EBADMSG;
2486 }
2487
2488 for (; len >= sizeof(long);
2489 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002490 unsigned long d = *((unsigned long *)bitmap);
2491 if (d == ~0UL)
2492 continue;
2493 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002494 bitflips += BITS_PER_LONG - weight;
2495 if (unlikely(bitflips > bitflips_threshold))
2496 return -EBADMSG;
2497 }
2498
2499 for (; len > 0; len--, bitmap++) {
2500 weight = hweight8(*bitmap);
2501 bitflips += BITS_PER_BYTE - weight;
2502 if (unlikely(bitflips > bitflips_threshold))
2503 return -EBADMSG;
2504 }
2505
2506 return bitflips;
2507}
2508
2509/**
2510 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2511 * 0xff data
2512 * @data: data buffer to test
2513 * @datalen: data length
2514 * @ecc: ECC buffer
2515 * @ecclen: ECC length
2516 * @extraoob: extra OOB buffer
2517 * @extraooblen: extra OOB length
2518 * @bitflips_threshold: maximum number of bitflips
2519 *
2520 * Check if a data buffer and its associated ECC and OOB data contains only
2521 * 0xff pattern, which means the underlying region has been erased and is
2522 * ready to be programmed.
2523 * The bitflips_threshold specify the maximum number of bitflips before
2524 * considering the region as not erased.
2525 *
2526 * Note:
2527 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2528 * different from the NAND page size. When fixing bitflips, ECC engines will
2529 * report the number of errors per chunk, and the NAND core infrastructure
2530 * expect you to return the maximum number of bitflips for the whole page.
2531 * This is why you should always use this function on a single chunk and
2532 * not on the whole page. After checking each chunk you should update your
2533 * max_bitflips value accordingly.
2534 * 2/ When checking for bitflips in erased pages you should not only check
2535 * the payload data but also their associated ECC data, because a user might
2536 * have programmed almost all bits to 1 but a few. In this case, we
2537 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2538 * this case.
2539 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2540 * data are protected by the ECC engine.
2541 * It could also be used if you support subpages and want to attach some
2542 * extra OOB data to an ECC chunk.
2543 *
2544 * Returns a positive number of bitflips less than or equal to
2545 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2546 * threshold. In case of success, the passed buffers are filled with 0xff.
2547 */
2548int nand_check_erased_ecc_chunk(void *data, int datalen,
2549 void *ecc, int ecclen,
2550 void *extraoob, int extraooblen,
2551 int bitflips_threshold)
2552{
2553 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2554
2555 data_bitflips = nand_check_erased_buf(data, datalen,
2556 bitflips_threshold);
2557 if (data_bitflips < 0)
2558 return data_bitflips;
2559
2560 bitflips_threshold -= data_bitflips;
2561
2562 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2563 if (ecc_bitflips < 0)
2564 return ecc_bitflips;
2565
2566 bitflips_threshold -= ecc_bitflips;
2567
2568 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2569 bitflips_threshold);
2570 if (extraoob_bitflips < 0)
2571 return extraoob_bitflips;
2572
2573 if (data_bitflips)
2574 memset(data, 0xff, datalen);
2575
2576 if (ecc_bitflips)
2577 memset(ecc, 0xff, ecclen);
2578
2579 if (extraoob_bitflips)
2580 memset(extraoob, 0xff, extraooblen);
2581
2582 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2583}
2584EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2585
2586/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002587 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002588 * @chip: nand chip info structure
2589 * @buf: buffer to store read data
2590 * @oob_required: caller requires OOB data read to chip->oob_poi
2591 * @page: page number to read
2592 *
2593 * Returns -ENOTSUPP unconditionally.
2594 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002595int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2596 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002597{
2598 return -ENOTSUPP;
2599}
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002600
2601/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002602 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002603 * @chip: nand chip info structure
2604 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002605 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002606 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002607 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002608 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002609 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002610int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2611 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002612{
Boris Brezillonb9761682018-09-06 14:05:20 +02002613 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002614 int ret;
2615
Boris Brezillon25f815f2017-11-30 18:01:30 +01002616 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002617 if (ret)
2618 return ret;
2619
2620 if (oob_required) {
2621 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2622 false);
2623 if (ret)
2624 return ret;
2625 }
2626
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002627 return 0;
2628}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002629EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002630
2631/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002632 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002633 * @chip: nand chip info structure
2634 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002635 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002636 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002637 *
2638 * We need a special oob layout and handling even when OOB isn't used.
2639 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002640static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07002641 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002642{
Boris Brezillonb9761682018-09-06 14:05:20 +02002643 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08002644 int eccsize = chip->ecc.size;
2645 int eccbytes = chip->ecc.bytes;
2646 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002647 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002648
Boris Brezillon25f815f2017-11-30 18:01:30 +01002649 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2650 if (ret)
2651 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002652
2653 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002654 ret = nand_read_data_op(chip, buf, eccsize, false);
2655 if (ret)
2656 return ret;
2657
David Brownell52ff49d2009-03-04 12:01:36 -08002658 buf += eccsize;
2659
2660 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002661 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2662 false);
2663 if (ret)
2664 return ret;
2665
David Brownell52ff49d2009-03-04 12:01:36 -08002666 oob += chip->ecc.prepad;
2667 }
2668
Boris Brezillon97d90da2017-11-30 18:01:29 +01002669 ret = nand_read_data_op(chip, oob, eccbytes, false);
2670 if (ret)
2671 return ret;
2672
David Brownell52ff49d2009-03-04 12:01:36 -08002673 oob += eccbytes;
2674
2675 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002676 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2677 false);
2678 if (ret)
2679 return ret;
2680
David Brownell52ff49d2009-03-04 12:01:36 -08002681 oob += chip->ecc.postpad;
2682 }
2683 }
2684
2685 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002686 if (size) {
2687 ret = nand_read_data_op(chip, oob, size, false);
2688 if (ret)
2689 return ret;
2690 }
David Brownell52ff49d2009-03-04 12:01:36 -08002691
2692 return 0;
2693}
2694
2695/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002696 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002697 * @chip: nand chip info structure
2698 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002699 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002700 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002701 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002702static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
2703 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Boris Brezillonb9761682018-09-06 14:05:20 +02002705 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002706 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002707 int eccbytes = chip->ecc.bytes;
2708 int eccsteps = chip->ecc.steps;
2709 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002710 uint8_t *ecc_calc = chip->ecc.calc_buf;
2711 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002712 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002713
Boris Brezillonb9761682018-09-06 14:05:20 +02002714 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002715
2716 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002717 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002718
Boris Brezillon846031d2016-02-03 20:11:00 +01002719 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2720 chip->ecc.total);
2721 if (ret)
2722 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002723
2724 eccsteps = chip->ecc.steps;
2725 p = buf;
2726
2727 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2728 int stat;
2729
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002730 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07002731 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002732 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002733 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002734 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002735 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2736 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002737 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002738 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01002739}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302742 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002743 * @chip: nand chip info structure
2744 * @data_offs: offset of requested data within the page
2745 * @readlen: data length
2746 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08002747 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01002748 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002749static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
2750 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01002751{
Boris Brezillonb9761682018-09-06 14:05:20 +02002752 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002753 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002754 uint8_t *p;
2755 int data_col_addr, i, gaps = 0;
2756 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
2757 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01002758 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07002759 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01002760 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01002761
Brian Norris7854d3f2011-06-23 14:12:08 -07002762 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01002763 start_step = data_offs / chip->ecc.size;
2764 end_step = (data_offs + readlen - 1) / chip->ecc.size;
2765 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10302766 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01002767
Brian Norris8b6e50c2011-05-25 14:59:01 -07002768 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01002769 datafrag_len = num_steps * chip->ecc.size;
2770 eccfrag_len = num_steps * chip->ecc.bytes;
2771
2772 data_col_addr = start_step * chip->ecc.size;
2773 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01002774 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01002775 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002776 if (ret)
2777 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002778
Brian Norris8b6e50c2011-05-25 14:59:01 -07002779 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01002780 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002781 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01002782
Brian Norris8b6e50c2011-05-25 14:59:01 -07002783 /*
2784 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07002785 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07002786 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002787 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
2788 if (ret)
2789 return ret;
2790
2791 if (oobregion.length < eccfrag_len)
2792 gaps = 1;
2793
Alexey Korolev3d459552008-05-15 17:23:18 +01002794 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002795 ret = nand_change_read_column_op(chip, mtd->writesize,
2796 chip->oob_poi, mtd->oobsize,
2797 false);
2798 if (ret)
2799 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002800 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002801 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002802 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07002803 * about buswidth alignment in read_buf.
2804 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002805 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01002806 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01002807 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002808 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01002809 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
2810 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002811 aligned_len++;
2812
Boris Brezillon97d90da2017-11-30 18:01:29 +01002813 ret = nand_change_read_column_op(chip,
2814 mtd->writesize + aligned_pos,
2815 &chip->oob_poi[aligned_pos],
2816 aligned_len, false);
2817 if (ret)
2818 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002819 }
2820
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002821 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01002822 chip->oob_poi, index, eccfrag_len);
2823 if (ret)
2824 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002825
2826 p = bufpoi + data_col_addr;
2827 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
2828 int stat;
2829
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002830 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002831 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002832 if (stat == -EBADMSG &&
2833 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2834 /* check for empty pages with bitflips */
2835 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002836 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002837 chip->ecc.bytes,
2838 NULL, 0,
2839 chip->ecc.strength);
2840 }
2841
Mike Dunn3f91e942012-04-25 12:06:09 -07002842 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002843 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002844 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01002845 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002846 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2847 }
Alexey Korolev3d459552008-05-15 17:23:18 +01002848 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002849 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01002850}
2851
2852/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002853 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002854 * @chip: nand chip info structure
2855 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002856 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002857 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002858 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002859 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002860 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002861static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
2862 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002863{
Boris Brezillonb9761682018-09-06 14:05:20 +02002864 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002865 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002866 int eccbytes = chip->ecc.bytes;
2867 int eccsteps = chip->ecc.steps;
2868 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002869 uint8_t *ecc_calc = chip->ecc.calc_buf;
2870 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002871 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002872
Boris Brezillon25f815f2017-11-30 18:01:30 +01002873 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2874 if (ret)
2875 return ret;
2876
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002877 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02002878 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002879
2880 ret = nand_read_data_op(chip, p, eccsize, false);
2881 if (ret)
2882 return ret;
2883
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002884 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002885 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002886
2887 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2888 if (ret)
2889 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002890
Boris Brezillon846031d2016-02-03 20:11:00 +01002891 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2892 chip->ecc.total);
2893 if (ret)
2894 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002895
2896 eccsteps = chip->ecc.steps;
2897 p = buf;
2898
2899 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2900 int stat;
2901
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002902 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002903 if (stat == -EBADMSG &&
2904 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2905 /* check for empty pages with bitflips */
2906 stat = nand_check_erased_ecc_chunk(p, eccsize,
2907 &ecc_code[i], eccbytes,
2908 NULL, 0,
2909 chip->ecc.strength);
2910 }
2911
Mike Dunn3f91e942012-04-25 12:06:09 -07002912 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002913 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002914 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002915 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002916 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2917 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002918 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002919 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002920}
2921
2922/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002923 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07002924 * @chip: nand chip info structure
2925 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002926 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002927 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002928 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002929 * Hardware ECC for large page chips, require OOB to be read first. For this
2930 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2931 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2932 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2933 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002934 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002935static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
2936 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002937{
Boris Brezillonb9761682018-09-06 14:05:20 +02002938 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01002939 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002940 int eccbytes = chip->ecc.bytes;
2941 int eccsteps = chip->ecc.steps;
2942 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002943 uint8_t *ecc_code = chip->ecc.code_buf;
2944 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002945 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002946
2947 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01002948 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2949 if (ret)
2950 return ret;
2951
2952 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2953 if (ret)
2954 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002955
Boris Brezillon846031d2016-02-03 20:11:00 +01002956 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2957 chip->ecc.total);
2958 if (ret)
2959 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002960
2961 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2962 int stat;
2963
Boris Brezillonec476362018-09-06 14:05:17 +02002964 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002965
2966 ret = nand_read_data_op(chip, p, eccsize, false);
2967 if (ret)
2968 return ret;
2969
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02002970 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002971
Boris Brezillon00da2ea2018-09-06 14:05:19 +02002972 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002973 if (stat == -EBADMSG &&
2974 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2975 /* check for empty pages with bitflips */
2976 stat = nand_check_erased_ecc_chunk(p, eccsize,
2977 &ecc_code[i], eccbytes,
2978 NULL, 0,
2979 chip->ecc.strength);
2980 }
2981
Mike Dunn3f91e942012-04-25 12:06:09 -07002982 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002983 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002984 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002985 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002986 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2987 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002988 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002989 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002990}
2991
2992/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002993 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07002994 * @chip: nand chip info structure
2995 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002996 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002997 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002998 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002999 * The hw generator calculates the error syndrome automatically. Therefore we
3000 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003001 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003002static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3003 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003004{
Boris Brezillonb9761682018-09-06 14:05:20 +02003005 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003006 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003007 int eccbytes = chip->ecc.bytes;
3008 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003009 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003010 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003011 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003012 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003013
Boris Brezillon25f815f2017-11-30 18:01:30 +01003014 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3015 if (ret)
3016 return ret;
3017
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003018 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3019 int stat;
3020
Boris Brezillonec476362018-09-06 14:05:17 +02003021 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003022
3023 ret = nand_read_data_op(chip, p, eccsize, false);
3024 if (ret)
3025 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003026
3027 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003028 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3029 false);
3030 if (ret)
3031 return ret;
3032
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003033 oob += chip->ecc.prepad;
3034 }
3035
Boris Brezillonec476362018-09-06 14:05:17 +02003036 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003037
3038 ret = nand_read_data_op(chip, oob, eccbytes, false);
3039 if (ret)
3040 return ret;
3041
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003042 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003043
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003044 oob += eccbytes;
3045
3046 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003047 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3048 false);
3049 if (ret)
3050 return ret;
3051
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003052 oob += chip->ecc.postpad;
3053 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003054
3055 if (stat == -EBADMSG &&
3056 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3057 /* check for empty pages with bitflips */
3058 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3059 oob - eccpadbytes,
3060 eccpadbytes,
3061 NULL, 0,
3062 chip->ecc.strength);
3063 }
3064
3065 if (stat < 0) {
3066 mtd->ecc_stats.failed++;
3067 } else {
3068 mtd->ecc_stats.corrected += stat;
3069 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3070 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003071 }
3072
3073 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003074 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003075 if (i) {
3076 ret = nand_read_data_op(chip, oob, i, false);
3077 if (ret)
3078 return ret;
3079 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003080
Mike Dunn3f91e942012-04-25 12:06:09 -07003081 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003082}
3083
3084/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003085 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon08136212018-11-11 08:55:03 +01003086 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003087 * @oob: oob destination address
3088 * @ops: oob ops structure
3089 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003090 */
Boris Brezillon08136212018-11-11 08:55:03 +01003091static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003092 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003093{
Boris Brezillon08136212018-11-11 08:55:03 +01003094 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003095 int ret;
3096
Florian Fainellif8ac0412010-09-07 13:23:43 +02003097 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003098
Brian Norris0612b9d2011-08-30 18:45:40 -07003099 case MTD_OPS_PLACE_OOB:
3100 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003101 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3102 return oob + len;
3103
Boris Brezillon846031d2016-02-03 20:11:00 +01003104 case MTD_OPS_AUTO_OOB:
3105 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3106 ops->ooboffs, len);
3107 BUG_ON(ret);
3108 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003109
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003110 default:
3111 BUG();
3112 }
3113 return NULL;
3114}
3115
3116/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003117 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003118 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003119 * @retry_mode: the retry mode to use
3120 *
3121 * Some vendors supply a special command to shift the Vt threshold, to be used
3122 * when there are too many bitflips in a page (i.e., ECC error). After setting
3123 * a new threshold, the host should retry reading the page.
3124 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003125static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003126{
Brian Norrisba84fb52014-01-03 15:13:33 -08003127 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3128
3129 if (retry_mode >= chip->read_retries)
3130 return -EINVAL;
3131
3132 if (!chip->setup_read_retry)
3133 return -EOPNOTSUPP;
3134
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003135 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003136}
3137
Boris Brezillon85e08e52018-07-27 09:44:17 +02003138static void nand_wait_readrdy(struct nand_chip *chip)
3139{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003140 const struct nand_sdr_timings *sdr;
3141
Boris Brezillon85e08e52018-07-27 09:44:17 +02003142 if (!(chip->options & NAND_NEED_READRDY))
3143 return;
3144
Boris Brezillon52f05b62018-07-27 09:44:18 +02003145 sdr = nand_get_sdr_timings(&chip->data_interface);
3146 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003147}
3148
Brian Norrisba84fb52014-01-03 15:13:33 -08003149/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003150 * nand_do_read_ops - [INTERN] Read data with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003151 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003152 * @from: offset to read from
3153 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003154 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003155 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003156 */
Boris Brezillon08136212018-11-11 08:55:03 +01003157static int nand_do_read_ops(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003158 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003159{
Brian Norrise47f3db2012-05-02 10:14:56 -07003160 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris Brezillon08136212018-11-11 08:55:03 +01003161 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003162 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003163 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003164 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003165 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003166
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003167 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003168 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003169 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003170 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003171 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003173 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003174 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003176 realpage = (int)(from >> chip->page_shift);
3177 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003179 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003181 buf = ops->datbuf;
3182 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003183 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003184
Florian Fainellif8ac0412010-09-07 13:23:43 +02003185 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003186 unsigned int ecc_failures = mtd->ecc_stats.failed;
3187
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003188 bytes = min(mtd->writesize - col, readlen);
3189 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003190
Kamal Dasu66507c72014-05-01 20:51:19 -04003191 if (!aligned)
3192 use_bufpoi = 1;
3193 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003194 use_bufpoi = !virt_addr_valid(buf) ||
3195 !IS_ALIGNED((unsigned long)buf,
3196 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003197 else
3198 use_bufpoi = 0;
3199
Brian Norris8b6e50c2011-05-25 14:59:01 -07003200 /* Is the current page in the buffer? */
Boris Brezillond9745412018-10-28 16:12:45 +01003201 if (realpage != chip->pagecache.page || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003202 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003203
3204 if (use_bufpoi && aligned)
3205 pr_debug("%s: using read bounce buffer for buf@%p\n",
3206 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Brian Norrisba84fb52014-01-03 15:13:33 -08003208read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003209 /*
3210 * Now read the page into the buffer. Absent an error,
3211 * the read methods return max bitflips per ecc step.
3212 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003213 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003214 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003215 oob_required,
3216 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003217 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3218 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003219 ret = chip->ecc.read_subpage(chip, col, bytes,
3220 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003221 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003222 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003223 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003224 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003225 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003226 /* Invalidate page cache */
Boris Brezillond9745412018-10-28 16:12:45 +01003227 chip->pagecache.page = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003228 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003229 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003230
3231 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003232 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003233 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003234 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003235 (ops->mode != MTD_OPS_RAW)) {
Boris Brezillond9745412018-10-28 16:12:45 +01003236 chip->pagecache.page = realpage;
3237 chip->pagecache.bitflips = ret;
Mike Dunnedbc45402012-04-25 12:06:11 -07003238 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003239 /* Invalidate page cache */
Boris Brezillond9745412018-10-28 16:12:45 +01003240 chip->pagecache.page = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003241 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003242 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003244
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003245 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003246 int toread = min(oobreadlen, max_oobsize);
3247
3248 if (toread) {
Boris Brezillon08136212018-11-11 08:55:03 +01003249 oob = nand_transfer_oob(chip, oob, ops,
3250 toread);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003251 oobreadlen -= toread;
3252 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003253 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003254
Boris Brezillon85e08e52018-07-27 09:44:17 +02003255 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003256
Brian Norrisba84fb52014-01-03 15:13:33 -08003257 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003258 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003259 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003260 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003261 retry_mode);
3262 if (ret < 0)
3263 break;
3264
3265 /* Reset failures; retry */
3266 mtd->ecc_stats.failed = ecc_failures;
3267 goto read_retry;
3268 } else {
3269 /* No more retry modes; real failure */
3270 ecc_fail = true;
3271 }
3272 }
3273
3274 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003275 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003276 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003277 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003278 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003279 max_bitflips = max_t(unsigned int, max_bitflips,
Boris Brezillond9745412018-10-28 16:12:45 +01003280 chip->pagecache.bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003283 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003284
Brian Norrisba84fb52014-01-03 15:13:33 -08003285 /* Reset to retry mode 0 */
3286 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003287 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003288 if (ret < 0)
3289 break;
3290 retry_mode = 0;
3291 }
3292
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003293 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Brian Norris8b6e50c2011-05-25 14:59:01 -07003296 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 col = 0;
3298 /* Increment page address */
3299 realpage++;
3300
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003301 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 /* Check, if we cross a chip boundary */
3303 if (!page) {
3304 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003305 nand_deselect_target(chip);
3306 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003309 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003311 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003312 if (oob)
3313 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Mike Dunn3f91e942012-04-25 12:06:09 -07003315 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003316 return ret;
3317
Brian Norrisb72f3df2013-12-03 11:04:14 -08003318 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003319 return -EBADMSG;
3320
Mike Dunnedbc45402012-04-25 12:06:11 -07003321 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003322}
3323
3324/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003325 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003326 * @chip: nand chip info structure
3327 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003328 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003329int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003330{
Boris Brezillonb9761682018-09-06 14:05:20 +02003331 struct mtd_info *mtd = nand_to_mtd(chip);
3332
Boris Brezillon97d90da2017-11-30 18:01:29 +01003333 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003334}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003335EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003336
3337/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003338 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003339 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003340 * @chip: nand chip info structure
3341 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003342 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003343static int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003344{
Boris Brezillonb9761682018-09-06 14:05:20 +02003345 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003346 int length = mtd->oobsize;
3347 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3348 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003349 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003350 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003351
Boris Brezillon97d90da2017-11-30 18:01:29 +01003352 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3353 if (ret)
3354 return ret;
3355
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003356 for (i = 0; i < chip->ecc.steps; i++) {
3357 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003358 int ret;
3359
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003360 pos = eccsize + i * (eccsize + chunk);
3361 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003362 ret = nand_change_read_column_op(chip, pos,
3363 NULL, 0,
3364 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003365 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003366 ret = nand_read_page_op(chip, page, pos, NULL,
3367 0);
3368
3369 if (ret)
3370 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003371 } else
3372 sndrnd = 1;
3373 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003374
3375 ret = nand_read_data_op(chip, bufpoi, toread, false);
3376 if (ret)
3377 return ret;
3378
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003379 bufpoi += toread;
3380 length -= toread;
3381 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003382 if (length > 0) {
3383 ret = nand_read_data_op(chip, bufpoi, length, false);
3384 if (ret)
3385 return ret;
3386 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003387
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003388 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003389}
3390
3391/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003392 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003393 * @chip: nand chip info structure
3394 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003395 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003396int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003397{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003398 struct mtd_info *mtd = nand_to_mtd(chip);
3399
Boris Brezillon97d90da2017-11-30 18:01:29 +01003400 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3401 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003402}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003403EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003404
3405/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003406 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003407 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003408 * @chip: nand chip info structure
3409 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003410 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003411static int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003412{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003413 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003414 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3415 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003416 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003417 const uint8_t *bufpoi = chip->oob_poi;
3418
3419 /*
3420 * data-ecc-data-ecc ... ecc-oob
3421 * or
3422 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3423 */
3424 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3425 pos = steps * (eccsize + chunk);
3426 steps = 0;
3427 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003428 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003429
Boris Brezillon97d90da2017-11-30 18:01:29 +01003430 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3431 if (ret)
3432 return ret;
3433
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003434 for (i = 0; i < steps; i++) {
3435 if (sndcmd) {
3436 if (mtd->writesize <= 512) {
3437 uint32_t fill = 0xFFFFFFFF;
3438
3439 len = eccsize;
3440 while (len > 0) {
3441 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003442
3443 ret = nand_write_data_op(chip, &fill,
3444 num, false);
3445 if (ret)
3446 return ret;
3447
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003448 len -= num;
3449 }
3450 } else {
3451 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003452 ret = nand_change_write_column_op(chip, pos,
3453 NULL, 0,
3454 false);
3455 if (ret)
3456 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003457 }
3458 } else
3459 sndcmd = 1;
3460 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003461
3462 ret = nand_write_data_op(chip, bufpoi, len, false);
3463 if (ret)
3464 return ret;
3465
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003466 bufpoi += len;
3467 length -= len;
3468 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003469 if (length > 0) {
3470 ret = nand_write_data_op(chip, bufpoi, length, false);
3471 if (ret)
3472 return ret;
3473 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003474
Boris Brezillon97d90da2017-11-30 18:01:29 +01003475 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003476}
3477
3478/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003479 * nand_do_read_oob - [INTERN] NAND read out-of-band
Boris Brezillon08136212018-11-11 08:55:03 +01003480 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003481 * @from: offset to read from
3482 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003484 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 */
Boris Brezillon08136212018-11-11 08:55:03 +01003486static int nand_do_read_oob(struct nand_chip *chip, loff_t from,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003487 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488{
Boris Brezillon08136212018-11-11 08:55:03 +01003489 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003490 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003491 int page, realpage, chipnr;
Brian Norris041e4572011-06-23 16:45:24 -07003492 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003493 int readlen = ops->ooblen;
3494 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003495 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003496 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Brian Norris289c0522011-07-19 10:06:09 -07003498 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303499 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Brian Norris041e4572011-06-23 16:45:24 -07003501 stats = mtd->ecc_stats;
3502
Boris BREZILLON29f10582016-03-07 10:46:52 +01003503 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003504
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003505 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003506 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003508 /* Shift to get page */
3509 realpage = (int)(from >> chip->page_shift);
3510 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Florian Fainellif8ac0412010-09-07 13:23:43 +02003512 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003513 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003514 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003515 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003516 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003517
3518 if (ret < 0)
3519 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003520
3521 len = min(len, readlen);
Boris Brezillon08136212018-11-11 08:55:03 +01003522 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003523
Boris Brezillon85e08e52018-07-27 09:44:17 +02003524 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003525
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003526 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3527
Vitaly Wool70145682006-11-03 18:20:38 +03003528 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003529 if (!readlen)
3530 break;
3531
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003532 /* Increment page address */
3533 realpage++;
3534
3535 page = realpage & chip->pagemask;
3536 /* Check, if we cross a chip boundary */
3537 if (!page) {
3538 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01003539 nand_deselect_target(chip);
3540 nand_select_target(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
Boris Brezillon1d017852018-11-11 08:55:14 +01003543 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003545 ops->oobretlen = ops->ooblen - readlen;
3546
3547 if (ret < 0)
3548 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003549
3550 if (mtd->ecc_stats.failed - stats.failed)
3551 return -EBADMSG;
3552
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003553 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554}
3555
3556/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003557 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003558 * @mtd: MTD device structure
3559 * @from: offset to read from
3560 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003562 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003564static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3565 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
Boris Brezillon08136212018-11-11 08:55:03 +01003567 struct nand_chip *chip = mtd_to_nand(mtd);
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003568 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003569
3570 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003572 if (ops->mode != MTD_OPS_PLACE_OOB &&
3573 ops->mode != MTD_OPS_AUTO_OOB &&
3574 ops->mode != MTD_OPS_RAW)
3575 return -ENOTSUPP;
3576
Boris Brezillon013e6292018-11-20 11:57:20 +01003577 ret = nand_get_device(chip);
3578 if (ret)
3579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003581 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01003582 ret = nand_do_read_oob(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003583 else
Boris Brezillon08136212018-11-11 08:55:03 +01003584 ret = nand_do_read_ops(chip, from, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003585
Boris Brezillon08136212018-11-11 08:55:03 +01003586 nand_release_device(chip);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003587 return ret;
3588}
3589
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003590/**
3591 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003592 * @chip: nand chip info structure
3593 * @buf: data buffer
3594 * @oob_required: must write chip->oob_poi to OOB
3595 * @page: page number to write
3596 *
3597 * Returns -ENOTSUPP unconditionally.
3598 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003599int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3600 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003601{
3602 return -ENOTSUPP;
3603}
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003604
3605/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003606 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003607 * @chip: nand chip info structure
3608 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003609 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003610 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003611 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003612 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003613 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003614int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3615 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003616{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003617 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003618 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003619
Boris Brezillon25f815f2017-11-30 18:01:30 +01003620 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003621 if (ret)
3622 return ret;
3623
3624 if (oob_required) {
3625 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3626 false);
3627 if (ret)
3628 return ret;
3629 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003630
Boris Brezillon25f815f2017-11-30 18:01:30 +01003631 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003633EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003635/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003636 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003637 * @chip: nand chip info structure
3638 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003639 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003640 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003641 *
3642 * We need a special oob layout and handling even when ECC isn't checked.
3643 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003644static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003645 const uint8_t *buf, int oob_required,
3646 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003647{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003648 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003649 int eccsize = chip->ecc.size;
3650 int eccbytes = chip->ecc.bytes;
3651 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003652 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003653
Boris Brezillon25f815f2017-11-30 18:01:30 +01003654 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3655 if (ret)
3656 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003657
3658 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003659 ret = nand_write_data_op(chip, buf, eccsize, false);
3660 if (ret)
3661 return ret;
3662
David Brownell52ff49d2009-03-04 12:01:36 -08003663 buf += eccsize;
3664
3665 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003666 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3667 false);
3668 if (ret)
3669 return ret;
3670
David Brownell52ff49d2009-03-04 12:01:36 -08003671 oob += chip->ecc.prepad;
3672 }
3673
Boris Brezillon97d90da2017-11-30 18:01:29 +01003674 ret = nand_write_data_op(chip, oob, eccbytes, false);
3675 if (ret)
3676 return ret;
3677
David Brownell52ff49d2009-03-04 12:01:36 -08003678 oob += eccbytes;
3679
3680 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003681 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3682 false);
3683 if (ret)
3684 return ret;
3685
David Brownell52ff49d2009-03-04 12:01:36 -08003686 oob += chip->ecc.postpad;
3687 }
3688 }
3689
3690 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003691 if (size) {
3692 ret = nand_write_data_op(chip, oob, size, false);
3693 if (ret)
3694 return ret;
3695 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003696
Boris Brezillon25f815f2017-11-30 18:01:30 +01003697 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003698}
3699/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003700 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003701 * @chip: nand chip info structure
3702 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003703 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003704 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003705 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003706static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
3707 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003708{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003709 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003710 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003711 int eccbytes = chip->ecc.bytes;
3712 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003713 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003714 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003715
Brian Norris7854d3f2011-06-23 14:12:08 -07003716 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003717 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003718 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003719
Boris Brezillon846031d2016-02-03 20:11:00 +01003720 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3721 chip->ecc.total);
3722 if (ret)
3723 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003724
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003725 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003726}
3727
3728/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003729 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003730 * @chip: nand chip info structure
3731 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003732 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003733 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003734 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003735static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
3736 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003737{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003738 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003739 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003740 int eccbytes = chip->ecc.bytes;
3741 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003742 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003743 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003744
Boris Brezillon25f815f2017-11-30 18:01:30 +01003745 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3746 if (ret)
3747 return ret;
3748
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003749 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003750 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003751
3752 ret = nand_write_data_op(chip, p, eccsize, false);
3753 if (ret)
3754 return ret;
3755
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003756 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003757 }
3758
Boris Brezillon846031d2016-02-03 20:11:00 +01003759 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3760 chip->ecc.total);
3761 if (ret)
3762 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003763
Boris Brezillon97d90da2017-11-30 18:01:29 +01003764 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3765 if (ret)
3766 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003767
Boris Brezillon25f815f2017-11-30 18:01:30 +01003768 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003769}
3770
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303771
3772/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08003773 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303774 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07003775 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303776 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07003777 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303778 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003779 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303780 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003781static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
3782 uint32_t data_len, const uint8_t *buf,
3783 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303784{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003785 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303786 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003787 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303788 int ecc_size = chip->ecc.size;
3789 int ecc_bytes = chip->ecc.bytes;
3790 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303791 uint32_t start_step = offset / ecc_size;
3792 uint32_t end_step = (offset + data_len - 1) / ecc_size;
3793 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01003794 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303795
Boris Brezillon25f815f2017-11-30 18:01:30 +01003796 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3797 if (ret)
3798 return ret;
3799
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303800 for (step = 0; step < ecc_steps; step++) {
3801 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02003802 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303803
3804 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003805 ret = nand_write_data_op(chip, buf, ecc_size, false);
3806 if (ret)
3807 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303808
3809 /* mask ECC of un-touched subpages by padding 0xFF */
3810 if ((step < start_step) || (step > end_step))
3811 memset(ecc_calc, 0xff, ecc_bytes);
3812 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003813 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303814
3815 /* mask OOB of un-touched subpages by padding 0xFF */
3816 /* if oob_required, preserve OOB metadata of written subpage */
3817 if (!oob_required || (step < start_step) || (step > end_step))
3818 memset(oob_buf, 0xff, oob_bytes);
3819
Brian Norrisd6a950802013-08-08 17:16:36 -07003820 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303821 ecc_calc += ecc_bytes;
3822 oob_buf += oob_bytes;
3823 }
3824
3825 /* copy calculated ECC for whole page to chip->buffer->oob */
3826 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003827 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01003828 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3829 chip->ecc.total);
3830 if (ret)
3831 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303832
3833 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003834 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3835 if (ret)
3836 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303837
Boris Brezillon25f815f2017-11-30 18:01:30 +01003838 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303839}
3840
3841
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003842/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003843 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003844 * @chip: nand chip info structure
3845 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003846 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003847 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003848 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003849 * The hw generator calculates the error syndrome automatically. Therefore we
3850 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003851 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003852static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
3853 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003854{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003855 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003856 int i, eccsize = chip->ecc.size;
3857 int eccbytes = chip->ecc.bytes;
3858 int eccsteps = chip->ecc.steps;
3859 const uint8_t *p = buf;
3860 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003861 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003862
Boris Brezillon25f815f2017-11-30 18:01:30 +01003863 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3864 if (ret)
3865 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003866
3867 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003868 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003869
3870 ret = nand_write_data_op(chip, p, eccsize, false);
3871 if (ret)
3872 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003873
3874 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003875 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3876 false);
3877 if (ret)
3878 return ret;
3879
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003880 oob += chip->ecc.prepad;
3881 }
3882
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003883 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003884
3885 ret = nand_write_data_op(chip, oob, eccbytes, false);
3886 if (ret)
3887 return ret;
3888
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003889 oob += eccbytes;
3890
3891 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003892 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3893 false);
3894 if (ret)
3895 return ret;
3896
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003897 oob += chip->ecc.postpad;
3898 }
3899 }
3900
3901 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003902 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003903 if (i) {
3904 ret = nand_write_data_op(chip, oob, i, false);
3905 if (ret)
3906 return ret;
3907 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003908
Boris Brezillon25f815f2017-11-30 18:01:30 +01003909 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003910}
3911
3912/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003913 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07003914 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303915 * @offset: address offset within the page
3916 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07003917 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07003918 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07003919 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003920 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003921 */
Boris Brezillon08136212018-11-11 08:55:03 +01003922static int nand_write_page(struct nand_chip *chip, uint32_t offset,
3923 int data_len, const uint8_t *buf, int oob_required,
3924 int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003925{
Boris Brezillon08136212018-11-11 08:55:03 +01003926 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303927 int status, subpage;
3928
3929 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3930 chip->ecc.write_subpage)
3931 subpage = offset || (data_len < mtd->writesize);
3932 else
3933 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003934
David Woodhouse956e9442006-09-25 17:12:39 +01003935 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003936 status = chip->ecc.write_page_raw(chip, buf, oob_required,
3937 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303938 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003939 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
3940 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003941 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003942 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08003943
3944 if (status < 0)
3945 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003946
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003947 return 0;
3948}
3949
Florian Fainellif8ac0412010-09-07 13:23:43 +02003950#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003951
3952/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003953 * nand_do_write_ops - [INTERN] NAND write with ECC
Boris Brezillon08136212018-11-11 08:55:03 +01003954 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07003955 * @to: offset to write to
3956 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003957 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003958 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003959 */
Boris Brezillon08136212018-11-11 08:55:03 +01003960static int nand_do_write_ops(struct nand_chip *chip, loff_t to,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003961 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003962{
Boris Brezillon08136212018-11-11 08:55:03 +01003963 struct mtd_info *mtd = nand_to_mtd(chip);
Corentin Labbe73600b62017-09-02 10:49:38 +02003964 int chipnr, realpage, page, column;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003965 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02003966
3967 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003968 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003969
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003970 uint8_t *oob = ops->oobbuf;
3971 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303972 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07003973 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003974
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003975 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003976 if (!writelen)
3977 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003978
Brian Norris8b6e50c2011-05-25 14:59:01 -07003979 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003980 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003981 pr_notice("%s: attempt to write non page aligned data\n",
3982 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003983 return -EINVAL;
3984 }
3985
Thomas Gleixner29072b92006-09-28 15:38:36 +02003986 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003987
Thomas Gleixner6a930962006-06-28 00:11:45 +02003988 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon1d017852018-11-11 08:55:14 +01003989 nand_select_target(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02003990
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003991 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01003992 if (nand_check_wp(chip)) {
Huang Shijieb0bb6902012-11-19 14:43:29 +08003993 ret = -EIO;
3994 goto err_out;
3995 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003996
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003997 realpage = (int)(to >> chip->page_shift);
3998 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003999
4000 /* Invalidate the page cache, when we write to the cached page */
Boris Brezillond9745412018-10-28 16:12:45 +01004001 if (to <= ((loff_t)chip->pagecache.page << chip->page_shift) &&
4002 ((loff_t)chip->pagecache.page << chip->page_shift) < (to + ops->len))
4003 chip->pagecache.page = -1;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004004
Maxim Levitsky782ce792010-02-22 20:39:36 +02004005 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004006 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4007 ret = -EINVAL;
4008 goto err_out;
4009 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004010
Florian Fainellif8ac0412010-09-07 13:23:43 +02004011 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004012 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004013 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004014 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004015 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004016
Kamal Dasu66507c72014-05-01 20:51:19 -04004017 if (part_pagewr)
4018 use_bufpoi = 1;
4019 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004020 use_bufpoi = !virt_addr_valid(buf) ||
4021 !IS_ALIGNED((unsigned long)buf,
4022 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004023 else
4024 use_bufpoi = 0;
4025
4026 /* Partial page write?, or need to use bounce buffer */
4027 if (use_bufpoi) {
4028 pr_debug("%s: using write bounce buffer for buf@%p\n",
4029 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004030 if (part_pagewr)
4031 bytes = min_t(int, bytes - column, writelen);
Boris Brezilloneeab7172018-10-28 15:27:55 +01004032 wbuf = nand_get_data_buf(chip);
4033 memset(wbuf, 0xff, mtd->writesize);
4034 memcpy(&wbuf[column], buf, bytes);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004035 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004036
Maxim Levitsky782ce792010-02-22 20:39:36 +02004037 if (unlikely(oob)) {
4038 size_t len = min(oobwritelen, oobmaxlen);
Boris Brezillon08136212018-11-11 08:55:03 +01004039 oob = nand_fill_oob(chip, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004040 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004041 } else {
4042 /* We still need to erase leftover OOB data */
4043 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004044 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004045
Boris Brezillon08136212018-11-11 08:55:03 +01004046 ret = nand_write_page(chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004047 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004048 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004049 if (ret)
4050 break;
4051
4052 writelen -= bytes;
4053 if (!writelen)
4054 break;
4055
Thomas Gleixner29072b92006-09-28 15:38:36 +02004056 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004057 buf += bytes;
4058 realpage++;
4059
4060 page = realpage & chip->pagemask;
4061 /* Check, if we cross a chip boundary */
4062 if (!page) {
4063 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004064 nand_deselect_target(chip);
4065 nand_select_target(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004066 }
4067 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004068
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004069 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004070 if (unlikely(oob))
4071 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004072
4073err_out:
Boris Brezillon1d017852018-11-11 08:55:14 +01004074 nand_deselect_target(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004075 return ret;
4076}
4077
4078/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004079 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004080 * @mtd: MTD device structure
4081 * @to: offset to write to
4082 * @len: number of bytes to write
4083 * @retlen: pointer to variable to store the number of written bytes
4084 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004085 *
4086 * NAND write with ECC. Used when performing writes in interrupt context, this
4087 * may for example be called by mtdoops when writing an oops while in panic.
4088 */
4089static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4090 size_t *retlen, const uint8_t *buf)
4091{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004092 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004093 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004094 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004095 int ret;
4096
Boris Brezillon1d017852018-11-11 08:55:14 +01004097 nand_select_target(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004098
4099 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004100 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004101
Brian Norris0ec56dc2015-02-28 02:02:30 -08004102 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004103 ops.len = len;
4104 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004105 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004106
Boris Brezillon08136212018-11-11 08:55:03 +01004107 ret = nand_do_write_ops(chip, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004108
Brian Norris4a89ff82011-08-30 18:45:45 -07004109 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004110 return ret;
4111}
4112
4113/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004114 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004115 * @mtd: MTD device structure
4116 * @to: offset to write to
4117 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004118 */
4119static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4120 struct mtd_oob_ops *ops)
4121{
Boris Brezillon08136212018-11-11 08:55:03 +01004122 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004123 int ret = -ENOTSUPP;
4124
4125 ops->retlen = 0;
4126
Boris Brezillon013e6292018-11-20 11:57:20 +01004127 ret = nand_get_device(chip);
4128 if (ret)
4129 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004130
Florian Fainellif8ac0412010-09-07 13:23:43 +02004131 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004132 case MTD_OPS_PLACE_OOB:
4133 case MTD_OPS_AUTO_OOB:
4134 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004135 break;
4136
4137 default:
4138 goto out;
4139 }
4140
4141 if (!ops->datbuf)
Boris Brezillon08136212018-11-11 08:55:03 +01004142 ret = nand_do_write_oob(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004143 else
Boris Brezillon08136212018-11-11 08:55:03 +01004144 ret = nand_do_write_ops(chip, to, ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004145
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004146out:
Boris Brezillon08136212018-11-11 08:55:03 +01004147 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return ret;
4149}
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004153 * @mtd: MTD device structure
4154 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004156 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004158static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004160 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004164 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004165 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004166 * @instr: erase instruction
4167 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004169 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004171int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004172 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
Masahiro Yamada2d73f3d2019-01-21 15:32:07 +09004174 int page, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004175 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
Brian Norris289c0522011-07-19 10:06:09 -07004177 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4178 __func__, (unsigned long long)instr->addr,
4179 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Boris Brezillon08136212018-11-11 08:55:03 +01004181 if (check_offs_len(chip, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 /* Grab the lock and see if the device is available */
Boris Brezillon013e6292018-11-20 11:57:20 +01004185 ret = nand_get_device(chip);
4186 if (ret)
4187 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004190 page = (int)(instr->addr >> chip->page_shift);
4191 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
4193 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004194 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
4196 /* Select the NAND device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004197 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 /* Check, if it is write protected */
Boris Brezillon08136212018-11-11 08:55:03 +01004200 if (nand_check_wp(chip)) {
Brian Norris289c0522011-07-19 10:06:09 -07004201 pr_debug("%s: device is write protected!\n",
4202 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004203 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 goto erase_exit;
4205 }
4206
4207 /* Loop through the pages */
4208 len = instr->len;
4209
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004211 /* Check if we have a bad block, we do not erase bad blocks! */
Boris Brezillon08136212018-11-11 08:55:03 +01004212 if (nand_block_checkbad(chip, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304213 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004214 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4215 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004216 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 goto erase_exit;
4218 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004219
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004220 /*
4221 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004222 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004223 */
Boris Brezillond9745412018-10-28 16:12:45 +01004224 if (page <= chip->pagecache.page && chip->pagecache.page <
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004225 (page + pages_per_block))
Boris Brezillond9745412018-10-28 16:12:45 +01004226 chip->pagecache.page = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Masahiro Yamada2d73f3d2019-01-21 15:32:07 +09004228 ret = nand_erase_op(chip, (page & chip->pagemask) >>
4229 (chip->phys_erase_shift - chip->page_shift));
4230 if (ret) {
Brian Norris289c0522011-07-19 10:06:09 -07004231 pr_debug("%s: failed erase, page 0x%08x\n",
4232 __func__, page);
Adrian Hunter69423d92008-12-10 13:37:21 +00004233 instr->fail_addr =
4234 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 goto erase_exit;
4236 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004239 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 page += pages_per_block;
4241
4242 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004243 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 chipnr++;
Boris Brezillon1d017852018-11-11 08:55:14 +01004245 nand_deselect_target(chip);
4246 nand_select_target(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 }
4248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004250 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004251erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004254 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004255 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 /* Return more or less happy */
4258 return ret;
4259}
4260
4261/**
4262 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004263 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004265 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004267static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268{
Boris Brezillon08136212018-11-11 08:55:03 +01004269 struct nand_chip *chip = mtd_to_nand(mtd);
4270
Brian Norris289c0522011-07-19 10:06:09 -07004271 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 /* Grab the lock and see if the device is available */
Boris Brezillon013e6292018-11-20 11:57:20 +01004274 WARN_ON(nand_get_device(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 /* Release it and go back */
Boris Brezillon08136212018-11-11 08:55:03 +01004276 nand_release_device(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277}
4278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004280 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004281 * @mtd: MTD device structure
4282 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004284static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304286 struct nand_chip *chip = mtd_to_nand(mtd);
4287 int chipnr = (int)(offs >> chip->chip_shift);
4288 int ret;
4289
4290 /* Select the NAND device */
Boris Brezillon013e6292018-11-20 11:57:20 +01004291 ret = nand_get_device(chip);
4292 if (ret)
4293 return ret;
4294
Boris Brezillon1d017852018-11-11 08:55:14 +01004295 nand_select_target(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304296
Boris Brezillon08136212018-11-11 08:55:03 +01004297 ret = nand_block_checkbad(chip, offs, 0);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304298
Boris Brezillon1d017852018-11-11 08:55:14 +01004299 nand_deselect_target(chip);
Boris Brezillon08136212018-11-11 08:55:03 +01004300 nand_release_device(chip);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304301
4302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303}
4304
4305/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004306 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004307 * @mtd: MTD device structure
4308 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004310static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 int ret;
4313
Florian Fainellif8ac0412010-09-07 13:23:43 +02004314 ret = nand_block_isbad(mtd, ofs);
4315 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004316 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 if (ret > 0)
4318 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004319 return ret;
4320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
Boris Brezillon08136212018-11-11 08:55:03 +01004322 return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
4324
4325/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004326 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004327 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004328 */
4329static int nand_suspend(struct mtd_info *mtd)
4330{
Boris Brezillon013e6292018-11-20 11:57:20 +01004331 struct nand_chip *chip = mtd_to_nand(mtd);
4332
4333 mutex_lock(&chip->lock);
4334 chip->suspended = 1;
4335 mutex_unlock(&chip->lock);
4336
4337 return 0;
Vitaly Wool962034f2005-09-15 14:58:53 +01004338}
4339
4340/**
4341 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004342 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004343 */
4344static void nand_resume(struct mtd_info *mtd)
4345{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004346 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004347
Boris Brezillon013e6292018-11-20 11:57:20 +01004348 mutex_lock(&chip->lock);
4349 if (chip->suspended)
4350 chip->suspended = 0;
Vitaly Wool962034f2005-09-15 14:58:53 +01004351 else
Brian Norrisd0370212011-07-19 10:06:08 -07004352 pr_err("%s called for a chip which is not in suspended state\n",
4353 __func__);
Boris Brezillon013e6292018-11-20 11:57:20 +01004354 mutex_unlock(&chip->lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01004355}
4356
Scott Branden72ea4032014-11-20 11:18:05 -08004357/**
4358 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4359 * prevent further operations
4360 * @mtd: MTD device structure
4361 */
4362static void nand_shutdown(struct mtd_info *mtd)
4363{
Boris Brezillon013e6292018-11-20 11:57:20 +01004364 nand_suspend(mtd);
Scott Branden72ea4032014-11-20 11:18:05 -08004365}
4366
Brian Norris8b6e50c2011-05-25 14:59:01 -07004367/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004368static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004369{
Boris Brezillon7b6a9b22018-11-20 10:02:39 +01004370 /* If no controller is provided, use the dummy, legacy one. */
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004371 if (!chip->controller) {
Boris Brezillon7b6a9b22018-11-20 10:02:39 +01004372 chip->controller = &chip->legacy.dummy_controller;
Miquel Raynal7da45132018-07-17 09:08:02 +02004373 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004374 }
4375
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01004376 nand_legacy_set_defaults(chip);
4377
Masahiro Yamada477544c2017-03-30 17:15:05 +09004378 if (!chip->buf_align)
4379 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004380}
4381
Brian Norris8b6e50c2011-05-25 14:59:01 -07004382/* Sanitize ONFI strings so we can safely print them */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004383void sanitize_string(uint8_t *s, size_t len)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004384{
4385 ssize_t i;
4386
Brian Norris8b6e50c2011-05-25 14:59:01 -07004387 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004388 s[len - 1] = 0;
4389
Brian Norris8b6e50c2011-05-25 14:59:01 -07004390 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004391 for (i = 0; i < len - 1; i++) {
4392 if (s[i] < ' ' || s[i] > 127)
4393 s[i] = '?';
4394 }
4395
Brian Norris8b6e50c2011-05-25 14:59:01 -07004396 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004397 strim(s);
4398}
4399
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004400/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07004401 * nand_id_has_period - Check if an ID string has a given wraparound period
4402 * @id_data: the ID string
4403 * @arrlen: the length of the @id_data array
4404 * @period: the period of repitition
4405 *
4406 * Check if an ID string is repeated within a given sequence of bytes at
4407 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08004408 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07004409 * if the repetition has a period of @period; otherwise, returns zero.
4410 */
4411static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4412{
4413 int i, j;
4414 for (i = 0; i < period; i++)
4415 for (j = i + period; j < arrlen; j += period)
4416 if (id_data[i] != id_data[j])
4417 return 0;
4418 return 1;
4419}
4420
4421/*
4422 * nand_id_len - Get the length of an ID string returned by CMD_READID
4423 * @id_data: the ID string
4424 * @arrlen: the length of the @id_data array
4425
4426 * Returns the length of the ID string, according to known wraparound/trailing
4427 * zero patterns. If no pattern exists, returns the length of the array.
4428 */
4429static int nand_id_len(u8 *id_data, int arrlen)
4430{
4431 int last_nonzero, period;
4432
4433 /* Find last non-zero byte */
4434 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4435 if (id_data[last_nonzero])
4436 break;
4437
4438 /* All zeros */
4439 if (last_nonzero < 0)
4440 return 0;
4441
4442 /* Calculate wraparound period */
4443 for (period = 1; period < arrlen; period++)
4444 if (nand_id_has_period(id_data, arrlen, period))
4445 break;
4446
4447 /* There's a repeated pattern */
4448 if (period < arrlen)
4449 return period;
4450
4451 /* There are trailing zeros */
4452 if (last_nonzero < arrlen - 1)
4453 return last_nonzero + 1;
4454
4455 /* No pattern detected */
4456 return arrlen;
4457}
4458
Huang Shijie7db906b2013-09-25 14:58:11 +08004459/* Extract the bits of per cell from the 3rd byte of the extended ID */
4460static int nand_get_bits_per_cell(u8 cellinfo)
4461{
4462 int bits;
4463
4464 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4465 bits >>= NAND_CI_CELLTYPE_SHIFT;
4466 return bits + 1;
4467}
4468
Brian Norrise3b88bd2012-09-24 20:40:52 -07004469/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004470 * Many new NAND share similar device ID codes, which represent the size of the
4471 * chip. The rest of the parameters must be decoded according to generic or
4472 * manufacturer-specific "extended ID" decoding patterns.
4473 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004474void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004475{
Boris Brezillon629a4422018-10-25 17:10:37 +02004476 struct nand_memory_organization *memorg;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004477 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02004478 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004479 u8 *id_data = chip->id.data;
Boris Brezillon629a4422018-10-25 17:10:37 +02004480
4481 memorg = nanddev_get_memorg(&chip->base);
4482
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004483 /* The 3rd id byte holds MLC / multichip data */
Boris Brezillon629a4422018-10-25 17:10:37 +02004484 memorg->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004485 /* The 4th id byte is the important one */
4486 extid = id_data[3];
4487
Boris Brezillon01389b62016-06-08 10:30:18 +02004488 /* Calc pagesize */
Boris Brezillon629a4422018-10-25 17:10:37 +02004489 memorg->pagesize = 1024 << (extid & 0x03);
4490 mtd->writesize = memorg->pagesize;
Boris Brezillon01389b62016-06-08 10:30:18 +02004491 extid >>= 2;
4492 /* Calc oobsize */
Boris Brezillon629a4422018-10-25 17:10:37 +02004493 memorg->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4494 mtd->oobsize = memorg->oobsize;
Boris Brezillon01389b62016-06-08 10:30:18 +02004495 extid >>= 2;
4496 /* Calc blocksize. Blocksize is multiples of 64KiB */
Boris Brezillon629a4422018-10-25 17:10:37 +02004497 memorg->pages_per_eraseblock = ((64 * 1024) << (extid & 0x03)) /
4498 memorg->pagesize;
Boris Brezillon01389b62016-06-08 10:30:18 +02004499 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4500 extid >>= 2;
4501 /* Get buswidth information */
4502 if (extid & 0x1)
4503 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004504}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004505EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004506
4507/*
Brian Norrisf23a4812012-09-24 20:40:51 -07004508 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4509 * decodes a matching ID table entry and assigns the MTD size parameters for
4510 * the chip.
4511 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004512static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07004513{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004514 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon629a4422018-10-25 17:10:37 +02004515 struct nand_memory_organization *memorg;
Brian Norrisf23a4812012-09-24 20:40:51 -07004516
Boris Brezillon629a4422018-10-25 17:10:37 +02004517 memorg = nanddev_get_memorg(&chip->base);
4518
4519 memorg->pages_per_eraseblock = type->erasesize / type->pagesize;
Brian Norrisf23a4812012-09-24 20:40:51 -07004520 mtd->erasesize = type->erasesize;
Boris Brezillon629a4422018-10-25 17:10:37 +02004521 memorg->pagesize = type->pagesize;
4522 mtd->writesize = memorg->pagesize;
4523 memorg->oobsize = memorg->pagesize / 32;
4524 mtd->oobsize = memorg->oobsize;
Brian Norrisf23a4812012-09-24 20:40:51 -07004525
Huang Shijie1c195e92013-09-25 14:58:12 +08004526 /* All legacy ID NAND are small-page, SLC */
Boris Brezillon629a4422018-10-25 17:10:37 +02004527 memorg->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07004528}
4529
4530/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004531 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4532 * heuristic patterns using various detected parameters (e.g., manufacturer,
4533 * page size, cell-type information).
4534 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004535static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07004536{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004537 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004538
4539 /* Set the bad block position */
4540 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
Frieder Schrempf04649ec2019-04-17 12:36:34 +00004541 chip->badblockpos = NAND_BBM_POS_LARGE;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004542 else
Frieder Schrempf04649ec2019-04-17 12:36:34 +00004543 chip->badblockpos = NAND_BBM_POS_SMALL;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004544}
4545
Huang Shijieec6e87e2013-03-15 11:01:00 +08004546static inline bool is_full_id_nand(struct nand_flash_dev *type)
4547{
4548 return type->id_len;
4549}
4550
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004551static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02004552 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004553{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004554 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon629a4422018-10-25 17:10:37 +02004555 struct nand_memory_organization *memorg;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004556 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004557
Boris Brezillon629a4422018-10-25 17:10:37 +02004558 memorg = nanddev_get_memorg(&chip->base);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004559
Boris Brezillon629a4422018-10-25 17:10:37 +02004560 if (!strncmp(type->id, id_data, type->id_len)) {
4561 memorg->pagesize = type->pagesize;
4562 mtd->writesize = memorg->pagesize;
4563 memorg->pages_per_eraseblock = type->erasesize /
4564 type->pagesize;
4565 mtd->erasesize = type->erasesize;
4566 memorg->oobsize = type->oobsize;
4567 mtd->oobsize = memorg->oobsize;
4568
4569 memorg->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Boris Brezillon629a4422018-10-25 17:10:37 +02004570 memorg->eraseblocks_per_lun =
4571 DIV_ROUND_DOWN_ULL((u64)type->chipsize << 20,
4572 memorg->pagesize *
4573 memorg->pages_per_eraseblock);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004574 chip->options |= type->options;
Boris Brezillon6a1b66d2018-11-04 16:09:42 +01004575 chip->base.eccreq.strength = NAND_ECC_STRENGTH(type);
4576 chip->base.eccreq.step_size = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004577 chip->onfi_timing_mode_default =
4578 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004579
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004580 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4581 if (!chip->parameters.model)
4582 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004583
Huang Shijieec6e87e2013-03-15 11:01:00 +08004584 return true;
4585 }
4586 return false;
4587}
4588
Brian Norris7e74c2d2012-09-24 20:40:49 -07004589/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004590 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4591 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4592 * table.
4593 */
4594static void nand_manufacturer_detect(struct nand_chip *chip)
4595{
4596 /*
4597 * Try manufacturer detection if available and use
4598 * nand_decode_ext_id() otherwise.
4599 */
4600 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004601 chip->manufacturer.desc->ops->detect) {
Boris Brezillon629a4422018-10-25 17:10:37 +02004602 struct nand_memory_organization *memorg;
4603
4604 memorg = nanddev_get_memorg(&chip->base);
4605
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004606 /* The 3rd id byte holds MLC / multichip data */
Boris Brezillon629a4422018-10-25 17:10:37 +02004607 memorg->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004608 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004609 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004610 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004611 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004612}
4613
4614/*
4615 * Manufacturer initialization. This function is called for all NANDs including
4616 * ONFI and JEDEC compliant ones.
4617 * Manufacturer drivers should put all their specific initialization code in
4618 * their ->init() hook.
4619 */
4620static int nand_manufacturer_init(struct nand_chip *chip)
4621{
4622 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4623 !chip->manufacturer.desc->ops->init)
4624 return 0;
4625
4626 return chip->manufacturer.desc->ops->init(chip);
4627}
4628
4629/*
4630 * Manufacturer cleanup. This function is called for all NANDs including
4631 * ONFI and JEDEC compliant ones.
4632 * Manufacturer drivers should put all their specific cleanup code in their
4633 * ->cleanup() hook.
4634 */
4635static void nand_manufacturer_cleanup(struct nand_chip *chip)
4636{
4637 /* Release manufacturer private data */
4638 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4639 chip->manufacturer.desc->ops->cleanup)
4640 chip->manufacturer.desc->ops->cleanup(chip);
4641}
4642
Boris Brezillon348d56a2018-09-07 00:38:48 +02004643static const char *
4644nand_manufacturer_name(const struct nand_manufacturer *manufacturer)
4645{
4646 return manufacturer ? manufacturer->name : "Unknown";
4647}
4648
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004649/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004650 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004651 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004652static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004653{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004654 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004655 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon629a4422018-10-25 17:10:37 +02004656 struct nand_memory_organization *memorg;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004657 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004658 u8 *id_data = chip->id.data;
4659 u8 maf_id, dev_id;
Boris Brezillon6c836d52018-10-29 11:22:16 +01004660 u64 targetsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Karl Beldanef89a882008-09-15 14:37:29 +02004662 /*
Boris Brezillon629a4422018-10-25 17:10:37 +02004663 * Let's start by initializing memorg fields that might be left
4664 * unassigned by the ID-based detection logic.
4665 */
4666 memorg = nanddev_get_memorg(&chip->base);
4667 memorg->planes_per_lun = 1;
4668 memorg->luns_per_target = 1;
4669 memorg->ntargets = 1;
4670
4671 /*
Karl Beldanef89a882008-09-15 14:37:29 +02004672 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004673 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004674 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004675 ret = nand_reset(chip, 0);
4676 if (ret)
4677 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004678
4679 /* Select the device */
Boris Brezillon1d017852018-11-11 08:55:14 +01004680 nand_select_target(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004681
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004683 ret = nand_readid_op(chip, 0, id_data, 2);
4684 if (ret)
4685 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
4687 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004688 maf_id = id_data[0];
4689 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Brian Norris8b6e50c2011-05-25 14:59:01 -07004691 /*
4692 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004693 * interface concerns can cause random data which looks like a
4694 * possibly credible NAND flash to appear. If the two results do
4695 * not match, ignore the device completely.
4696 */
4697
Brian Norris4aef9b72012-09-24 20:40:48 -07004698 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004699 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
4700 if (ret)
4701 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01004702
Boris Brezillon7f501f02016-05-24 19:20:05 +02004703 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004704 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004705 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004706 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004707 }
4708
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02004709 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02004710
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004711 /* Try to identify manufacturer */
4712 manufacturer = nand_get_manufacturer(maf_id);
4713 chip->manufacturer.desc = manufacturer;
4714
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004715 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004716 type = nand_flash_ids;
4717
Boris Brezillon29a198a2016-05-24 20:17:48 +02004718 /*
4719 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4720 * override it.
4721 * This is required to make sure initial NAND bus width set by the
4722 * NAND controller driver is coherent with the real NAND bus width
4723 * (extracted by auto-detection code).
4724 */
4725 busw = chip->options & NAND_BUSWIDTH_16;
4726
4727 /*
4728 * The flag is only set (never cleared), reset it to its default value
4729 * before starting auto-detection.
4730 */
4731 chip->options &= ~NAND_BUSWIDTH_16;
4732
Huang Shijieec6e87e2013-03-15 11:01:00 +08004733 for (; type->name != NULL; type++) {
4734 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004735 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004736 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004737 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004738 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004739 }
4740 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004741
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004742 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004743 /* Check if the chip is ONFI compliant */
Boris Brezillon1c325cc2018-09-07 00:38:50 +02004744 ret = nand_onfi_detect(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01004745 if (ret < 0)
4746 return ret;
4747 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004748 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004749
4750 /* Check if the chip is JEDEC compliant */
Boris Brezillon8ae3fbf2018-09-07 00:38:51 +02004751 ret = nand_jedec_detect(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01004752 if (ret < 0)
4753 return ret;
4754 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08004755 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004756 }
4757
David Woodhouse5e81e882010-02-26 18:32:56 +00004758 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004759 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004760
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004761 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
4762 if (!chip->parameters.model)
4763 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004764
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004765 if (!type->pagesize)
4766 nand_manufacturer_detect(chip);
4767 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004768 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004769
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004770 /* Get chip options */
4771 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004772
Boris Brezillon629a4422018-10-25 17:10:37 +02004773 memorg->eraseblocks_per_lun =
4774 DIV_ROUND_DOWN_ULL((u64)type->chipsize << 20,
4775 memorg->pagesize *
4776 memorg->pages_per_eraseblock);
4777
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004778ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01004779 if (!mtd->name)
4780 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004781
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004782 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004783 WARN_ON(busw & NAND_BUSWIDTH_16);
4784 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004785 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4786 /*
4787 * Check, if buswidth is correct. Hardware drivers should set
4788 * chip correct!
4789 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004790 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004791 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004792 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4793 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004794 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4795 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004796 ret = -EINVAL;
4797
4798 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004799 }
4800
Boris Brezillon7f501f02016-05-24 19:20:05 +02004801 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004802
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004803 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004804 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004805 /* Convert chipsize to number of pages per chip -1 */
Boris Brezillon6c836d52018-10-29 11:22:16 +01004806 targetsize = nanddev_target_size(&chip->base);
4807 chip->pagemask = (targetsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004808
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004809 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004810 ffs(mtd->erasesize) - 1;
Boris Brezillon6c836d52018-10-29 11:22:16 +01004811 if (targetsize & 0xffffffff)
4812 chip->chip_shift = ffs((unsigned)targetsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004813 else {
Boris Brezillon6c836d52018-10-29 11:22:16 +01004814 chip->chip_shift = ffs((unsigned)(targetsize >> 32));
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004815 chip->chip_shift += 32 - 1;
4816 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004817
Masahiro Yamada14157f82017-09-13 11:05:50 +09004818 if (chip->chip_shift - chip->page_shift > 16)
4819 chip->options |= NAND_ROW_ADDR_3;
4820
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004821 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004822
Boris Brezillon3d4af7c2018-09-07 00:38:49 +02004823 nand_legacy_adjust_cmdfunc(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004824
Ezequiel Garcia20171642013-11-25 08:30:31 -03004825 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004826 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01004827 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4828 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02004829 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Boris Brezillon6c836d52018-10-29 11:22:16 +01004830 (int)(targetsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004831 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004832 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02004833
4834free_detect_allocation:
4835 kfree(chip->parameters.model);
4836
4837 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004838}
4839
Boris Brezillond48f62b2016-04-01 14:54:32 +02004840static const char * const nand_ecc_modes[] = {
4841 [NAND_ECC_NONE] = "none",
4842 [NAND_ECC_SOFT] = "soft",
4843 [NAND_ECC_HW] = "hw",
4844 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4845 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004846 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004847};
4848
4849static int of_get_nand_ecc_mode(struct device_node *np)
4850{
4851 const char *pm;
4852 int err, i;
4853
4854 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4855 if (err < 0)
4856 return err;
4857
4858 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4859 if (!strcasecmp(pm, nand_ecc_modes[i]))
4860 return i;
4861
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004862 /*
4863 * For backward compatibility we support few obsoleted values that don't
4864 * have their mappings into nand_ecc_modes_t anymore (they were merged
4865 * with other enums).
4866 */
4867 if (!strcasecmp(pm, "soft_bch"))
4868 return NAND_ECC_SOFT;
4869
Boris Brezillond48f62b2016-04-01 14:54:32 +02004870 return -ENODEV;
4871}
4872
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004873static const char * const nand_ecc_algos[] = {
4874 [NAND_ECC_HAMMING] = "hamming",
4875 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02004876 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004877};
4878
Boris Brezillond48f62b2016-04-01 14:54:32 +02004879static int of_get_nand_ecc_algo(struct device_node *np)
4880{
4881 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004882 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004883
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004884 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4885 if (!err) {
4886 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4887 if (!strcasecmp(pm, nand_ecc_algos[i]))
4888 return i;
4889 return -ENODEV;
4890 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004891
4892 /*
4893 * For backward compatibility we also read "nand-ecc-mode" checking
4894 * for some obsoleted values that were specifying ECC algorithm.
4895 */
4896 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4897 if (err < 0)
4898 return err;
4899
4900 if (!strcasecmp(pm, "soft"))
4901 return NAND_ECC_HAMMING;
4902 else if (!strcasecmp(pm, "soft_bch"))
4903 return NAND_ECC_BCH;
4904
4905 return -ENODEV;
4906}
4907
4908static int of_get_nand_ecc_step_size(struct device_node *np)
4909{
4910 int ret;
4911 u32 val;
4912
4913 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4914 return ret ? ret : val;
4915}
4916
4917static int of_get_nand_ecc_strength(struct device_node *np)
4918{
4919 int ret;
4920 u32 val;
4921
4922 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4923 return ret ? ret : val;
4924}
4925
4926static int of_get_nand_bus_width(struct device_node *np)
4927{
4928 u32 val;
4929
4930 if (of_property_read_u32(np, "nand-bus-width", &val))
4931 return 8;
4932
4933 switch (val) {
4934 case 8:
4935 case 16:
4936 return val;
4937 default:
4938 return -EIO;
4939 }
4940}
4941
4942static bool of_get_nand_on_flash_bbt(struct device_node *np)
4943{
4944 return of_property_read_bool(np, "nand-on-flash-bbt");
4945}
4946
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004947static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004948{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004949 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004950 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004951
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004952 if (!dn)
4953 return 0;
4954
Brian Norris5844fee2015-01-23 00:22:27 -08004955 if (of_get_nand_bus_width(dn) == 16)
4956 chip->options |= NAND_BUSWIDTH_16;
4957
Stefan Agnerf922bd72018-06-24 23:27:23 +02004958 if (of_property_read_bool(dn, "nand-is-boot-medium"))
4959 chip->options |= NAND_IS_BOOT_MEDIUM;
4960
Brian Norris5844fee2015-01-23 00:22:27 -08004961 if (of_get_nand_on_flash_bbt(dn))
4962 chip->bbt_options |= NAND_BBT_USE_FLASH;
4963
4964 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004965 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004966 ecc_strength = of_get_nand_ecc_strength(dn);
4967 ecc_step = of_get_nand_ecc_step_size(dn);
4968
Brian Norris5844fee2015-01-23 00:22:27 -08004969 if (ecc_mode >= 0)
4970 chip->ecc.mode = ecc_mode;
4971
Rafał Miłecki79082452016-03-23 11:19:02 +01004972 if (ecc_algo >= 0)
4973 chip->ecc.algo = ecc_algo;
4974
Brian Norris5844fee2015-01-23 00:22:27 -08004975 if (ecc_strength >= 0)
4976 chip->ecc.strength = ecc_strength;
4977
4978 if (ecc_step > 0)
4979 chip->ecc.size = ecc_step;
4980
Boris Brezillonba78ee02016-06-08 17:04:22 +02004981 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4982 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4983
Brian Norris5844fee2015-01-23 00:22:27 -08004984 return 0;
4985}
4986
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004987/**
Miquel Raynal98732da2018-07-25 15:31:50 +02004988 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02004989 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004990 * @maxchips: number of chips to scan for
4991 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004992 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004993 * This is the first phase of the normal nand_scan() function. It reads the
4994 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004995 *
Miquel Raynal98732da2018-07-25 15:31:50 +02004996 * This helper used to be called directly from controller drivers that needed
4997 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
4998 * prevented dynamic allocations during this phase which was unconvenient and
4999 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005000 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005001static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005002 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005003{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005004 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon629a4422018-10-25 17:10:37 +02005005 struct nand_memory_organization *memorg;
Boris Brezillon871a4072018-08-04 22:59:22 +02005006 int nand_maf_id, nand_dev_id;
5007 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08005008 int ret;
5009
Boris Brezillon629a4422018-10-25 17:10:37 +02005010 memorg = nanddev_get_memorg(&chip->base);
5011
Boris Brezillonae2294b2018-11-11 08:55:15 +01005012 /* Assume all dies are deselected when we enter nand_scan_ident(). */
5013 chip->cur_cs = -1;
5014
Boris Brezillon013e6292018-11-20 11:57:20 +01005015 mutex_init(&chip->lock);
5016
Miquel Raynal17fa8042017-11-30 18:01:31 +01005017 /* Enforce the right timings for reset/detection */
5018 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5019
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005020 ret = nand_dt_init(chip);
5021 if (ret)
5022 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005023
Brian Norrisf7a8e382016-01-05 10:39:45 -08005024 if (!mtd->name && mtd->dev.parent)
5025 mtd->name = dev_name(mtd->dev.parent);
5026
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005027 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005028 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005029
Boris Brezillonf2abfeb2018-11-11 08:55:23 +01005030 ret = nand_legacy_check_hooks(chip);
5031 if (ret)
5032 return ret;
5033
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005034 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005035 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005036 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005037 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005038 pr_warn("No NAND device found\n");
Boris Brezillon1d017852018-11-11 08:55:14 +01005039 nand_deselect_target(chip);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005040 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 }
5042
Boris Brezillon7f501f02016-05-24 19:20:05 +02005043 nand_maf_id = chip->id.data[0];
5044 nand_dev_id = chip->id.data[1];
5045
Boris Brezillon1d017852018-11-11 08:55:14 +01005046 nand_deselect_target(chip);
Huang Shijie07300162012-11-09 16:23:45 +08005047
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005048 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005049 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005050 u8 id[2];
5051
Karl Beldanef89a882008-09-15 14:37:29 +02005052 /* See comment in nand_get_flash_type for reset */
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +09005053 ret = nand_reset(chip, i);
5054 if (ret)
5055 break;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005056
Boris Brezillon1d017852018-11-11 08:55:14 +01005057 nand_select_target(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 /* Send the command for reading device ID */
Masahiro Yamadaf9ffb402019-01-21 22:05:34 +09005059 ret = nand_readid_op(chip, 0, id, sizeof(id));
5060 if (ret)
5061 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005063 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon1d017852018-11-11 08:55:14 +01005064 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 break;
Huang Shijie07300162012-11-09 16:23:45 +08005066 }
Boris Brezillon1d017852018-11-11 08:55:14 +01005067 nand_deselect_target(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 }
5069 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005070 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 /* Store the number of chips and calc total size for mtd */
Boris Brezillon629a4422018-10-25 17:10:37 +02005073 memorg->ntargets = i;
Boris Brezillon6c836d52018-10-29 11:22:16 +01005074 mtd->size = i * nanddev_target_size(&chip->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
David Woodhouse3b85c322006-09-25 17:06:53 +01005076 return 0;
5077}
5078
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005079static void nand_scan_ident_cleanup(struct nand_chip *chip)
5080{
5081 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005082 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005083}
5084
Boris Brezillon08136212018-11-11 08:55:03 +01005085static int nand_set_ecc_soft_ops(struct nand_chip *chip)
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005086{
Boris Brezillon08136212018-11-11 08:55:03 +01005087 struct mtd_info *mtd = nand_to_mtd(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005088 struct nand_ecc_ctrl *ecc = &chip->ecc;
5089
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005090 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005091 return -EINVAL;
5092
5093 switch (ecc->algo) {
5094 case NAND_ECC_HAMMING:
5095 ecc->calculate = nand_calculate_ecc;
5096 ecc->correct = nand_correct_data;
5097 ecc->read_page = nand_read_page_swecc;
5098 ecc->read_subpage = nand_read_subpage;
5099 ecc->write_page = nand_write_page_swecc;
5100 ecc->read_page_raw = nand_read_page_raw;
5101 ecc->write_page_raw = nand_write_page_raw;
5102 ecc->read_oob = nand_read_oob_std;
5103 ecc->write_oob = nand_write_oob_std;
5104 if (!ecc->size)
5105 ecc->size = 256;
5106 ecc->bytes = 3;
5107 ecc->strength = 1;
Boris Brezillon309600c2018-09-04 16:23:28 +02005108
Miquel Raynal9bb94642019-02-08 08:48:37 +01005109 if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC))
Boris Brezillon309600c2018-09-04 16:23:28 +02005110 ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
5111
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005112 return 0;
5113 case NAND_ECC_BCH:
5114 if (!mtd_nand_has_bch()) {
Miquel Raynal714c0682019-02-06 15:12:27 +01005115 WARN(1, "CONFIG_MTD_NAND_ECC_SW_BCH not enabled\n");
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005116 return -EINVAL;
5117 }
5118 ecc->calculate = nand_bch_calculate_ecc;
5119 ecc->correct = nand_bch_correct_data;
5120 ecc->read_page = nand_read_page_swecc;
5121 ecc->read_subpage = nand_read_subpage;
5122 ecc->write_page = nand_write_page_swecc;
5123 ecc->read_page_raw = nand_read_page_raw;
5124 ecc->write_page_raw = nand_write_page_raw;
5125 ecc->read_oob = nand_read_oob_std;
5126 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005127
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005128 /*
5129 * Board driver should supply ecc.size and ecc.strength
5130 * values to select how many bits are correctable.
5131 * Otherwise, default to 4 bits for large page devices.
5132 */
5133 if (!ecc->size && (mtd->oobsize >= 64)) {
5134 ecc->size = 512;
5135 ecc->strength = 4;
5136 }
5137
5138 /*
5139 * if no ecc placement scheme was provided pickup the default
5140 * large page one.
5141 */
5142 if (!mtd->ooblayout) {
5143 /* handle large page devices only */
5144 if (mtd->oobsize < 64) {
5145 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5146 return -EINVAL;
5147 }
5148
5149 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005150
5151 }
5152
5153 /*
5154 * We can only maximize ECC config when the default layout is
5155 * used, otherwise we don't know how many bytes can really be
5156 * used.
5157 */
5158 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5159 ecc->options & NAND_ECC_MAXIMIZE) {
5160 int steps, bytes;
5161
5162 /* Always prefer 1k blocks over 512bytes ones */
5163 ecc->size = 1024;
5164 steps = mtd->writesize / ecc->size;
5165
5166 /* Reserve 2 bytes for the BBM */
5167 bytes = (mtd->oobsize - 2) / steps;
5168 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005169 }
5170
5171 /* See nand_bch_init() for details. */
5172 ecc->bytes = 0;
5173 ecc->priv = nand_bch_init(mtd);
5174 if (!ecc->priv) {
5175 WARN(1, "BCH ECC initialization failed!\n");
5176 return -EINVAL;
5177 }
5178 return 0;
5179 default:
5180 WARN(1, "Unsupported ECC algorithm!\n");
5181 return -EINVAL;
5182 }
5183}
5184
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005185/**
5186 * nand_check_ecc_caps - check the sanity of preset ECC settings
5187 * @chip: nand chip info structure
5188 * @caps: ECC caps info structure
5189 * @oobavail: OOB size that the ECC engine can use
5190 *
5191 * When ECC step size and strength are already set, check if they are supported
5192 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5193 * On success, the calculated ECC bytes is set.
5194 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305195static int
5196nand_check_ecc_caps(struct nand_chip *chip,
5197 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005198{
5199 struct mtd_info *mtd = nand_to_mtd(chip);
5200 const struct nand_ecc_step_info *stepinfo;
5201 int preset_step = chip->ecc.size;
5202 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305203 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005204 int i, j;
5205
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005206 for (i = 0; i < caps->nstepinfos; i++) {
5207 stepinfo = &caps->stepinfos[i];
5208
5209 if (stepinfo->stepsize != preset_step)
5210 continue;
5211
5212 for (j = 0; j < stepinfo->nstrengths; j++) {
5213 if (stepinfo->strengths[j] != preset_strength)
5214 continue;
5215
5216 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5217 preset_strength);
5218 if (WARN_ON_ONCE(ecc_bytes < 0))
5219 return ecc_bytes;
5220
5221 if (ecc_bytes * nsteps > oobavail) {
5222 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5223 preset_step, preset_strength);
5224 return -ENOSPC;
5225 }
5226
5227 chip->ecc.bytes = ecc_bytes;
5228
5229 return 0;
5230 }
5231 }
5232
5233 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5234 preset_step, preset_strength);
5235
5236 return -ENOTSUPP;
5237}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005238
5239/**
5240 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5241 * @chip: nand chip info structure
5242 * @caps: ECC engine caps info structure
5243 * @oobavail: OOB size that the ECC engine can use
5244 *
5245 * If a chip's ECC requirement is provided, try to meet it with the least
5246 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5247 * On success, the chosen ECC settings are set.
5248 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305249static int
5250nand_match_ecc_req(struct nand_chip *chip,
5251 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005252{
5253 struct mtd_info *mtd = nand_to_mtd(chip);
5254 const struct nand_ecc_step_info *stepinfo;
Boris Brezillon6a1b66d2018-11-04 16:09:42 +01005255 int req_step = chip->base.eccreq.step_size;
5256 int req_strength = chip->base.eccreq.strength;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005257 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5258 int best_step, best_strength, best_ecc_bytes;
5259 int best_ecc_bytes_total = INT_MAX;
5260 int i, j;
5261
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005262 /* No information provided by the NAND chip */
5263 if (!req_step || !req_strength)
5264 return -ENOTSUPP;
5265
5266 /* number of correctable bits the chip requires in a page */
5267 req_corr = mtd->writesize / req_step * req_strength;
5268
5269 for (i = 0; i < caps->nstepinfos; i++) {
5270 stepinfo = &caps->stepinfos[i];
5271 step_size = stepinfo->stepsize;
5272
5273 for (j = 0; j < stepinfo->nstrengths; j++) {
5274 strength = stepinfo->strengths[j];
5275
5276 /*
5277 * If both step size and strength are smaller than the
5278 * chip's requirement, it is not easy to compare the
5279 * resulted reliability.
5280 */
5281 if (step_size < req_step && strength < req_strength)
5282 continue;
5283
5284 if (mtd->writesize % step_size)
5285 continue;
5286
5287 nsteps = mtd->writesize / step_size;
5288
5289 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5290 if (WARN_ON_ONCE(ecc_bytes < 0))
5291 continue;
5292 ecc_bytes_total = ecc_bytes * nsteps;
5293
5294 if (ecc_bytes_total > oobavail ||
5295 strength * nsteps < req_corr)
5296 continue;
5297
5298 /*
5299 * We assume the best is to meet the chip's requrement
5300 * with the least number of ECC bytes.
5301 */
5302 if (ecc_bytes_total < best_ecc_bytes_total) {
5303 best_ecc_bytes_total = ecc_bytes_total;
5304 best_step = step_size;
5305 best_strength = strength;
5306 best_ecc_bytes = ecc_bytes;
5307 }
5308 }
5309 }
5310
5311 if (best_ecc_bytes_total == INT_MAX)
5312 return -ENOTSUPP;
5313
5314 chip->ecc.size = best_step;
5315 chip->ecc.strength = best_strength;
5316 chip->ecc.bytes = best_ecc_bytes;
5317
5318 return 0;
5319}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005320
5321/**
5322 * nand_maximize_ecc - choose the max ECC strength available
5323 * @chip: nand chip info structure
5324 * @caps: ECC engine caps info structure
5325 * @oobavail: OOB size that the ECC engine can use
5326 *
5327 * Choose the max ECC strength that is supported on the controller, and can fit
5328 * within the chip's OOB. On success, the chosen ECC settings are set.
5329 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305330static int
5331nand_maximize_ecc(struct nand_chip *chip,
5332 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005333{
5334 struct mtd_info *mtd = nand_to_mtd(chip);
5335 const struct nand_ecc_step_info *stepinfo;
5336 int step_size, strength, nsteps, ecc_bytes, corr;
5337 int best_corr = 0;
5338 int best_step = 0;
5339 int best_strength, best_ecc_bytes;
5340 int i, j;
5341
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005342 for (i = 0; i < caps->nstepinfos; i++) {
5343 stepinfo = &caps->stepinfos[i];
5344 step_size = stepinfo->stepsize;
5345
5346 /* If chip->ecc.size is already set, respect it */
5347 if (chip->ecc.size && step_size != chip->ecc.size)
5348 continue;
5349
5350 for (j = 0; j < stepinfo->nstrengths; j++) {
5351 strength = stepinfo->strengths[j];
5352
5353 if (mtd->writesize % step_size)
5354 continue;
5355
5356 nsteps = mtd->writesize / step_size;
5357
5358 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5359 if (WARN_ON_ONCE(ecc_bytes < 0))
5360 continue;
5361
5362 if (ecc_bytes * nsteps > oobavail)
5363 continue;
5364
5365 corr = strength * nsteps;
5366
5367 /*
5368 * If the number of correctable bits is the same,
5369 * bigger step_size has more reliability.
5370 */
5371 if (corr > best_corr ||
5372 (corr == best_corr && step_size > best_step)) {
5373 best_corr = corr;
5374 best_step = step_size;
5375 best_strength = strength;
5376 best_ecc_bytes = ecc_bytes;
5377 }
5378 }
5379 }
5380
5381 if (!best_corr)
5382 return -ENOTSUPP;
5383
5384 chip->ecc.size = best_step;
5385 chip->ecc.strength = best_strength;
5386 chip->ecc.bytes = best_ecc_bytes;
5387
5388 return 0;
5389}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005390
Abhishek Sahu181ace92018-06-20 12:57:28 +05305391/**
5392 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
5393 * @chip: nand chip info structure
5394 * @caps: ECC engine caps info structure
5395 * @oobavail: OOB size that the ECC engine can use
5396 *
5397 * Choose the ECC configuration according to following logic
5398 *
5399 * 1. If both ECC step size and ECC strength are already set (usually by DT)
5400 * then check if it is supported by this controller.
5401 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
5402 * 3. Otherwise, try to match the ECC step size and ECC strength closest
5403 * to the chip's requirement. If available OOB size can't fit the chip
5404 * requirement then fallback to the maximum ECC step size and ECC strength.
5405 *
5406 * On success, the chosen ECC settings are set.
5407 */
5408int nand_ecc_choose_conf(struct nand_chip *chip,
5409 const struct nand_ecc_caps *caps, int oobavail)
5410{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05305411 struct mtd_info *mtd = nand_to_mtd(chip);
5412
5413 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
5414 return -EINVAL;
5415
Abhishek Sahu181ace92018-06-20 12:57:28 +05305416 if (chip->ecc.size && chip->ecc.strength)
5417 return nand_check_ecc_caps(chip, caps, oobavail);
5418
5419 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
5420 return nand_maximize_ecc(chip, caps, oobavail);
5421
5422 if (!nand_match_ecc_req(chip, caps, oobavail))
5423 return 0;
5424
5425 return nand_maximize_ecc(chip, caps, oobavail);
5426}
5427EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
5428
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005429/*
5430 * Check if the chip configuration meet the datasheet requirements.
5431
5432 * If our configuration corrects A bits per B bytes and the minimum
5433 * required correction level is X bits per Y bytes, then we must ensure
5434 * both of the following are true:
5435 *
5436 * (1) A / B >= X / Y
5437 * (2) A >= X
5438 *
5439 * Requirement (1) ensures we can correct for the required bitflip density.
5440 * Requirement (2) ensures we can correct even when all bitflips are clumped
5441 * in the same sector.
5442 */
Boris Brezillon08136212018-11-11 08:55:03 +01005443static bool nand_ecc_strength_good(struct nand_chip *chip)
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005444{
Boris Brezillon08136212018-11-11 08:55:03 +01005445 struct mtd_info *mtd = nand_to_mtd(chip);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005446 struct nand_ecc_ctrl *ecc = &chip->ecc;
5447 int corr, ds_corr;
5448
Boris Brezillon6a1b66d2018-11-04 16:09:42 +01005449 if (ecc->size == 0 || chip->base.eccreq.step_size == 0)
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005450 /* Not enough information */
5451 return true;
5452
5453 /*
5454 * We get the number of corrected bits per page to compare
5455 * the correction density.
5456 */
5457 corr = (mtd->writesize * ecc->strength) / ecc->size;
Boris Brezillon6a1b66d2018-11-04 16:09:42 +01005458 ds_corr = (mtd->writesize * chip->base.eccreq.strength) /
5459 chip->base.eccreq.step_size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005460
Boris Brezillon6a1b66d2018-11-04 16:09:42 +01005461 return corr >= ds_corr && ecc->strength >= chip->base.eccreq.strength;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005462}
David Woodhouse3b85c322006-09-25 17:06:53 +01005463
Boris Brezillona7ab0852018-10-25 22:10:36 +02005464static int rawnand_erase(struct nand_device *nand, const struct nand_pos *pos)
5465{
5466 struct nand_chip *chip = container_of(nand, struct nand_chip,
5467 base);
5468 unsigned int eb = nanddev_pos_to_row(nand, pos);
5469 int ret;
5470
5471 eb >>= nand->rowconv.eraseblock_addr_shift;
5472
5473 nand_select_target(chip, pos->target);
5474 ret = nand_erase_op(chip, eb);
5475 nand_deselect_target(chip);
5476
5477 return ret;
5478}
5479
5480static int rawnand_markbad(struct nand_device *nand,
5481 const struct nand_pos *pos)
5482{
5483 struct nand_chip *chip = container_of(nand, struct nand_chip,
5484 base);
5485
5486 return nand_markbad_bbm(chip, nanddev_pos_to_offs(nand, pos));
5487}
5488
5489static bool rawnand_isbad(struct nand_device *nand, const struct nand_pos *pos)
5490{
5491 struct nand_chip *chip = container_of(nand, struct nand_chip,
5492 base);
5493 int ret;
5494
5495 nand_select_target(chip, pos->target);
5496 ret = nand_isbad_bbm(chip, nanddev_pos_to_offs(nand, pos));
5497 nand_deselect_target(chip);
5498
5499 return ret;
5500}
5501
5502static const struct nand_ops rawnand_ops = {
5503 .erase = rawnand_erase,
5504 .markbad = rawnand_markbad,
5505 .isbad = rawnand_isbad,
5506};
5507
David Woodhouse3b85c322006-09-25 17:06:53 +01005508/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005509 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005510 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01005511 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005512 * This is the second phase of the normal nand_scan() function. It fills out
5513 * all the uninitialized function pointers with the defaults and scans for a
5514 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01005515 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005516static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01005517{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005518 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08005519 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005520 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01005521
Brian Norrise2414f42012-02-06 13:44:00 -08005522 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005523 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07005524 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005525 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07005526 }
Brian Norrise2414f42012-02-06 13:44:00 -08005527
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005528 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005529 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02005530 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005531
Boris Brezillonf84674b2017-06-02 12:18:24 +02005532 /*
5533 * FIXME: some NAND manufacturer drivers expect the first die to be
5534 * selected when manufacturer->init() is called. They should be fixed
5535 * to explictly select the relevant die when interacting with the NAND
5536 * chip.
5537 */
Boris Brezillon1d017852018-11-11 08:55:14 +01005538 nand_select_target(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005539 ret = nand_manufacturer_init(chip);
Boris Brezillon1d017852018-11-11 08:55:14 +01005540 nand_deselect_target(chip);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005541 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005542 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005543
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01005544 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005545 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005546
5547 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005548 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005549 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005550 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005551 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005552 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01005555 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 break;
5557 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005558 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02005559 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005560 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02005562 /*
5563 * Expose the whole OOB area to users if ECC_NONE
5564 * is passed. We could do that for all kind of
5565 * ->oobsize, but we must keep the old large/small
5566 * page with ECC layout when ->oobsize <= 128 for
5567 * compatibility reasons.
5568 */
5569 if (ecc->mode == NAND_ECC_NONE) {
5570 mtd_set_ooblayout(mtd,
5571 &nand_ooblayout_lp_ops);
5572 break;
5573 }
5574
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005575 WARN(1, "No oob scheme defined for oobsize %d\n",
5576 mtd->oobsize);
5577 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005578 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 }
5580 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005581
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005582 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005583 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005584 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01005585 */
David Woodhouse956e9442006-09-25 17:12:39 +01005586
Huang Shijie97de79e02013-10-18 14:20:53 +08005587 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005588 case NAND_ECC_HW_OOB_FIRST:
5589 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08005590 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005591 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5592 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005593 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005594 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005595 if (!ecc->read_page)
5596 ecc->read_page = nand_read_page_hwecc_oob_first;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005597 /* fall through */
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005598
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005599 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07005600 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005601 if (!ecc->read_page)
5602 ecc->read_page = nand_read_page_hwecc;
5603 if (!ecc->write_page)
5604 ecc->write_page = nand_write_page_hwecc;
5605 if (!ecc->read_page_raw)
5606 ecc->read_page_raw = nand_read_page_raw;
5607 if (!ecc->write_page_raw)
5608 ecc->write_page_raw = nand_write_page_raw;
5609 if (!ecc->read_oob)
5610 ecc->read_oob = nand_read_oob_std;
5611 if (!ecc->write_oob)
5612 ecc->write_oob = nand_write_oob_std;
5613 if (!ecc->read_subpage)
5614 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02005615 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08005616 ecc->write_subpage = nand_write_subpage_hwecc;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005617 /* fall through */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005618
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005619 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08005620 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5621 (!ecc->read_page ||
5622 ecc->read_page == nand_read_page_hwecc ||
5623 !ecc->write_page ||
5624 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005625 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5626 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005627 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005628 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07005629 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005630 if (!ecc->read_page)
5631 ecc->read_page = nand_read_page_syndrome;
5632 if (!ecc->write_page)
5633 ecc->write_page = nand_write_page_syndrome;
5634 if (!ecc->read_page_raw)
5635 ecc->read_page_raw = nand_read_page_raw_syndrome;
5636 if (!ecc->write_page_raw)
5637 ecc->write_page_raw = nand_write_page_raw_syndrome;
5638 if (!ecc->read_oob)
5639 ecc->read_oob = nand_read_oob_syndrome;
5640 if (!ecc->write_oob)
5641 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005642
Huang Shijie97de79e02013-10-18 14:20:53 +08005643 if (mtd->writesize >= ecc->size) {
5644 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005645 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
5646 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005647 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07005648 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005649 break;
Mike Dunne2788c92012-04-25 12:06:10 -07005650 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005651 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5652 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08005653 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02005654 ecc->algo = NAND_ECC_HAMMING;
Mathieu Malaterre3175e122019-01-16 20:50:04 +01005655 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005657 case NAND_ECC_SOFT:
Boris Brezillon08136212018-11-11 08:55:03 +01005658 ret = nand_set_ecc_soft_ops(chip);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005659 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005660 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005661 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01005662 }
5663 break;
5664
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005665 case NAND_ECC_ON_DIE:
5666 if (!ecc->read_page || !ecc->write_page) {
5667 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
5668 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005669 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005670 }
5671 if (!ecc->read_oob)
5672 ecc->read_oob = nand_read_oob_std;
5673 if (!ecc->write_oob)
5674 ecc->write_oob = nand_write_oob_std;
5675 break;
5676
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005677 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005678 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08005679 ecc->read_page = nand_read_page_raw;
5680 ecc->write_page = nand_write_page_raw;
5681 ecc->read_oob = nand_read_oob_std;
5682 ecc->read_page_raw = nand_read_page_raw;
5683 ecc->write_page_raw = nand_write_page_raw;
5684 ecc->write_oob = nand_write_oob_std;
5685 ecc->size = mtd->writesize;
5686 ecc->bytes = 0;
5687 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005689
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005691 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5692 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005693 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005696 if (ecc->correct || ecc->calculate) {
5697 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5698 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5699 if (!ecc->calc_buf || !ecc->code_buf) {
5700 ret = -ENOMEM;
5701 goto err_nand_manuf_cleanup;
5702 }
5703 }
5704
Brian Norris9ce244b2011-08-30 18:45:37 -07005705 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005706 if (!ecc->read_oob_raw)
5707 ecc->read_oob_raw = ecc->read_oob;
5708 if (!ecc->write_oob_raw)
5709 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005710
Boris Brezillon846031d2016-02-03 20:11:00 +01005711 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005712 mtd->ecc_strength = ecc->strength;
5713 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005714
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005715 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005716 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005717 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005718 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005719 ecc->steps = mtd->writesize / ecc->size;
5720 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005721 WARN(1, "Invalid ECC parameters\n");
5722 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005723 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005725 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005726 if (ecc->total > mtd->oobsize) {
5727 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5728 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005729 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005730 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005731
Boris Brezillon846031d2016-02-03 20:11:00 +01005732 /*
5733 * The number of bytes available for a client to place data into
5734 * the out of band area.
5735 */
5736 ret = mtd_ooblayout_count_freebytes(mtd);
5737 if (ret < 0)
5738 ret = 0;
5739
5740 mtd->oobavail = ret;
5741
5742 /* ECC sanity check: warn if it's too weak */
Boris Brezillon08136212018-11-11 08:55:03 +01005743 if (!nand_ecc_strength_good(chip))
Boris Brezillon846031d2016-02-03 20:11:00 +01005744 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5745 mtd->name);
5746
Brian Norris8b6e50c2011-05-25 14:59:01 -07005747 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005748 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005749 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005750 case 2:
5751 mtd->subpage_sft = 1;
5752 break;
5753 case 4:
5754 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005755 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005756 mtd->subpage_sft = 2;
5757 break;
5758 }
5759 }
5760 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5761
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 /* Invalidate the pagebuffer reference */
Boris Brezillond9745412018-10-28 16:12:45 +01005763 chip->pagecache.page = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005765 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305766 switch (ecc->mode) {
5767 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305768 if (chip->page_shift > 9)
5769 chip->options |= NAND_SUBPAGE_READ;
5770 break;
5771
5772 default:
5773 break;
5774 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005775
Boris Brezillona7ab0852018-10-25 22:10:36 +02005776 ret = nanddev_init(&chip->base, &rawnand_ops, mtd->owner);
5777 if (ret)
5778 goto err_nand_manuf_cleanup;
5779
5780 /* Adjust the MTD_CAP_ flags when NAND_ROM is set. */
5781 if (chip->options & NAND_ROM)
5782 mtd->flags = MTD_CAP_ROM;
5783
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 /* Fill in remaining MTD driver data */
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005785 mtd->_erase = nand_erase;
5786 mtd->_point = NULL;
5787 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005788 mtd->_panic_write = panic_nand_write;
5789 mtd->_read_oob = nand_read_oob;
5790 mtd->_write_oob = nand_write_oob;
5791 mtd->_sync = nand_sync;
5792 mtd->_lock = NULL;
5793 mtd->_unlock = NULL;
5794 mtd->_suspend = nand_suspend;
5795 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005796 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005797 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005798 mtd->_block_isbad = nand_block_isbad;
5799 mtd->_block_markbad = nand_block_markbad;
Boris Brezillon7beb37e2018-11-04 14:50:28 +01005800 mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005802 /*
5803 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5804 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5805 * properly set.
5806 */
5807 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005808 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Boris Brezillonf84674b2017-06-02 12:18:24 +02005810 /* Initialize the ->data_interface field. */
5811 ret = nand_init_data_interface(chip);
5812 if (ret)
Boris Brezillona7ab0852018-10-25 22:10:36 +02005813 goto err_nanddev_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005814
5815 /* Enter fastest possible mode on all dies. */
Boris Brezillon32813e22018-10-29 11:58:29 +01005816 for (i = 0; i < nanddev_ntargets(&chip->base); i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005817 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005818 if (ret)
Boris Brezillona7ab0852018-10-25 22:10:36 +02005819 goto err_nanddev_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005820 }
5821
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005822 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005823 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
5826 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02005827 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07005828 if (ret)
Boris Brezillona7ab0852018-10-25 22:10:36 +02005829 goto err_nanddev_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005830
Brian Norris44d41822017-05-01 17:04:50 -07005831 return 0;
5832
Boris Brezillonf84674b2017-06-02 12:18:24 +02005833
Boris Brezillona7ab0852018-10-25 22:10:36 +02005834err_nanddev_cleanup:
5835 nanddev_cleanup(&chip->base);
5836
Boris Brezillonf84674b2017-06-02 12:18:24 +02005837err_nand_manuf_cleanup:
5838 nand_manufacturer_cleanup(chip);
5839
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005840err_free_buf:
5841 kfree(chip->data_buf);
5842 kfree(ecc->code_buf);
5843 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07005844
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005845 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846}
5847
Miquel Raynal05b54c72018-07-19 01:05:46 +02005848static int nand_attach(struct nand_chip *chip)
5849{
5850 if (chip->controller->ops && chip->controller->ops->attach_chip)
5851 return chip->controller->ops->attach_chip(chip);
5852
5853 return 0;
5854}
5855
5856static void nand_detach(struct nand_chip *chip)
5857{
5858 if (chip->controller->ops && chip->controller->ops->detach_chip)
5859 chip->controller->ops->detach_chip(chip);
5860}
5861
David Woodhouse3b85c322006-09-25 17:06:53 +01005862/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005863 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005864 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02005865 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005866 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01005867 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005868 * This fills out all the uninitialized function pointers with the defaults.
5869 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005870 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005871 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005872int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005873 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01005874{
5875 int ret;
5876
Boris Brezillon800342d2018-08-04 22:59:23 +02005877 if (!maxchips)
5878 return -EINVAL;
5879
5880 ret = nand_scan_ident(chip, maxchips, ids);
5881 if (ret)
5882 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005883
5884 ret = nand_attach(chip);
5885 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005886 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02005887
Boris Brezillon00ad3782018-09-06 14:05:14 +02005888 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005889 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005890 goto detach_chip;
5891
5892 return 0;
5893
5894detach_chip:
5895 nand_detach(chip);
5896cleanup_ident:
5897 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005898
David Woodhouse3b85c322006-09-25 17:06:53 +01005899 return ret;
5900}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02005901EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01005902
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005904 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5905 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005906 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005907void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005909 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005910 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005911 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5912
Jesper Juhlfa671642005-11-07 01:01:27 -08005913 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005914 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005915 kfree(chip->data_buf);
5916 kfree(chip->ecc.code_buf);
5917 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07005918
5919 /* Free bad block descriptor memory */
5920 if (chip->badblock_pattern && chip->badblock_pattern->options
5921 & NAND_BBT_DYNAMICSTRUCT)
5922 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005923
5924 /* Free manufacturer priv data. */
5925 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02005926
5927 /* Free controller specific allocations after chip identification */
5928 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005929
5930 /* Free identification phase allocations */
5931 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932}
Miquel Raynal05b54c72018-07-19 01:05:46 +02005933
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005934EXPORT_SYMBOL_GPL(nand_cleanup);
5935
5936/**
5937 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5938 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02005939 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005940 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02005941void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005942{
Boris Brezillon59ac2762018-09-06 14:05:15 +02005943 mtd_device_unregister(nand_to_mtd(chip));
5944 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005945}
David Woodhousee0c7d762006-05-13 18:07:53 +01005946EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005947
David Woodhousee0c7d762006-05-13 18:07:53 +01005948MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005949MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5950MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005951MODULE_DESCRIPTION("Generic NAND flash driver code");