blob: dcdf0f373100a31a40c7d64a0609d54c8e35ccd4 [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>
Ingo Molnar38b8d202017-02-08 18:51:31 +010039#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/types.h>
41#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020042#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010044#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020047#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020049#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010050
Huang Shijie6a8214a2012-11-19 14:43:30 +080051static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020053static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
Boris Brezillon41b207a2016-02-03 19:06:15 +010056/* Define default oob placement schemes for large and small page devices */
57static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59{
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020068 if (mtd->oobsize == 16)
69 oobregion->length = 4;
70 else
71 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010072 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020073 if (mtd->oobsize == 8)
74 return -ERANGE;
75
Boris Brezillon41b207a2016-02-03 19:06:15 +010076 oobregion->offset = 6;
77 oobregion->length = ecc->total - 4;
78 }
79
80 return 0;
81}
82
83static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85{
86 if (section > 1)
87 return -ERANGE;
88
89 if (mtd->oobsize == 16) {
90 if (section)
91 return -ERANGE;
92
93 oobregion->length = 8;
94 oobregion->offset = 8;
95 } else {
96 oobregion->length = 2;
97 if (!section)
98 oobregion->offset = 3;
99 else
100 oobregion->offset = 6;
101 }
102
103 return 0;
104}
105
106const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107 .ecc = nand_ooblayout_ecc_sp,
108 .free = nand_ooblayout_free_sp,
109};
110EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
111
112static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113 struct mtd_oob_region *oobregion)
114{
115 struct nand_chip *chip = mtd_to_nand(mtd);
116 struct nand_ecc_ctrl *ecc = &chip->ecc;
117
Miquel Raynal882fd152017-08-26 17:19:15 +0200118 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100119 return -ERANGE;
120
121 oobregion->length = ecc->total;
122 oobregion->offset = mtd->oobsize - oobregion->length;
123
124 return 0;
125}
126
127static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128 struct mtd_oob_region *oobregion)
129{
130 struct nand_chip *chip = mtd_to_nand(mtd);
131 struct nand_ecc_ctrl *ecc = &chip->ecc;
132
133 if (section)
134 return -ERANGE;
135
136 oobregion->length = mtd->oobsize - ecc->total - 2;
137 oobregion->offset = 2;
138
139 return 0;
140}
141
142const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143 .ecc = nand_ooblayout_ecc_lp,
144 .free = nand_ooblayout_free_lp,
145};
146EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200147
Alexander Couzens6a623e02017-05-02 12:19:00 +0200148/*
149 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
150 * are placed at a fixed offset.
151 */
152static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153 struct mtd_oob_region *oobregion)
154{
155 struct nand_chip *chip = mtd_to_nand(mtd);
156 struct nand_ecc_ctrl *ecc = &chip->ecc;
157
158 if (section)
159 return -ERANGE;
160
161 switch (mtd->oobsize) {
162 case 64:
163 oobregion->offset = 40;
164 break;
165 case 128:
166 oobregion->offset = 80;
167 break;
168 default:
169 return -EINVAL;
170 }
171
172 oobregion->length = ecc->total;
173 if (oobregion->offset + oobregion->length > mtd->oobsize)
174 return -ERANGE;
175
176 return 0;
177}
178
179static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180 struct mtd_oob_region *oobregion)
181{
182 struct nand_chip *chip = mtd_to_nand(mtd);
183 struct nand_ecc_ctrl *ecc = &chip->ecc;
184 int ecc_offset = 0;
185
186 if (section < 0 || section > 1)
187 return -ERANGE;
188
189 switch (mtd->oobsize) {
190 case 64:
191 ecc_offset = 40;
192 break;
193 case 128:
194 ecc_offset = 80;
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 if (section == 0) {
201 oobregion->offset = 2;
202 oobregion->length = ecc_offset - 2;
203 } else {
204 oobregion->offset = ecc_offset + ecc->total;
205 oobregion->length = mtd->oobsize - oobregion->offset;
206 }
207
208 return 0;
209}
210
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100211static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200212 .ecc = nand_ooblayout_ecc_lp_hamming,
213 .free = nand_ooblayout_free_lp_hamming,
214};
215
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530216static int check_offs_len(struct mtd_info *mtd,
217 loff_t ofs, uint64_t len)
218{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100219 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530220 int ret = 0;
221
222 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
228 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300229 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700230 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530231 ret = -EINVAL;
232 }
233
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530234 return ret;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700239 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800241 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100243static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100245 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200247 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 spin_lock(&chip->controller->lock);
249 chip->controller->active = NULL;
250 chip->state = FL_READY;
251 wake_up(&chip->controller->wq);
252 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700257 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700259 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200261static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100263 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200264 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/**
Masanari Iida064a7692012-11-09 23:20:58 +0900268 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700269 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700271 * Default read function for 16bit buswidth with endianness conversion.
272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200274static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100276 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700282 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700284 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286static u16 nand_read_word(struct mtd_info *mtd)
287{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100288 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200289 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700294 * @mtd: MTD device structure
295 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * Default select function for 1 chip devices.
298 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200299static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100301 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200302
303 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200305 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309
310 default:
311 BUG();
312 }
313}
314
315/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100316 * nand_write_byte - [DEFAULT] write single byte to chip
317 * @mtd: MTD device structure
318 * @byte: value to write
319 *
320 * Default function to write a byte to I/O[7:0]
321 */
322static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
323{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100324 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100325
326 chip->write_buf(mtd, &byte, 1);
327}
328
329/**
330 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
331 * @mtd: MTD device structure
332 * @byte: value to write
333 *
334 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
335 */
336static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
337{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100338 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100339 uint16_t word = byte;
340
341 /*
342 * It's not entirely clear what should happen to I/O[15:8] when writing
343 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
344 *
345 * When the host supports a 16-bit bus width, only data is
346 * transferred at the 16-bit width. All address and command line
347 * transfers shall use only the lower 8-bits of the data bus. During
348 * command transfers, the host may place any value on the upper
349 * 8-bits of the data bus. During address transfers, the host shall
350 * set the upper 8-bits of the data bus to 00h.
351 *
Miquel Raynalb9587582018-03-19 14:47:19 +0100352 * One user of the write_byte callback is nand_set_features. The
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100353 * four parameters are specified to be written to I/O[7:0], but this is
354 * neither an address nor a command transfer. Let's assume a 0 on the
355 * upper I/O lines is OK.
356 */
357 chip->write_buf(mtd, (uint8_t *)&word, 2);
358}
359
360/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700362 * @mtd: MTD device structure
363 * @buf: data buffer
364 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700366 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200368static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100370 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Alexander Shiyan76413832013-04-13 09:32:13 +0400372 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000376 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700377 * @mtd: MTD device structure
378 * @buf: buffer to store date
379 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700381 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200383static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100385 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alexander Shiyan76413832013-04-13 09:32:13 +0400387 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700392 * @mtd: MTD device structure
393 * @buf: data buffer
394 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700396 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200398static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100400 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000402
Alexander Shiyan76413832013-04-13 09:32:13 +0400403 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000407 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700408 * @mtd: MTD device structure
409 * @buf: buffer to store date
410 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700412 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200414static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100416 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Alexander Shiyan76413832013-04-13 09:32:13 +0400419 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700424 * @mtd: MTD device structure
425 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000427 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530429static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Masahiro Yamadac120e752017-03-23 05:07:01 +0900431 int page, page_end, res;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100432 struct nand_chip *chip = mtd_to_nand(mtd);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900433 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Brian Norris5fb15492011-05-31 16:31:21 -0700435 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700436 ofs += mtd->erasesize - mtd->writesize;
437
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100438 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900439 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100440
Masahiro Yamadac120e752017-03-23 05:07:01 +0900441 for (; page < page_end; page++) {
442 res = chip->ecc.read_oob(mtd, chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530443 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900444 return res;
445
446 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000447
Brian Norriscdbec052012-01-13 18:11:48 -0800448 if (likely(chip->badblockbits == 8))
449 res = bad != 0xFF;
450 else
451 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900452 if (res)
453 return res;
454 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200455
Masahiro Yamadac120e752017-03-23 05:07:01 +0900456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700460 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700461 * @mtd: MTD device structure
462 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700464 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700465 * specific driver. It provides the details for writing a bad block marker to a
466 * block.
467 */
468static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
469{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100470 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700471 struct mtd_oob_ops ops;
472 uint8_t buf[2] = { 0, 0 };
473 int ret = 0, res, i = 0;
474
Brian Norris0ec56dc2015-02-28 02:02:30 -0800475 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700476 ops.oobbuf = buf;
477 ops.ooboffs = chip->badblockpos;
478 if (chip->options & NAND_BUSWIDTH_16) {
479 ops.ooboffs &= ~0x01;
480 ops.len = ops.ooblen = 2;
481 } else {
482 ops.len = ops.ooblen = 1;
483 }
484 ops.mode = MTD_OPS_PLACE_OOB;
485
486 /* Write to first/last page(s) if necessary */
487 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
488 ofs += mtd->erasesize - mtd->writesize;
489 do {
490 res = nand_do_write_oob(mtd, ofs, &ops);
491 if (!ret)
492 ret = res;
493
494 i++;
495 ofs += mtd->writesize;
496 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
497
498 return ret;
499}
500
501/**
502 * nand_block_markbad_lowlevel - mark a block bad
503 * @mtd: MTD device structure
504 * @ofs: offset from device start
505 *
506 * This function performs the generic NAND bad block marking steps (i.e., bad
507 * block table(s) and/or marker(s)). We only allow the hardware driver to
508 * specify how to write bad block markers to OOB (chip->block_markbad).
509 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700510 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300511 *
Brian Norrise2414f42012-02-06 13:44:00 -0800512 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700513 * (2) write bad block marker to OOB area of affected block (unless flag
514 * NAND_BBT_NO_OOB_BBM is present)
515 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300516 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700517 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800518 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700520static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100522 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700523 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000524
Brian Norrisb32843b2013-07-30 17:52:59 -0700525 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800526 struct erase_info einfo;
527
528 /* Attempt erase before marking OOB */
529 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800530 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300531 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800532 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800533
Brian Norrisb32843b2013-07-30 17:52:59 -0700534 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800535 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700536 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300537 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200538 }
Brian Norrise2414f42012-02-06 13:44:00 -0800539
Brian Norrisb32843b2013-07-30 17:52:59 -0700540 /* Mark block bad in BBT */
541 if (chip->bbt) {
542 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800543 if (!ret)
544 ret = res;
545 }
546
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200547 if (!ret)
548 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300549
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200550 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000553/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700555 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700557 * Check, if the device is write protected. The function expects, that the
558 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100560static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100562 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100563 u8 status;
564 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200565
Brian Norris8b6e50c2011-05-25 14:59:01 -0700566 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200567 if (chip->options & NAND_BROKEN_XD)
568 return 0;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100571 ret = nand_status_op(chip, &status);
572 if (ret)
573 return ret;
574
575 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800579 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700580 * @mtd: MTD device structure
581 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300582 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800583 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300584 */
585static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
586{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100587 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300588
589 if (!chip->bbt)
590 return 0;
591 /* Return info from the table */
592 return nand_isreserved_bbt(mtd, ofs);
593}
594
595/**
596 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
597 * @mtd: MTD device structure
598 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700599 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *
601 * Check, if the block is bad. Either by reading the bad block table or
602 * calling of the scan function.
603 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530604static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100606 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000607
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200608 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530609 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100612 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200615/**
616 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700617 * @mtd: MTD device structure
618 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200619 *
620 * Helper function for nand_wait_ready used when needing to wait in interrupt
621 * context.
622 */
623static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
624{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100625 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200626 int i;
627
628 /* Wait for the device to get ready */
629 for (i = 0; i < timeo; i++) {
630 if (chip->dev_ready(mtd))
631 break;
632 touch_softlockup_watchdog();
633 mdelay(1);
634 }
635}
636
Alex Smithb70af9b2015-10-06 14:52:07 +0100637/**
638 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
639 * @mtd: MTD device structure
640 *
641 * Wait for the ready pin after a command, and warn if a timeout occurs.
642 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100643void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000644{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100645 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100646 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000647
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200648 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100649 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200650
Brian Norris7854d3f2011-06-23 14:12:08 -0700651 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100652 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000653 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200654 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300655 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100656 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000657 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100658
Brian Norris9ebfdf52016-03-04 17:19:23 -0800659 if (!chip->dev_ready(mtd))
660 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000661}
David Woodhouse4b648b02006-09-25 17:05:24 +0100662EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200665 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
666 * @mtd: MTD device structure
667 * @timeo: Timeout in ms
668 *
669 * Wait for status ready (i.e. command done) or timeout.
670 */
671static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
672{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100673 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100674 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200675
676 timeo = jiffies + msecs_to_jiffies(timeo);
677 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100678 u8 status;
679
680 ret = nand_read_data_op(chip, &status, sizeof(status), true);
681 if (ret)
682 return;
683
684 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200685 break;
686 touch_softlockup_watchdog();
687 } while (time_before(jiffies, timeo));
688};
689
690/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100691 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
692 * @chip: NAND chip structure
693 * @timeout_ms: Timeout in ms
694 *
695 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
696 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
697 * returned.
698 *
699 * This helper is intended to be used when the controller does not have access
700 * to the NAND R/B pin.
701 *
702 * Be aware that calling this helper from an ->exec_op() implementation means
703 * ->exec_op() must be re-entrant.
704 *
705 * Return 0 if the NAND chip is ready, a negative error otherwise.
706 */
707int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
708{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200709 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100710 u8 status = 0;
711 int ret;
712
713 if (!chip->exec_op)
714 return -ENOTSUPP;
715
Boris Brezillon3057fce2018-05-04 21:24:31 +0200716 /* Wait tWB before polling the STATUS reg. */
717 timings = nand_get_sdr_timings(&chip->data_interface);
718 ndelay(PSEC_TO_NSEC(timings->tWB_max));
719
Miquel Raynal8878b122017-11-09 14:16:45 +0100720 ret = nand_status_op(chip, NULL);
721 if (ret)
722 return ret;
723
724 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
725 do {
726 ret = nand_read_data_op(chip, &status, sizeof(status), true);
727 if (ret)
728 break;
729
730 if (status & NAND_STATUS_READY)
731 break;
732
733 /*
734 * Typical lowest execution time for a tR on most NANDs is 10us,
735 * use this as polling delay before doing something smarter (ie.
736 * deriving a delay from the timeout value, timeout_ms/ratio).
737 */
738 udelay(10);
739 } while (time_before(jiffies, timeout_ms));
740
741 /*
742 * We have to exit READ_STATUS mode in order to read real data on the
743 * bus in case the WAITRDY instruction is preceding a DATA_IN
744 * instruction.
745 */
746 nand_exit_status_op(chip);
747
748 if (ret)
749 return ret;
750
751 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
752};
753EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
754
755/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700757 * @mtd: MTD device structure
758 * @command: the command to be sent
759 * @column: the column address for this command, -1 if none
760 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700762 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200763 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200765static void nand_command(struct mtd_info *mtd, unsigned int command,
766 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100768 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200769 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Brian Norris8b6e50c2011-05-25 14:59:01 -0700771 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (command == NAND_CMD_SEQIN) {
773 int readcmd;
774
Joern Engel28318772006-05-22 23:18:05 +0200775 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200777 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 readcmd = NAND_CMD_READOOB;
779 } else if (column < 256) {
780 /* First 256 bytes --> READ0 */
781 readcmd = NAND_CMD_READ0;
782 } else {
783 column -= 256;
784 readcmd = NAND_CMD_READ1;
785 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200786 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200787 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100789 if (command != NAND_CMD_NONE)
790 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Brian Norris8b6e50c2011-05-25 14:59:01 -0700792 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200793 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
794 /* Serially input address */
795 if (column != -1) {
796 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800797 if (chip->options & NAND_BUSWIDTH_16 &&
798 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200799 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200800 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200801 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200803 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200804 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200805 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200806 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900807 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200808 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200809 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200810 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000811
812 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700813 * Program and erase have their own busy handlers status and sequential
814 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100815 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000817
Miquel Raynaldf467892017-11-08 17:00:27 +0100818 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 case NAND_CMD_PAGEPROG:
820 case NAND_CMD_ERASE1:
821 case NAND_CMD_ERASE2:
822 case NAND_CMD_SEQIN:
823 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900824 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900825 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return;
827
828 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200829 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200831 udelay(chip->chip_delay);
832 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200833 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200834 chip->cmd_ctrl(mtd,
835 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200836 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
837 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return;
839
David Woodhousee0c7d762006-05-13 18:07:53 +0100840 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200841 case NAND_CMD_READ0:
842 /*
843 * READ0 is sometimes used to exit GET STATUS mode. When this
844 * is the case no address cycles are requested, and we can use
845 * this information to detect that we should not wait for the
846 * device to be ready.
847 */
848 if (column == -1 && page_addr == -1)
849 return;
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000852 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * If we don't have access to the busy pin, we apply the given
854 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100855 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200856 if (!chip->dev_ready) {
857 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700861 /*
862 * Apply this short delay always to ensure that we do wait tWB in
863 * any case on any machine.
864 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100865 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000866
867 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200870static void nand_ccs_delay(struct nand_chip *chip)
871{
872 /*
873 * The controller already takes care of waiting for tCCS when the RNDIN
874 * or RNDOUT command is sent, return directly.
875 */
876 if (!(chip->options & NAND_WAIT_TCCS))
877 return;
878
879 /*
880 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
881 * (which should be safe for all NANDs).
882 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100883 if (chip->setup_data_interface)
884 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200885 else
886 ndelay(500);
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/**
890 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700891 * @mtd: MTD device structure
892 * @command: the command to be sent
893 * @column: the column address for this command, -1 if none
894 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200896 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700897 * devices. We don't have the separate regions as we have in the small page
898 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200900static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
901 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100903 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /* Emulate NAND_CMD_READOOB */
906 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200907 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 command = NAND_CMD_READ0;
909 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000910
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200911 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100912 if (command != NAND_CMD_NONE)
913 chip->cmd_ctrl(mtd, command,
914 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200917 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /* Serially input address */
920 if (column != -1) {
921 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800922 if (chip->options & NAND_BUSWIDTH_16 &&
923 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200925 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200926 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200927
Brian Norrisf5b88de2016-10-03 09:49:35 -0700928 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200929 if (!nand_opcode_8bits(command))
930 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200933 chip->cmd_ctrl(mtd, page_addr, ctrl);
934 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200935 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900936 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200937 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200938 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200941 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000942
943 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700944 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100945 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000948
Miquel Raynaldf467892017-11-08 17:00:27 +0100949 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 case NAND_CMD_CACHEDPROG:
951 case NAND_CMD_PAGEPROG:
952 case NAND_CMD_ERASE1:
953 case NAND_CMD_ERASE2:
954 case NAND_CMD_SEQIN:
955 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900956 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900957 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000958 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200960 case NAND_CMD_RNDIN:
961 nand_ccs_delay(chip);
962 return;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200965 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200967 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200968 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
969 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
970 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
971 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200972 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
973 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return;
975
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200976 case NAND_CMD_RNDOUT:
977 /* No ready / busy check necessary */
978 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
979 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
980 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
981 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200982
983 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200984 return;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200987 /*
988 * READ0 is sometimes used to exit GET STATUS mode. When this
989 * is the case no address cycles are requested, and we can use
990 * this information to detect that READSTART should not be
991 * issued.
992 */
993 if (column == -1 && page_addr == -1)
994 return;
995
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200996 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
997 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
998 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
999 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001000
David Woodhousee0c7d762006-05-13 18:07:53 +01001001 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001003 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -07001005 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +01001006 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001007 if (!chip->dev_ready) {
1008 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
Thomas Gleixner3b887752005-02-22 21:56:49 +00001012
Brian Norris8b6e50c2011-05-25 14:59:01 -07001013 /*
1014 * Apply this short delay always to ensure that we do wait tWB in
1015 * any case on any machine.
1016 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001017 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +00001018
1019 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001023 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001024 * @chip: the nand chip descriptor
1025 * @mtd: MTD device structure
1026 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001027 *
1028 * Used when in panic, no locks are taken.
1029 */
1030static void panic_nand_get_device(struct nand_chip *chip,
1031 struct mtd_info *mtd, int new_state)
1032{
Brian Norris7854d3f2011-06-23 14:12:08 -07001033 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001034 chip->controller->active = chip;
1035 chip->state = new_state;
1036}
1037
1038/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001040 * @mtd: MTD device structure
1041 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 *
1043 * Get the device and lock it for exclusive access
1044 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001045static int
Huang Shijie6a8214a2012-11-19 14:43:30 +08001046nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001048 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001049 spinlock_t *lock = &chip->controller->lock;
1050 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +01001051 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001052retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001053 spin_lock(lock);
1054
vimal singhb8b3ee92009-07-09 20:41:22 +05301055 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001056 if (!chip->controller->active)
1057 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001058
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001059 if (chip->controller->active == chip && chip->state == FL_READY) {
1060 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001061 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01001062 return 0;
1063 }
1064 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001065 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1066 chip->state = FL_PM_SUSPENDED;
1067 spin_unlock(lock);
1068 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001069 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001070 }
1071 set_current_state(TASK_UNINTERRUPTIBLE);
1072 add_wait_queue(wq, &wait);
1073 spin_unlock(lock);
1074 schedule();
1075 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 goto retry;
1077}
1078
1079/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001080 * panic_nand_wait - [GENERIC] wait until the command is done
1081 * @mtd: MTD device structure
1082 * @chip: NAND chip structure
1083 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001084 *
1085 * Wait for command done. This is a helper function for nand_wait used when
1086 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001087 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001088 */
1089static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1090 unsigned long timeo)
1091{
1092 int i;
1093 for (i = 0; i < timeo; i++) {
1094 if (chip->dev_ready) {
1095 if (chip->dev_ready(mtd))
1096 break;
1097 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001098 int ret;
1099 u8 status;
1100
1101 ret = nand_read_data_op(chip, &status, sizeof(status),
1102 true);
1103 if (ret)
1104 return;
1105
1106 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001107 break;
1108 }
1109 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001110 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001111}
1112
1113/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001114 * nand_wait - [DEFAULT] wait until the command is done
1115 * @mtd: MTD device structure
1116 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001118 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001119 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001120static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122
Alex Smithb70af9b2015-10-06 14:52:07 +01001123 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001124 u8 status;
1125 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Brian Norris8b6e50c2011-05-25 14:59:01 -07001127 /*
1128 * Apply this short delay always to ensure that we do wait tWB in any
1129 * case on any machine.
1130 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001131 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Boris Brezillon97d90da2017-11-30 18:01:29 +01001133 ret = nand_status_op(chip, NULL);
1134 if (ret)
1135 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001137 if (in_interrupt() || oops_in_progress)
1138 panic_nand_wait(mtd, chip, timeo);
1139 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001140 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001141 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001142 if (chip->dev_ready) {
1143 if (chip->dev_ready(mtd))
1144 break;
1145 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001146 ret = nand_read_data_op(chip, &status,
1147 sizeof(status), true);
1148 if (ret)
1149 return ret;
1150
1151 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001152 break;
1153 }
1154 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001155 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001157
Boris Brezillon97d90da2017-11-30 18:01:29 +01001158 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1159 if (ret)
1160 return ret;
1161
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001162 /* This can happen if in case of timeout or buggy dev_ready */
1163 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return status;
1165}
1166
Miquel Raynal789157e2018-03-19 14:47:28 +01001167static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +01001168{
Miquel Raynal789157e2018-03-19 14:47:28 +01001169 return (chip->parameters.supports_set_get_features &&
1170 test_bit(addr, chip->parameters.get_feature_list));
1171}
1172
1173static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1174{
1175 return (chip->parameters.supports_set_get_features &&
1176 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +01001177}
1178
1179/**
1180 * nand_get_features - wrapper to perform a GET_FEATURE
1181 * @chip: NAND chip info structure
1182 * @addr: feature address
1183 * @subfeature_param: the subfeature parameters, a four bytes array
1184 *
1185 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1186 * operation cannot be handled.
1187 */
1188int nand_get_features(struct nand_chip *chip, int addr,
1189 u8 *subfeature_param)
1190{
1191 struct mtd_info *mtd = nand_to_mtd(chip);
1192
Miquel Raynal789157e2018-03-19 14:47:28 +01001193 if (!nand_supports_get_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001194 return -ENOTSUPP;
1195
1196 return chip->get_features(mtd, chip, addr, subfeature_param);
1197}
1198EXPORT_SYMBOL_GPL(nand_get_features);
1199
1200/**
1201 * nand_set_features - wrapper to perform a SET_FEATURE
1202 * @chip: NAND chip info structure
1203 * @addr: feature address
1204 * @subfeature_param: the subfeature parameters, a four bytes array
1205 *
1206 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1207 * operation cannot be handled.
1208 */
1209int nand_set_features(struct nand_chip *chip, int addr,
1210 u8 *subfeature_param)
1211{
1212 struct mtd_info *mtd = nand_to_mtd(chip);
1213
Miquel Raynal789157e2018-03-19 14:47:28 +01001214 if (!nand_supports_set_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001215 return -ENOTSUPP;
1216
1217 return chip->set_features(mtd, chip, addr, subfeature_param);
1218}
1219EXPORT_SYMBOL_GPL(nand_set_features);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001222 * nand_reset_data_interface - Reset data interface and timings
1223 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001224 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001225 *
1226 * Reset the Data interface and timings to ONFI mode 0.
1227 *
1228 * Returns 0 for success or negative error code otherwise.
1229 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001230static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001231{
1232 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001233 int ret;
1234
1235 if (!chip->setup_data_interface)
1236 return 0;
1237
1238 /*
1239 * The ONFI specification says:
1240 * "
1241 * To transition from NV-DDR or NV-DDR2 to the SDR data
1242 * interface, the host shall use the Reset (FFh) command
1243 * using SDR timing mode 0. A device in any timing mode is
1244 * required to recognize Reset (FFh) command issued in SDR
1245 * timing mode 0.
1246 * "
1247 *
1248 * Configure the data interface in SDR mode and set the
1249 * timings to timing mode 0.
1250 */
1251
Miquel Raynal17fa8042017-11-30 18:01:31 +01001252 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1253 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001254 if (ret)
1255 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1256
1257 return ret;
1258}
1259
1260/**
1261 * nand_setup_data_interface - Setup the best data interface and timings
1262 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001263 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001264 *
1265 * Find and configure the best data interface and NAND timings supported by
1266 * the chip and the driver.
1267 * First tries to retrieve supported timing modes from ONFI information,
1268 * and if the NAND chip does not support ONFI, relies on the
1269 * ->onfi_timing_mode_default specified in the nand_ids table.
1270 *
1271 * Returns 0 for success or negative error code otherwise.
1272 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001273static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001274{
1275 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal97baea12018-03-19 14:47:20 +01001276 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1277 chip->onfi_timing_mode_default,
1278 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001279 int ret;
1280
Miquel Raynal17fa8042017-11-30 18:01:31 +01001281 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001282 return 0;
1283
Miquel Raynal993447b2018-03-19 14:47:21 +01001284 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +01001285 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Miquel Raynal29714d62018-03-19 14:47:23 +01001286 chip->select_chip(mtd, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001287 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1288 tmode_param);
Miquel Raynal29714d62018-03-19 14:47:23 +01001289 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001290 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +01001291 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001292 }
1293
Miquel Raynal97baea12018-03-19 14:47:20 +01001294 /* Change the mode on the controller side */
Miquel Raynal17fa8042017-11-30 18:01:31 +01001295 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +01001296 if (ret)
1297 return ret;
1298
1299 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +01001300 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +01001301 return 0;
1302
1303 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
1304 chip->select_chip(mtd, chipnr);
1305 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1306 tmode_param);
1307 chip->select_chip(mtd, -1);
1308 if (ret)
1309 goto err_reset_chip;
1310
1311 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1312 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1313 chip->onfi_timing_mode_default);
1314 goto err_reset_chip;
1315 }
1316
1317 return 0;
1318
1319err_reset_chip:
1320 /*
1321 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1322 * timing mode.
1323 */
1324 nand_reset_data_interface(chip, chipnr);
1325 chip->select_chip(mtd, chipnr);
1326 nand_reset_op(chip);
1327 chip->select_chip(mtd, -1);
1328
Boris Brezillond8e725d2016-09-15 10:32:50 +02001329 return ret;
1330}
1331
1332/**
1333 * nand_init_data_interface - find the best data interface and timings
1334 * @chip: The NAND chip
1335 *
1336 * Find the best data interface and NAND timings supported by the chip
1337 * and the driver.
1338 * First tries to retrieve supported timing modes from ONFI information,
1339 * and if the NAND chip does not support ONFI, relies on the
1340 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1341 * function nand_chip->data_interface is initialized with the best timing mode
1342 * available.
1343 *
1344 * Returns 0 for success or negative error code otherwise.
1345 */
1346static int nand_init_data_interface(struct nand_chip *chip)
1347{
1348 struct mtd_info *mtd = nand_to_mtd(chip);
1349 int modes, mode, ret;
1350
1351 if (!chip->setup_data_interface)
1352 return 0;
1353
1354 /*
1355 * First try to identify the best timings from ONFI parameters and
1356 * if the NAND does not support ONFI, fallback to the default ONFI
1357 * timing mode.
1358 */
1359 modes = onfi_get_async_timing_mode(chip);
1360 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1361 if (!chip->onfi_timing_mode_default)
1362 return 0;
1363
1364 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1365 }
1366
Boris Brezillond8e725d2016-09-15 10:32:50 +02001367
1368 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001369 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001370 if (ret)
1371 continue;
1372
Miquel Raynald787b8b2017-12-22 18:12:41 +01001373 /*
1374 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1375 * controller supports the requested timings.
1376 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001377 ret = chip->setup_data_interface(mtd,
1378 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001379 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001380 if (!ret) {
1381 chip->onfi_timing_mode_default = mode;
1382 break;
1383 }
1384 }
1385
1386 return 0;
1387}
1388
Boris Brezillond8e725d2016-09-15 10:32:50 +02001389/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001390 * nand_fill_column_cycles - fill the column cycles of an address
1391 * @chip: The NAND chip
1392 * @addrs: Array of address cycles to fill
1393 * @offset_in_page: The offset in the page
1394 *
1395 * Fills the first or the first two bytes of the @addrs field depending
1396 * on the NAND bus width and the page size.
1397 *
1398 * Returns the number of cycles needed to encode the column, or a negative
1399 * error code in case one of the arguments is invalid.
1400 */
1401static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1402 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Miquel Raynal8878b122017-11-09 14:16:45 +01001404 struct mtd_info *mtd = nand_to_mtd(chip);
1405
1406 /* Make sure the offset is less than the actual page size. */
1407 if (offset_in_page > mtd->writesize + mtd->oobsize)
1408 return -EINVAL;
1409
1410 /*
1411 * On small page NANDs, there's a dedicated command to access the OOB
1412 * area, and the column address is relative to the start of the OOB
1413 * area, not the start of the page. Asjust the address accordingly.
1414 */
1415 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1416 offset_in_page -= mtd->writesize;
1417
1418 /*
1419 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1420 * wide, then it must be divided by 2.
1421 */
1422 if (chip->options & NAND_BUSWIDTH_16) {
1423 if (WARN_ON(offset_in_page % 2))
1424 return -EINVAL;
1425
1426 offset_in_page /= 2;
1427 }
1428
1429 addrs[0] = offset_in_page;
1430
1431 /*
1432 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1433 * need 2
1434 */
1435 if (mtd->writesize <= 512)
1436 return 1;
1437
1438 addrs[1] = offset_in_page >> 8;
1439
1440 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Miquel Raynal8878b122017-11-09 14:16:45 +01001443static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1444 unsigned int offset_in_page, void *buf,
1445 unsigned int len)
1446{
1447 struct mtd_info *mtd = nand_to_mtd(chip);
1448 const struct nand_sdr_timings *sdr =
1449 nand_get_sdr_timings(&chip->data_interface);
1450 u8 addrs[4];
1451 struct nand_op_instr instrs[] = {
1452 NAND_OP_CMD(NAND_CMD_READ0, 0),
1453 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1454 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1455 PSEC_TO_NSEC(sdr->tRR_min)),
1456 NAND_OP_DATA_IN(len, buf, 0),
1457 };
1458 struct nand_operation op = NAND_OPERATION(instrs);
1459 int ret;
1460
1461 /* Drop the DATA_IN instruction if len is set to 0. */
1462 if (!len)
1463 op.ninstrs--;
1464
1465 if (offset_in_page >= mtd->writesize)
1466 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1467 else if (offset_in_page >= 256 &&
1468 !(chip->options & NAND_BUSWIDTH_16))
1469 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1470
1471 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1472 if (ret < 0)
1473 return ret;
1474
1475 addrs[1] = page;
1476 addrs[2] = page >> 8;
1477
1478 if (chip->options & NAND_ROW_ADDR_3) {
1479 addrs[3] = page >> 16;
1480 instrs[1].ctx.addr.naddrs++;
1481 }
1482
1483 return nand_exec_op(chip, &op);
1484}
1485
1486static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1487 unsigned int offset_in_page, void *buf,
1488 unsigned int len)
1489{
1490 const struct nand_sdr_timings *sdr =
1491 nand_get_sdr_timings(&chip->data_interface);
1492 u8 addrs[5];
1493 struct nand_op_instr instrs[] = {
1494 NAND_OP_CMD(NAND_CMD_READ0, 0),
1495 NAND_OP_ADDR(4, addrs, 0),
1496 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1497 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1498 PSEC_TO_NSEC(sdr->tRR_min)),
1499 NAND_OP_DATA_IN(len, buf, 0),
1500 };
1501 struct nand_operation op = NAND_OPERATION(instrs);
1502 int ret;
1503
1504 /* Drop the DATA_IN instruction if len is set to 0. */
1505 if (!len)
1506 op.ninstrs--;
1507
1508 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1509 if (ret < 0)
1510 return ret;
1511
1512 addrs[2] = page;
1513 addrs[3] = page >> 8;
1514
1515 if (chip->options & NAND_ROW_ADDR_3) {
1516 addrs[4] = page >> 16;
1517 instrs[1].ctx.addr.naddrs++;
1518 }
1519
1520 return nand_exec_op(chip, &op);
1521}
1522
1523/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001524 * nand_read_page_op - Do a READ PAGE operation
1525 * @chip: The NAND chip
1526 * @page: page to read
1527 * @offset_in_page: offset within the page
1528 * @buf: buffer used to store the data
1529 * @len: length of the buffer
1530 *
1531 * This function issues a READ PAGE operation.
1532 * This function does not select/unselect the CS line.
1533 *
1534 * Returns 0 on success, a negative error code otherwise.
1535 */
1536int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1537 unsigned int offset_in_page, void *buf, unsigned int len)
1538{
1539 struct mtd_info *mtd = nand_to_mtd(chip);
1540
1541 if (len && !buf)
1542 return -EINVAL;
1543
1544 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1545 return -EINVAL;
1546
Miquel Raynal8878b122017-11-09 14:16:45 +01001547 if (chip->exec_op) {
1548 if (mtd->writesize > 512)
1549 return nand_lp_exec_read_page_op(chip, page,
1550 offset_in_page, buf,
1551 len);
1552
1553 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1554 buf, len);
1555 }
1556
Boris Brezillon97d90da2017-11-30 18:01:29 +01001557 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1558 if (len)
1559 chip->read_buf(mtd, buf, len);
1560
1561 return 0;
1562}
1563EXPORT_SYMBOL_GPL(nand_read_page_op);
1564
1565/**
1566 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1567 * @chip: The NAND chip
1568 * @page: parameter page to read
1569 * @buf: buffer used to store the data
1570 * @len: length of the buffer
1571 *
1572 * This function issues a READ PARAMETER PAGE operation.
1573 * This function does not select/unselect the CS line.
1574 *
1575 * Returns 0 on success, a negative error code otherwise.
1576 */
1577static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1578 unsigned int len)
1579{
1580 struct mtd_info *mtd = nand_to_mtd(chip);
1581 unsigned int i;
1582 u8 *p = buf;
1583
1584 if (len && !buf)
1585 return -EINVAL;
1586
Miquel Raynal8878b122017-11-09 14:16:45 +01001587 if (chip->exec_op) {
1588 const struct nand_sdr_timings *sdr =
1589 nand_get_sdr_timings(&chip->data_interface);
1590 struct nand_op_instr instrs[] = {
1591 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1592 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1593 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1594 PSEC_TO_NSEC(sdr->tRR_min)),
1595 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1596 };
1597 struct nand_operation op = NAND_OPERATION(instrs);
1598
1599 /* Drop the DATA_IN instruction if len is set to 0. */
1600 if (!len)
1601 op.ninstrs--;
1602
1603 return nand_exec_op(chip, &op);
1604 }
1605
Boris Brezillon97d90da2017-11-30 18:01:29 +01001606 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1607 for (i = 0; i < len; i++)
1608 p[i] = chip->read_byte(mtd);
1609
1610 return 0;
1611}
1612
1613/**
1614 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1615 * @chip: The NAND chip
1616 * @offset_in_page: offset within the page
1617 * @buf: buffer used to store the data
1618 * @len: length of the buffer
1619 * @force_8bit: force 8-bit bus access
1620 *
1621 * This function issues a CHANGE READ COLUMN operation.
1622 * This function does not select/unselect the CS line.
1623 *
1624 * Returns 0 on success, a negative error code otherwise.
1625 */
1626int nand_change_read_column_op(struct nand_chip *chip,
1627 unsigned int offset_in_page, void *buf,
1628 unsigned int len, bool force_8bit)
1629{
1630 struct mtd_info *mtd = nand_to_mtd(chip);
1631
1632 if (len && !buf)
1633 return -EINVAL;
1634
1635 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1636 return -EINVAL;
1637
Miquel Raynal8878b122017-11-09 14:16:45 +01001638 /* Small page NANDs do not support column change. */
1639 if (mtd->writesize <= 512)
1640 return -ENOTSUPP;
1641
1642 if (chip->exec_op) {
1643 const struct nand_sdr_timings *sdr =
1644 nand_get_sdr_timings(&chip->data_interface);
1645 u8 addrs[2] = {};
1646 struct nand_op_instr instrs[] = {
1647 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1648 NAND_OP_ADDR(2, addrs, 0),
1649 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1650 PSEC_TO_NSEC(sdr->tCCS_min)),
1651 NAND_OP_DATA_IN(len, buf, 0),
1652 };
1653 struct nand_operation op = NAND_OPERATION(instrs);
1654 int ret;
1655
1656 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1657 if (ret < 0)
1658 return ret;
1659
1660 /* Drop the DATA_IN instruction if len is set to 0. */
1661 if (!len)
1662 op.ninstrs--;
1663
1664 instrs[3].ctx.data.force_8bit = force_8bit;
1665
1666 return nand_exec_op(chip, &op);
1667 }
1668
Boris Brezillon97d90da2017-11-30 18:01:29 +01001669 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1670 if (len)
1671 chip->read_buf(mtd, buf, len);
1672
1673 return 0;
1674}
1675EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1676
1677/**
1678 * nand_read_oob_op - Do a READ OOB operation
1679 * @chip: The NAND chip
1680 * @page: page to read
1681 * @offset_in_oob: offset within the OOB area
1682 * @buf: buffer used to store the data
1683 * @len: length of the buffer
1684 *
1685 * This function issues a READ OOB operation.
1686 * This function does not select/unselect the CS line.
1687 *
1688 * Returns 0 on success, a negative error code otherwise.
1689 */
1690int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1691 unsigned int offset_in_oob, void *buf, unsigned int len)
1692{
1693 struct mtd_info *mtd = nand_to_mtd(chip);
1694
1695 if (len && !buf)
1696 return -EINVAL;
1697
1698 if (offset_in_oob + len > mtd->oobsize)
1699 return -EINVAL;
1700
Miquel Raynal8878b122017-11-09 14:16:45 +01001701 if (chip->exec_op)
1702 return nand_read_page_op(chip, page,
1703 mtd->writesize + offset_in_oob,
1704 buf, len);
1705
Boris Brezillon97d90da2017-11-30 18:01:29 +01001706 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1707 if (len)
1708 chip->read_buf(mtd, buf, len);
1709
1710 return 0;
1711}
1712EXPORT_SYMBOL_GPL(nand_read_oob_op);
1713
Miquel Raynal8878b122017-11-09 14:16:45 +01001714static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1715 unsigned int offset_in_page, const void *buf,
1716 unsigned int len, bool prog)
1717{
1718 struct mtd_info *mtd = nand_to_mtd(chip);
1719 const struct nand_sdr_timings *sdr =
1720 nand_get_sdr_timings(&chip->data_interface);
1721 u8 addrs[5] = {};
1722 struct nand_op_instr instrs[] = {
1723 /*
1724 * The first instruction will be dropped if we're dealing
1725 * with a large page NAND and adjusted if we're dealing
1726 * with a small page NAND and the page offset is > 255.
1727 */
1728 NAND_OP_CMD(NAND_CMD_READ0, 0),
1729 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1730 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1731 NAND_OP_DATA_OUT(len, buf, 0),
1732 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1733 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1734 };
1735 struct nand_operation op = NAND_OPERATION(instrs);
1736 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1737 int ret;
1738 u8 status;
1739
1740 if (naddrs < 0)
1741 return naddrs;
1742
1743 addrs[naddrs++] = page;
1744 addrs[naddrs++] = page >> 8;
1745 if (chip->options & NAND_ROW_ADDR_3)
1746 addrs[naddrs++] = page >> 16;
1747
1748 instrs[2].ctx.addr.naddrs = naddrs;
1749
1750 /* Drop the last two instructions if we're not programming the page. */
1751 if (!prog) {
1752 op.ninstrs -= 2;
1753 /* Also drop the DATA_OUT instruction if empty. */
1754 if (!len)
1755 op.ninstrs--;
1756 }
1757
1758 if (mtd->writesize <= 512) {
1759 /*
1760 * Small pages need some more tweaking: we have to adjust the
1761 * first instruction depending on the page offset we're trying
1762 * to access.
1763 */
1764 if (offset_in_page >= mtd->writesize)
1765 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1766 else if (offset_in_page >= 256 &&
1767 !(chip->options & NAND_BUSWIDTH_16))
1768 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1769 } else {
1770 /*
1771 * Drop the first command if we're dealing with a large page
1772 * NAND.
1773 */
1774 op.instrs++;
1775 op.ninstrs--;
1776 }
1777
1778 ret = nand_exec_op(chip, &op);
1779 if (!prog || ret)
1780 return ret;
1781
1782 ret = nand_status_op(chip, &status);
1783 if (ret)
1784 return ret;
1785
1786 return status;
1787}
1788
Boris Brezillon97d90da2017-11-30 18:01:29 +01001789/**
1790 * nand_prog_page_begin_op - starts a PROG PAGE operation
1791 * @chip: The NAND chip
1792 * @page: page to write
1793 * @offset_in_page: offset within the page
1794 * @buf: buffer containing the data to write to the page
1795 * @len: length of the buffer
1796 *
1797 * This function issues the first half of a PROG PAGE operation.
1798 * This function does not select/unselect the CS line.
1799 *
1800 * Returns 0 on success, a negative error code otherwise.
1801 */
1802int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1803 unsigned int offset_in_page, const void *buf,
1804 unsigned int len)
1805{
1806 struct mtd_info *mtd = nand_to_mtd(chip);
1807
1808 if (len && !buf)
1809 return -EINVAL;
1810
1811 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1812 return -EINVAL;
1813
Miquel Raynal8878b122017-11-09 14:16:45 +01001814 if (chip->exec_op)
1815 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1816 len, false);
1817
Boris Brezillon97d90da2017-11-30 18:01:29 +01001818 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1819
1820 if (buf)
1821 chip->write_buf(mtd, buf, len);
1822
1823 return 0;
1824}
1825EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1826
1827/**
1828 * nand_prog_page_end_op - ends a PROG PAGE operation
1829 * @chip: The NAND chip
1830 *
1831 * This function issues the second half of a PROG PAGE operation.
1832 * This function does not select/unselect the CS line.
1833 *
1834 * Returns 0 on success, a negative error code otherwise.
1835 */
1836int nand_prog_page_end_op(struct nand_chip *chip)
1837{
1838 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001839 int ret;
1840 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001841
Miquel Raynal8878b122017-11-09 14:16:45 +01001842 if (chip->exec_op) {
1843 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_PAGEPROG,
1847 PSEC_TO_NSEC(sdr->tWB_max)),
1848 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1849 };
1850 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001851
Miquel Raynal8878b122017-11-09 14:16:45 +01001852 ret = nand_exec_op(chip, &op);
1853 if (ret)
1854 return ret;
1855
1856 ret = nand_status_op(chip, &status);
1857 if (ret)
1858 return ret;
1859 } else {
1860 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1861 ret = chip->waitfunc(mtd, chip);
1862 if (ret < 0)
1863 return ret;
1864
1865 status = ret;
1866 }
1867
Boris Brezillon97d90da2017-11-30 18:01:29 +01001868 if (status & NAND_STATUS_FAIL)
1869 return -EIO;
1870
1871 return 0;
1872}
1873EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1874
1875/**
1876 * nand_prog_page_op - Do a full PROG PAGE operation
1877 * @chip: The NAND chip
1878 * @page: page to write
1879 * @offset_in_page: offset within the page
1880 * @buf: buffer containing the data to write to the page
1881 * @len: length of the buffer
1882 *
1883 * This function issues a full PROG PAGE operation.
1884 * This function does not select/unselect the CS line.
1885 *
1886 * Returns 0 on success, a negative error code otherwise.
1887 */
1888int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1889 unsigned int offset_in_page, const void *buf,
1890 unsigned int len)
1891{
1892 struct mtd_info *mtd = nand_to_mtd(chip);
1893 int status;
1894
1895 if (!len || !buf)
1896 return -EINVAL;
1897
1898 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1899 return -EINVAL;
1900
Miquel Raynal8878b122017-11-09 14:16:45 +01001901 if (chip->exec_op) {
1902 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1903 len, true);
1904 } else {
1905 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1906 chip->write_buf(mtd, buf, len);
1907 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1908 status = chip->waitfunc(mtd, chip);
1909 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001910
Boris Brezillon97d90da2017-11-30 18:01:29 +01001911 if (status & NAND_STATUS_FAIL)
1912 return -EIO;
1913
1914 return 0;
1915}
1916EXPORT_SYMBOL_GPL(nand_prog_page_op);
1917
1918/**
1919 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1920 * @chip: The NAND chip
1921 * @offset_in_page: offset within the page
1922 * @buf: buffer containing the data to send to the NAND
1923 * @len: length of the buffer
1924 * @force_8bit: force 8-bit bus access
1925 *
1926 * This function issues a CHANGE WRITE COLUMN operation.
1927 * This function does not select/unselect the CS line.
1928 *
1929 * Returns 0 on success, a negative error code otherwise.
1930 */
1931int nand_change_write_column_op(struct nand_chip *chip,
1932 unsigned int offset_in_page,
1933 const void *buf, unsigned int len,
1934 bool force_8bit)
1935{
1936 struct mtd_info *mtd = nand_to_mtd(chip);
1937
1938 if (len && !buf)
1939 return -EINVAL;
1940
1941 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1942 return -EINVAL;
1943
Miquel Raynal8878b122017-11-09 14:16:45 +01001944 /* Small page NANDs do not support column change. */
1945 if (mtd->writesize <= 512)
1946 return -ENOTSUPP;
1947
1948 if (chip->exec_op) {
1949 const struct nand_sdr_timings *sdr =
1950 nand_get_sdr_timings(&chip->data_interface);
1951 u8 addrs[2];
1952 struct nand_op_instr instrs[] = {
1953 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1954 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1955 NAND_OP_DATA_OUT(len, buf, 0),
1956 };
1957 struct nand_operation op = NAND_OPERATION(instrs);
1958 int ret;
1959
1960 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1961 if (ret < 0)
1962 return ret;
1963
1964 instrs[2].ctx.data.force_8bit = force_8bit;
1965
1966 /* Drop the DATA_OUT instruction if len is set to 0. */
1967 if (!len)
1968 op.ninstrs--;
1969
1970 return nand_exec_op(chip, &op);
1971 }
1972
Boris Brezillon97d90da2017-11-30 18:01:29 +01001973 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1974 if (len)
1975 chip->write_buf(mtd, buf, len);
1976
1977 return 0;
1978}
1979EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1980
1981/**
1982 * nand_readid_op - Do a READID operation
1983 * @chip: The NAND chip
1984 * @addr: address cycle to pass after the READID command
1985 * @buf: buffer used to store the ID
1986 * @len: length of the buffer
1987 *
1988 * This function sends a READID command and reads back the ID returned by the
1989 * NAND.
1990 * This function does not select/unselect the CS line.
1991 *
1992 * Returns 0 on success, a negative error code otherwise.
1993 */
1994int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1995 unsigned int len)
1996{
1997 struct mtd_info *mtd = nand_to_mtd(chip);
1998 unsigned int i;
1999 u8 *id = buf;
2000
2001 if (len && !buf)
2002 return -EINVAL;
2003
Miquel Raynal8878b122017-11-09 14:16:45 +01002004 if (chip->exec_op) {
2005 const struct nand_sdr_timings *sdr =
2006 nand_get_sdr_timings(&chip->data_interface);
2007 struct nand_op_instr instrs[] = {
2008 NAND_OP_CMD(NAND_CMD_READID, 0),
2009 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
2010 NAND_OP_8BIT_DATA_IN(len, buf, 0),
2011 };
2012 struct nand_operation op = NAND_OPERATION(instrs);
2013
2014 /* Drop the DATA_IN instruction if len is set to 0. */
2015 if (!len)
2016 op.ninstrs--;
2017
2018 return nand_exec_op(chip, &op);
2019 }
2020
Boris Brezillon97d90da2017-11-30 18:01:29 +01002021 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
2022
2023 for (i = 0; i < len; i++)
2024 id[i] = chip->read_byte(mtd);
2025
2026 return 0;
2027}
2028EXPORT_SYMBOL_GPL(nand_readid_op);
2029
2030/**
2031 * nand_status_op - Do a STATUS operation
2032 * @chip: The NAND chip
2033 * @status: out variable to store the NAND status
2034 *
2035 * This function sends a STATUS command and reads back the status returned by
2036 * the NAND.
2037 * This function does not select/unselect the CS line.
2038 *
2039 * Returns 0 on success, a negative error code otherwise.
2040 */
2041int nand_status_op(struct nand_chip *chip, u8 *status)
2042{
2043 struct mtd_info *mtd = nand_to_mtd(chip);
2044
Miquel Raynal8878b122017-11-09 14:16:45 +01002045 if (chip->exec_op) {
2046 const struct nand_sdr_timings *sdr =
2047 nand_get_sdr_timings(&chip->data_interface);
2048 struct nand_op_instr instrs[] = {
2049 NAND_OP_CMD(NAND_CMD_STATUS,
2050 PSEC_TO_NSEC(sdr->tADL_min)),
2051 NAND_OP_8BIT_DATA_IN(1, status, 0),
2052 };
2053 struct nand_operation op = NAND_OPERATION(instrs);
2054
2055 if (!status)
2056 op.ninstrs--;
2057
2058 return nand_exec_op(chip, &op);
2059 }
2060
Boris Brezillon97d90da2017-11-30 18:01:29 +01002061 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
2062 if (status)
2063 *status = chip->read_byte(mtd);
2064
2065 return 0;
2066}
2067EXPORT_SYMBOL_GPL(nand_status_op);
2068
2069/**
2070 * nand_exit_status_op - Exit a STATUS operation
2071 * @chip: The NAND chip
2072 *
2073 * This function sends a READ0 command to cancel the effect of the STATUS
2074 * command to avoid reading only the status until a new read command is sent.
2075 *
2076 * This function does not select/unselect the CS line.
2077 *
2078 * Returns 0 on success, a negative error code otherwise.
2079 */
2080int nand_exit_status_op(struct nand_chip *chip)
2081{
2082 struct mtd_info *mtd = nand_to_mtd(chip);
2083
Miquel Raynal8878b122017-11-09 14:16:45 +01002084 if (chip->exec_op) {
2085 struct nand_op_instr instrs[] = {
2086 NAND_OP_CMD(NAND_CMD_READ0, 0),
2087 };
2088 struct nand_operation op = NAND_OPERATION(instrs);
2089
2090 return nand_exec_op(chip, &op);
2091 }
2092
Boris Brezillon97d90da2017-11-30 18:01:29 +01002093 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2094
2095 return 0;
2096}
2097EXPORT_SYMBOL_GPL(nand_exit_status_op);
2098
2099/**
2100 * nand_erase_op - Do an erase operation
2101 * @chip: The NAND chip
2102 * @eraseblock: block to erase
2103 *
2104 * This function sends an ERASE command and waits for the NAND to be ready
2105 * before returning.
2106 * This function does not select/unselect the CS line.
2107 *
2108 * Returns 0 on success, a negative error code otherwise.
2109 */
2110int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2111{
2112 struct mtd_info *mtd = nand_to_mtd(chip);
2113 unsigned int page = eraseblock <<
2114 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002115 int ret;
2116 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002117
Miquel Raynal8878b122017-11-09 14:16:45 +01002118 if (chip->exec_op) {
2119 const struct nand_sdr_timings *sdr =
2120 nand_get_sdr_timings(&chip->data_interface);
2121 u8 addrs[3] = { page, page >> 8, page >> 16 };
2122 struct nand_op_instr instrs[] = {
2123 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2124 NAND_OP_ADDR(2, addrs, 0),
2125 NAND_OP_CMD(NAND_CMD_ERASE2,
2126 PSEC_TO_MSEC(sdr->tWB_max)),
2127 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2128 };
2129 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002130
Miquel Raynal8878b122017-11-09 14:16:45 +01002131 if (chip->options & NAND_ROW_ADDR_3)
2132 instrs[1].ctx.addr.naddrs++;
2133
2134 ret = nand_exec_op(chip, &op);
2135 if (ret)
2136 return ret;
2137
2138 ret = nand_status_op(chip, &status);
2139 if (ret)
2140 return ret;
2141 } else {
2142 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2143 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2144
2145 ret = chip->waitfunc(mtd, chip);
2146 if (ret < 0)
2147 return ret;
2148
2149 status = ret;
2150 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002151
2152 if (status & NAND_STATUS_FAIL)
2153 return -EIO;
2154
2155 return 0;
2156}
2157EXPORT_SYMBOL_GPL(nand_erase_op);
2158
2159/**
2160 * nand_set_features_op - Do a SET FEATURES operation
2161 * @chip: The NAND chip
2162 * @feature: feature id
2163 * @data: 4 bytes of data
2164 *
2165 * This function sends a SET FEATURES command and waits for the NAND to be
2166 * ready before returning.
2167 * This function does not select/unselect the CS line.
2168 *
2169 * Returns 0 on success, a negative error code otherwise.
2170 */
2171static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2172 const void *data)
2173{
2174 struct mtd_info *mtd = nand_to_mtd(chip);
2175 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002176 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002177
Miquel Raynal8878b122017-11-09 14:16:45 +01002178 if (chip->exec_op) {
2179 const struct nand_sdr_timings *sdr =
2180 nand_get_sdr_timings(&chip->data_interface);
2181 struct nand_op_instr instrs[] = {
2182 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2183 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2184 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2185 PSEC_TO_NSEC(sdr->tWB_max)),
2186 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2187 };
2188 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002189
Boris Brezillon782d1962018-05-11 14:44:07 +02002190 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01002191 }
2192
Boris Brezillon782d1962018-05-11 14:44:07 +02002193 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
2194 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2195 chip->write_byte(mtd, params[i]);
2196
2197 ret = chip->waitfunc(mtd, chip);
2198 if (ret < 0)
2199 return ret;
2200
2201 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002202 return -EIO;
2203
2204 return 0;
2205}
2206
2207/**
2208 * nand_get_features_op - Do a GET FEATURES operation
2209 * @chip: The NAND chip
2210 * @feature: feature id
2211 * @data: 4 bytes of data
2212 *
2213 * This function sends a GET FEATURES command and waits for the NAND to be
2214 * ready before returning.
2215 * This function does not select/unselect the CS line.
2216 *
2217 * Returns 0 on success, a negative error code otherwise.
2218 */
2219static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2220 void *data)
2221{
2222 struct mtd_info *mtd = nand_to_mtd(chip);
2223 u8 *params = data;
2224 int i;
2225
Miquel Raynal8878b122017-11-09 14:16:45 +01002226 if (chip->exec_op) {
2227 const struct nand_sdr_timings *sdr =
2228 nand_get_sdr_timings(&chip->data_interface);
2229 struct nand_op_instr instrs[] = {
2230 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2231 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2232 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2233 PSEC_TO_NSEC(sdr->tRR_min)),
2234 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2235 data, 0),
2236 };
2237 struct nand_operation op = NAND_OPERATION(instrs);
2238
2239 return nand_exec_op(chip, &op);
2240 }
2241
Boris Brezillon97d90da2017-11-30 18:01:29 +01002242 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2243 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2244 params[i] = chip->read_byte(mtd);
2245
2246 return 0;
2247}
2248
2249/**
2250 * nand_reset_op - Do a reset operation
2251 * @chip: The NAND chip
2252 *
2253 * This function sends a RESET command and waits for the NAND to be ready
2254 * before returning.
2255 * This function does not select/unselect the CS line.
2256 *
2257 * Returns 0 on success, a negative error code otherwise.
2258 */
2259int nand_reset_op(struct nand_chip *chip)
2260{
2261 struct mtd_info *mtd = nand_to_mtd(chip);
2262
Miquel Raynal8878b122017-11-09 14:16:45 +01002263 if (chip->exec_op) {
2264 const struct nand_sdr_timings *sdr =
2265 nand_get_sdr_timings(&chip->data_interface);
2266 struct nand_op_instr instrs[] = {
2267 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2268 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2269 };
2270 struct nand_operation op = NAND_OPERATION(instrs);
2271
2272 return nand_exec_op(chip, &op);
2273 }
2274
Boris Brezillon97d90da2017-11-30 18:01:29 +01002275 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2276
2277 return 0;
2278}
2279EXPORT_SYMBOL_GPL(nand_reset_op);
2280
2281/**
2282 * nand_read_data_op - Read data from the NAND
2283 * @chip: The NAND chip
2284 * @buf: buffer used to store the data
2285 * @len: length of the buffer
2286 * @force_8bit: force 8-bit bus access
2287 *
2288 * This function does a raw data read on the bus. Usually used after launching
2289 * another NAND operation like nand_read_page_op().
2290 * This function does not select/unselect the CS line.
2291 *
2292 * Returns 0 on success, a negative error code otherwise.
2293 */
2294int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2295 bool force_8bit)
2296{
2297 struct mtd_info *mtd = nand_to_mtd(chip);
2298
2299 if (!len || !buf)
2300 return -EINVAL;
2301
Miquel Raynal8878b122017-11-09 14:16:45 +01002302 if (chip->exec_op) {
2303 struct nand_op_instr instrs[] = {
2304 NAND_OP_DATA_IN(len, buf, 0),
2305 };
2306 struct nand_operation op = NAND_OPERATION(instrs);
2307
2308 instrs[0].ctx.data.force_8bit = force_8bit;
2309
2310 return nand_exec_op(chip, &op);
2311 }
2312
Boris Brezillon97d90da2017-11-30 18:01:29 +01002313 if (force_8bit) {
2314 u8 *p = buf;
2315 unsigned int i;
2316
2317 for (i = 0; i < len; i++)
2318 p[i] = chip->read_byte(mtd);
2319 } else {
2320 chip->read_buf(mtd, buf, len);
2321 }
2322
2323 return 0;
2324}
2325EXPORT_SYMBOL_GPL(nand_read_data_op);
2326
2327/**
2328 * nand_write_data_op - Write data from the NAND
2329 * @chip: The NAND chip
2330 * @buf: buffer containing the data to send on the bus
2331 * @len: length of the buffer
2332 * @force_8bit: force 8-bit bus access
2333 *
2334 * This function does a raw data write on the bus. Usually used after launching
2335 * another NAND operation like nand_write_page_begin_op().
2336 * This function does not select/unselect the CS line.
2337 *
2338 * Returns 0 on success, a negative error code otherwise.
2339 */
2340int nand_write_data_op(struct nand_chip *chip, const void *buf,
2341 unsigned int len, bool force_8bit)
2342{
2343 struct mtd_info *mtd = nand_to_mtd(chip);
2344
2345 if (!len || !buf)
2346 return -EINVAL;
2347
Miquel Raynal8878b122017-11-09 14:16:45 +01002348 if (chip->exec_op) {
2349 struct nand_op_instr instrs[] = {
2350 NAND_OP_DATA_OUT(len, buf, 0),
2351 };
2352 struct nand_operation op = NAND_OPERATION(instrs);
2353
2354 instrs[0].ctx.data.force_8bit = force_8bit;
2355
2356 return nand_exec_op(chip, &op);
2357 }
2358
Boris Brezillon97d90da2017-11-30 18:01:29 +01002359 if (force_8bit) {
2360 const u8 *p = buf;
2361 unsigned int i;
2362
2363 for (i = 0; i < len; i++)
2364 chip->write_byte(mtd, p[i]);
2365 } else {
2366 chip->write_buf(mtd, buf, len);
2367 }
2368
2369 return 0;
2370}
2371EXPORT_SYMBOL_GPL(nand_write_data_op);
2372
2373/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002374 * struct nand_op_parser_ctx - Context used by the parser
2375 * @instrs: array of all the instructions that must be addressed
2376 * @ninstrs: length of the @instrs array
2377 * @subop: Sub-operation to be passed to the NAND controller
2378 *
2379 * This structure is used by the core to split NAND operations into
2380 * sub-operations that can be handled by the NAND controller.
2381 */
2382struct nand_op_parser_ctx {
2383 const struct nand_op_instr *instrs;
2384 unsigned int ninstrs;
2385 struct nand_subop subop;
2386};
2387
2388/**
2389 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2390 * @pat: the parser pattern element that matches @instr
2391 * @instr: pointer to the instruction to check
2392 * @start_offset: this is an in/out parameter. If @instr has already been
2393 * split, then @start_offset is the offset from which to start
2394 * (either an address cycle or an offset in the data buffer).
2395 * Conversely, if the function returns true (ie. instr must be
2396 * split), this parameter is updated to point to the first
2397 * data/address cycle that has not been taken care of.
2398 *
2399 * Some NAND controllers are limited and cannot send X address cycles with a
2400 * unique operation, or cannot read/write more than Y bytes at the same time.
2401 * In this case, split the instruction that does not fit in a single
2402 * controller-operation into two or more chunks.
2403 *
2404 * Returns true if the instruction must be split, false otherwise.
2405 * The @start_offset parameter is also updated to the offset at which the next
2406 * bundle of instruction must start (if an address or a data instruction).
2407 */
2408static bool
2409nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2410 const struct nand_op_instr *instr,
2411 unsigned int *start_offset)
2412{
2413 switch (pat->type) {
2414 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002415 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002416 break;
2417
2418 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002419 pat->ctx.addr.maxcycles) {
2420 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002421 return true;
2422 }
2423 break;
2424
2425 case NAND_OP_DATA_IN_INSTR:
2426 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002427 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002428 break;
2429
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002430 if (instr->ctx.data.len - *start_offset >
2431 pat->ctx.data.maxlen) {
2432 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002433 return true;
2434 }
2435 break;
2436
2437 default:
2438 break;
2439 }
2440
2441 return false;
2442}
2443
2444/**
2445 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2446 * remaining in the parser context
2447 * @pat: the pattern to test
2448 * @ctx: the parser context structure to match with the pattern @pat
2449 *
2450 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2451 * Returns true if this is the case, false ortherwise. When true is returned,
2452 * @ctx->subop is updated with the set of instructions to be passed to the
2453 * controller driver.
2454 */
2455static bool
2456nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2457 struct nand_op_parser_ctx *ctx)
2458{
2459 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2460 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2461 const struct nand_op_instr *instr = ctx->subop.instrs;
2462 unsigned int i, ninstrs;
2463
2464 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2465 /*
2466 * The pattern instruction does not match the operation
2467 * instruction. If the instruction is marked optional in the
2468 * pattern definition, we skip the pattern element and continue
2469 * to the next one. If the element is mandatory, there's no
2470 * match and we can return false directly.
2471 */
2472 if (instr->type != pat->elems[i].type) {
2473 if (!pat->elems[i].optional)
2474 return false;
2475
2476 continue;
2477 }
2478
2479 /*
2480 * Now check the pattern element constraints. If the pattern is
2481 * not able to handle the whole instruction in a single step,
2482 * we have to split it.
2483 * The last_instr_end_off value comes back updated to point to
2484 * the position where we have to split the instruction (the
2485 * start of the next subop chunk).
2486 */
2487 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2488 &instr_offset)) {
2489 ninstrs++;
2490 i++;
2491 break;
2492 }
2493
2494 instr++;
2495 ninstrs++;
2496 instr_offset = 0;
2497 }
2498
2499 /*
2500 * This can happen if all instructions of a pattern are optional.
2501 * Still, if there's not at least one instruction handled by this
2502 * pattern, this is not a match, and we should try the next one (if
2503 * any).
2504 */
2505 if (!ninstrs)
2506 return false;
2507
2508 /*
2509 * We had a match on the pattern head, but the pattern may be longer
2510 * than the instructions we're asked to execute. We need to make sure
2511 * there's no mandatory elements in the pattern tail.
2512 */
2513 for (; i < pat->nelems; i++) {
2514 if (!pat->elems[i].optional)
2515 return false;
2516 }
2517
2518 /*
2519 * We have a match: update the subop structure accordingly and return
2520 * true.
2521 */
2522 ctx->subop.ninstrs = ninstrs;
2523 ctx->subop.last_instr_end_off = instr_offset;
2524
2525 return true;
2526}
2527
2528#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2529static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2530{
2531 const struct nand_op_instr *instr;
2532 char *prefix = " ";
2533 unsigned int i;
2534
2535 pr_debug("executing subop:\n");
2536
2537 for (i = 0; i < ctx->ninstrs; i++) {
2538 instr = &ctx->instrs[i];
2539
2540 if (instr == &ctx->subop.instrs[0])
2541 prefix = " ->";
2542
2543 switch (instr->type) {
2544 case NAND_OP_CMD_INSTR:
2545 pr_debug("%sCMD [0x%02x]\n", prefix,
2546 instr->ctx.cmd.opcode);
2547 break;
2548 case NAND_OP_ADDR_INSTR:
2549 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2550 instr->ctx.addr.naddrs,
2551 instr->ctx.addr.naddrs < 64 ?
2552 instr->ctx.addr.naddrs : 64,
2553 instr->ctx.addr.addrs);
2554 break;
2555 case NAND_OP_DATA_IN_INSTR:
2556 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2557 instr->ctx.data.len,
2558 instr->ctx.data.force_8bit ?
2559 ", force 8-bit" : "");
2560 break;
2561 case NAND_OP_DATA_OUT_INSTR:
2562 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2563 instr->ctx.data.len,
2564 instr->ctx.data.force_8bit ?
2565 ", force 8-bit" : "");
2566 break;
2567 case NAND_OP_WAITRDY_INSTR:
2568 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2569 instr->ctx.waitrdy.timeout_ms);
2570 break;
2571 }
2572
2573 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2574 prefix = " ";
2575 }
2576}
2577#else
2578static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2579{
2580 /* NOP */
2581}
2582#endif
2583
2584/**
2585 * nand_op_parser_exec_op - exec_op parser
2586 * @chip: the NAND chip
2587 * @parser: patterns description provided by the controller driver
2588 * @op: the NAND operation to address
2589 * @check_only: when true, the function only checks if @op can be handled but
2590 * does not execute the operation
2591 *
2592 * Helper function designed to ease integration of NAND controller drivers that
2593 * only support a limited set of instruction sequences. The supported sequences
2594 * are described in @parser, and the framework takes care of splitting @op into
2595 * multiple sub-operations (if required) and pass them back to the ->exec()
2596 * callback of the matching pattern if @check_only is set to false.
2597 *
2598 * NAND controller drivers should call this function from their own ->exec_op()
2599 * implementation.
2600 *
2601 * Returns 0 on success, a negative error code otherwise. A failure can be
2602 * caused by an unsupported operation (none of the supported patterns is able
2603 * to handle the requested operation), or an error returned by one of the
2604 * matching pattern->exec() hook.
2605 */
2606int nand_op_parser_exec_op(struct nand_chip *chip,
2607 const struct nand_op_parser *parser,
2608 const struct nand_operation *op, bool check_only)
2609{
2610 struct nand_op_parser_ctx ctx = {
2611 .subop.instrs = op->instrs,
2612 .instrs = op->instrs,
2613 .ninstrs = op->ninstrs,
2614 };
2615 unsigned int i;
2616
2617 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2618 int ret;
2619
2620 for (i = 0; i < parser->npatterns; i++) {
2621 const struct nand_op_parser_pattern *pattern;
2622
2623 pattern = &parser->patterns[i];
2624 if (!nand_op_parser_match_pat(pattern, &ctx))
2625 continue;
2626
2627 nand_op_parser_trace(&ctx);
2628
2629 if (check_only)
2630 break;
2631
2632 ret = pattern->exec(chip, &ctx.subop);
2633 if (ret)
2634 return ret;
2635
2636 break;
2637 }
2638
2639 if (i == parser->npatterns) {
2640 pr_debug("->exec_op() parser: pattern not found!\n");
2641 return -ENOTSUPP;
2642 }
2643
2644 /*
2645 * Update the context structure by pointing to the start of the
2646 * next subop.
2647 */
2648 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2649 if (ctx.subop.last_instr_end_off)
2650 ctx.subop.instrs -= 1;
2651
2652 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2653 }
2654
2655 return 0;
2656}
2657EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2658
2659static bool nand_instr_is_data(const struct nand_op_instr *instr)
2660{
2661 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2662 instr->type == NAND_OP_DATA_OUT_INSTR);
2663}
2664
2665static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2666 unsigned int instr_idx)
2667{
2668 return subop && instr_idx < subop->ninstrs;
2669}
2670
Miquel Raynal760c4352018-07-19 00:09:12 +02002671static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2672 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002673{
2674 if (instr_idx)
2675 return 0;
2676
2677 return subop->first_instr_start_off;
2678}
2679
2680/**
2681 * nand_subop_get_addr_start_off - Get the start offset in an address array
2682 * @subop: The entire sub-operation
2683 * @instr_idx: Index of the instruction inside the sub-operation
2684 *
2685 * During driver development, one could be tempted to directly use the
2686 * ->addr.addrs field of address instructions. This is wrong as address
2687 * instructions might be split.
2688 *
2689 * Given an address instruction, returns the offset of the first cycle to issue.
2690 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002691unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2692 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002693{
Miquel Raynal760c4352018-07-19 00:09:12 +02002694 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2695 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2696 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002697
2698 return nand_subop_get_start_off(subop, instr_idx);
2699}
2700EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2701
2702/**
2703 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2704 * @subop: The entire sub-operation
2705 * @instr_idx: Index of the instruction inside the sub-operation
2706 *
2707 * During driver development, one could be tempted to directly use the
2708 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2709 * might be split.
2710 *
2711 * Given an address instruction, returns the number of address cycle to issue.
2712 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002713unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2714 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002715{
2716 int start_off, end_off;
2717
Miquel Raynal760c4352018-07-19 00:09:12 +02002718 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2719 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2720 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002721
2722 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2723
2724 if (instr_idx == subop->ninstrs - 1 &&
2725 subop->last_instr_end_off)
2726 end_off = subop->last_instr_end_off;
2727 else
2728 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2729
2730 return end_off - start_off;
2731}
2732EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2733
2734/**
2735 * nand_subop_get_data_start_off - Get the start offset in a data array
2736 * @subop: The entire sub-operation
2737 * @instr_idx: Index of the instruction inside the sub-operation
2738 *
2739 * During driver development, one could be tempted to directly use the
2740 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2741 * instructions might be split.
2742 *
2743 * Given a data instruction, returns the offset to start from.
2744 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002745unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2746 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002747{
Miquel Raynal760c4352018-07-19 00:09:12 +02002748 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2749 !nand_instr_is_data(&subop->instrs[instr_idx])))
2750 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002751
2752 return nand_subop_get_start_off(subop, instr_idx);
2753}
2754EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2755
2756/**
2757 * nand_subop_get_data_len - Get the number of bytes to retrieve
2758 * @subop: The entire sub-operation
2759 * @instr_idx: Index of the instruction inside the sub-operation
2760 *
2761 * During driver development, one could be tempted to directly use the
2762 * ->data->len field of a data instruction. This is wrong as data instructions
2763 * might be split.
2764 *
2765 * Returns the length of the chunk of data to send/receive.
2766 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002767unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2768 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002769{
2770 int start_off = 0, end_off;
2771
Miquel Raynal760c4352018-07-19 00:09:12 +02002772 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2773 !nand_instr_is_data(&subop->instrs[instr_idx])))
2774 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002775
2776 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2777
2778 if (instr_idx == subop->ninstrs - 1 &&
2779 subop->last_instr_end_off)
2780 end_off = subop->last_instr_end_off;
2781 else
2782 end_off = subop->instrs[instr_idx].ctx.data.len;
2783
2784 return end_off - start_off;
2785}
2786EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002789 * nand_reset - Reset and initialize a NAND device
2790 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002791 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002792 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002793 * Save the timings data structure, then apply SDR timings mode 0 (see
2794 * nand_reset_data_interface for details), do the reset operation, and
2795 * apply back the previous timings.
2796 *
2797 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002798 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002799int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002800{
2801 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal17fa8042017-11-30 18:01:31 +01002802 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002803 int ret;
2804
Boris Brezillon104e4422017-03-16 09:35:58 +01002805 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002806 if (ret)
2807 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002808
Boris Brezillon73f907f2016-10-24 16:46:20 +02002809 /*
2810 * The CS line has to be released before we can apply the new NAND
2811 * interface settings, hence this weird ->select_chip() dance.
2812 */
2813 chip->select_chip(mtd, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002814 ret = nand_reset_op(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02002815 chip->select_chip(mtd, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002816 if (ret)
2817 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002818
Miquel Raynal107b7d62018-03-19 14:47:25 +01002819 /*
2820 * A nand_reset_data_interface() put both the NAND chip and the NAND
2821 * controller in timings mode 0. If the default mode for this chip is
2822 * also 0, no need to proceed to the change again. Plus, at probe time,
2823 * nand_setup_data_interface() uses ->set/get_features() which would
2824 * fail anyway as the parameter page is not available yet.
2825 */
2826 if (!chip->onfi_timing_mode_default)
2827 return 0;
2828
Miquel Raynal17fa8042017-11-30 18:01:31 +01002829 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002830 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002831 if (ret)
2832 return ret;
2833
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002834 return 0;
2835}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002836EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002837
2838/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002839 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2840 * @buf: buffer to test
2841 * @len: buffer length
2842 * @bitflips_threshold: maximum number of bitflips
2843 *
2844 * Check if a buffer contains only 0xff, which means the underlying region
2845 * has been erased and is ready to be programmed.
2846 * The bitflips_threshold specify the maximum number of bitflips before
2847 * considering the region is not erased.
2848 * Note: The logic of this function has been extracted from the memweight
2849 * implementation, except that nand_check_erased_buf function exit before
2850 * testing the whole buffer if the number of bitflips exceed the
2851 * bitflips_threshold value.
2852 *
2853 * Returns a positive number of bitflips less than or equal to
2854 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2855 * threshold.
2856 */
2857static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2858{
2859 const unsigned char *bitmap = buf;
2860 int bitflips = 0;
2861 int weight;
2862
2863 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2864 len--, bitmap++) {
2865 weight = hweight8(*bitmap);
2866 bitflips += BITS_PER_BYTE - weight;
2867 if (unlikely(bitflips > bitflips_threshold))
2868 return -EBADMSG;
2869 }
2870
2871 for (; len >= sizeof(long);
2872 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002873 unsigned long d = *((unsigned long *)bitmap);
2874 if (d == ~0UL)
2875 continue;
2876 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002877 bitflips += BITS_PER_LONG - weight;
2878 if (unlikely(bitflips > bitflips_threshold))
2879 return -EBADMSG;
2880 }
2881
2882 for (; len > 0; len--, bitmap++) {
2883 weight = hweight8(*bitmap);
2884 bitflips += BITS_PER_BYTE - weight;
2885 if (unlikely(bitflips > bitflips_threshold))
2886 return -EBADMSG;
2887 }
2888
2889 return bitflips;
2890}
2891
2892/**
2893 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2894 * 0xff data
2895 * @data: data buffer to test
2896 * @datalen: data length
2897 * @ecc: ECC buffer
2898 * @ecclen: ECC length
2899 * @extraoob: extra OOB buffer
2900 * @extraooblen: extra OOB length
2901 * @bitflips_threshold: maximum number of bitflips
2902 *
2903 * Check if a data buffer and its associated ECC and OOB data contains only
2904 * 0xff pattern, which means the underlying region has been erased and is
2905 * ready to be programmed.
2906 * The bitflips_threshold specify the maximum number of bitflips before
2907 * considering the region as not erased.
2908 *
2909 * Note:
2910 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2911 * different from the NAND page size. When fixing bitflips, ECC engines will
2912 * report the number of errors per chunk, and the NAND core infrastructure
2913 * expect you to return the maximum number of bitflips for the whole page.
2914 * This is why you should always use this function on a single chunk and
2915 * not on the whole page. After checking each chunk you should update your
2916 * max_bitflips value accordingly.
2917 * 2/ When checking for bitflips in erased pages you should not only check
2918 * the payload data but also their associated ECC data, because a user might
2919 * have programmed almost all bits to 1 but a few. In this case, we
2920 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2921 * this case.
2922 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2923 * data are protected by the ECC engine.
2924 * It could also be used if you support subpages and want to attach some
2925 * extra OOB data to an ECC chunk.
2926 *
2927 * Returns a positive number of bitflips less than or equal to
2928 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2929 * threshold. In case of success, the passed buffers are filled with 0xff.
2930 */
2931int nand_check_erased_ecc_chunk(void *data, int datalen,
2932 void *ecc, int ecclen,
2933 void *extraoob, int extraooblen,
2934 int bitflips_threshold)
2935{
2936 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2937
2938 data_bitflips = nand_check_erased_buf(data, datalen,
2939 bitflips_threshold);
2940 if (data_bitflips < 0)
2941 return data_bitflips;
2942
2943 bitflips_threshold -= data_bitflips;
2944
2945 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2946 if (ecc_bitflips < 0)
2947 return ecc_bitflips;
2948
2949 bitflips_threshold -= ecc_bitflips;
2950
2951 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2952 bitflips_threshold);
2953 if (extraoob_bitflips < 0)
2954 return extraoob_bitflips;
2955
2956 if (data_bitflips)
2957 memset(data, 0xff, datalen);
2958
2959 if (ecc_bitflips)
2960 memset(ecc, 0xff, ecclen);
2961
2962 if (extraoob_bitflips)
2963 memset(extraoob, 0xff, extraooblen);
2964
2965 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2966}
2967EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2968
2969/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002970 * nand_read_page_raw_notsupp - dummy read raw page function
2971 * @mtd: mtd info structure
2972 * @chip: nand chip info structure
2973 * @buf: buffer to store read data
2974 * @oob_required: caller requires OOB data read to chip->oob_poi
2975 * @page: page number to read
2976 *
2977 * Returns -ENOTSUPP unconditionally.
2978 */
2979int nand_read_page_raw_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
2980 u8 *buf, int oob_required, int page)
2981{
2982 return -ENOTSUPP;
2983}
2984EXPORT_SYMBOL(nand_read_page_raw_notsupp);
2985
2986/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002987 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002988 * @mtd: mtd info structure
2989 * @chip: nand chip info structure
2990 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002991 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002992 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002993 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002994 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002995 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002996int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2997 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002998{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002999 int ret;
3000
Boris Brezillon25f815f2017-11-30 18:01:30 +01003001 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003002 if (ret)
3003 return ret;
3004
3005 if (oob_required) {
3006 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
3007 false);
3008 if (ret)
3009 return ret;
3010 }
3011
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003012 return 0;
3013}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003014EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003015
3016/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003017 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07003018 * @mtd: mtd info structure
3019 * @chip: nand chip info structure
3020 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003021 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003022 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003023 *
3024 * We need a special oob layout and handling even when OOB isn't used.
3025 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003026static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003027 struct nand_chip *chip, uint8_t *buf,
3028 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003029{
3030 int eccsize = chip->ecc.size;
3031 int eccbytes = chip->ecc.bytes;
3032 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003033 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003034
Boris Brezillon25f815f2017-11-30 18:01:30 +01003035 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3036 if (ret)
3037 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003038
3039 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003040 ret = nand_read_data_op(chip, buf, eccsize, false);
3041 if (ret)
3042 return ret;
3043
David Brownell52ff49d2009-03-04 12:01:36 -08003044 buf += eccsize;
3045
3046 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003047 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3048 false);
3049 if (ret)
3050 return ret;
3051
David Brownell52ff49d2009-03-04 12:01:36 -08003052 oob += chip->ecc.prepad;
3053 }
3054
Boris Brezillon97d90da2017-11-30 18:01:29 +01003055 ret = nand_read_data_op(chip, oob, eccbytes, false);
3056 if (ret)
3057 return ret;
3058
David Brownell52ff49d2009-03-04 12:01:36 -08003059 oob += eccbytes;
3060
3061 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003062 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3063 false);
3064 if (ret)
3065 return ret;
3066
David Brownell52ff49d2009-03-04 12:01:36 -08003067 oob += chip->ecc.postpad;
3068 }
3069 }
3070
3071 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003072 if (size) {
3073 ret = nand_read_data_op(chip, oob, size, false);
3074 if (ret)
3075 return ret;
3076 }
David Brownell52ff49d2009-03-04 12:01:36 -08003077
3078 return 0;
3079}
3080
3081/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003082 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003083 * @mtd: mtd info structure
3084 * @chip: nand chip info structure
3085 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003086 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003087 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003088 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003089static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003090 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Boris Brezillon846031d2016-02-03 20:11:00 +01003092 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003093 int eccbytes = chip->ecc.bytes;
3094 int eccsteps = chip->ecc.steps;
3095 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003096 uint8_t *ecc_calc = chip->ecc.calc_buf;
3097 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003098 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003099
Brian Norris1fbb9382012-05-02 10:14:55 -07003100 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003101
3102 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3103 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3104
Boris Brezillon846031d2016-02-03 20:11:00 +01003105 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3106 chip->ecc.total);
3107 if (ret)
3108 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003109
3110 eccsteps = chip->ecc.steps;
3111 p = buf;
3112
3113 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3114 int stat;
3115
3116 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003117 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003118 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003119 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003120 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003121 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3122 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003123 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003124 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303128 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003129 * @mtd: mtd info structure
3130 * @chip: nand chip info structure
3131 * @data_offs: offset of requested data within the page
3132 * @readlen: data length
3133 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003134 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003135 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003136static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003137 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3138 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003139{
Boris Brezillon846031d2016-02-03 20:11:00 +01003140 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003141 uint8_t *p;
3142 int data_col_addr, i, gaps = 0;
3143 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3144 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003145 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003146 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003147 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003148
Brian Norris7854d3f2011-06-23 14:12:08 -07003149 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003150 start_step = data_offs / chip->ecc.size;
3151 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3152 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303153 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003154
Brian Norris8b6e50c2011-05-25 14:59:01 -07003155 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003156 datafrag_len = num_steps * chip->ecc.size;
3157 eccfrag_len = num_steps * chip->ecc.bytes;
3158
3159 data_col_addr = start_step * chip->ecc.size;
3160 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003161 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003162 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003163 if (ret)
3164 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003165
Brian Norris8b6e50c2011-05-25 14:59:01 -07003166 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003167 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003168 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003169
Brian Norris8b6e50c2011-05-25 14:59:01 -07003170 /*
3171 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003172 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003173 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003174 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3175 if (ret)
3176 return ret;
3177
3178 if (oobregion.length < eccfrag_len)
3179 gaps = 1;
3180
Alexey Korolev3d459552008-05-15 17:23:18 +01003181 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003182 ret = nand_change_read_column_op(chip, mtd->writesize,
3183 chip->oob_poi, mtd->oobsize,
3184 false);
3185 if (ret)
3186 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003187 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003188 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003189 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003190 * about buswidth alignment in read_buf.
3191 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003192 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003193 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003194 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003195 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003196 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3197 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003198 aligned_len++;
3199
Boris Brezillon97d90da2017-11-30 18:01:29 +01003200 ret = nand_change_read_column_op(chip,
3201 mtd->writesize + aligned_pos,
3202 &chip->oob_poi[aligned_pos],
3203 aligned_len, false);
3204 if (ret)
3205 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003206 }
3207
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003208 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003209 chip->oob_poi, index, eccfrag_len);
3210 if (ret)
3211 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003212
3213 p = bufpoi + data_col_addr;
3214 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3215 int stat;
3216
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003217 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3218 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003219 if (stat == -EBADMSG &&
3220 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3221 /* check for empty pages with bitflips */
3222 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003223 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003224 chip->ecc.bytes,
3225 NULL, 0,
3226 chip->ecc.strength);
3227 }
3228
Mike Dunn3f91e942012-04-25 12:06:09 -07003229 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003230 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003231 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003232 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003233 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3234 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003235 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003236 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003237}
3238
3239/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003240 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003241 * @mtd: mtd info structure
3242 * @chip: nand chip info structure
3243 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003244 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003245 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003246 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003247 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003248 */
3249static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003250 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003251{
Boris Brezillon846031d2016-02-03 20:11:00 +01003252 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003253 int eccbytes = chip->ecc.bytes;
3254 int eccsteps = chip->ecc.steps;
3255 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003256 uint8_t *ecc_calc = chip->ecc.calc_buf;
3257 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003258 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003259
Boris Brezillon25f815f2017-11-30 18:01:30 +01003260 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3261 if (ret)
3262 return ret;
3263
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003264 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3265 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003266
3267 ret = nand_read_data_op(chip, p, eccsize, false);
3268 if (ret)
3269 return ret;
3270
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003271 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3272 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003273
3274 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3275 if (ret)
3276 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003277
Boris Brezillon846031d2016-02-03 20:11:00 +01003278 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3279 chip->ecc.total);
3280 if (ret)
3281 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003282
3283 eccsteps = chip->ecc.steps;
3284 p = buf;
3285
3286 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3287 int stat;
3288
3289 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003290 if (stat == -EBADMSG &&
3291 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3292 /* check for empty pages with bitflips */
3293 stat = nand_check_erased_ecc_chunk(p, eccsize,
3294 &ecc_code[i], eccbytes,
3295 NULL, 0,
3296 chip->ecc.strength);
3297 }
3298
Mike Dunn3f91e942012-04-25 12:06:09 -07003299 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003300 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003301 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003302 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003303 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3304 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003305 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003306 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003307}
3308
3309/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003310 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003311 * @mtd: mtd info structure
3312 * @chip: nand chip info structure
3313 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003314 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003315 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003316 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003317 * Hardware ECC for large page chips, require OOB to be read first. For this
3318 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3319 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3320 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3321 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003322 */
3323static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003324 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003325{
Boris Brezillon846031d2016-02-03 20:11:00 +01003326 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003327 int eccbytes = chip->ecc.bytes;
3328 int eccsteps = chip->ecc.steps;
3329 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003330 uint8_t *ecc_code = chip->ecc.code_buf;
3331 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003332 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003333
3334 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003335 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3336 if (ret)
3337 return ret;
3338
3339 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3340 if (ret)
3341 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003342
Boris Brezillon846031d2016-02-03 20:11:00 +01003343 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3344 chip->ecc.total);
3345 if (ret)
3346 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003347
3348 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3349 int stat;
3350
3351 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003352
3353 ret = nand_read_data_op(chip, p, eccsize, false);
3354 if (ret)
3355 return ret;
3356
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003357 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3358
3359 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003360 if (stat == -EBADMSG &&
3361 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3362 /* check for empty pages with bitflips */
3363 stat = nand_check_erased_ecc_chunk(p, eccsize,
3364 &ecc_code[i], eccbytes,
3365 NULL, 0,
3366 chip->ecc.strength);
3367 }
3368
Mike Dunn3f91e942012-04-25 12:06:09 -07003369 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003370 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003371 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003372 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003373 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3374 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003375 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003376 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003377}
3378
3379/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003380 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003381 * @mtd: mtd info structure
3382 * @chip: nand chip info structure
3383 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003384 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003385 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003386 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003387 * The hw generator calculates the error syndrome automatically. Therefore we
3388 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003389 */
3390static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003391 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003392{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003393 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003394 int eccbytes = chip->ecc.bytes;
3395 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003396 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003397 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003398 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003399 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003400
Boris Brezillon25f815f2017-11-30 18:01:30 +01003401 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3402 if (ret)
3403 return ret;
3404
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003405 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3406 int stat;
3407
3408 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003409
3410 ret = nand_read_data_op(chip, p, eccsize, false);
3411 if (ret)
3412 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003413
3414 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003415 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3416 false);
3417 if (ret)
3418 return ret;
3419
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003420 oob += chip->ecc.prepad;
3421 }
3422
3423 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003424
3425 ret = nand_read_data_op(chip, oob, eccbytes, false);
3426 if (ret)
3427 return ret;
3428
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003429 stat = chip->ecc.correct(mtd, p, oob, NULL);
3430
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003431 oob += eccbytes;
3432
3433 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003434 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3435 false);
3436 if (ret)
3437 return ret;
3438
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003439 oob += chip->ecc.postpad;
3440 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003441
3442 if (stat == -EBADMSG &&
3443 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3444 /* check for empty pages with bitflips */
3445 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3446 oob - eccpadbytes,
3447 eccpadbytes,
3448 NULL, 0,
3449 chip->ecc.strength);
3450 }
3451
3452 if (stat < 0) {
3453 mtd->ecc_stats.failed++;
3454 } else {
3455 mtd->ecc_stats.corrected += stat;
3456 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3457 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003458 }
3459
3460 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003461 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003462 if (i) {
3463 ret = nand_read_data_op(chip, oob, i, false);
3464 if (ret)
3465 return ret;
3466 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003467
Mike Dunn3f91e942012-04-25 12:06:09 -07003468 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003469}
3470
3471/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003472 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003473 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003474 * @oob: oob destination address
3475 * @ops: oob ops structure
3476 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003477 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003478static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003479 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003480{
Boris Brezillon846031d2016-02-03 20:11:00 +01003481 struct nand_chip *chip = mtd_to_nand(mtd);
3482 int ret;
3483
Florian Fainellif8ac0412010-09-07 13:23:43 +02003484 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003485
Brian Norris0612b9d2011-08-30 18:45:40 -07003486 case MTD_OPS_PLACE_OOB:
3487 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003488 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3489 return oob + len;
3490
Boris Brezillon846031d2016-02-03 20:11:00 +01003491 case MTD_OPS_AUTO_OOB:
3492 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3493 ops->ooboffs, len);
3494 BUG_ON(ret);
3495 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003496
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003497 default:
3498 BUG();
3499 }
3500 return NULL;
3501}
3502
3503/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003504 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3505 * @mtd: MTD device structure
3506 * @retry_mode: the retry mode to use
3507 *
3508 * Some vendors supply a special command to shift the Vt threshold, to be used
3509 * when there are too many bitflips in a page (i.e., ECC error). After setting
3510 * a new threshold, the host should retry reading the page.
3511 */
3512static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3513{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003514 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08003515
3516 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3517
3518 if (retry_mode >= chip->read_retries)
3519 return -EINVAL;
3520
3521 if (!chip->setup_read_retry)
3522 return -EOPNOTSUPP;
3523
3524 return chip->setup_read_retry(mtd, retry_mode);
3525}
3526
3527/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003528 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003529 * @mtd: MTD device structure
3530 * @from: offset to read from
3531 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003532 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003533 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003534 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003535static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3536 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003537{
Brian Norrise47f3db2012-05-02 10:14:56 -07003538 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003539 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003540 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003541 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003542 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003543 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003544
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003545 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003546 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003547 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003548 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003549 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003551 chipnr = (int)(from >> chip->chip_shift);
3552 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003554 realpage = (int)(from >> chip->page_shift);
3555 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003557 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003559 buf = ops->datbuf;
3560 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003561 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003562
Florian Fainellif8ac0412010-09-07 13:23:43 +02003563 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003564 unsigned int ecc_failures = mtd->ecc_stats.failed;
3565
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003566 bytes = min(mtd->writesize - col, readlen);
3567 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003568
Kamal Dasu66507c72014-05-01 20:51:19 -04003569 if (!aligned)
3570 use_bufpoi = 1;
3571 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003572 use_bufpoi = !virt_addr_valid(buf) ||
3573 !IS_ALIGNED((unsigned long)buf,
3574 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003575 else
3576 use_bufpoi = 0;
3577
Brian Norris8b6e50c2011-05-25 14:59:01 -07003578 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003579 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003580 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003581
3582 if (use_bufpoi && aligned)
3583 pr_debug("%s: using read bounce buffer for buf@%p\n",
3584 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Brian Norrisba84fb52014-01-03 15:13:33 -08003586read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003587 /*
3588 * Now read the page into the buffer. Absent an error,
3589 * the read methods return max bitflips per ecc step.
3590 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003591 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07003592 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003593 oob_required,
3594 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003595 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3596 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003597 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003598 col, bytes, bufpoi,
3599 page);
David Woodhouse956e9442006-09-25 17:12:39 +01003600 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07003601 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003602 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003603 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003604 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003605 /* Invalidate page cache */
3606 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003607 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003608 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003609
3610 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003611 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003612 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003613 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003614 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003615 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003616 chip->pagebuf_bitflips = ret;
3617 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003618 /* Invalidate page cache */
3619 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003620 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003621 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003623
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003624 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003625 int toread = min(oobreadlen, max_oobsize);
3626
3627 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003628 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003629 oob, ops, toread);
3630 oobreadlen -= toread;
3631 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003632 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003633
3634 if (chip->options & NAND_NEED_READRDY) {
3635 /* Apply delay or wait for ready/busy pin */
3636 if (!chip->dev_ready)
3637 udelay(chip->chip_delay);
3638 else
3639 nand_wait_ready(mtd);
3640 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08003641
Brian Norrisba84fb52014-01-03 15:13:33 -08003642 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003643 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003644 retry_mode++;
3645 ret = nand_setup_read_retry(mtd,
3646 retry_mode);
3647 if (ret < 0)
3648 break;
3649
3650 /* Reset failures; retry */
3651 mtd->ecc_stats.failed = ecc_failures;
3652 goto read_retry;
3653 } else {
3654 /* No more retry modes; real failure */
3655 ecc_fail = true;
3656 }
3657 }
3658
3659 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003660 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003661 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003662 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003663 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003664 max_bitflips = max_t(unsigned int, max_bitflips,
3665 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003668 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003669
Brian Norrisba84fb52014-01-03 15:13:33 -08003670 /* Reset to retry mode 0 */
3671 if (retry_mode) {
3672 ret = nand_setup_read_retry(mtd, 0);
3673 if (ret < 0)
3674 break;
3675 retry_mode = 0;
3676 }
3677
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003678 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Brian Norris8b6e50c2011-05-25 14:59:01 -07003681 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 col = 0;
3683 /* Increment page address */
3684 realpage++;
3685
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003686 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 /* Check, if we cross a chip boundary */
3688 if (!page) {
3689 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003690 chip->select_chip(mtd, -1);
3691 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003694 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003696 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003697 if (oob)
3698 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Mike Dunn3f91e942012-04-25 12:06:09 -07003700 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003701 return ret;
3702
Brian Norrisb72f3df2013-12-03 11:04:14 -08003703 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003704 return -EBADMSG;
3705
Mike Dunnedbc45402012-04-25 12:06:11 -07003706 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003707}
3708
3709/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003710 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003711 * @mtd: mtd info structure
3712 * @chip: nand chip info structure
3713 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003714 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003715int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003716{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003717 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003718}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003719EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003720
3721/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003722 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003723 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003724 * @mtd: mtd info structure
3725 * @chip: nand chip info structure
3726 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003727 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003728int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3729 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003730{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003731 int length = mtd->oobsize;
3732 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3733 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003734 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003735 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003736
Boris Brezillon97d90da2017-11-30 18:01:29 +01003737 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3738 if (ret)
3739 return ret;
3740
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003741 for (i = 0; i < chip->ecc.steps; i++) {
3742 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003743 int ret;
3744
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003745 pos = eccsize + i * (eccsize + chunk);
3746 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003747 ret = nand_change_read_column_op(chip, pos,
3748 NULL, 0,
3749 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003750 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003751 ret = nand_read_page_op(chip, page, pos, NULL,
3752 0);
3753
3754 if (ret)
3755 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003756 } else
3757 sndrnd = 1;
3758 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003759
3760 ret = nand_read_data_op(chip, bufpoi, toread, false);
3761 if (ret)
3762 return ret;
3763
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003764 bufpoi += toread;
3765 length -= toread;
3766 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003767 if (length > 0) {
3768 ret = nand_read_data_op(chip, bufpoi, length, false);
3769 if (ret)
3770 return ret;
3771 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003772
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003773 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003774}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003775EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003776
3777/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003778 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003779 * @mtd: mtd info structure
3780 * @chip: nand chip info structure
3781 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003782 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003783int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003784{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003785 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3786 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003787}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003788EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003789
3790/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003791 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003792 * with syndrome - only for large page flash
3793 * @mtd: mtd info structure
3794 * @chip: nand chip info structure
3795 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003796 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003797int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3798 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003799{
3800 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3801 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003802 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003803 const uint8_t *bufpoi = chip->oob_poi;
3804
3805 /*
3806 * data-ecc-data-ecc ... ecc-oob
3807 * or
3808 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3809 */
3810 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3811 pos = steps * (eccsize + chunk);
3812 steps = 0;
3813 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003814 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003815
Boris Brezillon97d90da2017-11-30 18:01:29 +01003816 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3817 if (ret)
3818 return ret;
3819
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003820 for (i = 0; i < steps; i++) {
3821 if (sndcmd) {
3822 if (mtd->writesize <= 512) {
3823 uint32_t fill = 0xFFFFFFFF;
3824
3825 len = eccsize;
3826 while (len > 0) {
3827 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003828
3829 ret = nand_write_data_op(chip, &fill,
3830 num, false);
3831 if (ret)
3832 return ret;
3833
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003834 len -= num;
3835 }
3836 } else {
3837 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003838 ret = nand_change_write_column_op(chip, pos,
3839 NULL, 0,
3840 false);
3841 if (ret)
3842 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003843 }
3844 } else
3845 sndcmd = 1;
3846 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003847
3848 ret = nand_write_data_op(chip, bufpoi, len, false);
3849 if (ret)
3850 return ret;
3851
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003852 bufpoi += len;
3853 length -= len;
3854 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003855 if (length > 0) {
3856 ret = nand_write_data_op(chip, bufpoi, length, false);
3857 if (ret)
3858 return ret;
3859 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003860
Boris Brezillon97d90da2017-11-30 18:01:29 +01003861 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003862}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003863EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003864
3865/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003866 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003867 * @mtd: MTD device structure
3868 * @from: offset to read from
3869 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003871 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003873static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3874 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003876 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003877 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003878 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003879 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003880 int readlen = ops->ooblen;
3881 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003882 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003883 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Brian Norris289c0522011-07-19 10:06:09 -07003885 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303886 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Brian Norris041e4572011-06-23 16:45:24 -07003888 stats = mtd->ecc_stats;
3889
Boris BREZILLON29f10582016-03-07 10:46:52 +01003890 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003891
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003892 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003893 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003895 /* Shift to get page */
3896 realpage = (int)(from >> chip->page_shift);
3897 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Florian Fainellif8ac0412010-09-07 13:23:43 +02003899 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003900 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003901 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003902 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003903 ret = chip->ecc.read_oob(mtd, chip, page);
3904
3905 if (ret < 0)
3906 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003907
3908 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003909 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003910
Brian Norris5bc7c332013-03-13 09:51:31 -07003911 if (chip->options & NAND_NEED_READRDY) {
3912 /* Apply delay or wait for ready/busy pin */
3913 if (!chip->dev_ready)
3914 udelay(chip->chip_delay);
3915 else
3916 nand_wait_ready(mtd);
3917 }
3918
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003919 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3920
Vitaly Wool70145682006-11-03 18:20:38 +03003921 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003922 if (!readlen)
3923 break;
3924
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003925 /* Increment page address */
3926 realpage++;
3927
3928 page = realpage & chip->pagemask;
3929 /* Check, if we cross a chip boundary */
3930 if (!page) {
3931 chipnr++;
3932 chip->select_chip(mtd, -1);
3933 chip->select_chip(mtd, chipnr);
3934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003936 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003938 ops->oobretlen = ops->ooblen - readlen;
3939
3940 if (ret < 0)
3941 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003942
3943 if (mtd->ecc_stats.failed - stats.failed)
3944 return -EBADMSG;
3945
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003946 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947}
3948
3949/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003950 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003951 * @mtd: MTD device structure
3952 * @from: offset to read from
3953 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003955 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003957static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3958 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003960 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003961
3962 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003964 if (ops->mode != MTD_OPS_PLACE_OOB &&
3965 ops->mode != MTD_OPS_AUTO_OOB &&
3966 ops->mode != MTD_OPS_RAW)
3967 return -ENOTSUPP;
3968
Huang Shijie6a8214a2012-11-19 14:43:30 +08003969 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003971 if (!ops->datbuf)
3972 ret = nand_do_read_oob(mtd, from, ops);
3973 else
3974 ret = nand_do_read_ops(mtd, from, ops);
3975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003977 return ret;
3978}
3979
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003980/**
3981 * nand_write_page_raw_notsupp - dummy raw page write function
3982 * @mtd: mtd info structure
3983 * @chip: nand chip info structure
3984 * @buf: data buffer
3985 * @oob_required: must write chip->oob_poi to OOB
3986 * @page: page number to write
3987 *
3988 * Returns -ENOTSUPP unconditionally.
3989 */
3990int nand_write_page_raw_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
3991 const u8 *buf, int oob_required, int page)
3992{
3993 return -ENOTSUPP;
3994}
3995EXPORT_SYMBOL(nand_write_page_raw_notsupp);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003996
3997/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003998 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003999 * @mtd: mtd info structure
4000 * @chip: nand chip info structure
4001 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004002 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004003 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004004 *
Brian Norris7854d3f2011-06-23 14:12:08 -07004005 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004006 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02004007int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
4008 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004009{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004010 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004011
Boris Brezillon25f815f2017-11-30 18:01:30 +01004012 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004013 if (ret)
4014 return ret;
4015
4016 if (oob_required) {
4017 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
4018 false);
4019 if (ret)
4020 return ret;
4021 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004022
Boris Brezillon25f815f2017-11-30 18:01:30 +01004023 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02004025EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004027/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004028 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004029 * @mtd: mtd info structure
4030 * @chip: nand chip info structure
4031 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004032 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004033 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004034 *
4035 * We need a special oob layout and handling even when ECC isn't checked.
4036 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004037static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004038 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004039 const uint8_t *buf, int oob_required,
4040 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004041{
4042 int eccsize = chip->ecc.size;
4043 int eccbytes = chip->ecc.bytes;
4044 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004045 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004046
Boris Brezillon25f815f2017-11-30 18:01:30 +01004047 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4048 if (ret)
4049 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004050
4051 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004052 ret = nand_write_data_op(chip, buf, eccsize, false);
4053 if (ret)
4054 return ret;
4055
David Brownell52ff49d2009-03-04 12:01:36 -08004056 buf += eccsize;
4057
4058 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004059 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4060 false);
4061 if (ret)
4062 return ret;
4063
David Brownell52ff49d2009-03-04 12:01:36 -08004064 oob += chip->ecc.prepad;
4065 }
4066
Boris Brezillon97d90da2017-11-30 18:01:29 +01004067 ret = nand_write_data_op(chip, oob, eccbytes, false);
4068 if (ret)
4069 return ret;
4070
David Brownell52ff49d2009-03-04 12:01:36 -08004071 oob += eccbytes;
4072
4073 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004074 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4075 false);
4076 if (ret)
4077 return ret;
4078
David Brownell52ff49d2009-03-04 12:01:36 -08004079 oob += chip->ecc.postpad;
4080 }
4081 }
4082
4083 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004084 if (size) {
4085 ret = nand_write_data_op(chip, oob, size, false);
4086 if (ret)
4087 return ret;
4088 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004089
Boris Brezillon25f815f2017-11-30 18:01:30 +01004090 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004091}
4092/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004093 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004094 * @mtd: mtd info structure
4095 * @chip: nand chip info structure
4096 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004097 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004098 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004099 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004100static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004101 const uint8_t *buf, int oob_required,
4102 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004103{
Boris Brezillon846031d2016-02-03 20:11:00 +01004104 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004105 int eccbytes = chip->ecc.bytes;
4106 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004107 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004108 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004109
Brian Norris7854d3f2011-06-23 14:12:08 -07004110 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004111 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
4112 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004113
Boris Brezillon846031d2016-02-03 20:11:00 +01004114 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4115 chip->ecc.total);
4116 if (ret)
4117 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004118
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004119 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004120}
4121
4122/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004123 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004124 * @mtd: mtd info structure
4125 * @chip: nand chip info structure
4126 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004127 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004128 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004129 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004130static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004131 const uint8_t *buf, int oob_required,
4132 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004133{
Boris Brezillon846031d2016-02-03 20:11:00 +01004134 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004135 int eccbytes = chip->ecc.bytes;
4136 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004137 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004138 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004139
Boris Brezillon25f815f2017-11-30 18:01:30 +01004140 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4141 if (ret)
4142 return ret;
4143
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004144 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4145 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004146
4147 ret = nand_write_data_op(chip, p, eccsize, false);
4148 if (ret)
4149 return ret;
4150
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004151 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
4152 }
4153
Boris Brezillon846031d2016-02-03 20:11:00 +01004154 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4155 chip->ecc.total);
4156 if (ret)
4157 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004158
Boris Brezillon97d90da2017-11-30 18:01:29 +01004159 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4160 if (ret)
4161 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004162
Boris Brezillon25f815f2017-11-30 18:01:30 +01004163 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004164}
4165
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304166
4167/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004168 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304169 * @mtd: mtd info structure
4170 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004171 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304172 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004173 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304174 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004175 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304176 */
4177static int nand_write_subpage_hwecc(struct mtd_info *mtd,
4178 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07004179 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004180 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304181{
4182 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004183 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304184 int ecc_size = chip->ecc.size;
4185 int ecc_bytes = chip->ecc.bytes;
4186 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304187 uint32_t start_step = offset / ecc_size;
4188 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4189 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004190 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304191
Boris Brezillon25f815f2017-11-30 18:01:30 +01004192 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4193 if (ret)
4194 return ret;
4195
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304196 for (step = 0; step < ecc_steps; step++) {
4197 /* configure controller for WRITE access */
4198 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
4199
4200 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004201 ret = nand_write_data_op(chip, buf, ecc_size, false);
4202 if (ret)
4203 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304204
4205 /* mask ECC of un-touched subpages by padding 0xFF */
4206 if ((step < start_step) || (step > end_step))
4207 memset(ecc_calc, 0xff, ecc_bytes);
4208 else
Brian Norrisd6a950802013-08-08 17:16:36 -07004209 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304210
4211 /* mask OOB of un-touched subpages by padding 0xFF */
4212 /* if oob_required, preserve OOB metadata of written subpage */
4213 if (!oob_required || (step < start_step) || (step > end_step))
4214 memset(oob_buf, 0xff, oob_bytes);
4215
Brian Norrisd6a950802013-08-08 17:16:36 -07004216 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304217 ecc_calc += ecc_bytes;
4218 oob_buf += oob_bytes;
4219 }
4220
4221 /* copy calculated ECC for whole page to chip->buffer->oob */
4222 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004223 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004224 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4225 chip->ecc.total);
4226 if (ret)
4227 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304228
4229 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004230 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4231 if (ret)
4232 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304233
Boris Brezillon25f815f2017-11-30 18:01:30 +01004234 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304235}
4236
4237
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004238/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004239 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004240 * @mtd: mtd info structure
4241 * @chip: nand chip info structure
4242 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004243 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004244 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004245 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004246 * The hw generator calculates the error syndrome automatically. Therefore we
4247 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004248 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004249static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07004250 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004251 const uint8_t *buf, int oob_required,
4252 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004253{
4254 int i, eccsize = chip->ecc.size;
4255 int eccbytes = chip->ecc.bytes;
4256 int eccsteps = chip->ecc.steps;
4257 const uint8_t *p = buf;
4258 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004259 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004260
Boris Brezillon25f815f2017-11-30 18:01:30 +01004261 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4262 if (ret)
4263 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004264
4265 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004266 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004267
4268 ret = nand_write_data_op(chip, p, eccsize, false);
4269 if (ret)
4270 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004271
4272 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004273 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4274 false);
4275 if (ret)
4276 return ret;
4277
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004278 oob += chip->ecc.prepad;
4279 }
4280
4281 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004282
4283 ret = nand_write_data_op(chip, oob, eccbytes, false);
4284 if (ret)
4285 return ret;
4286
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004287 oob += eccbytes;
4288
4289 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004290 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4291 false);
4292 if (ret)
4293 return ret;
4294
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004295 oob += chip->ecc.postpad;
4296 }
4297 }
4298
4299 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004300 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004301 if (i) {
4302 ret = nand_write_data_op(chip, oob, i, false);
4303 if (ret)
4304 return ret;
4305 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004306
Boris Brezillon25f815f2017-11-30 18:01:30 +01004307 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004308}
4309
4310/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004311 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004312 * @mtd: MTD device structure
4313 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304314 * @offset: address offset within the page
4315 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004316 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004317 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004318 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004319 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004320 */
4321static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304322 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004323 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004324{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304325 int status, subpage;
4326
4327 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4328 chip->ecc.write_subpage)
4329 subpage = offset || (data_len < mtd->writesize);
4330 else
4331 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004332
David Woodhouse956e9442006-09-25 17:12:39 +01004333 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304334 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004335 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304336 else if (subpage)
4337 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004338 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004339 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004340 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
4341 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004342
4343 if (status < 0)
4344 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004345
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004346 return 0;
4347}
4348
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004349/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004350 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004351 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004352 * @oob: oob data buffer
4353 * @len: oob data write length
4354 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004355 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004356static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4357 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004358{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004359 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004360 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004361
4362 /*
4363 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4364 * data from a previous OOB read.
4365 */
4366 memset(chip->oob_poi, 0xff, mtd->oobsize);
4367
Florian Fainellif8ac0412010-09-07 13:23:43 +02004368 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004369
Brian Norris0612b9d2011-08-30 18:45:40 -07004370 case MTD_OPS_PLACE_OOB:
4371 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004372 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4373 return oob + len;
4374
Boris Brezillon846031d2016-02-03 20:11:00 +01004375 case MTD_OPS_AUTO_OOB:
4376 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4377 ops->ooboffs, len);
4378 BUG_ON(ret);
4379 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004380
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004381 default:
4382 BUG();
4383 }
4384 return NULL;
4385}
4386
Florian Fainellif8ac0412010-09-07 13:23:43 +02004387#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004388
4389/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004390 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004391 * @mtd: MTD device structure
4392 * @to: offset to write to
4393 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004394 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004395 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004396 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004397static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4398 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004399{
Corentin Labbe73600b62017-09-02 10:49:38 +02004400 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004401 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004402 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004403
4404 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004405 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004406
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004407 uint8_t *oob = ops->oobbuf;
4408 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304409 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004410 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004411
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004412 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004413 if (!writelen)
4414 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004415
Brian Norris8b6e50c2011-05-25 14:59:01 -07004416 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004417 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004418 pr_notice("%s: attempt to write non page aligned data\n",
4419 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004420 return -EINVAL;
4421 }
4422
Thomas Gleixner29072b92006-09-28 15:38:36 +02004423 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004424
Thomas Gleixner6a930962006-06-28 00:11:45 +02004425 chipnr = (int)(to >> chip->chip_shift);
4426 chip->select_chip(mtd, chipnr);
4427
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004428 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004429 if (nand_check_wp(mtd)) {
4430 ret = -EIO;
4431 goto err_out;
4432 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004433
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004434 realpage = (int)(to >> chip->page_shift);
4435 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004436
4437 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004438 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4439 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004440 chip->pagebuf = -1;
4441
Maxim Levitsky782ce792010-02-22 20:39:36 +02004442 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004443 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4444 ret = -EINVAL;
4445 goto err_out;
4446 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004447
Florian Fainellif8ac0412010-09-07 13:23:43 +02004448 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004449 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004450 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004451 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004452 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004453
Kamal Dasu66507c72014-05-01 20:51:19 -04004454 if (part_pagewr)
4455 use_bufpoi = 1;
4456 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004457 use_bufpoi = !virt_addr_valid(buf) ||
4458 !IS_ALIGNED((unsigned long)buf,
4459 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004460 else
4461 use_bufpoi = 0;
4462
4463 /* Partial page write?, or need to use bounce buffer */
4464 if (use_bufpoi) {
4465 pr_debug("%s: using write bounce buffer for buf@%p\n",
4466 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004467 if (part_pagewr)
4468 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004469 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004470 memset(chip->data_buf, 0xff, mtd->writesize);
4471 memcpy(&chip->data_buf[column], buf, bytes);
4472 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004473 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004474
Maxim Levitsky782ce792010-02-22 20:39:36 +02004475 if (unlikely(oob)) {
4476 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004477 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004478 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004479 } else {
4480 /* We still need to erase leftover OOB data */
4481 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004482 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004483
4484 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004485 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004486 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004487 if (ret)
4488 break;
4489
4490 writelen -= bytes;
4491 if (!writelen)
4492 break;
4493
Thomas Gleixner29072b92006-09-28 15:38:36 +02004494 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004495 buf += bytes;
4496 realpage++;
4497
4498 page = realpage & chip->pagemask;
4499 /* Check, if we cross a chip boundary */
4500 if (!page) {
4501 chipnr++;
4502 chip->select_chip(mtd, -1);
4503 chip->select_chip(mtd, chipnr);
4504 }
4505 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004506
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004507 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004508 if (unlikely(oob))
4509 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004510
4511err_out:
4512 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004513 return ret;
4514}
4515
4516/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004517 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004518 * @mtd: MTD device structure
4519 * @to: offset to write to
4520 * @len: number of bytes to write
4521 * @retlen: pointer to variable to store the number of written bytes
4522 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004523 *
4524 * NAND write with ECC. Used when performing writes in interrupt context, this
4525 * may for example be called by mtdoops when writing an oops while in panic.
4526 */
4527static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4528 size_t *retlen, const uint8_t *buf)
4529{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004530 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004531 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004532 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004533 int ret;
4534
Brian Norris8b6e50c2011-05-25 14:59:01 -07004535 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004536 panic_nand_get_device(chip, mtd, FL_WRITING);
4537
Brent Taylor30863e382017-10-30 22:32:45 -05004538 chip->select_chip(mtd, chipnr);
4539
4540 /* Wait for the device to get ready */
4541 panic_nand_wait(mtd, chip, 400);
4542
Brian Norris0ec56dc2015-02-28 02:02:30 -08004543 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004544 ops.len = len;
4545 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004546 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004547
Brian Norris4a89ff82011-08-30 18:45:45 -07004548 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004549
Brian Norris4a89ff82011-08-30 18:45:45 -07004550 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004551 return ret;
4552}
4553
4554/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004555 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004556 * @mtd: MTD device structure
4557 * @to: offset to write to
4558 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004559 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004560 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004561 */
4562static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4563 struct mtd_oob_ops *ops)
4564{
Adrian Hunter03736152007-01-31 17:58:29 +02004565 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004566 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
Brian Norris289c0522011-07-19 10:06:09 -07004568 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304569 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Boris BREZILLON29f10582016-03-07 10:46:52 +01004571 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004574 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004575 pr_debug("%s: attempt to write past end of page\n",
4576 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 return -EINVAL;
4578 }
4579
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004580 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004581
4582 /*
4583 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4584 * of my DiskOnChip 2000 test units) will clear the whole data page too
4585 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4586 * it in the doc2000 driver in August 1999. dwmw2.
4587 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004588 nand_reset(chip, chipnr);
4589
4590 chip->select_chip(mtd, chipnr);
4591
4592 /* Shift to get page */
4593 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
4595 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004596 if (nand_check_wp(mtd)) {
4597 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004598 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004599 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004602 if (page == chip->pagebuf)
4603 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004605 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004606
Brian Norris0612b9d2011-08-30 18:45:40 -07004607 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07004608 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
4609 else
4610 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004611
Huang Shijieb0bb6902012-11-19 14:43:29 +08004612 chip->select_chip(mtd, -1);
4613
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004614 if (status)
4615 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Vitaly Wool70145682006-11-03 18:20:38 +03004617 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004619 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004620}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004622/**
4623 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004624 * @mtd: MTD device structure
4625 * @to: offset to write to
4626 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004627 */
4628static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4629 struct mtd_oob_ops *ops)
4630{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004631 int ret = -ENOTSUPP;
4632
4633 ops->retlen = 0;
4634
Huang Shijie6a8214a2012-11-19 14:43:30 +08004635 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004636
Florian Fainellif8ac0412010-09-07 13:23:43 +02004637 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004638 case MTD_OPS_PLACE_OOB:
4639 case MTD_OPS_AUTO_OOB:
4640 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004641 break;
4642
4643 default:
4644 goto out;
4645 }
4646
4647 if (!ops->datbuf)
4648 ret = nand_do_write_oob(mtd, to, ops);
4649 else
4650 ret = nand_do_write_ops(mtd, to, ops);
4651
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004652out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004653 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 return ret;
4655}
4656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657/**
Brian Norris49c50b92014-05-06 16:02:19 -07004658 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004659 * @mtd: MTD device structure
4660 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 *
Brian Norris49c50b92014-05-06 16:02:19 -07004662 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 */
Brian Norris49c50b92014-05-06 16:02:19 -07004664static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004666 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004667 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004668
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004670 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004671
Boris Brezillon97d90da2017-11-30 18:01:29 +01004672 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673}
4674
4675/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004677 * @mtd: MTD device structure
4678 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004680 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004682static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
David Woodhousee0c7d762006-05-13 18:07:53 +01004684 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004688 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004689 * @mtd: MTD device structure
4690 * @instr: erase instruction
4691 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004693 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004695int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4696 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
Adrian Hunter69423d92008-12-10 13:37:21 +00004698 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004699 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00004700 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Brian Norris289c0522011-07-19 10:06:09 -07004702 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4703 __func__, (unsigned long long)instr->addr,
4704 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304706 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004710 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
4712 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004713 page = (int)(instr->addr >> chip->page_shift);
4714 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
4716 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004717 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
4719 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004720 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 /* Check, if it is write protected */
4723 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004724 pr_debug("%s: device is write protected!\n",
4725 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004726 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 goto erase_exit;
4728 }
4729
4730 /* Loop through the pages */
4731 len = instr->len;
4732
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004734 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004735 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304736 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004737 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4738 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004739 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 goto erase_exit;
4741 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004742
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004743 /*
4744 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004745 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004746 */
4747 if (page <= chip->pagebuf && chip->pagebuf <
4748 (page + pages_per_block))
4749 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
Brian Norris49c50b92014-05-06 16:02:19 -07004751 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
4753 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004754 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004755 pr_debug("%s: failed erase, page 0x%08x\n",
4756 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004757 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004758 instr->fail_addr =
4759 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 goto erase_exit;
4761 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004764 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 page += pages_per_block;
4766
4767 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004768 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004770 chip->select_chip(mtd, -1);
4771 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 }
4773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004775 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004776erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004779 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 nand_release_device(mtd);
4781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 /* Return more or less happy */
4783 return ret;
4784}
4785
4786/**
4787 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004788 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004790 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004792static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793{
Brian Norris289c0522011-07-19 10:06:09 -07004794 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
4796 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004797 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004799 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800}
4801
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004803 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004804 * @mtd: MTD device structure
4805 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004807static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304809 struct nand_chip *chip = mtd_to_nand(mtd);
4810 int chipnr = (int)(offs >> chip->chip_shift);
4811 int ret;
4812
4813 /* Select the NAND device */
4814 nand_get_device(mtd, FL_READING);
4815 chip->select_chip(mtd, chipnr);
4816
4817 ret = nand_block_checkbad(mtd, offs, 0);
4818
4819 chip->select_chip(mtd, -1);
4820 nand_release_device(mtd);
4821
4822 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
4825/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004826 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004827 * @mtd: MTD device structure
4828 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004830static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 int ret;
4833
Florian Fainellif8ac0412010-09-07 13:23:43 +02004834 ret = nand_block_isbad(mtd, ofs);
4835 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004836 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 if (ret > 0)
4838 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004839 return ret;
4840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Brian Norris5a0edb22013-07-30 17:52:58 -07004842 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843}
4844
4845/**
Zach Brown56718422017-01-10 13:30:20 -06004846 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4847 * @mtd: MTD device structure
4848 * @ofs: offset relative to mtd start
4849 * @len: length of mtd
4850 */
4851static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4852{
4853 struct nand_chip *chip = mtd_to_nand(mtd);
4854 u32 part_start_block;
4855 u32 part_end_block;
4856 u32 part_start_die;
4857 u32 part_end_die;
4858
4859 /*
4860 * max_bb_per_die and blocks_per_die used to determine
4861 * the maximum bad block count.
4862 */
4863 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4864 return -ENOTSUPP;
4865
4866 /* Get the start and end of the partition in erase blocks. */
4867 part_start_block = mtd_div_by_eb(ofs, mtd);
4868 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4869
4870 /* Get the start and end LUNs of the partition. */
4871 part_start_die = part_start_block / chip->blocks_per_die;
4872 part_end_die = part_end_block / chip->blocks_per_die;
4873
4874 /*
4875 * Look up the bad blocks per unit and multiply by the number of units
4876 * that the partition spans.
4877 */
4878 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4879}
4880
4881/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004882 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004883 * @mtd: MTD device structure
4884 * @chip: nand chip info structure
4885 * @addr: feature address.
4886 * @subfeature_param: the subfeature parameters, a four bytes array.
4887 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004888static int nand_default_set_features(struct mtd_info *mtd,
4889 struct nand_chip *chip, int addr,
4890 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004891{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004892 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004893}
4894
4895/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004896 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004897 * @mtd: MTD device structure
4898 * @chip: nand chip info structure
4899 * @addr: feature address.
4900 * @subfeature_param: the subfeature parameters, a four bytes array.
4901 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004902static int nand_default_get_features(struct mtd_info *mtd,
4903 struct nand_chip *chip, int addr,
4904 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004905{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004906 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004907}
4908
4909/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004910 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004911 * @mtd: MTD device structure
4912 * @chip: nand chip info structure
4913 * @addr: feature address.
4914 * @subfeature_param: the subfeature parameters, a four bytes array.
4915 *
4916 * Should be used by NAND controller drivers that do not support the SET/GET
4917 * FEATURES operations.
4918 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004919int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4920 int addr, u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004921{
4922 return -ENOTSUPP;
4923}
Miquel Raynalb9587582018-03-19 14:47:19 +01004924EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004925
4926/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004927 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004928 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004929 */
4930static int nand_suspend(struct mtd_info *mtd)
4931{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004932 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004933}
4934
4935/**
4936 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004937 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004938 */
4939static void nand_resume(struct mtd_info *mtd)
4940{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004941 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004942
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004943 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004944 nand_release_device(mtd);
4945 else
Brian Norrisd0370212011-07-19 10:06:08 -07004946 pr_err("%s called for a chip which is not in suspended state\n",
4947 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004948}
4949
Scott Branden72ea4032014-11-20 11:18:05 -08004950/**
4951 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4952 * prevent further operations
4953 * @mtd: MTD device structure
4954 */
4955static void nand_shutdown(struct mtd_info *mtd)
4956{
Brian Norris9ca641b2015-11-09 16:37:28 -08004957 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004958}
4959
Brian Norris8b6e50c2011-05-25 14:59:01 -07004960/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004961static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004962{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004963 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004966 if (!chip->chip_delay)
4967 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
4969 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004970 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004971 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
4973 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004974 if (chip->waitfunc == NULL)
4975 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004977 if (!chip->select_chip)
4978 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004979
Huang Shijie4204ccc2013-08-16 10:10:07 +08004980 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004981 if (!chip->set_features)
4982 chip->set_features = nand_default_set_features;
4983 if (!chip->get_features)
4984 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004985
Brian Norris68e80782013-07-18 01:17:02 -07004986 /* If called twice, pointers that depend on busw may need to be reset */
4987 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004988 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4989 if (!chip->read_word)
4990 chip->read_word = nand_read_word;
4991 if (!chip->block_bad)
4992 chip->block_bad = nand_block_bad;
4993 if (!chip->block_markbad)
4994 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004995 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004996 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004997 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4998 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004999 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005000 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02005001
5002 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02005003 chip->controller = &chip->dummy_controller;
5004 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02005005 }
5006
Masahiro Yamada477544c2017-03-30 17:15:05 +09005007 if (!chip->buf_align)
5008 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005009}
5010
Brian Norris8b6e50c2011-05-25 14:59:01 -07005011/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005012static void sanitize_string(uint8_t *s, size_t len)
5013{
5014 ssize_t i;
5015
Brian Norris8b6e50c2011-05-25 14:59:01 -07005016 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005017 s[len - 1] = 0;
5018
Brian Norris8b6e50c2011-05-25 14:59:01 -07005019 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005020 for (i = 0; i < len - 1; i++) {
5021 if (s[i] < ' ' || s[i] > 127)
5022 s[i] = '?';
5023 }
5024
Brian Norris8b6e50c2011-05-25 14:59:01 -07005025 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005026 strim(s);
5027}
5028
5029static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
5030{
5031 int i;
5032 while (len--) {
5033 crc ^= *p++ << 8;
5034 for (i = 0; i < 8; i++)
5035 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
5036 }
5037
5038 return crc;
5039}
5040
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005041/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005042static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
5043 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005044{
5045 struct onfi_ext_param_page *ep;
5046 struct onfi_ext_section *s;
5047 struct onfi_ext_ecc_info *ecc;
5048 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005049 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005050 int len;
5051 int i;
5052
5053 len = le16_to_cpu(p->ext_param_page_length) * 16;
5054 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005055 if (!ep)
5056 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005057
5058 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005059 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5060 if (ret)
5061 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005062
5063 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005064 ret = nand_change_read_column_op(chip,
5065 sizeof(*p) * p->num_of_param_pages,
5066 ep, len, true);
5067 if (ret)
5068 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005069
Boris Brezillon97d90da2017-11-30 18:01:29 +01005070 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005071 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5072 != le16_to_cpu(ep->crc))) {
5073 pr_debug("fail in the CRC.\n");
5074 goto ext_out;
5075 }
5076
5077 /*
5078 * Check the signature.
5079 * Do not strictly follow the ONFI spec, maybe changed in future.
5080 */
5081 if (strncmp(ep->sig, "EPPS", 4)) {
5082 pr_debug("The signature is invalid.\n");
5083 goto ext_out;
5084 }
5085
5086 /* find the ECC section. */
5087 cursor = (uint8_t *)(ep + 1);
5088 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5089 s = ep->sections + i;
5090 if (s->type == ONFI_SECTION_TYPE_2)
5091 break;
5092 cursor += s->length * 16;
5093 }
5094 if (i == ONFI_EXT_SECTION_MAX) {
5095 pr_debug("We can not find the ECC section.\n");
5096 goto ext_out;
5097 }
5098
5099 /* get the info we want. */
5100 ecc = (struct onfi_ext_ecc_info *)cursor;
5101
Brian Norris4ae7d222013-09-16 18:20:21 -07005102 if (!ecc->codeword_size) {
5103 pr_debug("Invalid codeword size\n");
5104 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005105 }
5106
Brian Norris4ae7d222013-09-16 18:20:21 -07005107 chip->ecc_strength_ds = ecc->ecc_bits;
5108 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005109 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005110
5111ext_out:
5112 kfree(ep);
5113 return ret;
5114}
5115
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005116/*
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005117 * Recover data with bit-wise majority
5118 */
5119static void nand_bit_wise_majority(const void **srcbufs,
5120 unsigned int nsrcbufs,
5121 void *dstbuf,
5122 unsigned int bufsize)
5123{
5124 int i, j, k;
5125
5126 for (i = 0; i < bufsize; i++) {
5127 u8 val = 0;
5128
5129 for (j = 0; j < 8; j++) {
5130 unsigned int cnt = 0;
5131
5132 for (k = 0; k < nsrcbufs; k++) {
5133 const u8 *srcbuf = srcbufs[k];
5134
5135 if (srcbuf[i] & BIT(j))
5136 cnt++;
5137 }
5138
5139 if (cnt > nsrcbufs / 2)
5140 val |= BIT(j);
5141 }
5142
5143 ((u8 *)dstbuf)[i] = val;
5144 }
5145}
5146
5147/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005148 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005149 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005150static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005151{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005152 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005153 struct nand_onfi_params *p;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005154 char id[4];
5155 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005156
Brian Norris7854d3f2011-06-23 14:12:08 -07005157 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005158 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5159 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005160 return 0;
5161
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005162 /* ONFI chip: allocate a buffer to hold its parameter page */
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005163 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005164 if (!p)
5165 return -ENOMEM;
5166
Boris Brezillon97d90da2017-11-30 18:01:29 +01005167 ret = nand_read_param_page_op(chip, 0, NULL, 0);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005168 if (ret) {
5169 ret = 0;
5170 goto free_onfi_param_page;
5171 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005172
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005173 for (i = 0; i < 3; i++) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005174 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005175 if (ret) {
5176 ret = 0;
5177 goto free_onfi_param_page;
5178 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005179
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005180 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005181 le16_to_cpu(p->crc)) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005182 if (i)
5183 memcpy(p, &p[i], sizeof(*p));
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005184 break;
5185 }
5186 }
5187
Brian Norrisc7f23a72013-08-13 10:51:55 -07005188 if (i == 3) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005189 const void *srcbufs[3] = {p, p + 1, p + 2};
5190
5191 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5192 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5193 sizeof(*p));
5194
5195 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5196 le16_to_cpu(p->crc)) {
5197 pr_err("ONFI parameter recovery failed, aborting\n");
5198 goto free_onfi_param_page;
5199 }
Brian Norrisc7f23a72013-08-13 10:51:55 -07005200 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005201
Chris Packham00ce4e02018-06-25 10:44:44 +12005202 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5203 chip->manufacturer.desc->ops->fixup_onfi_param_page)
5204 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5205
Brian Norris8b6e50c2011-05-25 14:59:01 -07005206 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005207 val = le16_to_cpu(p->revision);
Chris Packham872b71f2018-06-25 10:44:45 +12005208 if (val & ONFI_VERSION_2_3)
Miquel Raynala97421c2018-03-19 14:47:27 +01005209 chip->parameters.onfi.version = 23;
Chris Packham872b71f2018-06-25 10:44:45 +12005210 else if (val & ONFI_VERSION_2_2)
Miquel Raynala97421c2018-03-19 14:47:27 +01005211 chip->parameters.onfi.version = 22;
Chris Packham872b71f2018-06-25 10:44:45 +12005212 else if (val & ONFI_VERSION_2_1)
Miquel Raynala97421c2018-03-19 14:47:27 +01005213 chip->parameters.onfi.version = 21;
Chris Packham872b71f2018-06-25 10:44:45 +12005214 else if (val & ONFI_VERSION_2_0)
Miquel Raynala97421c2018-03-19 14:47:27 +01005215 chip->parameters.onfi.version = 20;
Chris Packham872b71f2018-06-25 10:44:45 +12005216 else if (val & ONFI_VERSION_1_0)
Miquel Raynala97421c2018-03-19 14:47:27 +01005217 chip->parameters.onfi.version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005218
Miquel Raynala97421c2018-03-19 14:47:27 +01005219 if (!chip->parameters.onfi.version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005220 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005221 goto free_onfi_param_page;
5222 } else {
5223 ret = 1;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005224 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005225
5226 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5227 sanitize_string(p->model, sizeof(p->model));
Miquel Raynalf4531b22018-03-19 14:47:26 +01005228 strncpy(chip->parameters.model, p->model,
5229 sizeof(chip->parameters.model) - 1);
Brian Norris4355b702013-08-27 18:45:10 -07005230
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005231 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005232
5233 /*
5234 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5235 * (don't ask me who thought of this...). MTD assumes that these
5236 * dimensions will be power-of-2, so just truncate the remaining area.
5237 */
5238 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5239 mtd->erasesize *= mtd->writesize;
5240
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005241 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005242
5243 /* See erasesize comment */
5244 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005245 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005246 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005247
Zach Brown34da5f52017-01-10 13:30:21 -06005248 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5249 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5250
Miquel Raynala97421c2018-03-19 14:47:27 +01005251 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005252 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005253
Huang Shijie10c86ba2013-05-17 11:17:26 +08005254 if (p->ecc_bits != 0xff) {
5255 chip->ecc_strength_ds = p->ecc_bits;
5256 chip->ecc_step_ds = 512;
Miquel Raynala97421c2018-03-19 14:47:27 +01005257 } else if (chip->parameters.onfi.version >= 21 &&
5258 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005259
5260 /*
5261 * The nand_flash_detect_ext_param_page() uses the
5262 * Change Read Column command which maybe not supported
5263 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5264 * now. We do not replace user supplied command function.
5265 */
5266 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5267 chip->cmdfunc = nand_command_lp;
5268
5269 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005270 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005271 pr_warn("Failed to detect ONFI extended param page\n");
5272 } else {
5273 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005274 }
5275
Miquel Raynalf4531b22018-03-19 14:47:26 +01005276 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005277 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005278 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005279 bitmap_set(chip->parameters.get_feature_list,
5280 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5281 bitmap_set(chip->parameters.set_feature_list,
5282 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5283 }
Miquel Raynala97421c2018-03-19 14:47:27 +01005284 chip->parameters.onfi.tPROG = le16_to_cpu(p->t_prog);
5285 chip->parameters.onfi.tBERS = le16_to_cpu(p->t_bers);
5286 chip->parameters.onfi.tR = le16_to_cpu(p->t_r);
5287 chip->parameters.onfi.tCCS = le16_to_cpu(p->t_ccs);
5288 chip->parameters.onfi.async_timing_mode =
5289 le16_to_cpu(p->async_timing_mode);
5290 chip->parameters.onfi.vendor_revision =
5291 le16_to_cpu(p->vendor_revision);
5292 memcpy(chip->parameters.onfi.vendor, p->vendor,
5293 sizeof(p->vendor));
Miquel Raynalf4531b22018-03-19 14:47:26 +01005294
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005295free_onfi_param_page:
5296 kfree(p);
5297 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005298}
5299
5300/*
Huang Shijie91361812014-02-21 13:39:40 +08005301 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5302 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005303static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005304{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005305 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005306 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005307 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005308 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005309 char id[5];
5310 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005311
5312 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005313 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5314 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005315 return 0;
5316
Miquel Raynal480139d2018-03-19 14:47:30 +01005317 /* JEDEC chip: allocate a buffer to hold its parameter page */
5318 p = kzalloc(sizeof(*p), GFP_KERNEL);
5319 if (!p)
5320 return -ENOMEM;
5321
Boris Brezillon97d90da2017-11-30 18:01:29 +01005322 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005323 if (ret) {
5324 ret = 0;
5325 goto free_jedec_param_page;
5326 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005327
Huang Shijie91361812014-02-21 13:39:40 +08005328 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005329 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005330 if (ret) {
5331 ret = 0;
5332 goto free_jedec_param_page;
5333 }
Huang Shijie91361812014-02-21 13:39:40 +08005334
5335 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5336 le16_to_cpu(p->crc))
5337 break;
5338 }
5339
5340 if (i == 3) {
5341 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005342 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005343 }
5344
5345 /* Check version */
5346 val = le16_to_cpu(p->revision);
5347 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005348 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005349 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005350 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005351
Miquel Raynal480139d2018-03-19 14:47:30 +01005352 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005353 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005354 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005355 }
5356
5357 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5358 sanitize_string(p->model, sizeof(p->model));
Miquel Raynalf4531b22018-03-19 14:47:26 +01005359 strncpy(chip->parameters.model, p->model,
5360 sizeof(chip->parameters.model) - 1);
Huang Shijie91361812014-02-21 13:39:40 +08005361
5362 mtd->writesize = le32_to_cpu(p->byte_per_page);
5363
5364 /* Please reference to the comment for nand_flash_detect_onfi. */
5365 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5366 mtd->erasesize *= mtd->writesize;
5367
5368 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5369
5370 /* Please reference to the comment for nand_flash_detect_onfi. */
5371 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5372 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5373 chip->bits_per_cell = p->bits_per_cell;
5374
Miquel Raynal480139d2018-03-19 14:47:30 +01005375 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005376 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005377
5378 /* ECC info */
5379 ecc = &p->ecc_info[0];
5380
5381 if (ecc->codeword_size >= 9) {
5382 chip->ecc_strength_ds = ecc->ecc_bits;
5383 chip->ecc_step_ds = 1 << ecc->codeword_size;
5384 } else {
5385 pr_warn("Invalid codeword size\n");
5386 }
5387
Miquel Raynal480139d2018-03-19 14:47:30 +01005388free_jedec_param_page:
5389 kfree(p);
5390 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005391}
5392
5393/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005394 * nand_id_has_period - Check if an ID string has a given wraparound period
5395 * @id_data: the ID string
5396 * @arrlen: the length of the @id_data array
5397 * @period: the period of repitition
5398 *
5399 * Check if an ID string is repeated within a given sequence of bytes at
5400 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005401 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005402 * if the repetition has a period of @period; otherwise, returns zero.
5403 */
5404static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5405{
5406 int i, j;
5407 for (i = 0; i < period; i++)
5408 for (j = i + period; j < arrlen; j += period)
5409 if (id_data[i] != id_data[j])
5410 return 0;
5411 return 1;
5412}
5413
5414/*
5415 * nand_id_len - Get the length of an ID string returned by CMD_READID
5416 * @id_data: the ID string
5417 * @arrlen: the length of the @id_data array
5418
5419 * Returns the length of the ID string, according to known wraparound/trailing
5420 * zero patterns. If no pattern exists, returns the length of the array.
5421 */
5422static int nand_id_len(u8 *id_data, int arrlen)
5423{
5424 int last_nonzero, period;
5425
5426 /* Find last non-zero byte */
5427 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5428 if (id_data[last_nonzero])
5429 break;
5430
5431 /* All zeros */
5432 if (last_nonzero < 0)
5433 return 0;
5434
5435 /* Calculate wraparound period */
5436 for (period = 1; period < arrlen; period++)
5437 if (nand_id_has_period(id_data, arrlen, period))
5438 break;
5439
5440 /* There's a repeated pattern */
5441 if (period < arrlen)
5442 return period;
5443
5444 /* There are trailing zeros */
5445 if (last_nonzero < arrlen - 1)
5446 return last_nonzero + 1;
5447
5448 /* No pattern detected */
5449 return arrlen;
5450}
5451
Huang Shijie7db906b2013-09-25 14:58:11 +08005452/* Extract the bits of per cell from the 3rd byte of the extended ID */
5453static int nand_get_bits_per_cell(u8 cellinfo)
5454{
5455 int bits;
5456
5457 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5458 bits >>= NAND_CI_CELLTYPE_SHIFT;
5459 return bits + 1;
5460}
5461
Brian Norrise3b88bd2012-09-24 20:40:52 -07005462/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005463 * Many new NAND share similar device ID codes, which represent the size of the
5464 * chip. The rest of the parameters must be decoded according to generic or
5465 * manufacturer-specific "extended ID" decoding patterns.
5466 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005467void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005468{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005469 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005470 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005471 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005472 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005473 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005474 /* The 4th id byte is the important one */
5475 extid = id_data[3];
5476
Boris Brezillon01389b62016-06-08 10:30:18 +02005477 /* Calc pagesize */
5478 mtd->writesize = 1024 << (extid & 0x03);
5479 extid >>= 2;
5480 /* Calc oobsize */
5481 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5482 extid >>= 2;
5483 /* Calc blocksize. Blocksize is multiples of 64KiB */
5484 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5485 extid >>= 2;
5486 /* Get buswidth information */
5487 if (extid & 0x1)
5488 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005489}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005490EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005491
5492/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005493 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5494 * decodes a matching ID table entry and assigns the MTD size parameters for
5495 * the chip.
5496 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005497static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005498{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005499 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005500
5501 mtd->erasesize = type->erasesize;
5502 mtd->writesize = type->pagesize;
5503 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005504
Huang Shijie1c195e92013-09-25 14:58:12 +08005505 /* All legacy ID NAND are small-page, SLC */
5506 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005507}
5508
5509/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005510 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5511 * heuristic patterns using various detected parameters (e.g., manufacturer,
5512 * page size, cell-type information).
5513 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005514static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005515{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005516 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005517
5518 /* Set the bad block position */
5519 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5520 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5521 else
5522 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005523}
5524
Huang Shijieec6e87e2013-03-15 11:01:00 +08005525static inline bool is_full_id_nand(struct nand_flash_dev *type)
5526{
5527 return type->id_len;
5528}
5529
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005530static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005531 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005532{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005533 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005534 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005535
Huang Shijieec6e87e2013-03-15 11:01:00 +08005536 if (!strncmp(type->id, id_data, type->id_len)) {
5537 mtd->writesize = type->pagesize;
5538 mtd->erasesize = type->erasesize;
5539 mtd->oobsize = type->oobsize;
5540
Huang Shijie7db906b2013-09-25 14:58:11 +08005541 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005542 chip->chipsize = (uint64_t)type->chipsize << 20;
5543 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005544 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5545 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005546 chip->onfi_timing_mode_default =
5547 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005548
Miquel Raynalf4531b22018-03-19 14:47:26 +01005549 strncpy(chip->parameters.model, type->name,
5550 sizeof(chip->parameters.model) - 1);
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005551
Huang Shijieec6e87e2013-03-15 11:01:00 +08005552 return true;
5553 }
5554 return false;
5555}
5556
Brian Norris7e74c2d2012-09-24 20:40:49 -07005557/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005558 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5559 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5560 * table.
5561 */
5562static void nand_manufacturer_detect(struct nand_chip *chip)
5563{
5564 /*
5565 * Try manufacturer detection if available and use
5566 * nand_decode_ext_id() otherwise.
5567 */
5568 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005569 chip->manufacturer.desc->ops->detect) {
5570 /* The 3rd id byte holds MLC / multichip data */
5571 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005572 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005573 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005574 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005575 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005576}
5577
5578/*
5579 * Manufacturer initialization. This function is called for all NANDs including
5580 * ONFI and JEDEC compliant ones.
5581 * Manufacturer drivers should put all their specific initialization code in
5582 * their ->init() hook.
5583 */
5584static int nand_manufacturer_init(struct nand_chip *chip)
5585{
5586 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5587 !chip->manufacturer.desc->ops->init)
5588 return 0;
5589
5590 return chip->manufacturer.desc->ops->init(chip);
5591}
5592
5593/*
5594 * Manufacturer cleanup. This function is called for all NANDs including
5595 * ONFI and JEDEC compliant ones.
5596 * Manufacturer drivers should put all their specific cleanup code in their
5597 * ->cleanup() hook.
5598 */
5599static void nand_manufacturer_cleanup(struct nand_chip *chip)
5600{
5601 /* Release manufacturer private data */
5602 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5603 chip->manufacturer.desc->ops->cleanup)
5604 chip->manufacturer.desc->ops->cleanup(chip);
5605}
5606
5607/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005608 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005609 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005610static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005611{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005612 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005613 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005614 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005615 u8 *id_data = chip->id.data;
5616 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
Karl Beldanef89a882008-09-15 14:37:29 +02005618 /*
5619 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005620 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005621 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005622 ret = nand_reset(chip, 0);
5623 if (ret)
5624 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005625
5626 /* Select the device */
5627 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005628
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005630 ret = nand_readid_op(chip, 0, id_data, 2);
5631 if (ret)
5632 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
5634 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005635 maf_id = id_data[0];
5636 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
Brian Norris8b6e50c2011-05-25 14:59:01 -07005638 /*
5639 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005640 * interface concerns can cause random data which looks like a
5641 * possibly credible NAND flash to appear. If the two results do
5642 * not match, ignore the device completely.
5643 */
5644
Brian Norris4aef9b72012-09-24 20:40:48 -07005645 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005646 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5647 if (ret)
5648 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005649
Boris Brezillon7f501f02016-05-24 19:20:05 +02005650 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005651 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005652 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005653 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005654 }
5655
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005656 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005657
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005658 /* Try to identify manufacturer */
5659 manufacturer = nand_get_manufacturer(maf_id);
5660 chip->manufacturer.desc = manufacturer;
5661
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005662 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005663 type = nand_flash_ids;
5664
Boris Brezillon29a198a2016-05-24 20:17:48 +02005665 /*
5666 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5667 * override it.
5668 * This is required to make sure initial NAND bus width set by the
5669 * NAND controller driver is coherent with the real NAND bus width
5670 * (extracted by auto-detection code).
5671 */
5672 busw = chip->options & NAND_BUSWIDTH_16;
5673
5674 /*
5675 * The flag is only set (never cleared), reset it to its default value
5676 * before starting auto-detection.
5677 */
5678 chip->options &= ~NAND_BUSWIDTH_16;
5679
Huang Shijieec6e87e2013-03-15 11:01:00 +08005680 for (; type->name != NULL; type++) {
5681 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005682 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005683 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005684 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005685 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005686 }
5687 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005688
Miquel Raynala97421c2018-03-19 14:47:27 +01005689 chip->parameters.onfi.version = 0;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005690 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005691 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005692 ret = nand_flash_detect_onfi(chip);
5693 if (ret < 0)
5694 return ret;
5695 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005696 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005697
5698 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005699 ret = nand_flash_detect_jedec(chip);
5700 if (ret < 0)
5701 return ret;
5702 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005703 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005704 }
5705
David Woodhouse5e81e882010-02-26 18:32:56 +00005706 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005707 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005708
Miquel Raynalf4531b22018-03-19 14:47:26 +01005709 strncpy(chip->parameters.model, type->name,
5710 sizeof(chip->parameters.model) - 1);
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005711
Adrian Hunter69423d92008-12-10 13:37:21 +00005712 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005713
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005714 if (!type->pagesize)
5715 nand_manufacturer_detect(chip);
5716 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005717 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005718
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005719 /* Get chip options */
5720 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005721
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005722ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005723 if (!mtd->name)
5724 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005725
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005726 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005727 WARN_ON(busw & NAND_BUSWIDTH_16);
5728 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005729 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5730 /*
5731 * Check, if buswidth is correct. Hardware drivers should set
5732 * chip correct!
5733 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005734 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005735 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005736 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5737 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005738 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5739 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005740 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005741 }
5742
Boris Brezillon7f501f02016-05-24 19:20:05 +02005743 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005744
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005745 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005746 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005747 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005748 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005749
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005750 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005751 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005752 if (chip->chipsize & 0xffffffff)
5753 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005754 else {
5755 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5756 chip->chip_shift += 32 - 1;
5757 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005758
Masahiro Yamada14157f82017-09-13 11:05:50 +09005759 if (chip->chip_shift - chip->page_shift > 16)
5760 chip->options |= NAND_ROW_ADDR_3;
5761
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005762 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005763 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005764
Brian Norris8b6e50c2011-05-25 14:59:01 -07005765 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005766 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5767 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005768
Ezequiel Garcia20171642013-11-25 08:30:31 -03005769 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005770 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005771 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5772 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005773 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005774 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005775 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005776 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005777}
5778
Boris Brezillond48f62b2016-04-01 14:54:32 +02005779static const char * const nand_ecc_modes[] = {
5780 [NAND_ECC_NONE] = "none",
5781 [NAND_ECC_SOFT] = "soft",
5782 [NAND_ECC_HW] = "hw",
5783 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5784 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005785 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005786};
5787
5788static int of_get_nand_ecc_mode(struct device_node *np)
5789{
5790 const char *pm;
5791 int err, i;
5792
5793 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5794 if (err < 0)
5795 return err;
5796
5797 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5798 if (!strcasecmp(pm, nand_ecc_modes[i]))
5799 return i;
5800
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005801 /*
5802 * For backward compatibility we support few obsoleted values that don't
5803 * have their mappings into nand_ecc_modes_t anymore (they were merged
5804 * with other enums).
5805 */
5806 if (!strcasecmp(pm, "soft_bch"))
5807 return NAND_ECC_SOFT;
5808
Boris Brezillond48f62b2016-04-01 14:54:32 +02005809 return -ENODEV;
5810}
5811
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005812static const char * const nand_ecc_algos[] = {
5813 [NAND_ECC_HAMMING] = "hamming",
5814 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02005815 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005816};
5817
Boris Brezillond48f62b2016-04-01 14:54:32 +02005818static int of_get_nand_ecc_algo(struct device_node *np)
5819{
5820 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005821 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005822
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005823 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5824 if (!err) {
5825 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5826 if (!strcasecmp(pm, nand_ecc_algos[i]))
5827 return i;
5828 return -ENODEV;
5829 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005830
5831 /*
5832 * For backward compatibility we also read "nand-ecc-mode" checking
5833 * for some obsoleted values that were specifying ECC algorithm.
5834 */
5835 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5836 if (err < 0)
5837 return err;
5838
5839 if (!strcasecmp(pm, "soft"))
5840 return NAND_ECC_HAMMING;
5841 else if (!strcasecmp(pm, "soft_bch"))
5842 return NAND_ECC_BCH;
5843
5844 return -ENODEV;
5845}
5846
5847static int of_get_nand_ecc_step_size(struct device_node *np)
5848{
5849 int ret;
5850 u32 val;
5851
5852 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5853 return ret ? ret : val;
5854}
5855
5856static int of_get_nand_ecc_strength(struct device_node *np)
5857{
5858 int ret;
5859 u32 val;
5860
5861 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5862 return ret ? ret : val;
5863}
5864
5865static int of_get_nand_bus_width(struct device_node *np)
5866{
5867 u32 val;
5868
5869 if (of_property_read_u32(np, "nand-bus-width", &val))
5870 return 8;
5871
5872 switch (val) {
5873 case 8:
5874 case 16:
5875 return val;
5876 default:
5877 return -EIO;
5878 }
5879}
5880
5881static bool of_get_nand_on_flash_bbt(struct device_node *np)
5882{
5883 return of_property_read_bool(np, "nand-on-flash-bbt");
5884}
5885
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005886static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005887{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005888 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005889 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005890
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005891 if (!dn)
5892 return 0;
5893
Brian Norris5844fee2015-01-23 00:22:27 -08005894 if (of_get_nand_bus_width(dn) == 16)
5895 chip->options |= NAND_BUSWIDTH_16;
5896
Stefan Agnerf922bd72018-06-24 23:27:23 +02005897 if (of_property_read_bool(dn, "nand-is-boot-medium"))
5898 chip->options |= NAND_IS_BOOT_MEDIUM;
5899
Brian Norris5844fee2015-01-23 00:22:27 -08005900 if (of_get_nand_on_flash_bbt(dn))
5901 chip->bbt_options |= NAND_BBT_USE_FLASH;
5902
5903 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005904 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005905 ecc_strength = of_get_nand_ecc_strength(dn);
5906 ecc_step = of_get_nand_ecc_step_size(dn);
5907
Brian Norris5844fee2015-01-23 00:22:27 -08005908 if (ecc_mode >= 0)
5909 chip->ecc.mode = ecc_mode;
5910
Rafał Miłecki79082452016-03-23 11:19:02 +01005911 if (ecc_algo >= 0)
5912 chip->ecc.algo = ecc_algo;
5913
Brian Norris5844fee2015-01-23 00:22:27 -08005914 if (ecc_strength >= 0)
5915 chip->ecc.strength = ecc_strength;
5916
5917 if (ecc_step > 0)
5918 chip->ecc.size = ecc_step;
5919
Boris Brezillonba78ee02016-06-08 17:04:22 +02005920 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5921 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5922
Brian Norris5844fee2015-01-23 00:22:27 -08005923 return 0;
5924}
5925
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005926/**
David Woodhouse3b85c322006-09-25 17:06:53 +01005927 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005928 * @mtd: MTD device structure
5929 * @maxchips: number of chips to scan for
5930 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005931 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005932 * This is the first phase of the normal nand_scan() function. It reads the
5933 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005934 *
5935 */
David Woodhouse5e81e882010-02-26 18:32:56 +00005936int nand_scan_ident(struct mtd_info *mtd, int maxchips,
5937 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005938{
Cai Zhiyongbb770822013-12-25 20:11:15 +08005939 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01005940 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08005941 int ret;
5942
Miquel Raynal17fa8042017-11-30 18:01:31 +01005943 /* Enforce the right timings for reset/detection */
5944 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5945
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005946 ret = nand_dt_init(chip);
5947 if (ret)
5948 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005949
Brian Norrisf7a8e382016-01-05 10:39:45 -08005950 if (!mtd->name && mtd->dev.parent)
5951 mtd->name = dev_name(mtd->dev.parent);
5952
Miquel Raynal8878b122017-11-09 14:16:45 +01005953 /*
5954 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5955 * populated.
5956 */
5957 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005958 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005959 * Default functions assigned for ->cmdfunc() and
5960 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005961 */
Miquel Raynal8878b122017-11-09 14:16:45 +01005962 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5963 pr_err("->cmd_ctrl() should be provided\n");
5964 return -EINVAL;
5965 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005966 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005967
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005968 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005969 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005970
5971 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005972 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005973 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005974 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005975 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005976 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005977 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 }
5979
Boris Brezillon7f501f02016-05-24 19:20:05 +02005980 nand_maf_id = chip->id.data[0];
5981 nand_dev_id = chip->id.data[1];
5982
Huang Shijie07300162012-11-09 16:23:45 +08005983 chip->select_chip(mtd, -1);
5984
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005985 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005986 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005987 u8 id[2];
5988
Karl Beldanef89a882008-09-15 14:37:29 +02005989 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005990 nand_reset(chip, i);
5991
5992 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005994 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005996 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08005997 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 break;
Huang Shijie07300162012-11-09 16:23:45 +08005999 }
6000 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 }
6002 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03006003 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006004
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006006 chip->numchips = i;
6007 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
David Woodhouse3b85c322006-09-25 17:06:53 +01006009 return 0;
6010}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006011EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01006012
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006013static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
6014{
6015 struct nand_chip *chip = mtd_to_nand(mtd);
6016 struct nand_ecc_ctrl *ecc = &chip->ecc;
6017
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006018 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006019 return -EINVAL;
6020
6021 switch (ecc->algo) {
6022 case NAND_ECC_HAMMING:
6023 ecc->calculate = nand_calculate_ecc;
6024 ecc->correct = nand_correct_data;
6025 ecc->read_page = nand_read_page_swecc;
6026 ecc->read_subpage = nand_read_subpage;
6027 ecc->write_page = nand_write_page_swecc;
6028 ecc->read_page_raw = nand_read_page_raw;
6029 ecc->write_page_raw = nand_write_page_raw;
6030 ecc->read_oob = nand_read_oob_std;
6031 ecc->write_oob = nand_write_oob_std;
6032 if (!ecc->size)
6033 ecc->size = 256;
6034 ecc->bytes = 3;
6035 ecc->strength = 1;
6036 return 0;
6037 case NAND_ECC_BCH:
6038 if (!mtd_nand_has_bch()) {
6039 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
6040 return -EINVAL;
6041 }
6042 ecc->calculate = nand_bch_calculate_ecc;
6043 ecc->correct = nand_bch_correct_data;
6044 ecc->read_page = nand_read_page_swecc;
6045 ecc->read_subpage = nand_read_subpage;
6046 ecc->write_page = nand_write_page_swecc;
6047 ecc->read_page_raw = nand_read_page_raw;
6048 ecc->write_page_raw = nand_write_page_raw;
6049 ecc->read_oob = nand_read_oob_std;
6050 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02006051
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006052 /*
6053 * Board driver should supply ecc.size and ecc.strength
6054 * values to select how many bits are correctable.
6055 * Otherwise, default to 4 bits for large page devices.
6056 */
6057 if (!ecc->size && (mtd->oobsize >= 64)) {
6058 ecc->size = 512;
6059 ecc->strength = 4;
6060 }
6061
6062 /*
6063 * if no ecc placement scheme was provided pickup the default
6064 * large page one.
6065 */
6066 if (!mtd->ooblayout) {
6067 /* handle large page devices only */
6068 if (mtd->oobsize < 64) {
6069 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6070 return -EINVAL;
6071 }
6072
6073 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02006074
6075 }
6076
6077 /*
6078 * We can only maximize ECC config when the default layout is
6079 * used, otherwise we don't know how many bytes can really be
6080 * used.
6081 */
6082 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6083 ecc->options & NAND_ECC_MAXIMIZE) {
6084 int steps, bytes;
6085
6086 /* Always prefer 1k blocks over 512bytes ones */
6087 ecc->size = 1024;
6088 steps = mtd->writesize / ecc->size;
6089
6090 /* Reserve 2 bytes for the BBM */
6091 bytes = (mtd->oobsize - 2) / steps;
6092 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006093 }
6094
6095 /* See nand_bch_init() for details. */
6096 ecc->bytes = 0;
6097 ecc->priv = nand_bch_init(mtd);
6098 if (!ecc->priv) {
6099 WARN(1, "BCH ECC initialization failed!\n");
6100 return -EINVAL;
6101 }
6102 return 0;
6103 default:
6104 WARN(1, "Unsupported ECC algorithm!\n");
6105 return -EINVAL;
6106 }
6107}
6108
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006109/**
6110 * nand_check_ecc_caps - check the sanity of preset ECC settings
6111 * @chip: nand chip info structure
6112 * @caps: ECC caps info structure
6113 * @oobavail: OOB size that the ECC engine can use
6114 *
6115 * When ECC step size and strength are already set, check if they are supported
6116 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6117 * On success, the calculated ECC bytes is set.
6118 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306119static int
6120nand_check_ecc_caps(struct nand_chip *chip,
6121 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006122{
6123 struct mtd_info *mtd = nand_to_mtd(chip);
6124 const struct nand_ecc_step_info *stepinfo;
6125 int preset_step = chip->ecc.size;
6126 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306127 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006128 int i, j;
6129
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006130 for (i = 0; i < caps->nstepinfos; i++) {
6131 stepinfo = &caps->stepinfos[i];
6132
6133 if (stepinfo->stepsize != preset_step)
6134 continue;
6135
6136 for (j = 0; j < stepinfo->nstrengths; j++) {
6137 if (stepinfo->strengths[j] != preset_strength)
6138 continue;
6139
6140 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6141 preset_strength);
6142 if (WARN_ON_ONCE(ecc_bytes < 0))
6143 return ecc_bytes;
6144
6145 if (ecc_bytes * nsteps > oobavail) {
6146 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6147 preset_step, preset_strength);
6148 return -ENOSPC;
6149 }
6150
6151 chip->ecc.bytes = ecc_bytes;
6152
6153 return 0;
6154 }
6155 }
6156
6157 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6158 preset_step, preset_strength);
6159
6160 return -ENOTSUPP;
6161}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006162
6163/**
6164 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6165 * @chip: nand chip info structure
6166 * @caps: ECC engine caps info structure
6167 * @oobavail: OOB size that the ECC engine can use
6168 *
6169 * If a chip's ECC requirement is provided, try to meet it with the least
6170 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6171 * On success, the chosen ECC settings are set.
6172 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306173static int
6174nand_match_ecc_req(struct nand_chip *chip,
6175 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006176{
6177 struct mtd_info *mtd = nand_to_mtd(chip);
6178 const struct nand_ecc_step_info *stepinfo;
6179 int req_step = chip->ecc_step_ds;
6180 int req_strength = chip->ecc_strength_ds;
6181 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6182 int best_step, best_strength, best_ecc_bytes;
6183 int best_ecc_bytes_total = INT_MAX;
6184 int i, j;
6185
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006186 /* No information provided by the NAND chip */
6187 if (!req_step || !req_strength)
6188 return -ENOTSUPP;
6189
6190 /* number of correctable bits the chip requires in a page */
6191 req_corr = mtd->writesize / req_step * req_strength;
6192
6193 for (i = 0; i < caps->nstepinfos; i++) {
6194 stepinfo = &caps->stepinfos[i];
6195 step_size = stepinfo->stepsize;
6196
6197 for (j = 0; j < stepinfo->nstrengths; j++) {
6198 strength = stepinfo->strengths[j];
6199
6200 /*
6201 * If both step size and strength are smaller than the
6202 * chip's requirement, it is not easy to compare the
6203 * resulted reliability.
6204 */
6205 if (step_size < req_step && strength < req_strength)
6206 continue;
6207
6208 if (mtd->writesize % step_size)
6209 continue;
6210
6211 nsteps = mtd->writesize / step_size;
6212
6213 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6214 if (WARN_ON_ONCE(ecc_bytes < 0))
6215 continue;
6216 ecc_bytes_total = ecc_bytes * nsteps;
6217
6218 if (ecc_bytes_total > oobavail ||
6219 strength * nsteps < req_corr)
6220 continue;
6221
6222 /*
6223 * We assume the best is to meet the chip's requrement
6224 * with the least number of ECC bytes.
6225 */
6226 if (ecc_bytes_total < best_ecc_bytes_total) {
6227 best_ecc_bytes_total = ecc_bytes_total;
6228 best_step = step_size;
6229 best_strength = strength;
6230 best_ecc_bytes = ecc_bytes;
6231 }
6232 }
6233 }
6234
6235 if (best_ecc_bytes_total == INT_MAX)
6236 return -ENOTSUPP;
6237
6238 chip->ecc.size = best_step;
6239 chip->ecc.strength = best_strength;
6240 chip->ecc.bytes = best_ecc_bytes;
6241
6242 return 0;
6243}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006244
6245/**
6246 * nand_maximize_ecc - choose the max ECC strength available
6247 * @chip: nand chip info structure
6248 * @caps: ECC engine caps info structure
6249 * @oobavail: OOB size that the ECC engine can use
6250 *
6251 * Choose the max ECC strength that is supported on the controller, and can fit
6252 * within the chip's OOB. On success, the chosen ECC settings are set.
6253 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306254static int
6255nand_maximize_ecc(struct nand_chip *chip,
6256 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006257{
6258 struct mtd_info *mtd = nand_to_mtd(chip);
6259 const struct nand_ecc_step_info *stepinfo;
6260 int step_size, strength, nsteps, ecc_bytes, corr;
6261 int best_corr = 0;
6262 int best_step = 0;
6263 int best_strength, best_ecc_bytes;
6264 int i, j;
6265
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006266 for (i = 0; i < caps->nstepinfos; i++) {
6267 stepinfo = &caps->stepinfos[i];
6268 step_size = stepinfo->stepsize;
6269
6270 /* If chip->ecc.size is already set, respect it */
6271 if (chip->ecc.size && step_size != chip->ecc.size)
6272 continue;
6273
6274 for (j = 0; j < stepinfo->nstrengths; j++) {
6275 strength = stepinfo->strengths[j];
6276
6277 if (mtd->writesize % step_size)
6278 continue;
6279
6280 nsteps = mtd->writesize / step_size;
6281
6282 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6283 if (WARN_ON_ONCE(ecc_bytes < 0))
6284 continue;
6285
6286 if (ecc_bytes * nsteps > oobavail)
6287 continue;
6288
6289 corr = strength * nsteps;
6290
6291 /*
6292 * If the number of correctable bits is the same,
6293 * bigger step_size has more reliability.
6294 */
6295 if (corr > best_corr ||
6296 (corr == best_corr && step_size > best_step)) {
6297 best_corr = corr;
6298 best_step = step_size;
6299 best_strength = strength;
6300 best_ecc_bytes = ecc_bytes;
6301 }
6302 }
6303 }
6304
6305 if (!best_corr)
6306 return -ENOTSUPP;
6307
6308 chip->ecc.size = best_step;
6309 chip->ecc.strength = best_strength;
6310 chip->ecc.bytes = best_ecc_bytes;
6311
6312 return 0;
6313}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006314
Abhishek Sahu181ace92018-06-20 12:57:28 +05306315/**
6316 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
6317 * @chip: nand chip info structure
6318 * @caps: ECC engine caps info structure
6319 * @oobavail: OOB size that the ECC engine can use
6320 *
6321 * Choose the ECC configuration according to following logic
6322 *
6323 * 1. If both ECC step size and ECC strength are already set (usually by DT)
6324 * then check if it is supported by this controller.
6325 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
6326 * 3. Otherwise, try to match the ECC step size and ECC strength closest
6327 * to the chip's requirement. If available OOB size can't fit the chip
6328 * requirement then fallback to the maximum ECC step size and ECC strength.
6329 *
6330 * On success, the chosen ECC settings are set.
6331 */
6332int nand_ecc_choose_conf(struct nand_chip *chip,
6333 const struct nand_ecc_caps *caps, int oobavail)
6334{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306335 struct mtd_info *mtd = nand_to_mtd(chip);
6336
6337 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
6338 return -EINVAL;
6339
Abhishek Sahu181ace92018-06-20 12:57:28 +05306340 if (chip->ecc.size && chip->ecc.strength)
6341 return nand_check_ecc_caps(chip, caps, oobavail);
6342
6343 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
6344 return nand_maximize_ecc(chip, caps, oobavail);
6345
6346 if (!nand_match_ecc_req(chip, caps, oobavail))
6347 return 0;
6348
6349 return nand_maximize_ecc(chip, caps, oobavail);
6350}
6351EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
6352
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006353/*
6354 * Check if the chip configuration meet the datasheet requirements.
6355
6356 * If our configuration corrects A bits per B bytes and the minimum
6357 * required correction level is X bits per Y bytes, then we must ensure
6358 * both of the following are true:
6359 *
6360 * (1) A / B >= X / Y
6361 * (2) A >= X
6362 *
6363 * Requirement (1) ensures we can correct for the required bitflip density.
6364 * Requirement (2) ensures we can correct even when all bitflips are clumped
6365 * in the same sector.
6366 */
6367static bool nand_ecc_strength_good(struct mtd_info *mtd)
6368{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006369 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006370 struct nand_ecc_ctrl *ecc = &chip->ecc;
6371 int corr, ds_corr;
6372
6373 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6374 /* Not enough information */
6375 return true;
6376
6377 /*
6378 * We get the number of corrected bits per page to compare
6379 * the correction density.
6380 */
6381 corr = (mtd->writesize * ecc->strength) / ecc->size;
6382 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6383
6384 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6385}
David Woodhouse3b85c322006-09-25 17:06:53 +01006386
6387/**
6388 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006389 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01006390 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006391 * This is the second phase of the normal nand_scan() function. It fills out
6392 * all the uninitialized function pointers with the defaults and scans for a
6393 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006394 */
6395int nand_scan_tail(struct mtd_info *mtd)
6396{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006397 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08006398 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006399 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006400
Brian Norrise2414f42012-02-06 13:44:00 -08006401 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006402 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006403 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006404 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006405 }
Brian Norrise2414f42012-02-06 13:44:00 -08006406
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006407 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006408 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006409 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006410
Boris Brezillonf84674b2017-06-02 12:18:24 +02006411 /*
6412 * FIXME: some NAND manufacturer drivers expect the first die to be
6413 * selected when manufacturer->init() is called. They should be fixed
6414 * to explictly select the relevant die when interacting with the NAND
6415 * chip.
6416 */
6417 chip->select_chip(mtd, 0);
6418 ret = nand_manufacturer_init(chip);
6419 chip->select_chip(mtd, -1);
6420 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006421 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006422
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006423 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006424 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006425
6426 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006427 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006428 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006429 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006430 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006431 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006434 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 break;
6436 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006437 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006438 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006441 /*
6442 * Expose the whole OOB area to users if ECC_NONE
6443 * is passed. We could do that for all kind of
6444 * ->oobsize, but we must keep the old large/small
6445 * page with ECC layout when ->oobsize <= 128 for
6446 * compatibility reasons.
6447 */
6448 if (ecc->mode == NAND_ECC_NONE) {
6449 mtd_set_ooblayout(mtd,
6450 &nand_ooblayout_lp_ops);
6451 break;
6452 }
6453
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006454 WARN(1, "No oob scheme defined for oobsize %d\n",
6455 mtd->oobsize);
6456 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006457 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 }
6459 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006460
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006461 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006462 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006463 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006464 */
David Woodhouse956e9442006-09-25 17:12:39 +01006465
Huang Shijie97de79e02013-10-18 14:20:53 +08006466 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006467 case NAND_ECC_HW_OOB_FIRST:
6468 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006469 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006470 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6471 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006472 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006473 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006474 if (!ecc->read_page)
6475 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006476
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006477 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006478 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006479 if (!ecc->read_page)
6480 ecc->read_page = nand_read_page_hwecc;
6481 if (!ecc->write_page)
6482 ecc->write_page = nand_write_page_hwecc;
6483 if (!ecc->read_page_raw)
6484 ecc->read_page_raw = nand_read_page_raw;
6485 if (!ecc->write_page_raw)
6486 ecc->write_page_raw = nand_write_page_raw;
6487 if (!ecc->read_oob)
6488 ecc->read_oob = nand_read_oob_std;
6489 if (!ecc->write_oob)
6490 ecc->write_oob = nand_write_oob_std;
6491 if (!ecc->read_subpage)
6492 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006493 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006494 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006495
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006496 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006497 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6498 (!ecc->read_page ||
6499 ecc->read_page == nand_read_page_hwecc ||
6500 !ecc->write_page ||
6501 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006502 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6503 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006504 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006505 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006506 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006507 if (!ecc->read_page)
6508 ecc->read_page = nand_read_page_syndrome;
6509 if (!ecc->write_page)
6510 ecc->write_page = nand_write_page_syndrome;
6511 if (!ecc->read_page_raw)
6512 ecc->read_page_raw = nand_read_page_raw_syndrome;
6513 if (!ecc->write_page_raw)
6514 ecc->write_page_raw = nand_write_page_raw_syndrome;
6515 if (!ecc->read_oob)
6516 ecc->read_oob = nand_read_oob_syndrome;
6517 if (!ecc->write_oob)
6518 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006519
Huang Shijie97de79e02013-10-18 14:20:53 +08006520 if (mtd->writesize >= ecc->size) {
6521 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006522 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6523 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006524 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006525 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006526 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006527 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006528 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6529 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006530 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006531 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006533 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006534 ret = nand_set_ecc_soft_ops(mtd);
6535 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006536 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006537 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006538 }
6539 break;
6540
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006541 case NAND_ECC_ON_DIE:
6542 if (!ecc->read_page || !ecc->write_page) {
6543 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6544 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006545 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006546 }
6547 if (!ecc->read_oob)
6548 ecc->read_oob = nand_read_oob_std;
6549 if (!ecc->write_oob)
6550 ecc->write_oob = nand_write_oob_std;
6551 break;
6552
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006553 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006554 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006555 ecc->read_page = nand_read_page_raw;
6556 ecc->write_page = nand_write_page_raw;
6557 ecc->read_oob = nand_read_oob_std;
6558 ecc->read_page_raw = nand_read_page_raw;
6559 ecc->write_page_raw = nand_write_page_raw;
6560 ecc->write_oob = nand_write_oob_std;
6561 ecc->size = mtd->writesize;
6562 ecc->bytes = 0;
6563 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006565
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006567 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6568 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006569 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006572 if (ecc->correct || ecc->calculate) {
6573 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6574 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6575 if (!ecc->calc_buf || !ecc->code_buf) {
6576 ret = -ENOMEM;
6577 goto err_nand_manuf_cleanup;
6578 }
6579 }
6580
Brian Norris9ce244b2011-08-30 18:45:37 -07006581 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006582 if (!ecc->read_oob_raw)
6583 ecc->read_oob_raw = ecc->read_oob;
6584 if (!ecc->write_oob_raw)
6585 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006586
Boris Brezillon846031d2016-02-03 20:11:00 +01006587 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006588 mtd->ecc_strength = ecc->strength;
6589 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006590
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006591 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006592 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006593 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006594 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006595 ecc->steps = mtd->writesize / ecc->size;
6596 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006597 WARN(1, "Invalid ECC parameters\n");
6598 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006599 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006601 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006602 if (ecc->total > mtd->oobsize) {
6603 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6604 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006605 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006606 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006607
Boris Brezillon846031d2016-02-03 20:11:00 +01006608 /*
6609 * The number of bytes available for a client to place data into
6610 * the out of band area.
6611 */
6612 ret = mtd_ooblayout_count_freebytes(mtd);
6613 if (ret < 0)
6614 ret = 0;
6615
6616 mtd->oobavail = ret;
6617
6618 /* ECC sanity check: warn if it's too weak */
6619 if (!nand_ecc_strength_good(mtd))
6620 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6621 mtd->name);
6622
Brian Norris8b6e50c2011-05-25 14:59:01 -07006623 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006624 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006625 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006626 case 2:
6627 mtd->subpage_sft = 1;
6628 break;
6629 case 4:
6630 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006631 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006632 mtd->subpage_sft = 2;
6633 break;
6634 }
6635 }
6636 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6637
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006638 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006639 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006642 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006644 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306645 switch (ecc->mode) {
6646 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306647 if (chip->page_shift > 9)
6648 chip->options |= NAND_SUBPAGE_READ;
6649 break;
6650
6651 default:
6652 break;
6653 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006654
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006656 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006657 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6658 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006659 mtd->_erase = nand_erase;
6660 mtd->_point = NULL;
6661 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006662 mtd->_panic_write = panic_nand_write;
6663 mtd->_read_oob = nand_read_oob;
6664 mtd->_write_oob = nand_write_oob;
6665 mtd->_sync = nand_sync;
6666 mtd->_lock = NULL;
6667 mtd->_unlock = NULL;
6668 mtd->_suspend = nand_suspend;
6669 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006670 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006671 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006672 mtd->_block_isbad = nand_block_isbad;
6673 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006674 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006675 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006677 /*
6678 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6679 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6680 * properly set.
6681 */
6682 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006683 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684
Boris Brezillonf84674b2017-06-02 12:18:24 +02006685 /* Initialize the ->data_interface field. */
6686 ret = nand_init_data_interface(chip);
6687 if (ret)
6688 goto err_nand_manuf_cleanup;
6689
6690 /* Enter fastest possible mode on all dies. */
6691 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006692 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006693 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006694 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006695 }
6696
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006697 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006698 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700
6701 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02006702 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07006703 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006704 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006705
Brian Norris44d41822017-05-01 17:04:50 -07006706 return 0;
6707
Boris Brezillonf84674b2017-06-02 12:18:24 +02006708
6709err_nand_manuf_cleanup:
6710 nand_manufacturer_cleanup(chip);
6711
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006712err_free_buf:
6713 kfree(chip->data_buf);
6714 kfree(ecc->code_buf);
6715 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006716
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006717 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006719EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720
David Woodhouse3b85c322006-09-25 17:06:53 +01006721/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006722 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006723 * @mtd: MTD device structure
6724 * @maxchips: number of chips to scan for
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006725 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01006726 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006727 * This fills out all the uninitialized function pointers with the defaults.
6728 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006729 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006730 */
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006731int nand_scan_with_ids(struct mtd_info *mtd, int maxchips,
6732 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01006733{
6734 int ret;
6735
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006736 ret = nand_scan_ident(mtd, maxchips, ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006737 if (!ret)
6738 ret = nand_scan_tail(mtd);
6739 return ret;
6740}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006741EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006742
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006744 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6745 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006746 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006747void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006749 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006750 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006751 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6752
Jesper Juhlfa671642005-11-07 01:01:27 -08006753 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006754 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006755 kfree(chip->data_buf);
6756 kfree(chip->ecc.code_buf);
6757 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006758
6759 /* Free bad block descriptor memory */
6760 if (chip->badblock_pattern && chip->badblock_pattern->options
6761 & NAND_BBT_DYNAMICSTRUCT)
6762 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006763
6764 /* Free manufacturer priv data. */
6765 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006767EXPORT_SYMBOL_GPL(nand_cleanup);
6768
6769/**
6770 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6771 * held by the NAND device
6772 * @mtd: MTD device structure
6773 */
6774void nand_release(struct mtd_info *mtd)
6775{
6776 mtd_device_unregister(mtd);
6777 nand_cleanup(mtd_to_nand(mtd));
6778}
David Woodhousee0c7d762006-05-13 18:07:53 +01006779EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006780
David Woodhousee0c7d762006-05-13 18:07:53 +01006781MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006782MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6783MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006784MODULE_DESCRIPTION("Generic NAND flash driver code");