blob: 802cd9ed44a1af16bc8efb098a4323fa3744fd34 [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);
443 if (res)
444 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));
530 einfo.mtd = mtd;
531 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300532 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800533 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800534
Brian Norrisb32843b2013-07-30 17:52:59 -0700535 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800536 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700537 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300538 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200539 }
Brian Norrise2414f42012-02-06 13:44:00 -0800540
Brian Norrisb32843b2013-07-30 17:52:59 -0700541 /* Mark block bad in BBT */
542 if (chip->bbt) {
543 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800544 if (!ret)
545 ret = res;
546 }
547
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200548 if (!ret)
549 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300550
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000554/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700556 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700558 * Check, if the device is write protected. The function expects, that the
559 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100561static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100563 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100564 u8 status;
565 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200566
Brian Norris8b6e50c2011-05-25 14:59:01 -0700567 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200568 if (chip->options & NAND_BROKEN_XD)
569 return 0;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100572 ret = nand_status_op(chip, &status);
573 if (ret)
574 return ret;
575
576 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800580 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700581 * @mtd: MTD device structure
582 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300583 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800584 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300585 */
586static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
587{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100588 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300589
590 if (!chip->bbt)
591 return 0;
592 /* Return info from the table */
593 return nand_isreserved_bbt(mtd, ofs);
594}
595
596/**
597 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
598 * @mtd: MTD device structure
599 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700600 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
602 * Check, if the block is bad. Either by reading the bad block table or
603 * calling of the scan function.
604 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530605static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100607 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000608
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200609 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530610 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100613 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200616/**
617 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700618 * @mtd: MTD device structure
619 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200620 *
621 * Helper function for nand_wait_ready used when needing to wait in interrupt
622 * context.
623 */
624static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
625{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100626 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200627 int i;
628
629 /* Wait for the device to get ready */
630 for (i = 0; i < timeo; i++) {
631 if (chip->dev_ready(mtd))
632 break;
633 touch_softlockup_watchdog();
634 mdelay(1);
635 }
636}
637
Alex Smithb70af9b2015-10-06 14:52:07 +0100638/**
639 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
640 * @mtd: MTD device structure
641 *
642 * Wait for the ready pin after a command, and warn if a timeout occurs.
643 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100644void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000645{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100646 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100647 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000648
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200649 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100650 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200651
Brian Norris7854d3f2011-06-23 14:12:08 -0700652 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100653 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000654 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300656 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100657 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000658 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100659
Brian Norris9ebfdf52016-03-04 17:19:23 -0800660 if (!chip->dev_ready(mtd))
661 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000662}
David Woodhouse4b648b02006-09-25 17:05:24 +0100663EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200666 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
667 * @mtd: MTD device structure
668 * @timeo: Timeout in ms
669 *
670 * Wait for status ready (i.e. command done) or timeout.
671 */
672static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
673{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100674 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100675 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200676
677 timeo = jiffies + msecs_to_jiffies(timeo);
678 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100679 u8 status;
680
681 ret = nand_read_data_op(chip, &status, sizeof(status), true);
682 if (ret)
683 return;
684
685 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200686 break;
687 touch_softlockup_watchdog();
688 } while (time_before(jiffies, timeo));
689};
690
691/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100692 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
693 * @chip: NAND chip structure
694 * @timeout_ms: Timeout in ms
695 *
696 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
697 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
698 * returned.
699 *
700 * This helper is intended to be used when the controller does not have access
701 * to the NAND R/B pin.
702 *
703 * Be aware that calling this helper from an ->exec_op() implementation means
704 * ->exec_op() must be re-entrant.
705 *
706 * Return 0 if the NAND chip is ready, a negative error otherwise.
707 */
708int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
709{
710 u8 status = 0;
711 int ret;
712
713 if (!chip->exec_op)
714 return -ENOTSUPP;
715
716 ret = nand_status_op(chip, NULL);
717 if (ret)
718 return ret;
719
720 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
721 do {
722 ret = nand_read_data_op(chip, &status, sizeof(status), true);
723 if (ret)
724 break;
725
726 if (status & NAND_STATUS_READY)
727 break;
728
729 /*
730 * Typical lowest execution time for a tR on most NANDs is 10us,
731 * use this as polling delay before doing something smarter (ie.
732 * deriving a delay from the timeout value, timeout_ms/ratio).
733 */
734 udelay(10);
735 } while (time_before(jiffies, timeout_ms));
736
737 /*
738 * We have to exit READ_STATUS mode in order to read real data on the
739 * bus in case the WAITRDY instruction is preceding a DATA_IN
740 * instruction.
741 */
742 nand_exit_status_op(chip);
743
744 if (ret)
745 return ret;
746
747 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
748};
749EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
750
751/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700753 * @mtd: MTD device structure
754 * @command: the command to be sent
755 * @column: the column address for this command, -1 if none
756 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700758 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200759 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200761static void nand_command(struct mtd_info *mtd, unsigned int command,
762 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100764 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200765 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Brian Norris8b6e50c2011-05-25 14:59:01 -0700767 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (command == NAND_CMD_SEQIN) {
769 int readcmd;
770
Joern Engel28318772006-05-22 23:18:05 +0200771 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200773 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 readcmd = NAND_CMD_READOOB;
775 } else if (column < 256) {
776 /* First 256 bytes --> READ0 */
777 readcmd = NAND_CMD_READ0;
778 } else {
779 column -= 256;
780 readcmd = NAND_CMD_READ1;
781 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200782 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200783 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100785 if (command != NAND_CMD_NONE)
786 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Brian Norris8b6e50c2011-05-25 14:59:01 -0700788 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200789 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
790 /* Serially input address */
791 if (column != -1) {
792 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800793 if (chip->options & NAND_BUSWIDTH_16 &&
794 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200795 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200796 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200797 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200799 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200800 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200801 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200802 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900803 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200804 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200805 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200806 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000807
808 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700809 * Program and erase have their own busy handlers status and sequential
810 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100811 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000813
Miquel Raynaldf467892017-11-08 17:00:27 +0100814 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 case NAND_CMD_PAGEPROG:
816 case NAND_CMD_ERASE1:
817 case NAND_CMD_ERASE2:
818 case NAND_CMD_SEQIN:
819 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900820 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900821 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return;
823
824 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200825 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200827 udelay(chip->chip_delay);
828 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200829 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200830 chip->cmd_ctrl(mtd,
831 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200832 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
833 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return;
835
David Woodhousee0c7d762006-05-13 18:07:53 +0100836 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200837 case NAND_CMD_READ0:
838 /*
839 * READ0 is sometimes used to exit GET STATUS mode. When this
840 * is the case no address cycles are requested, and we can use
841 * this information to detect that we should not wait for the
842 * device to be ready.
843 */
844 if (column == -1 && page_addr == -1)
845 return;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000848 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * If we don't have access to the busy pin, we apply the given
850 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100851 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200852 if (!chip->dev_ready) {
853 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700857 /*
858 * Apply this short delay always to ensure that we do wait tWB in
859 * any case on any machine.
860 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100861 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000862
863 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200866static void nand_ccs_delay(struct nand_chip *chip)
867{
868 /*
869 * The controller already takes care of waiting for tCCS when the RNDIN
870 * or RNDOUT command is sent, return directly.
871 */
872 if (!(chip->options & NAND_WAIT_TCCS))
873 return;
874
875 /*
876 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
877 * (which should be safe for all NANDs).
878 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100879 if (chip->setup_data_interface)
880 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200881 else
882 ndelay(500);
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/**
886 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700887 * @mtd: MTD device structure
888 * @command: the command to be sent
889 * @column: the column address for this command, -1 if none
890 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200892 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700893 * devices. We don't have the separate regions as we have in the small page
894 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200896static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
897 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100899 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* Emulate NAND_CMD_READOOB */
902 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200903 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 command = NAND_CMD_READ0;
905 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000906
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200907 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100908 if (command != NAND_CMD_NONE)
909 chip->cmd_ctrl(mtd, command,
910 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200913 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /* Serially input address */
916 if (column != -1) {
917 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800918 if (chip->options & NAND_BUSWIDTH_16 &&
919 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200921 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200922 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200923
Brian Norrisf5b88de2016-10-03 09:49:35 -0700924 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200925 if (!nand_opcode_8bits(command))
926 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200929 chip->cmd_ctrl(mtd, page_addr, ctrl);
930 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200931 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900932 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200933 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200934 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200937 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000938
939 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700940 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100941 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000944
Miquel Raynaldf467892017-11-08 17:00:27 +0100945 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 case NAND_CMD_CACHEDPROG:
947 case NAND_CMD_PAGEPROG:
948 case NAND_CMD_ERASE1:
949 case NAND_CMD_ERASE2:
950 case NAND_CMD_SEQIN:
951 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900952 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900953 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000954 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200956 case NAND_CMD_RNDIN:
957 nand_ccs_delay(chip);
958 return;
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200961 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200963 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200964 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
965 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
966 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
967 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200968 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
969 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return;
971
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200972 case NAND_CMD_RNDOUT:
973 /* No ready / busy check necessary */
974 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
975 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
976 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
977 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200978
979 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200980 return;
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200983 /*
984 * READ0 is sometimes used to exit GET STATUS mode. When this
985 * is the case no address cycles are requested, and we can use
986 * this information to detect that READSTART should not be
987 * issued.
988 */
989 if (column == -1 && page_addr == -1)
990 return;
991
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200992 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
993 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
994 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
995 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000996
David Woodhousee0c7d762006-05-13 18:07:53 +0100997 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000999 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -07001001 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +01001002 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001003 if (!chip->dev_ready) {
1004 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Thomas Gleixner3b887752005-02-22 21:56:49 +00001008
Brian Norris8b6e50c2011-05-25 14:59:01 -07001009 /*
1010 * Apply this short delay always to ensure that we do wait tWB in
1011 * any case on any machine.
1012 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001013 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +00001014
1015 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001019 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001020 * @chip: the nand chip descriptor
1021 * @mtd: MTD device structure
1022 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001023 *
1024 * Used when in panic, no locks are taken.
1025 */
1026static void panic_nand_get_device(struct nand_chip *chip,
1027 struct mtd_info *mtd, int new_state)
1028{
Brian Norris7854d3f2011-06-23 14:12:08 -07001029 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001030 chip->controller->active = chip;
1031 chip->state = new_state;
1032}
1033
1034/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001036 * @mtd: MTD device structure
1037 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 *
1039 * Get the device and lock it for exclusive access
1040 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001041static int
Huang Shijie6a8214a2012-11-19 14:43:30 +08001042nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001044 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001045 spinlock_t *lock = &chip->controller->lock;
1046 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +01001047 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001048retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001049 spin_lock(lock);
1050
vimal singhb8b3ee92009-07-09 20:41:22 +05301051 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001052 if (!chip->controller->active)
1053 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001054
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001055 if (chip->controller->active == chip && chip->state == FL_READY) {
1056 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001057 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01001058 return 0;
1059 }
1060 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001061 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1062 chip->state = FL_PM_SUSPENDED;
1063 spin_unlock(lock);
1064 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001065 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001066 }
1067 set_current_state(TASK_UNINTERRUPTIBLE);
1068 add_wait_queue(wq, &wait);
1069 spin_unlock(lock);
1070 schedule();
1071 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 goto retry;
1073}
1074
1075/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001076 * panic_nand_wait - [GENERIC] wait until the command is done
1077 * @mtd: MTD device structure
1078 * @chip: NAND chip structure
1079 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001080 *
1081 * Wait for command done. This is a helper function for nand_wait used when
1082 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001083 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001084 */
1085static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1086 unsigned long timeo)
1087{
1088 int i;
1089 for (i = 0; i < timeo; i++) {
1090 if (chip->dev_ready) {
1091 if (chip->dev_ready(mtd))
1092 break;
1093 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001094 int ret;
1095 u8 status;
1096
1097 ret = nand_read_data_op(chip, &status, sizeof(status),
1098 true);
1099 if (ret)
1100 return;
1101
1102 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001103 break;
1104 }
1105 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001106 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001107}
1108
1109/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001110 * nand_wait - [DEFAULT] wait until the command is done
1111 * @mtd: MTD device structure
1112 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001114 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001115 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001116static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118
Alex Smithb70af9b2015-10-06 14:52:07 +01001119 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001120 u8 status;
1121 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Brian Norris8b6e50c2011-05-25 14:59:01 -07001123 /*
1124 * Apply this short delay always to ensure that we do wait tWB in any
1125 * case on any machine.
1126 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001127 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Boris Brezillon97d90da2017-11-30 18:01:29 +01001129 ret = nand_status_op(chip, NULL);
1130 if (ret)
1131 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001133 if (in_interrupt() || oops_in_progress)
1134 panic_nand_wait(mtd, chip, timeo);
1135 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001136 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001137 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001138 if (chip->dev_ready) {
1139 if (chip->dev_ready(mtd))
1140 break;
1141 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001142 ret = nand_read_data_op(chip, &status,
1143 sizeof(status), true);
1144 if (ret)
1145 return ret;
1146
1147 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001148 break;
1149 }
1150 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001151 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001153
Boris Brezillon97d90da2017-11-30 18:01:29 +01001154 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1155 if (ret)
1156 return ret;
1157
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001158 /* This can happen if in case of timeout or buggy dev_ready */
1159 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return status;
1161}
1162
1163/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001164 * nand_reset_data_interface - Reset data interface and timings
1165 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001166 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001167 *
1168 * Reset the Data interface and timings to ONFI mode 0.
1169 *
1170 * Returns 0 for success or negative error code otherwise.
1171 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001172static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001173{
1174 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001175 int ret;
1176
1177 if (!chip->setup_data_interface)
1178 return 0;
1179
1180 /*
1181 * The ONFI specification says:
1182 * "
1183 * To transition from NV-DDR or NV-DDR2 to the SDR data
1184 * interface, the host shall use the Reset (FFh) command
1185 * using SDR timing mode 0. A device in any timing mode is
1186 * required to recognize Reset (FFh) command issued in SDR
1187 * timing mode 0.
1188 * "
1189 *
1190 * Configure the data interface in SDR mode and set the
1191 * timings to timing mode 0.
1192 */
1193
Miquel Raynal17fa8042017-11-30 18:01:31 +01001194 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1195 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001196 if (ret)
1197 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1198
1199 return ret;
1200}
1201
1202/**
1203 * nand_setup_data_interface - Setup the best data interface and timings
1204 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001205 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001206 *
1207 * Find and configure the best data interface and NAND timings supported by
1208 * the chip and the driver.
1209 * First tries to retrieve supported timing modes from ONFI information,
1210 * and if the NAND chip does not support ONFI, relies on the
1211 * ->onfi_timing_mode_default specified in the nand_ids table.
1212 *
1213 * Returns 0 for success or negative error code otherwise.
1214 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001215static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001216{
1217 struct mtd_info *mtd = nand_to_mtd(chip);
1218 int ret;
1219
Miquel Raynal17fa8042017-11-30 18:01:31 +01001220 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001221 return 0;
1222
1223 /*
1224 * Ensure the timing mode has been changed on the chip side
1225 * before changing timings on the controller side.
1226 */
Boris Brezillona11bf5e2017-07-31 10:29:56 +02001227 if (chip->onfi_version &&
1228 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1229 ONFI_OPT_CMD_SET_GET_FEATURES)) {
Boris Brezillond8e725d2016-09-15 10:32:50 +02001230 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1231 chip->onfi_timing_mode_default,
1232 };
1233
Miquel Raynalb9587582018-03-19 14:47:19 +01001234 ret = chip->set_features(mtd, chip,
1235 ONFI_FEATURE_ADDR_TIMING_MODE,
1236 tmode_param);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001237 if (ret)
1238 goto err;
1239 }
1240
Miquel Raynal17fa8042017-11-30 18:01:31 +01001241 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001242err:
1243 return ret;
1244}
1245
1246/**
1247 * nand_init_data_interface - find the best data interface and timings
1248 * @chip: The NAND chip
1249 *
1250 * Find the best data interface and NAND timings supported by the chip
1251 * and the driver.
1252 * First tries to retrieve supported timing modes from ONFI information,
1253 * and if the NAND chip does not support ONFI, relies on the
1254 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1255 * function nand_chip->data_interface is initialized with the best timing mode
1256 * available.
1257 *
1258 * Returns 0 for success or negative error code otherwise.
1259 */
1260static int nand_init_data_interface(struct nand_chip *chip)
1261{
1262 struct mtd_info *mtd = nand_to_mtd(chip);
1263 int modes, mode, ret;
1264
1265 if (!chip->setup_data_interface)
1266 return 0;
1267
1268 /*
1269 * First try to identify the best timings from ONFI parameters and
1270 * if the NAND does not support ONFI, fallback to the default ONFI
1271 * timing mode.
1272 */
1273 modes = onfi_get_async_timing_mode(chip);
1274 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1275 if (!chip->onfi_timing_mode_default)
1276 return 0;
1277
1278 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1279 }
1280
Boris Brezillond8e725d2016-09-15 10:32:50 +02001281
1282 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001283 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001284 if (ret)
1285 continue;
1286
Miquel Raynald787b8b2017-12-22 18:12:41 +01001287 /*
1288 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1289 * controller supports the requested timings.
1290 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001291 ret = chip->setup_data_interface(mtd,
1292 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001293 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001294 if (!ret) {
1295 chip->onfi_timing_mode_default = mode;
1296 break;
1297 }
1298 }
1299
1300 return 0;
1301}
1302
Boris Brezillond8e725d2016-09-15 10:32:50 +02001303/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001304 * nand_fill_column_cycles - fill the column cycles of an address
1305 * @chip: The NAND chip
1306 * @addrs: Array of address cycles to fill
1307 * @offset_in_page: The offset in the page
1308 *
1309 * Fills the first or the first two bytes of the @addrs field depending
1310 * on the NAND bus width and the page size.
1311 *
1312 * Returns the number of cycles needed to encode the column, or a negative
1313 * error code in case one of the arguments is invalid.
1314 */
1315static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1316 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Miquel Raynal8878b122017-11-09 14:16:45 +01001318 struct mtd_info *mtd = nand_to_mtd(chip);
1319
1320 /* Make sure the offset is less than the actual page size. */
1321 if (offset_in_page > mtd->writesize + mtd->oobsize)
1322 return -EINVAL;
1323
1324 /*
1325 * On small page NANDs, there's a dedicated command to access the OOB
1326 * area, and the column address is relative to the start of the OOB
1327 * area, not the start of the page. Asjust the address accordingly.
1328 */
1329 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1330 offset_in_page -= mtd->writesize;
1331
1332 /*
1333 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1334 * wide, then it must be divided by 2.
1335 */
1336 if (chip->options & NAND_BUSWIDTH_16) {
1337 if (WARN_ON(offset_in_page % 2))
1338 return -EINVAL;
1339
1340 offset_in_page /= 2;
1341 }
1342
1343 addrs[0] = offset_in_page;
1344
1345 /*
1346 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1347 * need 2
1348 */
1349 if (mtd->writesize <= 512)
1350 return 1;
1351
1352 addrs[1] = offset_in_page >> 8;
1353
1354 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Miquel Raynal8878b122017-11-09 14:16:45 +01001357static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1358 unsigned int offset_in_page, void *buf,
1359 unsigned int len)
1360{
1361 struct mtd_info *mtd = nand_to_mtd(chip);
1362 const struct nand_sdr_timings *sdr =
1363 nand_get_sdr_timings(&chip->data_interface);
1364 u8 addrs[4];
1365 struct nand_op_instr instrs[] = {
1366 NAND_OP_CMD(NAND_CMD_READ0, 0),
1367 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1368 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1369 PSEC_TO_NSEC(sdr->tRR_min)),
1370 NAND_OP_DATA_IN(len, buf, 0),
1371 };
1372 struct nand_operation op = NAND_OPERATION(instrs);
1373 int ret;
1374
1375 /* Drop the DATA_IN instruction if len is set to 0. */
1376 if (!len)
1377 op.ninstrs--;
1378
1379 if (offset_in_page >= mtd->writesize)
1380 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1381 else if (offset_in_page >= 256 &&
1382 !(chip->options & NAND_BUSWIDTH_16))
1383 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1384
1385 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1386 if (ret < 0)
1387 return ret;
1388
1389 addrs[1] = page;
1390 addrs[2] = page >> 8;
1391
1392 if (chip->options & NAND_ROW_ADDR_3) {
1393 addrs[3] = page >> 16;
1394 instrs[1].ctx.addr.naddrs++;
1395 }
1396
1397 return nand_exec_op(chip, &op);
1398}
1399
1400static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1401 unsigned int offset_in_page, void *buf,
1402 unsigned int len)
1403{
1404 const struct nand_sdr_timings *sdr =
1405 nand_get_sdr_timings(&chip->data_interface);
1406 u8 addrs[5];
1407 struct nand_op_instr instrs[] = {
1408 NAND_OP_CMD(NAND_CMD_READ0, 0),
1409 NAND_OP_ADDR(4, addrs, 0),
1410 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1411 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1412 PSEC_TO_NSEC(sdr->tRR_min)),
1413 NAND_OP_DATA_IN(len, buf, 0),
1414 };
1415 struct nand_operation op = NAND_OPERATION(instrs);
1416 int ret;
1417
1418 /* Drop the DATA_IN instruction if len is set to 0. */
1419 if (!len)
1420 op.ninstrs--;
1421
1422 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1423 if (ret < 0)
1424 return ret;
1425
1426 addrs[2] = page;
1427 addrs[3] = page >> 8;
1428
1429 if (chip->options & NAND_ROW_ADDR_3) {
1430 addrs[4] = page >> 16;
1431 instrs[1].ctx.addr.naddrs++;
1432 }
1433
1434 return nand_exec_op(chip, &op);
1435}
1436
1437/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001438 * nand_read_page_op - Do a READ PAGE operation
1439 * @chip: The NAND chip
1440 * @page: page to read
1441 * @offset_in_page: offset within the page
1442 * @buf: buffer used to store the data
1443 * @len: length of the buffer
1444 *
1445 * This function issues a READ PAGE operation.
1446 * This function does not select/unselect the CS line.
1447 *
1448 * Returns 0 on success, a negative error code otherwise.
1449 */
1450int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1451 unsigned int offset_in_page, void *buf, unsigned int len)
1452{
1453 struct mtd_info *mtd = nand_to_mtd(chip);
1454
1455 if (len && !buf)
1456 return -EINVAL;
1457
1458 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1459 return -EINVAL;
1460
Miquel Raynal8878b122017-11-09 14:16:45 +01001461 if (chip->exec_op) {
1462 if (mtd->writesize > 512)
1463 return nand_lp_exec_read_page_op(chip, page,
1464 offset_in_page, buf,
1465 len);
1466
1467 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1468 buf, len);
1469 }
1470
Boris Brezillon97d90da2017-11-30 18:01:29 +01001471 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1472 if (len)
1473 chip->read_buf(mtd, buf, len);
1474
1475 return 0;
1476}
1477EXPORT_SYMBOL_GPL(nand_read_page_op);
1478
1479/**
1480 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1481 * @chip: The NAND chip
1482 * @page: parameter page to read
1483 * @buf: buffer used to store the data
1484 * @len: length of the buffer
1485 *
1486 * This function issues a READ PARAMETER PAGE operation.
1487 * This function does not select/unselect the CS line.
1488 *
1489 * Returns 0 on success, a negative error code otherwise.
1490 */
1491static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1492 unsigned int len)
1493{
1494 struct mtd_info *mtd = nand_to_mtd(chip);
1495 unsigned int i;
1496 u8 *p = buf;
1497
1498 if (len && !buf)
1499 return -EINVAL;
1500
Miquel Raynal8878b122017-11-09 14:16:45 +01001501 if (chip->exec_op) {
1502 const struct nand_sdr_timings *sdr =
1503 nand_get_sdr_timings(&chip->data_interface);
1504 struct nand_op_instr instrs[] = {
1505 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1506 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1507 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1508 PSEC_TO_NSEC(sdr->tRR_min)),
1509 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1510 };
1511 struct nand_operation op = NAND_OPERATION(instrs);
1512
1513 /* Drop the DATA_IN instruction if len is set to 0. */
1514 if (!len)
1515 op.ninstrs--;
1516
1517 return nand_exec_op(chip, &op);
1518 }
1519
Boris Brezillon97d90da2017-11-30 18:01:29 +01001520 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1521 for (i = 0; i < len; i++)
1522 p[i] = chip->read_byte(mtd);
1523
1524 return 0;
1525}
1526
1527/**
1528 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1529 * @chip: The NAND chip
1530 * @offset_in_page: offset within the page
1531 * @buf: buffer used to store the data
1532 * @len: length of the buffer
1533 * @force_8bit: force 8-bit bus access
1534 *
1535 * This function issues a CHANGE READ COLUMN operation.
1536 * This function does not select/unselect the CS line.
1537 *
1538 * Returns 0 on success, a negative error code otherwise.
1539 */
1540int nand_change_read_column_op(struct nand_chip *chip,
1541 unsigned int offset_in_page, void *buf,
1542 unsigned int len, bool force_8bit)
1543{
1544 struct mtd_info *mtd = nand_to_mtd(chip);
1545
1546 if (len && !buf)
1547 return -EINVAL;
1548
1549 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1550 return -EINVAL;
1551
Miquel Raynal8878b122017-11-09 14:16:45 +01001552 /* Small page NANDs do not support column change. */
1553 if (mtd->writesize <= 512)
1554 return -ENOTSUPP;
1555
1556 if (chip->exec_op) {
1557 const struct nand_sdr_timings *sdr =
1558 nand_get_sdr_timings(&chip->data_interface);
1559 u8 addrs[2] = {};
1560 struct nand_op_instr instrs[] = {
1561 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1562 NAND_OP_ADDR(2, addrs, 0),
1563 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1564 PSEC_TO_NSEC(sdr->tCCS_min)),
1565 NAND_OP_DATA_IN(len, buf, 0),
1566 };
1567 struct nand_operation op = NAND_OPERATION(instrs);
1568 int ret;
1569
1570 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1571 if (ret < 0)
1572 return ret;
1573
1574 /* Drop the DATA_IN instruction if len is set to 0. */
1575 if (!len)
1576 op.ninstrs--;
1577
1578 instrs[3].ctx.data.force_8bit = force_8bit;
1579
1580 return nand_exec_op(chip, &op);
1581 }
1582
Boris Brezillon97d90da2017-11-30 18:01:29 +01001583 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1584 if (len)
1585 chip->read_buf(mtd, buf, len);
1586
1587 return 0;
1588}
1589EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1590
1591/**
1592 * nand_read_oob_op - Do a READ OOB operation
1593 * @chip: The NAND chip
1594 * @page: page to read
1595 * @offset_in_oob: offset within the OOB area
1596 * @buf: buffer used to store the data
1597 * @len: length of the buffer
1598 *
1599 * This function issues a READ OOB operation.
1600 * This function does not select/unselect the CS line.
1601 *
1602 * Returns 0 on success, a negative error code otherwise.
1603 */
1604int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1605 unsigned int offset_in_oob, void *buf, unsigned int len)
1606{
1607 struct mtd_info *mtd = nand_to_mtd(chip);
1608
1609 if (len && !buf)
1610 return -EINVAL;
1611
1612 if (offset_in_oob + len > mtd->oobsize)
1613 return -EINVAL;
1614
Miquel Raynal8878b122017-11-09 14:16:45 +01001615 if (chip->exec_op)
1616 return nand_read_page_op(chip, page,
1617 mtd->writesize + offset_in_oob,
1618 buf, len);
1619
Boris Brezillon97d90da2017-11-30 18:01:29 +01001620 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1621 if (len)
1622 chip->read_buf(mtd, buf, len);
1623
1624 return 0;
1625}
1626EXPORT_SYMBOL_GPL(nand_read_oob_op);
1627
Miquel Raynal8878b122017-11-09 14:16:45 +01001628static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1629 unsigned int offset_in_page, const void *buf,
1630 unsigned int len, bool prog)
1631{
1632 struct mtd_info *mtd = nand_to_mtd(chip);
1633 const struct nand_sdr_timings *sdr =
1634 nand_get_sdr_timings(&chip->data_interface);
1635 u8 addrs[5] = {};
1636 struct nand_op_instr instrs[] = {
1637 /*
1638 * The first instruction will be dropped if we're dealing
1639 * with a large page NAND and adjusted if we're dealing
1640 * with a small page NAND and the page offset is > 255.
1641 */
1642 NAND_OP_CMD(NAND_CMD_READ0, 0),
1643 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1644 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1645 NAND_OP_DATA_OUT(len, buf, 0),
1646 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1647 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1648 };
1649 struct nand_operation op = NAND_OPERATION(instrs);
1650 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1651 int ret;
1652 u8 status;
1653
1654 if (naddrs < 0)
1655 return naddrs;
1656
1657 addrs[naddrs++] = page;
1658 addrs[naddrs++] = page >> 8;
1659 if (chip->options & NAND_ROW_ADDR_3)
1660 addrs[naddrs++] = page >> 16;
1661
1662 instrs[2].ctx.addr.naddrs = naddrs;
1663
1664 /* Drop the last two instructions if we're not programming the page. */
1665 if (!prog) {
1666 op.ninstrs -= 2;
1667 /* Also drop the DATA_OUT instruction if empty. */
1668 if (!len)
1669 op.ninstrs--;
1670 }
1671
1672 if (mtd->writesize <= 512) {
1673 /*
1674 * Small pages need some more tweaking: we have to adjust the
1675 * first instruction depending on the page offset we're trying
1676 * to access.
1677 */
1678 if (offset_in_page >= mtd->writesize)
1679 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1680 else if (offset_in_page >= 256 &&
1681 !(chip->options & NAND_BUSWIDTH_16))
1682 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1683 } else {
1684 /*
1685 * Drop the first command if we're dealing with a large page
1686 * NAND.
1687 */
1688 op.instrs++;
1689 op.ninstrs--;
1690 }
1691
1692 ret = nand_exec_op(chip, &op);
1693 if (!prog || ret)
1694 return ret;
1695
1696 ret = nand_status_op(chip, &status);
1697 if (ret)
1698 return ret;
1699
1700 return status;
1701}
1702
Boris Brezillon97d90da2017-11-30 18:01:29 +01001703/**
1704 * nand_prog_page_begin_op - starts a PROG PAGE operation
1705 * @chip: The NAND chip
1706 * @page: page to write
1707 * @offset_in_page: offset within the page
1708 * @buf: buffer containing the data to write to the page
1709 * @len: length of the buffer
1710 *
1711 * This function issues the first half of a PROG PAGE operation.
1712 * This function does not select/unselect the CS line.
1713 *
1714 * Returns 0 on success, a negative error code otherwise.
1715 */
1716int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1717 unsigned int offset_in_page, const void *buf,
1718 unsigned int len)
1719{
1720 struct mtd_info *mtd = nand_to_mtd(chip);
1721
1722 if (len && !buf)
1723 return -EINVAL;
1724
1725 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1726 return -EINVAL;
1727
Miquel Raynal8878b122017-11-09 14:16:45 +01001728 if (chip->exec_op)
1729 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1730 len, false);
1731
Boris Brezillon97d90da2017-11-30 18:01:29 +01001732 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1733
1734 if (buf)
1735 chip->write_buf(mtd, buf, len);
1736
1737 return 0;
1738}
1739EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1740
1741/**
1742 * nand_prog_page_end_op - ends a PROG PAGE operation
1743 * @chip: The NAND chip
1744 *
1745 * This function issues the second half of a PROG PAGE operation.
1746 * This function does not select/unselect the CS line.
1747 *
1748 * Returns 0 on success, a negative error code otherwise.
1749 */
1750int nand_prog_page_end_op(struct nand_chip *chip)
1751{
1752 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001753 int ret;
1754 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001755
Miquel Raynal8878b122017-11-09 14:16:45 +01001756 if (chip->exec_op) {
1757 const struct nand_sdr_timings *sdr =
1758 nand_get_sdr_timings(&chip->data_interface);
1759 struct nand_op_instr instrs[] = {
1760 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1761 PSEC_TO_NSEC(sdr->tWB_max)),
1762 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1763 };
1764 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001765
Miquel Raynal8878b122017-11-09 14:16:45 +01001766 ret = nand_exec_op(chip, &op);
1767 if (ret)
1768 return ret;
1769
1770 ret = nand_status_op(chip, &status);
1771 if (ret)
1772 return ret;
1773 } else {
1774 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1775 ret = chip->waitfunc(mtd, chip);
1776 if (ret < 0)
1777 return ret;
1778
1779 status = ret;
1780 }
1781
Boris Brezillon97d90da2017-11-30 18:01:29 +01001782 if (status & NAND_STATUS_FAIL)
1783 return -EIO;
1784
1785 return 0;
1786}
1787EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1788
1789/**
1790 * nand_prog_page_op - Do a full 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 a full 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_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 int status;
1808
1809 if (!len || !buf)
1810 return -EINVAL;
1811
1812 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1813 return -EINVAL;
1814
Miquel Raynal8878b122017-11-09 14:16:45 +01001815 if (chip->exec_op) {
1816 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1817 len, true);
1818 } else {
1819 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1820 chip->write_buf(mtd, buf, len);
1821 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1822 status = chip->waitfunc(mtd, chip);
1823 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001824
Boris Brezillon97d90da2017-11-30 18:01:29 +01001825 if (status & NAND_STATUS_FAIL)
1826 return -EIO;
1827
1828 return 0;
1829}
1830EXPORT_SYMBOL_GPL(nand_prog_page_op);
1831
1832/**
1833 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1834 * @chip: The NAND chip
1835 * @offset_in_page: offset within the page
1836 * @buf: buffer containing the data to send to the NAND
1837 * @len: length of the buffer
1838 * @force_8bit: force 8-bit bus access
1839 *
1840 * This function issues a CHANGE WRITE COLUMN operation.
1841 * This function does not select/unselect the CS line.
1842 *
1843 * Returns 0 on success, a negative error code otherwise.
1844 */
1845int nand_change_write_column_op(struct nand_chip *chip,
1846 unsigned int offset_in_page,
1847 const void *buf, unsigned int len,
1848 bool force_8bit)
1849{
1850 struct mtd_info *mtd = nand_to_mtd(chip);
1851
1852 if (len && !buf)
1853 return -EINVAL;
1854
1855 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1856 return -EINVAL;
1857
Miquel Raynal8878b122017-11-09 14:16:45 +01001858 /* Small page NANDs do not support column change. */
1859 if (mtd->writesize <= 512)
1860 return -ENOTSUPP;
1861
1862 if (chip->exec_op) {
1863 const struct nand_sdr_timings *sdr =
1864 nand_get_sdr_timings(&chip->data_interface);
1865 u8 addrs[2];
1866 struct nand_op_instr instrs[] = {
1867 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1868 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1869 NAND_OP_DATA_OUT(len, buf, 0),
1870 };
1871 struct nand_operation op = NAND_OPERATION(instrs);
1872 int ret;
1873
1874 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1875 if (ret < 0)
1876 return ret;
1877
1878 instrs[2].ctx.data.force_8bit = force_8bit;
1879
1880 /* Drop the DATA_OUT instruction if len is set to 0. */
1881 if (!len)
1882 op.ninstrs--;
1883
1884 return nand_exec_op(chip, &op);
1885 }
1886
Boris Brezillon97d90da2017-11-30 18:01:29 +01001887 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1888 if (len)
1889 chip->write_buf(mtd, buf, len);
1890
1891 return 0;
1892}
1893EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1894
1895/**
1896 * nand_readid_op - Do a READID operation
1897 * @chip: The NAND chip
1898 * @addr: address cycle to pass after the READID command
1899 * @buf: buffer used to store the ID
1900 * @len: length of the buffer
1901 *
1902 * This function sends a READID command and reads back the ID returned by the
1903 * NAND.
1904 * This function does not select/unselect the CS line.
1905 *
1906 * Returns 0 on success, a negative error code otherwise.
1907 */
1908int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1909 unsigned int len)
1910{
1911 struct mtd_info *mtd = nand_to_mtd(chip);
1912 unsigned int i;
1913 u8 *id = buf;
1914
1915 if (len && !buf)
1916 return -EINVAL;
1917
Miquel Raynal8878b122017-11-09 14:16:45 +01001918 if (chip->exec_op) {
1919 const struct nand_sdr_timings *sdr =
1920 nand_get_sdr_timings(&chip->data_interface);
1921 struct nand_op_instr instrs[] = {
1922 NAND_OP_CMD(NAND_CMD_READID, 0),
1923 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1924 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1925 };
1926 struct nand_operation op = NAND_OPERATION(instrs);
1927
1928 /* Drop the DATA_IN instruction if len is set to 0. */
1929 if (!len)
1930 op.ninstrs--;
1931
1932 return nand_exec_op(chip, &op);
1933 }
1934
Boris Brezillon97d90da2017-11-30 18:01:29 +01001935 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1936
1937 for (i = 0; i < len; i++)
1938 id[i] = chip->read_byte(mtd);
1939
1940 return 0;
1941}
1942EXPORT_SYMBOL_GPL(nand_readid_op);
1943
1944/**
1945 * nand_status_op - Do a STATUS operation
1946 * @chip: The NAND chip
1947 * @status: out variable to store the NAND status
1948 *
1949 * This function sends a STATUS command and reads back the status returned by
1950 * the NAND.
1951 * This function does not select/unselect the CS line.
1952 *
1953 * Returns 0 on success, a negative error code otherwise.
1954 */
1955int nand_status_op(struct nand_chip *chip, u8 *status)
1956{
1957 struct mtd_info *mtd = nand_to_mtd(chip);
1958
Miquel Raynal8878b122017-11-09 14:16:45 +01001959 if (chip->exec_op) {
1960 const struct nand_sdr_timings *sdr =
1961 nand_get_sdr_timings(&chip->data_interface);
1962 struct nand_op_instr instrs[] = {
1963 NAND_OP_CMD(NAND_CMD_STATUS,
1964 PSEC_TO_NSEC(sdr->tADL_min)),
1965 NAND_OP_8BIT_DATA_IN(1, status, 0),
1966 };
1967 struct nand_operation op = NAND_OPERATION(instrs);
1968
1969 if (!status)
1970 op.ninstrs--;
1971
1972 return nand_exec_op(chip, &op);
1973 }
1974
Boris Brezillon97d90da2017-11-30 18:01:29 +01001975 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1976 if (status)
1977 *status = chip->read_byte(mtd);
1978
1979 return 0;
1980}
1981EXPORT_SYMBOL_GPL(nand_status_op);
1982
1983/**
1984 * nand_exit_status_op - Exit a STATUS operation
1985 * @chip: The NAND chip
1986 *
1987 * This function sends a READ0 command to cancel the effect of the STATUS
1988 * command to avoid reading only the status until a new read command is sent.
1989 *
1990 * This function does not select/unselect the CS line.
1991 *
1992 * Returns 0 on success, a negative error code otherwise.
1993 */
1994int nand_exit_status_op(struct nand_chip *chip)
1995{
1996 struct mtd_info *mtd = nand_to_mtd(chip);
1997
Miquel Raynal8878b122017-11-09 14:16:45 +01001998 if (chip->exec_op) {
1999 struct nand_op_instr instrs[] = {
2000 NAND_OP_CMD(NAND_CMD_READ0, 0),
2001 };
2002 struct nand_operation op = NAND_OPERATION(instrs);
2003
2004 return nand_exec_op(chip, &op);
2005 }
2006
Boris Brezillon97d90da2017-11-30 18:01:29 +01002007 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2008
2009 return 0;
2010}
2011EXPORT_SYMBOL_GPL(nand_exit_status_op);
2012
2013/**
2014 * nand_erase_op - Do an erase operation
2015 * @chip: The NAND chip
2016 * @eraseblock: block to erase
2017 *
2018 * This function sends an ERASE command and waits for the NAND to be ready
2019 * before returning.
2020 * This function does not select/unselect the CS line.
2021 *
2022 * Returns 0 on success, a negative error code otherwise.
2023 */
2024int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2025{
2026 struct mtd_info *mtd = nand_to_mtd(chip);
2027 unsigned int page = eraseblock <<
2028 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002029 int ret;
2030 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002031
Miquel Raynal8878b122017-11-09 14:16:45 +01002032 if (chip->exec_op) {
2033 const struct nand_sdr_timings *sdr =
2034 nand_get_sdr_timings(&chip->data_interface);
2035 u8 addrs[3] = { page, page >> 8, page >> 16 };
2036 struct nand_op_instr instrs[] = {
2037 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2038 NAND_OP_ADDR(2, addrs, 0),
2039 NAND_OP_CMD(NAND_CMD_ERASE2,
2040 PSEC_TO_MSEC(sdr->tWB_max)),
2041 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2042 };
2043 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002044
Miquel Raynal8878b122017-11-09 14:16:45 +01002045 if (chip->options & NAND_ROW_ADDR_3)
2046 instrs[1].ctx.addr.naddrs++;
2047
2048 ret = nand_exec_op(chip, &op);
2049 if (ret)
2050 return ret;
2051
2052 ret = nand_status_op(chip, &status);
2053 if (ret)
2054 return ret;
2055 } else {
2056 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2057 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2058
2059 ret = chip->waitfunc(mtd, chip);
2060 if (ret < 0)
2061 return ret;
2062
2063 status = ret;
2064 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002065
2066 if (status & NAND_STATUS_FAIL)
2067 return -EIO;
2068
2069 return 0;
2070}
2071EXPORT_SYMBOL_GPL(nand_erase_op);
2072
2073/**
2074 * nand_set_features_op - Do a SET FEATURES operation
2075 * @chip: The NAND chip
2076 * @feature: feature id
2077 * @data: 4 bytes of data
2078 *
2079 * This function sends a SET FEATURES command and waits for the NAND to be
2080 * ready before returning.
2081 * This function does not select/unselect the CS line.
2082 *
2083 * Returns 0 on success, a negative error code otherwise.
2084 */
2085static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2086 const void *data)
2087{
2088 struct mtd_info *mtd = nand_to_mtd(chip);
2089 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002090 int i, ret;
2091 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002092
Miquel Raynal8878b122017-11-09 14:16:45 +01002093 if (chip->exec_op) {
2094 const struct nand_sdr_timings *sdr =
2095 nand_get_sdr_timings(&chip->data_interface);
2096 struct nand_op_instr instrs[] = {
2097 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2098 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2099 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2100 PSEC_TO_NSEC(sdr->tWB_max)),
2101 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2102 };
2103 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002104
Miquel Raynal8878b122017-11-09 14:16:45 +01002105 ret = nand_exec_op(chip, &op);
2106 if (ret)
2107 return ret;
2108
2109 ret = nand_status_op(chip, &status);
2110 if (ret)
2111 return ret;
2112 } else {
2113 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
2114 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2115 chip->write_byte(mtd, params[i]);
2116
2117 ret = chip->waitfunc(mtd, chip);
2118 if (ret < 0)
2119 return ret;
2120
2121 status = ret;
2122 }
2123
Boris Brezillon97d90da2017-11-30 18:01:29 +01002124 if (status & NAND_STATUS_FAIL)
2125 return -EIO;
2126
2127 return 0;
2128}
2129
2130/**
2131 * nand_get_features_op - Do a GET FEATURES operation
2132 * @chip: The NAND chip
2133 * @feature: feature id
2134 * @data: 4 bytes of data
2135 *
2136 * This function sends a GET FEATURES command and waits for the NAND to be
2137 * ready before returning.
2138 * This function does not select/unselect the CS line.
2139 *
2140 * Returns 0 on success, a negative error code otherwise.
2141 */
2142static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2143 void *data)
2144{
2145 struct mtd_info *mtd = nand_to_mtd(chip);
2146 u8 *params = data;
2147 int i;
2148
Miquel Raynal8878b122017-11-09 14:16:45 +01002149 if (chip->exec_op) {
2150 const struct nand_sdr_timings *sdr =
2151 nand_get_sdr_timings(&chip->data_interface);
2152 struct nand_op_instr instrs[] = {
2153 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2154 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2155 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2156 PSEC_TO_NSEC(sdr->tRR_min)),
2157 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2158 data, 0),
2159 };
2160 struct nand_operation op = NAND_OPERATION(instrs);
2161
2162 return nand_exec_op(chip, &op);
2163 }
2164
Boris Brezillon97d90da2017-11-30 18:01:29 +01002165 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2166 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2167 params[i] = chip->read_byte(mtd);
2168
2169 return 0;
2170}
2171
2172/**
2173 * nand_reset_op - Do a reset operation
2174 * @chip: The NAND chip
2175 *
2176 * This function sends a RESET command and waits for the NAND to be ready
2177 * before returning.
2178 * This function does not select/unselect the CS line.
2179 *
2180 * Returns 0 on success, a negative error code otherwise.
2181 */
2182int nand_reset_op(struct nand_chip *chip)
2183{
2184 struct mtd_info *mtd = nand_to_mtd(chip);
2185
Miquel Raynal8878b122017-11-09 14:16:45 +01002186 if (chip->exec_op) {
2187 const struct nand_sdr_timings *sdr =
2188 nand_get_sdr_timings(&chip->data_interface);
2189 struct nand_op_instr instrs[] = {
2190 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2191 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2192 };
2193 struct nand_operation op = NAND_OPERATION(instrs);
2194
2195 return nand_exec_op(chip, &op);
2196 }
2197
Boris Brezillon97d90da2017-11-30 18:01:29 +01002198 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2199
2200 return 0;
2201}
2202EXPORT_SYMBOL_GPL(nand_reset_op);
2203
2204/**
2205 * nand_read_data_op - Read data from the NAND
2206 * @chip: The NAND chip
2207 * @buf: buffer used to store the data
2208 * @len: length of the buffer
2209 * @force_8bit: force 8-bit bus access
2210 *
2211 * This function does a raw data read on the bus. Usually used after launching
2212 * another NAND operation like nand_read_page_op().
2213 * This function does not select/unselect the CS line.
2214 *
2215 * Returns 0 on success, a negative error code otherwise.
2216 */
2217int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2218 bool force_8bit)
2219{
2220 struct mtd_info *mtd = nand_to_mtd(chip);
2221
2222 if (!len || !buf)
2223 return -EINVAL;
2224
Miquel Raynal8878b122017-11-09 14:16:45 +01002225 if (chip->exec_op) {
2226 struct nand_op_instr instrs[] = {
2227 NAND_OP_DATA_IN(len, buf, 0),
2228 };
2229 struct nand_operation op = NAND_OPERATION(instrs);
2230
2231 instrs[0].ctx.data.force_8bit = force_8bit;
2232
2233 return nand_exec_op(chip, &op);
2234 }
2235
Boris Brezillon97d90da2017-11-30 18:01:29 +01002236 if (force_8bit) {
2237 u8 *p = buf;
2238 unsigned int i;
2239
2240 for (i = 0; i < len; i++)
2241 p[i] = chip->read_byte(mtd);
2242 } else {
2243 chip->read_buf(mtd, buf, len);
2244 }
2245
2246 return 0;
2247}
2248EXPORT_SYMBOL_GPL(nand_read_data_op);
2249
2250/**
2251 * nand_write_data_op - Write data from the NAND
2252 * @chip: The NAND chip
2253 * @buf: buffer containing the data to send on the bus
2254 * @len: length of the buffer
2255 * @force_8bit: force 8-bit bus access
2256 *
2257 * This function does a raw data write on the bus. Usually used after launching
2258 * another NAND operation like nand_write_page_begin_op().
2259 * This function does not select/unselect the CS line.
2260 *
2261 * Returns 0 on success, a negative error code otherwise.
2262 */
2263int nand_write_data_op(struct nand_chip *chip, const void *buf,
2264 unsigned int len, bool force_8bit)
2265{
2266 struct mtd_info *mtd = nand_to_mtd(chip);
2267
2268 if (!len || !buf)
2269 return -EINVAL;
2270
Miquel Raynal8878b122017-11-09 14:16:45 +01002271 if (chip->exec_op) {
2272 struct nand_op_instr instrs[] = {
2273 NAND_OP_DATA_OUT(len, buf, 0),
2274 };
2275 struct nand_operation op = NAND_OPERATION(instrs);
2276
2277 instrs[0].ctx.data.force_8bit = force_8bit;
2278
2279 return nand_exec_op(chip, &op);
2280 }
2281
Boris Brezillon97d90da2017-11-30 18:01:29 +01002282 if (force_8bit) {
2283 const u8 *p = buf;
2284 unsigned int i;
2285
2286 for (i = 0; i < len; i++)
2287 chip->write_byte(mtd, p[i]);
2288 } else {
2289 chip->write_buf(mtd, buf, len);
2290 }
2291
2292 return 0;
2293}
2294EXPORT_SYMBOL_GPL(nand_write_data_op);
2295
2296/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002297 * struct nand_op_parser_ctx - Context used by the parser
2298 * @instrs: array of all the instructions that must be addressed
2299 * @ninstrs: length of the @instrs array
2300 * @subop: Sub-operation to be passed to the NAND controller
2301 *
2302 * This structure is used by the core to split NAND operations into
2303 * sub-operations that can be handled by the NAND controller.
2304 */
2305struct nand_op_parser_ctx {
2306 const struct nand_op_instr *instrs;
2307 unsigned int ninstrs;
2308 struct nand_subop subop;
2309};
2310
2311/**
2312 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2313 * @pat: the parser pattern element that matches @instr
2314 * @instr: pointer to the instruction to check
2315 * @start_offset: this is an in/out parameter. If @instr has already been
2316 * split, then @start_offset is the offset from which to start
2317 * (either an address cycle or an offset in the data buffer).
2318 * Conversely, if the function returns true (ie. instr must be
2319 * split), this parameter is updated to point to the first
2320 * data/address cycle that has not been taken care of.
2321 *
2322 * Some NAND controllers are limited and cannot send X address cycles with a
2323 * unique operation, or cannot read/write more than Y bytes at the same time.
2324 * In this case, split the instruction that does not fit in a single
2325 * controller-operation into two or more chunks.
2326 *
2327 * Returns true if the instruction must be split, false otherwise.
2328 * The @start_offset parameter is also updated to the offset at which the next
2329 * bundle of instruction must start (if an address or a data instruction).
2330 */
2331static bool
2332nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2333 const struct nand_op_instr *instr,
2334 unsigned int *start_offset)
2335{
2336 switch (pat->type) {
2337 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002338 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002339 break;
2340
2341 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002342 pat->ctx.addr.maxcycles) {
2343 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002344 return true;
2345 }
2346 break;
2347
2348 case NAND_OP_DATA_IN_INSTR:
2349 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002350 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002351 break;
2352
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002353 if (instr->ctx.data.len - *start_offset >
2354 pat->ctx.data.maxlen) {
2355 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002356 return true;
2357 }
2358 break;
2359
2360 default:
2361 break;
2362 }
2363
2364 return false;
2365}
2366
2367/**
2368 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2369 * remaining in the parser context
2370 * @pat: the pattern to test
2371 * @ctx: the parser context structure to match with the pattern @pat
2372 *
2373 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2374 * Returns true if this is the case, false ortherwise. When true is returned,
2375 * @ctx->subop is updated with the set of instructions to be passed to the
2376 * controller driver.
2377 */
2378static bool
2379nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2380 struct nand_op_parser_ctx *ctx)
2381{
2382 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2383 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2384 const struct nand_op_instr *instr = ctx->subop.instrs;
2385 unsigned int i, ninstrs;
2386
2387 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2388 /*
2389 * The pattern instruction does not match the operation
2390 * instruction. If the instruction is marked optional in the
2391 * pattern definition, we skip the pattern element and continue
2392 * to the next one. If the element is mandatory, there's no
2393 * match and we can return false directly.
2394 */
2395 if (instr->type != pat->elems[i].type) {
2396 if (!pat->elems[i].optional)
2397 return false;
2398
2399 continue;
2400 }
2401
2402 /*
2403 * Now check the pattern element constraints. If the pattern is
2404 * not able to handle the whole instruction in a single step,
2405 * we have to split it.
2406 * The last_instr_end_off value comes back updated to point to
2407 * the position where we have to split the instruction (the
2408 * start of the next subop chunk).
2409 */
2410 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2411 &instr_offset)) {
2412 ninstrs++;
2413 i++;
2414 break;
2415 }
2416
2417 instr++;
2418 ninstrs++;
2419 instr_offset = 0;
2420 }
2421
2422 /*
2423 * This can happen if all instructions of a pattern are optional.
2424 * Still, if there's not at least one instruction handled by this
2425 * pattern, this is not a match, and we should try the next one (if
2426 * any).
2427 */
2428 if (!ninstrs)
2429 return false;
2430
2431 /*
2432 * We had a match on the pattern head, but the pattern may be longer
2433 * than the instructions we're asked to execute. We need to make sure
2434 * there's no mandatory elements in the pattern tail.
2435 */
2436 for (; i < pat->nelems; i++) {
2437 if (!pat->elems[i].optional)
2438 return false;
2439 }
2440
2441 /*
2442 * We have a match: update the subop structure accordingly and return
2443 * true.
2444 */
2445 ctx->subop.ninstrs = ninstrs;
2446 ctx->subop.last_instr_end_off = instr_offset;
2447
2448 return true;
2449}
2450
2451#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2452static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2453{
2454 const struct nand_op_instr *instr;
2455 char *prefix = " ";
2456 unsigned int i;
2457
2458 pr_debug("executing subop:\n");
2459
2460 for (i = 0; i < ctx->ninstrs; i++) {
2461 instr = &ctx->instrs[i];
2462
2463 if (instr == &ctx->subop.instrs[0])
2464 prefix = " ->";
2465
2466 switch (instr->type) {
2467 case NAND_OP_CMD_INSTR:
2468 pr_debug("%sCMD [0x%02x]\n", prefix,
2469 instr->ctx.cmd.opcode);
2470 break;
2471 case NAND_OP_ADDR_INSTR:
2472 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2473 instr->ctx.addr.naddrs,
2474 instr->ctx.addr.naddrs < 64 ?
2475 instr->ctx.addr.naddrs : 64,
2476 instr->ctx.addr.addrs);
2477 break;
2478 case NAND_OP_DATA_IN_INSTR:
2479 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2480 instr->ctx.data.len,
2481 instr->ctx.data.force_8bit ?
2482 ", force 8-bit" : "");
2483 break;
2484 case NAND_OP_DATA_OUT_INSTR:
2485 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2486 instr->ctx.data.len,
2487 instr->ctx.data.force_8bit ?
2488 ", force 8-bit" : "");
2489 break;
2490 case NAND_OP_WAITRDY_INSTR:
2491 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2492 instr->ctx.waitrdy.timeout_ms);
2493 break;
2494 }
2495
2496 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2497 prefix = " ";
2498 }
2499}
2500#else
2501static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2502{
2503 /* NOP */
2504}
2505#endif
2506
2507/**
2508 * nand_op_parser_exec_op - exec_op parser
2509 * @chip: the NAND chip
2510 * @parser: patterns description provided by the controller driver
2511 * @op: the NAND operation to address
2512 * @check_only: when true, the function only checks if @op can be handled but
2513 * does not execute the operation
2514 *
2515 * Helper function designed to ease integration of NAND controller drivers that
2516 * only support a limited set of instruction sequences. The supported sequences
2517 * are described in @parser, and the framework takes care of splitting @op into
2518 * multiple sub-operations (if required) and pass them back to the ->exec()
2519 * callback of the matching pattern if @check_only is set to false.
2520 *
2521 * NAND controller drivers should call this function from their own ->exec_op()
2522 * implementation.
2523 *
2524 * Returns 0 on success, a negative error code otherwise. A failure can be
2525 * caused by an unsupported operation (none of the supported patterns is able
2526 * to handle the requested operation), or an error returned by one of the
2527 * matching pattern->exec() hook.
2528 */
2529int nand_op_parser_exec_op(struct nand_chip *chip,
2530 const struct nand_op_parser *parser,
2531 const struct nand_operation *op, bool check_only)
2532{
2533 struct nand_op_parser_ctx ctx = {
2534 .subop.instrs = op->instrs,
2535 .instrs = op->instrs,
2536 .ninstrs = op->ninstrs,
2537 };
2538 unsigned int i;
2539
2540 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2541 int ret;
2542
2543 for (i = 0; i < parser->npatterns; i++) {
2544 const struct nand_op_parser_pattern *pattern;
2545
2546 pattern = &parser->patterns[i];
2547 if (!nand_op_parser_match_pat(pattern, &ctx))
2548 continue;
2549
2550 nand_op_parser_trace(&ctx);
2551
2552 if (check_only)
2553 break;
2554
2555 ret = pattern->exec(chip, &ctx.subop);
2556 if (ret)
2557 return ret;
2558
2559 break;
2560 }
2561
2562 if (i == parser->npatterns) {
2563 pr_debug("->exec_op() parser: pattern not found!\n");
2564 return -ENOTSUPP;
2565 }
2566
2567 /*
2568 * Update the context structure by pointing to the start of the
2569 * next subop.
2570 */
2571 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2572 if (ctx.subop.last_instr_end_off)
2573 ctx.subop.instrs -= 1;
2574
2575 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2576 }
2577
2578 return 0;
2579}
2580EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2581
2582static bool nand_instr_is_data(const struct nand_op_instr *instr)
2583{
2584 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2585 instr->type == NAND_OP_DATA_OUT_INSTR);
2586}
2587
2588static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2589 unsigned int instr_idx)
2590{
2591 return subop && instr_idx < subop->ninstrs;
2592}
2593
2594static int nand_subop_get_start_off(const struct nand_subop *subop,
2595 unsigned int instr_idx)
2596{
2597 if (instr_idx)
2598 return 0;
2599
2600 return subop->first_instr_start_off;
2601}
2602
2603/**
2604 * nand_subop_get_addr_start_off - Get the start offset in an address array
2605 * @subop: The entire sub-operation
2606 * @instr_idx: Index of the instruction inside the sub-operation
2607 *
2608 * During driver development, one could be tempted to directly use the
2609 * ->addr.addrs field of address instructions. This is wrong as address
2610 * instructions might be split.
2611 *
2612 * Given an address instruction, returns the offset of the first cycle to issue.
2613 */
2614int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2615 unsigned int instr_idx)
2616{
2617 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2618 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2619 return -EINVAL;
2620
2621 return nand_subop_get_start_off(subop, instr_idx);
2622}
2623EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2624
2625/**
2626 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2627 * @subop: The entire sub-operation
2628 * @instr_idx: Index of the instruction inside the sub-operation
2629 *
2630 * During driver development, one could be tempted to directly use the
2631 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2632 * might be split.
2633 *
2634 * Given an address instruction, returns the number of address cycle to issue.
2635 */
2636int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2637 unsigned int instr_idx)
2638{
2639 int start_off, end_off;
2640
2641 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2642 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2643 return -EINVAL;
2644
2645 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2646
2647 if (instr_idx == subop->ninstrs - 1 &&
2648 subop->last_instr_end_off)
2649 end_off = subop->last_instr_end_off;
2650 else
2651 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2652
2653 return end_off - start_off;
2654}
2655EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2656
2657/**
2658 * nand_subop_get_data_start_off - Get the start offset in a data array
2659 * @subop: The entire sub-operation
2660 * @instr_idx: Index of the instruction inside the sub-operation
2661 *
2662 * During driver development, one could be tempted to directly use the
2663 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2664 * instructions might be split.
2665 *
2666 * Given a data instruction, returns the offset to start from.
2667 */
2668int nand_subop_get_data_start_off(const struct nand_subop *subop,
2669 unsigned int instr_idx)
2670{
2671 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2672 !nand_instr_is_data(&subop->instrs[instr_idx]))
2673 return -EINVAL;
2674
2675 return nand_subop_get_start_off(subop, instr_idx);
2676}
2677EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2678
2679/**
2680 * nand_subop_get_data_len - Get the number of bytes to retrieve
2681 * @subop: The entire sub-operation
2682 * @instr_idx: Index of the instruction inside the sub-operation
2683 *
2684 * During driver development, one could be tempted to directly use the
2685 * ->data->len field of a data instruction. This is wrong as data instructions
2686 * might be split.
2687 *
2688 * Returns the length of the chunk of data to send/receive.
2689 */
2690int nand_subop_get_data_len(const struct nand_subop *subop,
2691 unsigned int instr_idx)
2692{
2693 int start_off = 0, end_off;
2694
2695 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2696 !nand_instr_is_data(&subop->instrs[instr_idx]))
2697 return -EINVAL;
2698
2699 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2700
2701 if (instr_idx == subop->ninstrs - 1 &&
2702 subop->last_instr_end_off)
2703 end_off = subop->last_instr_end_off;
2704 else
2705 end_off = subop->instrs[instr_idx].ctx.data.len;
2706
2707 return end_off - start_off;
2708}
2709EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002712 * nand_reset - Reset and initialize a NAND device
2713 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002714 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002715 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002716 * Save the timings data structure, then apply SDR timings mode 0 (see
2717 * nand_reset_data_interface for details), do the reset operation, and
2718 * apply back the previous timings.
2719 *
2720 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002721 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002722int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002723{
2724 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal17fa8042017-11-30 18:01:31 +01002725 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002726 int ret;
2727
Boris Brezillon104e4422017-03-16 09:35:58 +01002728 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002729 if (ret)
2730 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002731
Boris Brezillon73f907f2016-10-24 16:46:20 +02002732 /*
2733 * The CS line has to be released before we can apply the new NAND
2734 * interface settings, hence this weird ->select_chip() dance.
2735 */
2736 chip->select_chip(mtd, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002737 ret = nand_reset_op(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02002738 chip->select_chip(mtd, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002739 if (ret)
2740 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002741
Boris Brezillon73f907f2016-10-24 16:46:20 +02002742 chip->select_chip(mtd, chipnr);
Miquel Raynal17fa8042017-11-30 18:01:31 +01002743 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002744 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02002745 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002746 if (ret)
2747 return ret;
2748
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002749 return 0;
2750}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002751EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002752
2753/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002754 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2755 * @buf: buffer to test
2756 * @len: buffer length
2757 * @bitflips_threshold: maximum number of bitflips
2758 *
2759 * Check if a buffer contains only 0xff, which means the underlying region
2760 * has been erased and is ready to be programmed.
2761 * The bitflips_threshold specify the maximum number of bitflips before
2762 * considering the region is not erased.
2763 * Note: The logic of this function has been extracted from the memweight
2764 * implementation, except that nand_check_erased_buf function exit before
2765 * testing the whole buffer if the number of bitflips exceed the
2766 * bitflips_threshold value.
2767 *
2768 * Returns a positive number of bitflips less than or equal to
2769 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2770 * threshold.
2771 */
2772static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2773{
2774 const unsigned char *bitmap = buf;
2775 int bitflips = 0;
2776 int weight;
2777
2778 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2779 len--, bitmap++) {
2780 weight = hweight8(*bitmap);
2781 bitflips += BITS_PER_BYTE - weight;
2782 if (unlikely(bitflips > bitflips_threshold))
2783 return -EBADMSG;
2784 }
2785
2786 for (; len >= sizeof(long);
2787 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002788 unsigned long d = *((unsigned long *)bitmap);
2789 if (d == ~0UL)
2790 continue;
2791 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002792 bitflips += BITS_PER_LONG - weight;
2793 if (unlikely(bitflips > bitflips_threshold))
2794 return -EBADMSG;
2795 }
2796
2797 for (; len > 0; len--, bitmap++) {
2798 weight = hweight8(*bitmap);
2799 bitflips += BITS_PER_BYTE - weight;
2800 if (unlikely(bitflips > bitflips_threshold))
2801 return -EBADMSG;
2802 }
2803
2804 return bitflips;
2805}
2806
2807/**
2808 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2809 * 0xff data
2810 * @data: data buffer to test
2811 * @datalen: data length
2812 * @ecc: ECC buffer
2813 * @ecclen: ECC length
2814 * @extraoob: extra OOB buffer
2815 * @extraooblen: extra OOB length
2816 * @bitflips_threshold: maximum number of bitflips
2817 *
2818 * Check if a data buffer and its associated ECC and OOB data contains only
2819 * 0xff pattern, which means the underlying region has been erased and is
2820 * ready to be programmed.
2821 * The bitflips_threshold specify the maximum number of bitflips before
2822 * considering the region as not erased.
2823 *
2824 * Note:
2825 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2826 * different from the NAND page size. When fixing bitflips, ECC engines will
2827 * report the number of errors per chunk, and the NAND core infrastructure
2828 * expect you to return the maximum number of bitflips for the whole page.
2829 * This is why you should always use this function on a single chunk and
2830 * not on the whole page. After checking each chunk you should update your
2831 * max_bitflips value accordingly.
2832 * 2/ When checking for bitflips in erased pages you should not only check
2833 * the payload data but also their associated ECC data, because a user might
2834 * have programmed almost all bits to 1 but a few. In this case, we
2835 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2836 * this case.
2837 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2838 * data are protected by the ECC engine.
2839 * It could also be used if you support subpages and want to attach some
2840 * extra OOB data to an ECC chunk.
2841 *
2842 * Returns a positive number of bitflips less than or equal to
2843 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2844 * threshold. In case of success, the passed buffers are filled with 0xff.
2845 */
2846int nand_check_erased_ecc_chunk(void *data, int datalen,
2847 void *ecc, int ecclen,
2848 void *extraoob, int extraooblen,
2849 int bitflips_threshold)
2850{
2851 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2852
2853 data_bitflips = nand_check_erased_buf(data, datalen,
2854 bitflips_threshold);
2855 if (data_bitflips < 0)
2856 return data_bitflips;
2857
2858 bitflips_threshold -= data_bitflips;
2859
2860 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2861 if (ecc_bitflips < 0)
2862 return ecc_bitflips;
2863
2864 bitflips_threshold -= ecc_bitflips;
2865
2866 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2867 bitflips_threshold);
2868 if (extraoob_bitflips < 0)
2869 return extraoob_bitflips;
2870
2871 if (data_bitflips)
2872 memset(data, 0xff, datalen);
2873
2874 if (ecc_bitflips)
2875 memset(ecc, 0xff, ecclen);
2876
2877 if (extraoob_bitflips)
2878 memset(extraoob, 0xff, extraooblen);
2879
2880 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2881}
2882EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2883
2884/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002885 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002886 * @mtd: mtd info structure
2887 * @chip: nand chip info structure
2888 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002889 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002890 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002891 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002892 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002893 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002894int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2895 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002896{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002897 int ret;
2898
Boris Brezillon25f815f2017-11-30 18:01:30 +01002899 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002900 if (ret)
2901 return ret;
2902
2903 if (oob_required) {
2904 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2905 false);
2906 if (ret)
2907 return ret;
2908 }
2909
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002910 return 0;
2911}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002912EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002913
2914/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002915 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002916 * @mtd: mtd info structure
2917 * @chip: nand chip info structure
2918 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002919 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002920 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002921 *
2922 * We need a special oob layout and handling even when OOB isn't used.
2923 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002924static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002925 struct nand_chip *chip, uint8_t *buf,
2926 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002927{
2928 int eccsize = chip->ecc.size;
2929 int eccbytes = chip->ecc.bytes;
2930 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002931 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002932
Boris Brezillon25f815f2017-11-30 18:01:30 +01002933 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2934 if (ret)
2935 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002936
2937 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002938 ret = nand_read_data_op(chip, buf, eccsize, false);
2939 if (ret)
2940 return ret;
2941
David Brownell52ff49d2009-03-04 12:01:36 -08002942 buf += eccsize;
2943
2944 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002945 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2946 false);
2947 if (ret)
2948 return ret;
2949
David Brownell52ff49d2009-03-04 12:01:36 -08002950 oob += chip->ecc.prepad;
2951 }
2952
Boris Brezillon97d90da2017-11-30 18:01:29 +01002953 ret = nand_read_data_op(chip, oob, eccbytes, false);
2954 if (ret)
2955 return ret;
2956
David Brownell52ff49d2009-03-04 12:01:36 -08002957 oob += eccbytes;
2958
2959 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002960 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2961 false);
2962 if (ret)
2963 return ret;
2964
David Brownell52ff49d2009-03-04 12:01:36 -08002965 oob += chip->ecc.postpad;
2966 }
2967 }
2968
2969 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002970 if (size) {
2971 ret = nand_read_data_op(chip, oob, size, false);
2972 if (ret)
2973 return ret;
2974 }
David Brownell52ff49d2009-03-04 12:01:36 -08002975
2976 return 0;
2977}
2978
2979/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002980 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002981 * @mtd: mtd info structure
2982 * @chip: nand chip info structure
2983 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002984 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002985 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002986 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002987static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002988 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Boris Brezillon846031d2016-02-03 20:11:00 +01002990 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002991 int eccbytes = chip->ecc.bytes;
2992 int eccsteps = chip->ecc.steps;
2993 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002994 uint8_t *ecc_calc = chip->ecc.calc_buf;
2995 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002996 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002997
Brian Norris1fbb9382012-05-02 10:14:55 -07002998 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002999
3000 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3001 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3002
Boris Brezillon846031d2016-02-03 20:11:00 +01003003 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3004 chip->ecc.total);
3005 if (ret)
3006 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003007
3008 eccsteps = chip->ecc.steps;
3009 p = buf;
3010
3011 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3012 int stat;
3013
3014 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003015 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003016 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003017 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003018 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003019 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3020 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003021 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003022 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003023}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303026 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003027 * @mtd: mtd info structure
3028 * @chip: nand chip info structure
3029 * @data_offs: offset of requested data within the page
3030 * @readlen: data length
3031 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003032 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003033 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003034static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003035 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3036 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003037{
Boris Brezillon846031d2016-02-03 20:11:00 +01003038 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003039 uint8_t *p;
3040 int data_col_addr, i, gaps = 0;
3041 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3042 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003043 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003044 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003045 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003046
Brian Norris7854d3f2011-06-23 14:12:08 -07003047 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003048 start_step = data_offs / chip->ecc.size;
3049 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3050 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303051 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003052
Brian Norris8b6e50c2011-05-25 14:59:01 -07003053 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003054 datafrag_len = num_steps * chip->ecc.size;
3055 eccfrag_len = num_steps * chip->ecc.bytes;
3056
3057 data_col_addr = start_step * chip->ecc.size;
3058 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003059 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003060 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003061 if (ret)
3062 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003063
Brian Norris8b6e50c2011-05-25 14:59:01 -07003064 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003065 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003066 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003067
Brian Norris8b6e50c2011-05-25 14:59:01 -07003068 /*
3069 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003070 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003071 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003072 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3073 if (ret)
3074 return ret;
3075
3076 if (oobregion.length < eccfrag_len)
3077 gaps = 1;
3078
Alexey Korolev3d459552008-05-15 17:23:18 +01003079 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003080 ret = nand_change_read_column_op(chip, mtd->writesize,
3081 chip->oob_poi, mtd->oobsize,
3082 false);
3083 if (ret)
3084 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003085 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003086 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003087 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003088 * about buswidth alignment in read_buf.
3089 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003090 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003091 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003092 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003093 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003094 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3095 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003096 aligned_len++;
3097
Boris Brezillon97d90da2017-11-30 18:01:29 +01003098 ret = nand_change_read_column_op(chip,
3099 mtd->writesize + aligned_pos,
3100 &chip->oob_poi[aligned_pos],
3101 aligned_len, false);
3102 if (ret)
3103 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003104 }
3105
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003106 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003107 chip->oob_poi, index, eccfrag_len);
3108 if (ret)
3109 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003110
3111 p = bufpoi + data_col_addr;
3112 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3113 int stat;
3114
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003115 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3116 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003117 if (stat == -EBADMSG &&
3118 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3119 /* check for empty pages with bitflips */
3120 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003121 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003122 chip->ecc.bytes,
3123 NULL, 0,
3124 chip->ecc.strength);
3125 }
3126
Mike Dunn3f91e942012-04-25 12:06:09 -07003127 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003128 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003129 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003130 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003131 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3132 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003133 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003134 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003135}
3136
3137/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003138 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003139 * @mtd: mtd info structure
3140 * @chip: nand chip info structure
3141 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003142 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003143 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003144 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003145 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003146 */
3147static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003148 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003149{
Boris Brezillon846031d2016-02-03 20:11:00 +01003150 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003151 int eccbytes = chip->ecc.bytes;
3152 int eccsteps = chip->ecc.steps;
3153 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003154 uint8_t *ecc_calc = chip->ecc.calc_buf;
3155 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003156 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003157
Boris Brezillon25f815f2017-11-30 18:01:30 +01003158 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3159 if (ret)
3160 return ret;
3161
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003162 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3163 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003164
3165 ret = nand_read_data_op(chip, p, eccsize, false);
3166 if (ret)
3167 return ret;
3168
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003169 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3170 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003171
3172 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3173 if (ret)
3174 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003175
Boris Brezillon846031d2016-02-03 20:11:00 +01003176 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3177 chip->ecc.total);
3178 if (ret)
3179 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003180
3181 eccsteps = chip->ecc.steps;
3182 p = buf;
3183
3184 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3185 int stat;
3186
3187 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003188 if (stat == -EBADMSG &&
3189 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3190 /* check for empty pages with bitflips */
3191 stat = nand_check_erased_ecc_chunk(p, eccsize,
3192 &ecc_code[i], eccbytes,
3193 NULL, 0,
3194 chip->ecc.strength);
3195 }
3196
Mike Dunn3f91e942012-04-25 12:06:09 -07003197 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003198 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003199 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003200 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003201 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3202 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003203 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003204 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003205}
3206
3207/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003208 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003209 * @mtd: mtd info structure
3210 * @chip: nand chip info structure
3211 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003212 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003213 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003214 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003215 * Hardware ECC for large page chips, require OOB to be read first. For this
3216 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3217 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3218 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3219 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003220 */
3221static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003222 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003223{
Boris Brezillon846031d2016-02-03 20:11:00 +01003224 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003225 int eccbytes = chip->ecc.bytes;
3226 int eccsteps = chip->ecc.steps;
3227 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003228 uint8_t *ecc_code = chip->ecc.code_buf;
3229 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003230 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003231
3232 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003233 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3234 if (ret)
3235 return ret;
3236
3237 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3238 if (ret)
3239 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003240
Boris Brezillon846031d2016-02-03 20:11:00 +01003241 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3242 chip->ecc.total);
3243 if (ret)
3244 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003245
3246 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3247 int stat;
3248
3249 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003250
3251 ret = nand_read_data_op(chip, p, eccsize, false);
3252 if (ret)
3253 return ret;
3254
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003255 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3256
3257 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003258 if (stat == -EBADMSG &&
3259 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3260 /* check for empty pages with bitflips */
3261 stat = nand_check_erased_ecc_chunk(p, eccsize,
3262 &ecc_code[i], eccbytes,
3263 NULL, 0,
3264 chip->ecc.strength);
3265 }
3266
Mike Dunn3f91e942012-04-25 12:06:09 -07003267 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003268 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003269 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003270 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003271 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3272 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003273 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003274 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003275}
3276
3277/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003278 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003279 * @mtd: mtd info structure
3280 * @chip: nand chip info structure
3281 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003282 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003283 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003284 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003285 * The hw generator calculates the error syndrome automatically. Therefore we
3286 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003287 */
3288static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003289 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003290{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003291 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003292 int eccbytes = chip->ecc.bytes;
3293 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003294 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003295 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003296 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003297 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003298
Boris Brezillon25f815f2017-11-30 18:01:30 +01003299 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3300 if (ret)
3301 return ret;
3302
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003303 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3304 int stat;
3305
3306 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003307
3308 ret = nand_read_data_op(chip, p, eccsize, false);
3309 if (ret)
3310 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003311
3312 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003313 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3314 false);
3315 if (ret)
3316 return ret;
3317
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003318 oob += chip->ecc.prepad;
3319 }
3320
3321 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003322
3323 ret = nand_read_data_op(chip, oob, eccbytes, false);
3324 if (ret)
3325 return ret;
3326
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003327 stat = chip->ecc.correct(mtd, p, oob, NULL);
3328
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003329 oob += eccbytes;
3330
3331 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003332 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3333 false);
3334 if (ret)
3335 return ret;
3336
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003337 oob += chip->ecc.postpad;
3338 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003339
3340 if (stat == -EBADMSG &&
3341 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3342 /* check for empty pages with bitflips */
3343 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3344 oob - eccpadbytes,
3345 eccpadbytes,
3346 NULL, 0,
3347 chip->ecc.strength);
3348 }
3349
3350 if (stat < 0) {
3351 mtd->ecc_stats.failed++;
3352 } else {
3353 mtd->ecc_stats.corrected += stat;
3354 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3355 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003356 }
3357
3358 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003359 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003360 if (i) {
3361 ret = nand_read_data_op(chip, oob, i, false);
3362 if (ret)
3363 return ret;
3364 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003365
Mike Dunn3f91e942012-04-25 12:06:09 -07003366 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003367}
3368
3369/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003370 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003371 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003372 * @oob: oob destination address
3373 * @ops: oob ops structure
3374 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003375 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003376static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003377 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003378{
Boris Brezillon846031d2016-02-03 20:11:00 +01003379 struct nand_chip *chip = mtd_to_nand(mtd);
3380 int ret;
3381
Florian Fainellif8ac0412010-09-07 13:23:43 +02003382 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003383
Brian Norris0612b9d2011-08-30 18:45:40 -07003384 case MTD_OPS_PLACE_OOB:
3385 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003386 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3387 return oob + len;
3388
Boris Brezillon846031d2016-02-03 20:11:00 +01003389 case MTD_OPS_AUTO_OOB:
3390 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3391 ops->ooboffs, len);
3392 BUG_ON(ret);
3393 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003394
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003395 default:
3396 BUG();
3397 }
3398 return NULL;
3399}
3400
3401/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003402 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3403 * @mtd: MTD device structure
3404 * @retry_mode: the retry mode to use
3405 *
3406 * Some vendors supply a special command to shift the Vt threshold, to be used
3407 * when there are too many bitflips in a page (i.e., ECC error). After setting
3408 * a new threshold, the host should retry reading the page.
3409 */
3410static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3411{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003412 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08003413
3414 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3415
3416 if (retry_mode >= chip->read_retries)
3417 return -EINVAL;
3418
3419 if (!chip->setup_read_retry)
3420 return -EOPNOTSUPP;
3421
3422 return chip->setup_read_retry(mtd, retry_mode);
3423}
3424
3425/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003426 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003427 * @mtd: MTD device structure
3428 * @from: offset to read from
3429 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003430 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003431 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003432 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003433static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3434 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003435{
Brian Norrise47f3db2012-05-02 10:14:56 -07003436 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003437 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003438 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003439 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003440 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003441 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003442
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003443 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003444 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003445 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003446 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003447 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003449 chipnr = (int)(from >> chip->chip_shift);
3450 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003452 realpage = (int)(from >> chip->page_shift);
3453 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003455 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003457 buf = ops->datbuf;
3458 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003459 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003460
Florian Fainellif8ac0412010-09-07 13:23:43 +02003461 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003462 unsigned int ecc_failures = mtd->ecc_stats.failed;
3463
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003464 bytes = min(mtd->writesize - col, readlen);
3465 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003466
Kamal Dasu66507c72014-05-01 20:51:19 -04003467 if (!aligned)
3468 use_bufpoi = 1;
3469 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003470 use_bufpoi = !virt_addr_valid(buf) ||
3471 !IS_ALIGNED((unsigned long)buf,
3472 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003473 else
3474 use_bufpoi = 0;
3475
Brian Norris8b6e50c2011-05-25 14:59:01 -07003476 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003477 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003478 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003479
3480 if (use_bufpoi && aligned)
3481 pr_debug("%s: using read bounce buffer for buf@%p\n",
3482 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Brian Norrisba84fb52014-01-03 15:13:33 -08003484read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003485 /*
3486 * Now read the page into the buffer. Absent an error,
3487 * the read methods return max bitflips per ecc step.
3488 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003489 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07003490 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003491 oob_required,
3492 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003493 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3494 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003495 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003496 col, bytes, bufpoi,
3497 page);
David Woodhouse956e9442006-09-25 17:12:39 +01003498 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07003499 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003500 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003501 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003502 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003503 /* Invalidate page cache */
3504 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003505 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003506 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003507
3508 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003509 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003510 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003511 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003512 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003513 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003514 chip->pagebuf_bitflips = ret;
3515 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003516 /* Invalidate page cache */
3517 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003518 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003519 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003521
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003522 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003523 int toread = min(oobreadlen, max_oobsize);
3524
3525 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003526 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003527 oob, ops, toread);
3528 oobreadlen -= toread;
3529 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003530 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003531
3532 if (chip->options & NAND_NEED_READRDY) {
3533 /* Apply delay or wait for ready/busy pin */
3534 if (!chip->dev_ready)
3535 udelay(chip->chip_delay);
3536 else
3537 nand_wait_ready(mtd);
3538 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08003539
Brian Norrisba84fb52014-01-03 15:13:33 -08003540 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003541 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003542 retry_mode++;
3543 ret = nand_setup_read_retry(mtd,
3544 retry_mode);
3545 if (ret < 0)
3546 break;
3547
3548 /* Reset failures; retry */
3549 mtd->ecc_stats.failed = ecc_failures;
3550 goto read_retry;
3551 } else {
3552 /* No more retry modes; real failure */
3553 ecc_fail = true;
3554 }
3555 }
3556
3557 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003558 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003559 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003560 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003561 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003562 max_bitflips = max_t(unsigned int, max_bitflips,
3563 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003566 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003567
Brian Norrisba84fb52014-01-03 15:13:33 -08003568 /* Reset to retry mode 0 */
3569 if (retry_mode) {
3570 ret = nand_setup_read_retry(mtd, 0);
3571 if (ret < 0)
3572 break;
3573 retry_mode = 0;
3574 }
3575
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003576 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003577 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Brian Norris8b6e50c2011-05-25 14:59:01 -07003579 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 col = 0;
3581 /* Increment page address */
3582 realpage++;
3583
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003584 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 /* Check, if we cross a chip boundary */
3586 if (!page) {
3587 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003588 chip->select_chip(mtd, -1);
3589 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003592 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003594 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003595 if (oob)
3596 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Mike Dunn3f91e942012-04-25 12:06:09 -07003598 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003599 return ret;
3600
Brian Norrisb72f3df2013-12-03 11:04:14 -08003601 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003602 return -EBADMSG;
3603
Mike Dunnedbc45402012-04-25 12:06:11 -07003604 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003605}
3606
3607/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003608 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003609 * @mtd: mtd info structure
3610 * @chip: nand chip info structure
3611 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003612 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003613int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003614{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003615 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003616}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003617EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003618
3619/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003620 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003621 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003622 * @mtd: mtd info structure
3623 * @chip: nand chip info structure
3624 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003625 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003626int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3627 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003628{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003629 int length = mtd->oobsize;
3630 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3631 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003632 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003633 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003634
Boris Brezillon97d90da2017-11-30 18:01:29 +01003635 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3636 if (ret)
3637 return ret;
3638
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003639 for (i = 0; i < chip->ecc.steps; i++) {
3640 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003641 int ret;
3642
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003643 pos = eccsize + i * (eccsize + chunk);
3644 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003645 ret = nand_change_read_column_op(chip, pos,
3646 NULL, 0,
3647 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003648 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003649 ret = nand_read_page_op(chip, page, pos, NULL,
3650 0);
3651
3652 if (ret)
3653 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003654 } else
3655 sndrnd = 1;
3656 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003657
3658 ret = nand_read_data_op(chip, bufpoi, toread, false);
3659 if (ret)
3660 return ret;
3661
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003662 bufpoi += toread;
3663 length -= toread;
3664 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003665 if (length > 0) {
3666 ret = nand_read_data_op(chip, bufpoi, length, false);
3667 if (ret)
3668 return ret;
3669 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003670
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003671 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003672}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003673EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003674
3675/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003676 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003677 * @mtd: mtd info structure
3678 * @chip: nand chip info structure
3679 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003680 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003681int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003682{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003683 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3684 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003685}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003686EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003687
3688/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003689 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003690 * with syndrome - only for large page flash
3691 * @mtd: mtd info structure
3692 * @chip: nand chip info structure
3693 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003694 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003695int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3696 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003697{
3698 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3699 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003700 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003701 const uint8_t *bufpoi = chip->oob_poi;
3702
3703 /*
3704 * data-ecc-data-ecc ... ecc-oob
3705 * or
3706 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3707 */
3708 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3709 pos = steps * (eccsize + chunk);
3710 steps = 0;
3711 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003712 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003713
Boris Brezillon97d90da2017-11-30 18:01:29 +01003714 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3715 if (ret)
3716 return ret;
3717
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003718 for (i = 0; i < steps; i++) {
3719 if (sndcmd) {
3720 if (mtd->writesize <= 512) {
3721 uint32_t fill = 0xFFFFFFFF;
3722
3723 len = eccsize;
3724 while (len > 0) {
3725 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003726
3727 ret = nand_write_data_op(chip, &fill,
3728 num, false);
3729 if (ret)
3730 return ret;
3731
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003732 len -= num;
3733 }
3734 } else {
3735 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003736 ret = nand_change_write_column_op(chip, pos,
3737 NULL, 0,
3738 false);
3739 if (ret)
3740 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003741 }
3742 } else
3743 sndcmd = 1;
3744 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003745
3746 ret = nand_write_data_op(chip, bufpoi, len, false);
3747 if (ret)
3748 return ret;
3749
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003750 bufpoi += len;
3751 length -= len;
3752 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003753 if (length > 0) {
3754 ret = nand_write_data_op(chip, bufpoi, length, false);
3755 if (ret)
3756 return ret;
3757 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003758
Boris Brezillon97d90da2017-11-30 18:01:29 +01003759 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003760}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003761EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003762
3763/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003764 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003765 * @mtd: MTD device structure
3766 * @from: offset to read from
3767 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003769 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003771static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3772 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003774 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003775 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003776 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003777 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003778 int readlen = ops->ooblen;
3779 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003780 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003781 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Brian Norris289c0522011-07-19 10:06:09 -07003783 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303784 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Brian Norris041e4572011-06-23 16:45:24 -07003786 stats = mtd->ecc_stats;
3787
Boris BREZILLON29f10582016-03-07 10:46:52 +01003788 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003789
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003790 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003791 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003793 /* Shift to get page */
3794 realpage = (int)(from >> chip->page_shift);
3795 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Florian Fainellif8ac0412010-09-07 13:23:43 +02003797 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003798 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003799 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003800 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003801 ret = chip->ecc.read_oob(mtd, chip, page);
3802
3803 if (ret < 0)
3804 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003805
3806 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003807 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003808
Brian Norris5bc7c332013-03-13 09:51:31 -07003809 if (chip->options & NAND_NEED_READRDY) {
3810 /* Apply delay or wait for ready/busy pin */
3811 if (!chip->dev_ready)
3812 udelay(chip->chip_delay);
3813 else
3814 nand_wait_ready(mtd);
3815 }
3816
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003817 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3818
Vitaly Wool70145682006-11-03 18:20:38 +03003819 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003820 if (!readlen)
3821 break;
3822
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003823 /* Increment page address */
3824 realpage++;
3825
3826 page = realpage & chip->pagemask;
3827 /* Check, if we cross a chip boundary */
3828 if (!page) {
3829 chipnr++;
3830 chip->select_chip(mtd, -1);
3831 chip->select_chip(mtd, chipnr);
3832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003834 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003836 ops->oobretlen = ops->ooblen - readlen;
3837
3838 if (ret < 0)
3839 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003840
3841 if (mtd->ecc_stats.failed - stats.failed)
3842 return -EBADMSG;
3843
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003844 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845}
3846
3847/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003848 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003849 * @mtd: MTD device structure
3850 * @from: offset to read from
3851 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003853 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003855static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3856 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003858 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003859
3860 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003862 if (ops->mode != MTD_OPS_PLACE_OOB &&
3863 ops->mode != MTD_OPS_AUTO_OOB &&
3864 ops->mode != MTD_OPS_RAW)
3865 return -ENOTSUPP;
3866
Huang Shijie6a8214a2012-11-19 14:43:30 +08003867 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003869 if (!ops->datbuf)
3870 ret = nand_do_read_oob(mtd, from, ops);
3871 else
3872 ret = nand_do_read_ops(mtd, from, ops);
3873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003875 return ret;
3876}
3877
3878
3879/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003880 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003881 * @mtd: mtd info structure
3882 * @chip: nand chip info structure
3883 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003884 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003885 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003886 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003887 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003888 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003889int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
3890 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003891{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003892 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003893
Boris Brezillon25f815f2017-11-30 18:01:30 +01003894 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003895 if (ret)
3896 return ret;
3897
3898 if (oob_required) {
3899 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3900 false);
3901 if (ret)
3902 return ret;
3903 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003904
Boris Brezillon25f815f2017-11-30 18:01:30 +01003905 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003907EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003909/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003910 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003911 * @mtd: mtd info structure
3912 * @chip: nand chip info structure
3913 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003914 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003915 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003916 *
3917 * We need a special oob layout and handling even when ECC isn't checked.
3918 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003919static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003920 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003921 const uint8_t *buf, int oob_required,
3922 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003923{
3924 int eccsize = chip->ecc.size;
3925 int eccbytes = chip->ecc.bytes;
3926 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003927 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003928
Boris Brezillon25f815f2017-11-30 18:01:30 +01003929 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3930 if (ret)
3931 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003932
3933 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003934 ret = nand_write_data_op(chip, buf, eccsize, false);
3935 if (ret)
3936 return ret;
3937
David Brownell52ff49d2009-03-04 12:01:36 -08003938 buf += eccsize;
3939
3940 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003941 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3942 false);
3943 if (ret)
3944 return ret;
3945
David Brownell52ff49d2009-03-04 12:01:36 -08003946 oob += chip->ecc.prepad;
3947 }
3948
Boris Brezillon97d90da2017-11-30 18:01:29 +01003949 ret = nand_write_data_op(chip, oob, eccbytes, false);
3950 if (ret)
3951 return ret;
3952
David Brownell52ff49d2009-03-04 12:01:36 -08003953 oob += eccbytes;
3954
3955 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003956 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3957 false);
3958 if (ret)
3959 return ret;
3960
David Brownell52ff49d2009-03-04 12:01:36 -08003961 oob += chip->ecc.postpad;
3962 }
3963 }
3964
3965 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003966 if (size) {
3967 ret = nand_write_data_op(chip, oob, size, false);
3968 if (ret)
3969 return ret;
3970 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003971
Boris Brezillon25f815f2017-11-30 18:01:30 +01003972 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003973}
3974/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003975 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003976 * @mtd: mtd info structure
3977 * @chip: nand chip info structure
3978 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003979 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003980 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003981 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003982static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003983 const uint8_t *buf, int oob_required,
3984 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003985{
Boris Brezillon846031d2016-02-03 20:11:00 +01003986 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003987 int eccbytes = chip->ecc.bytes;
3988 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003989 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003990 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003991
Brian Norris7854d3f2011-06-23 14:12:08 -07003992 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003993 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3994 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003995
Boris Brezillon846031d2016-02-03 20:11:00 +01003996 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3997 chip->ecc.total);
3998 if (ret)
3999 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004000
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004001 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004002}
4003
4004/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004005 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004006 * @mtd: mtd info structure
4007 * @chip: nand chip info structure
4008 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004009 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004010 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004011 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004012static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004013 const uint8_t *buf, int oob_required,
4014 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004015{
Boris Brezillon846031d2016-02-03 20:11:00 +01004016 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004017 int eccbytes = chip->ecc.bytes;
4018 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004019 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004020 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004021
Boris Brezillon25f815f2017-11-30 18:01:30 +01004022 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4023 if (ret)
4024 return ret;
4025
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004026 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4027 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004028
4029 ret = nand_write_data_op(chip, p, eccsize, false);
4030 if (ret)
4031 return ret;
4032
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004033 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
4034 }
4035
Boris Brezillon846031d2016-02-03 20:11:00 +01004036 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4037 chip->ecc.total);
4038 if (ret)
4039 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004040
Boris Brezillon97d90da2017-11-30 18:01:29 +01004041 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4042 if (ret)
4043 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004044
Boris Brezillon25f815f2017-11-30 18:01:30 +01004045 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004046}
4047
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304048
4049/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004050 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304051 * @mtd: mtd info structure
4052 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004053 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304054 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004055 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304056 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004057 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304058 */
4059static int nand_write_subpage_hwecc(struct mtd_info *mtd,
4060 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07004061 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004062 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304063{
4064 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004065 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304066 int ecc_size = chip->ecc.size;
4067 int ecc_bytes = chip->ecc.bytes;
4068 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304069 uint32_t start_step = offset / ecc_size;
4070 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4071 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004072 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304073
Boris Brezillon25f815f2017-11-30 18:01:30 +01004074 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4075 if (ret)
4076 return ret;
4077
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304078 for (step = 0; step < ecc_steps; step++) {
4079 /* configure controller for WRITE access */
4080 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
4081
4082 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004083 ret = nand_write_data_op(chip, buf, ecc_size, false);
4084 if (ret)
4085 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304086
4087 /* mask ECC of un-touched subpages by padding 0xFF */
4088 if ((step < start_step) || (step > end_step))
4089 memset(ecc_calc, 0xff, ecc_bytes);
4090 else
Brian Norrisd6a950802013-08-08 17:16:36 -07004091 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304092
4093 /* mask OOB of un-touched subpages by padding 0xFF */
4094 /* if oob_required, preserve OOB metadata of written subpage */
4095 if (!oob_required || (step < start_step) || (step > end_step))
4096 memset(oob_buf, 0xff, oob_bytes);
4097
Brian Norrisd6a950802013-08-08 17:16:36 -07004098 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304099 ecc_calc += ecc_bytes;
4100 oob_buf += oob_bytes;
4101 }
4102
4103 /* copy calculated ECC for whole page to chip->buffer->oob */
4104 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004105 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004106 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4107 chip->ecc.total);
4108 if (ret)
4109 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304110
4111 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004112 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4113 if (ret)
4114 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304115
Boris Brezillon25f815f2017-11-30 18:01:30 +01004116 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304117}
4118
4119
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004120/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004121 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004122 * @mtd: mtd info structure
4123 * @chip: nand chip info structure
4124 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004125 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004126 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004127 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004128 * The hw generator calculates the error syndrome automatically. Therefore we
4129 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004130 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004131static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07004132 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004133 const uint8_t *buf, int oob_required,
4134 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004135{
4136 int i, eccsize = chip->ecc.size;
4137 int eccbytes = chip->ecc.bytes;
4138 int eccsteps = chip->ecc.steps;
4139 const uint8_t *p = buf;
4140 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004141 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004142
Boris Brezillon25f815f2017-11-30 18:01:30 +01004143 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4144 if (ret)
4145 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004146
4147 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004148 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004149
4150 ret = nand_write_data_op(chip, p, eccsize, false);
4151 if (ret)
4152 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004153
4154 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004155 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4156 false);
4157 if (ret)
4158 return ret;
4159
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004160 oob += chip->ecc.prepad;
4161 }
4162
4163 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004164
4165 ret = nand_write_data_op(chip, oob, eccbytes, false);
4166 if (ret)
4167 return ret;
4168
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004169 oob += eccbytes;
4170
4171 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004172 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4173 false);
4174 if (ret)
4175 return ret;
4176
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004177 oob += chip->ecc.postpad;
4178 }
4179 }
4180
4181 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004182 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004183 if (i) {
4184 ret = nand_write_data_op(chip, oob, i, false);
4185 if (ret)
4186 return ret;
4187 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004188
Boris Brezillon25f815f2017-11-30 18:01:30 +01004189 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004190}
4191
4192/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004193 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004194 * @mtd: MTD device structure
4195 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304196 * @offset: address offset within the page
4197 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004198 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004199 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004200 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004201 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004202 */
4203static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304204 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004205 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004206{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304207 int status, subpage;
4208
4209 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4210 chip->ecc.write_subpage)
4211 subpage = offset || (data_len < mtd->writesize);
4212 else
4213 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004214
David Woodhouse956e9442006-09-25 17:12:39 +01004215 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304216 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004217 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304218 else if (subpage)
4219 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004220 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004221 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004222 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
4223 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004224
4225 if (status < 0)
4226 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004227
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004228 return 0;
4229}
4230
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004231/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004232 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004233 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004234 * @oob: oob data buffer
4235 * @len: oob data write length
4236 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004237 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004238static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4239 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004240{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004241 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004242 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004243
4244 /*
4245 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4246 * data from a previous OOB read.
4247 */
4248 memset(chip->oob_poi, 0xff, mtd->oobsize);
4249
Florian Fainellif8ac0412010-09-07 13:23:43 +02004250 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004251
Brian Norris0612b9d2011-08-30 18:45:40 -07004252 case MTD_OPS_PLACE_OOB:
4253 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004254 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4255 return oob + len;
4256
Boris Brezillon846031d2016-02-03 20:11:00 +01004257 case MTD_OPS_AUTO_OOB:
4258 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4259 ops->ooboffs, len);
4260 BUG_ON(ret);
4261 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004262
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004263 default:
4264 BUG();
4265 }
4266 return NULL;
4267}
4268
Florian Fainellif8ac0412010-09-07 13:23:43 +02004269#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004270
4271/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004272 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004273 * @mtd: MTD device structure
4274 * @to: offset to write to
4275 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004276 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004277 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004278 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004279static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4280 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004281{
Corentin Labbe73600b62017-09-02 10:49:38 +02004282 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004283 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004284 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004285
4286 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004287 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004288
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004289 uint8_t *oob = ops->oobbuf;
4290 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304291 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004292 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004293
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004294 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004295 if (!writelen)
4296 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004297
Brian Norris8b6e50c2011-05-25 14:59:01 -07004298 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004299 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004300 pr_notice("%s: attempt to write non page aligned data\n",
4301 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004302 return -EINVAL;
4303 }
4304
Thomas Gleixner29072b92006-09-28 15:38:36 +02004305 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004306
Thomas Gleixner6a930962006-06-28 00:11:45 +02004307 chipnr = (int)(to >> chip->chip_shift);
4308 chip->select_chip(mtd, chipnr);
4309
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004310 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004311 if (nand_check_wp(mtd)) {
4312 ret = -EIO;
4313 goto err_out;
4314 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004315
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004316 realpage = (int)(to >> chip->page_shift);
4317 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004318
4319 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004320 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4321 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004322 chip->pagebuf = -1;
4323
Maxim Levitsky782ce792010-02-22 20:39:36 +02004324 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004325 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4326 ret = -EINVAL;
4327 goto err_out;
4328 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004329
Florian Fainellif8ac0412010-09-07 13:23:43 +02004330 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004331 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004332 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004333 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004334 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004335
Kamal Dasu66507c72014-05-01 20:51:19 -04004336 if (part_pagewr)
4337 use_bufpoi = 1;
4338 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004339 use_bufpoi = !virt_addr_valid(buf) ||
4340 !IS_ALIGNED((unsigned long)buf,
4341 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004342 else
4343 use_bufpoi = 0;
4344
4345 /* Partial page write?, or need to use bounce buffer */
4346 if (use_bufpoi) {
4347 pr_debug("%s: using write bounce buffer for buf@%p\n",
4348 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004349 if (part_pagewr)
4350 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004351 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004352 memset(chip->data_buf, 0xff, mtd->writesize);
4353 memcpy(&chip->data_buf[column], buf, bytes);
4354 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004355 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004356
Maxim Levitsky782ce792010-02-22 20:39:36 +02004357 if (unlikely(oob)) {
4358 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004359 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004360 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004361 } else {
4362 /* We still need to erase leftover OOB data */
4363 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004364 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004365
4366 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004367 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004368 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004369 if (ret)
4370 break;
4371
4372 writelen -= bytes;
4373 if (!writelen)
4374 break;
4375
Thomas Gleixner29072b92006-09-28 15:38:36 +02004376 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004377 buf += bytes;
4378 realpage++;
4379
4380 page = realpage & chip->pagemask;
4381 /* Check, if we cross a chip boundary */
4382 if (!page) {
4383 chipnr++;
4384 chip->select_chip(mtd, -1);
4385 chip->select_chip(mtd, chipnr);
4386 }
4387 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004388
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004389 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004390 if (unlikely(oob))
4391 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004392
4393err_out:
4394 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004395 return ret;
4396}
4397
4398/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004399 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004400 * @mtd: MTD device structure
4401 * @to: offset to write to
4402 * @len: number of bytes to write
4403 * @retlen: pointer to variable to store the number of written bytes
4404 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004405 *
4406 * NAND write with ECC. Used when performing writes in interrupt context, this
4407 * may for example be called by mtdoops when writing an oops while in panic.
4408 */
4409static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4410 size_t *retlen, const uint8_t *buf)
4411{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004412 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004413 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004414 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004415 int ret;
4416
Brian Norris8b6e50c2011-05-25 14:59:01 -07004417 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004418 panic_nand_get_device(chip, mtd, FL_WRITING);
4419
Brent Taylor30863e382017-10-30 22:32:45 -05004420 chip->select_chip(mtd, chipnr);
4421
4422 /* Wait for the device to get ready */
4423 panic_nand_wait(mtd, chip, 400);
4424
Brian Norris0ec56dc2015-02-28 02:02:30 -08004425 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004426 ops.len = len;
4427 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004428 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004429
Brian Norris4a89ff82011-08-30 18:45:45 -07004430 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004431
Brian Norris4a89ff82011-08-30 18:45:45 -07004432 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004433 return ret;
4434}
4435
4436/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004437 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004438 * @mtd: MTD device structure
4439 * @to: offset to write to
4440 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004441 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004442 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004443 */
4444static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4445 struct mtd_oob_ops *ops)
4446{
Adrian Hunter03736152007-01-31 17:58:29 +02004447 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004448 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Brian Norris289c0522011-07-19 10:06:09 -07004450 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304451 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
Boris BREZILLON29f10582016-03-07 10:46:52 +01004453 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004454
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004456 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004457 pr_debug("%s: attempt to write past end of page\n",
4458 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 return -EINVAL;
4460 }
4461
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004462 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004463
4464 /*
4465 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4466 * of my DiskOnChip 2000 test units) will clear the whole data page too
4467 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4468 * it in the doc2000 driver in August 1999. dwmw2.
4469 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004470 nand_reset(chip, chipnr);
4471
4472 chip->select_chip(mtd, chipnr);
4473
4474 /* Shift to get page */
4475 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
4477 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004478 if (nand_check_wp(mtd)) {
4479 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004480 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004481 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004484 if (page == chip->pagebuf)
4485 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004487 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004488
Brian Norris0612b9d2011-08-30 18:45:40 -07004489 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07004490 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
4491 else
4492 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004493
Huang Shijieb0bb6902012-11-19 14:43:29 +08004494 chip->select_chip(mtd, -1);
4495
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004496 if (status)
4497 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Vitaly Wool70145682006-11-03 18:20:38 +03004499 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004501 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004504/**
4505 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004506 * @mtd: MTD device structure
4507 * @to: offset to write to
4508 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004509 */
4510static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4511 struct mtd_oob_ops *ops)
4512{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004513 int ret = -ENOTSUPP;
4514
4515 ops->retlen = 0;
4516
Huang Shijie6a8214a2012-11-19 14:43:30 +08004517 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004518
Florian Fainellif8ac0412010-09-07 13:23:43 +02004519 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004520 case MTD_OPS_PLACE_OOB:
4521 case MTD_OPS_AUTO_OOB:
4522 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004523 break;
4524
4525 default:
4526 goto out;
4527 }
4528
4529 if (!ops->datbuf)
4530 ret = nand_do_write_oob(mtd, to, ops);
4531 else
4532 ret = nand_do_write_ops(mtd, to, ops);
4533
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004534out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004535 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 return ret;
4537}
4538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539/**
Brian Norris49c50b92014-05-06 16:02:19 -07004540 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004541 * @mtd: MTD device structure
4542 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 *
Brian Norris49c50b92014-05-06 16:02:19 -07004544 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 */
Brian Norris49c50b92014-05-06 16:02:19 -07004546static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004548 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004549 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004550
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004552 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004553
Boris Brezillon97d90da2017-11-30 18:01:29 +01004554 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555}
4556
4557/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004559 * @mtd: MTD device structure
4560 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004562 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004564static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
David Woodhousee0c7d762006-05-13 18:07:53 +01004566 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004568
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004570 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004571 * @mtd: MTD device structure
4572 * @instr: erase instruction
4573 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004575 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004577int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4578 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579{
Adrian Hunter69423d92008-12-10 13:37:21 +00004580 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004581 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00004582 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Brian Norris289c0522011-07-19 10:06:09 -07004584 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4585 __func__, (unsigned long long)instr->addr,
4586 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304588 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004592 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
4594 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004595 page = (int)(instr->addr >> chip->page_shift);
4596 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
4598 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004599 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
4601 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004602 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 /* Check, if it is write protected */
4605 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004606 pr_debug("%s: device is write protected!\n",
4607 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 instr->state = MTD_ERASE_FAILED;
4609 goto erase_exit;
4610 }
4611
4612 /* Loop through the pages */
4613 len = instr->len;
4614
4615 instr->state = MTD_ERASING;
4616
4617 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004618 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004619 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304620 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004621 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4622 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 instr->state = MTD_ERASE_FAILED;
4624 goto erase_exit;
4625 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004626
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004627 /*
4628 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004629 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004630 */
4631 if (page <= chip->pagebuf && chip->pagebuf <
4632 (page + pages_per_block))
4633 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
Brian Norris49c50b92014-05-06 16:02:19 -07004635 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
4637 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004638 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004639 pr_debug("%s: failed erase, page 0x%08x\n",
4640 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00004642 instr->fail_addr =
4643 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 goto erase_exit;
4645 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004648 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 page += pages_per_block;
4650
4651 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004652 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004654 chip->select_chip(mtd, -1);
4655 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 }
4657 }
4658 instr->state = MTD_ERASE_DONE;
4659
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004660erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
4662 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
4664 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004665 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 nand_release_device(mtd);
4667
David Woodhouse49defc02007-10-06 15:01:59 -04004668 /* Do call back function */
4669 if (!ret)
4670 mtd_erase_callback(instr);
4671
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 /* Return more or less happy */
4673 return ret;
4674}
4675
4676/**
4677 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004678 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004680 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004682static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
Brian Norris289c0522011-07-19 10:06:09 -07004684 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
4686 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004687 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004689 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690}
4691
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004693 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004694 * @mtd: MTD device structure
4695 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004697static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304699 struct nand_chip *chip = mtd_to_nand(mtd);
4700 int chipnr = (int)(offs >> chip->chip_shift);
4701 int ret;
4702
4703 /* Select the NAND device */
4704 nand_get_device(mtd, FL_READING);
4705 chip->select_chip(mtd, chipnr);
4706
4707 ret = nand_block_checkbad(mtd, offs, 0);
4708
4709 chip->select_chip(mtd, -1);
4710 nand_release_device(mtd);
4711
4712 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
4715/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004716 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004717 * @mtd: MTD device structure
4718 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004720static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 int ret;
4723
Florian Fainellif8ac0412010-09-07 13:23:43 +02004724 ret = nand_block_isbad(mtd, ofs);
4725 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004726 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 if (ret > 0)
4728 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004729 return ret;
4730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Brian Norris5a0edb22013-07-30 17:52:58 -07004732 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733}
4734
4735/**
Zach Brown56718422017-01-10 13:30:20 -06004736 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4737 * @mtd: MTD device structure
4738 * @ofs: offset relative to mtd start
4739 * @len: length of mtd
4740 */
4741static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4742{
4743 struct nand_chip *chip = mtd_to_nand(mtd);
4744 u32 part_start_block;
4745 u32 part_end_block;
4746 u32 part_start_die;
4747 u32 part_end_die;
4748
4749 /*
4750 * max_bb_per_die and blocks_per_die used to determine
4751 * the maximum bad block count.
4752 */
4753 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4754 return -ENOTSUPP;
4755
4756 /* Get the start and end of the partition in erase blocks. */
4757 part_start_block = mtd_div_by_eb(ofs, mtd);
4758 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4759
4760 /* Get the start and end LUNs of the partition. */
4761 part_start_die = part_start_block / chip->blocks_per_die;
4762 part_end_die = part_end_block / chip->blocks_per_die;
4763
4764 /*
4765 * Look up the bad blocks per unit and multiply by the number of units
4766 * that the partition spans.
4767 */
4768 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4769}
4770
4771/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004772 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004773 * @mtd: MTD device structure
4774 * @chip: nand chip info structure
4775 * @addr: feature address.
4776 * @subfeature_param: the subfeature parameters, a four bytes array.
4777 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004778static int nand_default_set_features(struct mtd_info *mtd,
4779 struct nand_chip *chip, int addr,
4780 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004781{
David Mosbergerd914c932013-05-29 15:30:13 +03004782 if (!chip->onfi_version ||
4783 !(le16_to_cpu(chip->onfi_params.opt_cmd)
4784 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08004785 return -EINVAL;
4786
Boris Brezillon97d90da2017-11-30 18:01:29 +01004787 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004788}
4789
4790/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004791 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004792 * @mtd: MTD device structure
4793 * @chip: nand chip info structure
4794 * @addr: feature address.
4795 * @subfeature_param: the subfeature parameters, a four bytes array.
4796 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004797static int nand_default_get_features(struct mtd_info *mtd,
4798 struct nand_chip *chip, int addr,
4799 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004800{
David Mosbergerd914c932013-05-29 15:30:13 +03004801 if (!chip->onfi_version ||
4802 !(le16_to_cpu(chip->onfi_params.opt_cmd)
4803 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08004804 return -EINVAL;
4805
Boris Brezillon97d90da2017-11-30 18:01:29 +01004806 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004807}
4808
4809/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004810 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004811 * @mtd: MTD device structure
4812 * @chip: nand chip info structure
4813 * @addr: feature address.
4814 * @subfeature_param: the subfeature parameters, a four bytes array.
4815 *
4816 * Should be used by NAND controller drivers that do not support the SET/GET
4817 * FEATURES operations.
4818 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004819int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4820 int addr, u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004821{
4822 return -ENOTSUPP;
4823}
Miquel Raynalb9587582018-03-19 14:47:19 +01004824EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004825
4826/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004827 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004828 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004829 */
4830static int nand_suspend(struct mtd_info *mtd)
4831{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004832 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004833}
4834
4835/**
4836 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004837 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004838 */
4839static void nand_resume(struct mtd_info *mtd)
4840{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004841 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004842
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004843 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004844 nand_release_device(mtd);
4845 else
Brian Norrisd0370212011-07-19 10:06:08 -07004846 pr_err("%s called for a chip which is not in suspended state\n",
4847 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004848}
4849
Scott Branden72ea4032014-11-20 11:18:05 -08004850/**
4851 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4852 * prevent further operations
4853 * @mtd: MTD device structure
4854 */
4855static void nand_shutdown(struct mtd_info *mtd)
4856{
Brian Norris9ca641b2015-11-09 16:37:28 -08004857 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004858}
4859
Brian Norris8b6e50c2011-05-25 14:59:01 -07004860/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004861static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004862{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004863 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004866 if (!chip->chip_delay)
4867 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
4869 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004870 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004871 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004874 if (chip->waitfunc == NULL)
4875 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004877 if (!chip->select_chip)
4878 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004879
Huang Shijie4204ccc2013-08-16 10:10:07 +08004880 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004881 if (!chip->set_features)
4882 chip->set_features = nand_default_set_features;
4883 if (!chip->get_features)
4884 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004885
Brian Norris68e80782013-07-18 01:17:02 -07004886 /* If called twice, pointers that depend on busw may need to be reset */
4887 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004888 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4889 if (!chip->read_word)
4890 chip->read_word = nand_read_word;
4891 if (!chip->block_bad)
4892 chip->block_bad = nand_block_bad;
4893 if (!chip->block_markbad)
4894 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004895 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004896 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004897 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4898 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004899 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004900 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004901 if (!chip->scan_bbt)
4902 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004903
4904 if (!chip->controller) {
4905 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02004906 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004907 }
4908
Masahiro Yamada477544c2017-03-30 17:15:05 +09004909 if (!chip->buf_align)
4910 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004911}
4912
Brian Norris8b6e50c2011-05-25 14:59:01 -07004913/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004914static void sanitize_string(uint8_t *s, size_t len)
4915{
4916 ssize_t i;
4917
Brian Norris8b6e50c2011-05-25 14:59:01 -07004918 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004919 s[len - 1] = 0;
4920
Brian Norris8b6e50c2011-05-25 14:59:01 -07004921 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004922 for (i = 0; i < len - 1; i++) {
4923 if (s[i] < ' ' || s[i] > 127)
4924 s[i] = '?';
4925 }
4926
Brian Norris8b6e50c2011-05-25 14:59:01 -07004927 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004928 strim(s);
4929}
4930
4931static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4932{
4933 int i;
4934 while (len--) {
4935 crc ^= *p++ << 8;
4936 for (i = 0; i < 8; i++)
4937 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
4938 }
4939
4940 return crc;
4941}
4942
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004943/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004944static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
4945 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004946{
4947 struct onfi_ext_param_page *ep;
4948 struct onfi_ext_section *s;
4949 struct onfi_ext_ecc_info *ecc;
4950 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004951 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004952 int len;
4953 int i;
4954
4955 len = le16_to_cpu(p->ext_param_page_length) * 16;
4956 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07004957 if (!ep)
4958 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004959
4960 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004961 ret = nand_read_param_page_op(chip, 0, NULL, 0);
4962 if (ret)
4963 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004964
4965 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004966 ret = nand_change_read_column_op(chip,
4967 sizeof(*p) * p->num_of_param_pages,
4968 ep, len, true);
4969 if (ret)
4970 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004971
Boris Brezillon97d90da2017-11-30 18:01:29 +01004972 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004973 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
4974 != le16_to_cpu(ep->crc))) {
4975 pr_debug("fail in the CRC.\n");
4976 goto ext_out;
4977 }
4978
4979 /*
4980 * Check the signature.
4981 * Do not strictly follow the ONFI spec, maybe changed in future.
4982 */
4983 if (strncmp(ep->sig, "EPPS", 4)) {
4984 pr_debug("The signature is invalid.\n");
4985 goto ext_out;
4986 }
4987
4988 /* find the ECC section. */
4989 cursor = (uint8_t *)(ep + 1);
4990 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
4991 s = ep->sections + i;
4992 if (s->type == ONFI_SECTION_TYPE_2)
4993 break;
4994 cursor += s->length * 16;
4995 }
4996 if (i == ONFI_EXT_SECTION_MAX) {
4997 pr_debug("We can not find the ECC section.\n");
4998 goto ext_out;
4999 }
5000
5001 /* get the info we want. */
5002 ecc = (struct onfi_ext_ecc_info *)cursor;
5003
Brian Norris4ae7d222013-09-16 18:20:21 -07005004 if (!ecc->codeword_size) {
5005 pr_debug("Invalid codeword size\n");
5006 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005007 }
5008
Brian Norris4ae7d222013-09-16 18:20:21 -07005009 chip->ecc_strength_ds = ecc->ecc_bits;
5010 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005011 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005012
5013ext_out:
5014 kfree(ep);
5015 return ret;
5016}
5017
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005018/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005019 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005020 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005021static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005022{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005023 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005024 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005025 char id[4];
5026 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005027
Brian Norris7854d3f2011-06-23 14:12:08 -07005028 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005029 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5030 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005031 return 0;
5032
Boris Brezillon97d90da2017-11-30 18:01:29 +01005033 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5034 if (ret)
5035 return 0;
5036
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005037 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005038 ret = nand_read_data_op(chip, p, sizeof(*p), true);
5039 if (ret)
5040 return 0;
5041
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005042 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
5043 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005044 break;
5045 }
5046 }
5047
Brian Norrisc7f23a72013-08-13 10:51:55 -07005048 if (i == 3) {
5049 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005050 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07005051 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005052
Brian Norris8b6e50c2011-05-25 14:59:01 -07005053 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005054 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005055 if (val & (1 << 5))
5056 chip->onfi_version = 23;
5057 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005058 chip->onfi_version = 22;
5059 else if (val & (1 << 3))
5060 chip->onfi_version = 21;
5061 else if (val & (1 << 2))
5062 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005063 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005064 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005065
5066 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005067 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005068 return 0;
5069 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005070
5071 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5072 sanitize_string(p->model, sizeof(p->model));
5073 if (!mtd->name)
5074 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07005075
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005076 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005077
5078 /*
5079 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5080 * (don't ask me who thought of this...). MTD assumes that these
5081 * dimensions will be power-of-2, so just truncate the remaining area.
5082 */
5083 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5084 mtd->erasesize *= mtd->writesize;
5085
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005086 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005087
5088 /* See erasesize comment */
5089 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005090 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005091 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005092
Zach Brown34da5f52017-01-10 13:30:21 -06005093 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5094 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5095
Huang Shijiee2985fc2013-05-17 11:17:30 +08005096 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005097 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005098
Huang Shijie10c86ba2013-05-17 11:17:26 +08005099 if (p->ecc_bits != 0xff) {
5100 chip->ecc_strength_ds = p->ecc_bits;
5101 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005102 } else if (chip->onfi_version >= 21 &&
5103 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
5104
5105 /*
5106 * The nand_flash_detect_ext_param_page() uses the
5107 * Change Read Column command which maybe not supported
5108 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5109 * now. We do not replace user supplied command function.
5110 */
5111 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5112 chip->cmdfunc = nand_command_lp;
5113
5114 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005115 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005116 pr_warn("Failed to detect ONFI extended param page\n");
5117 } else {
5118 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005119 }
5120
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005121 return 1;
5122}
5123
5124/*
Huang Shijie91361812014-02-21 13:39:40 +08005125 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5126 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005127static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005128{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005129 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08005130 struct nand_jedec_params *p = &chip->jedec_params;
5131 struct jedec_ecc_info *ecc;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005132 char id[5];
5133 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005134
5135 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005136 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5137 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005138 return 0;
5139
Boris Brezillon97d90da2017-11-30 18:01:29 +01005140 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
5141 if (ret)
5142 return 0;
5143
Huang Shijie91361812014-02-21 13:39:40 +08005144 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005145 ret = nand_read_data_op(chip, p, sizeof(*p), true);
5146 if (ret)
5147 return 0;
Huang Shijie91361812014-02-21 13:39:40 +08005148
5149 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5150 le16_to_cpu(p->crc))
5151 break;
5152 }
5153
5154 if (i == 3) {
5155 pr_err("Could not find valid JEDEC parameter page; aborting\n");
5156 return 0;
5157 }
5158
5159 /* Check version */
5160 val = le16_to_cpu(p->revision);
5161 if (val & (1 << 2))
5162 chip->jedec_version = 10;
5163 else if (val & (1 << 1))
5164 chip->jedec_version = 1; /* vendor specific version */
5165
5166 if (!chip->jedec_version) {
5167 pr_info("unsupported JEDEC version: %d\n", val);
5168 return 0;
5169 }
5170
5171 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5172 sanitize_string(p->model, sizeof(p->model));
5173 if (!mtd->name)
5174 mtd->name = p->model;
5175
5176 mtd->writesize = le32_to_cpu(p->byte_per_page);
5177
5178 /* Please reference to the comment for nand_flash_detect_onfi. */
5179 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5180 mtd->erasesize *= mtd->writesize;
5181
5182 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5183
5184 /* Please reference to the comment for nand_flash_detect_onfi. */
5185 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5186 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5187 chip->bits_per_cell = p->bits_per_cell;
5188
5189 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005190 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005191
5192 /* ECC info */
5193 ecc = &p->ecc_info[0];
5194
5195 if (ecc->codeword_size >= 9) {
5196 chip->ecc_strength_ds = ecc->ecc_bits;
5197 chip->ecc_step_ds = 1 << ecc->codeword_size;
5198 } else {
5199 pr_warn("Invalid codeword size\n");
5200 }
5201
5202 return 1;
5203}
5204
5205/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005206 * nand_id_has_period - Check if an ID string has a given wraparound period
5207 * @id_data: the ID string
5208 * @arrlen: the length of the @id_data array
5209 * @period: the period of repitition
5210 *
5211 * Check if an ID string is repeated within a given sequence of bytes at
5212 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005213 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005214 * if the repetition has a period of @period; otherwise, returns zero.
5215 */
5216static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5217{
5218 int i, j;
5219 for (i = 0; i < period; i++)
5220 for (j = i + period; j < arrlen; j += period)
5221 if (id_data[i] != id_data[j])
5222 return 0;
5223 return 1;
5224}
5225
5226/*
5227 * nand_id_len - Get the length of an ID string returned by CMD_READID
5228 * @id_data: the ID string
5229 * @arrlen: the length of the @id_data array
5230
5231 * Returns the length of the ID string, according to known wraparound/trailing
5232 * zero patterns. If no pattern exists, returns the length of the array.
5233 */
5234static int nand_id_len(u8 *id_data, int arrlen)
5235{
5236 int last_nonzero, period;
5237
5238 /* Find last non-zero byte */
5239 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5240 if (id_data[last_nonzero])
5241 break;
5242
5243 /* All zeros */
5244 if (last_nonzero < 0)
5245 return 0;
5246
5247 /* Calculate wraparound period */
5248 for (period = 1; period < arrlen; period++)
5249 if (nand_id_has_period(id_data, arrlen, period))
5250 break;
5251
5252 /* There's a repeated pattern */
5253 if (period < arrlen)
5254 return period;
5255
5256 /* There are trailing zeros */
5257 if (last_nonzero < arrlen - 1)
5258 return last_nonzero + 1;
5259
5260 /* No pattern detected */
5261 return arrlen;
5262}
5263
Huang Shijie7db906b2013-09-25 14:58:11 +08005264/* Extract the bits of per cell from the 3rd byte of the extended ID */
5265static int nand_get_bits_per_cell(u8 cellinfo)
5266{
5267 int bits;
5268
5269 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5270 bits >>= NAND_CI_CELLTYPE_SHIFT;
5271 return bits + 1;
5272}
5273
Brian Norrise3b88bd2012-09-24 20:40:52 -07005274/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005275 * Many new NAND share similar device ID codes, which represent the size of the
5276 * chip. The rest of the parameters must be decoded according to generic or
5277 * manufacturer-specific "extended ID" decoding patterns.
5278 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005279void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005280{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005281 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005282 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005283 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005284 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005285 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005286 /* The 4th id byte is the important one */
5287 extid = id_data[3];
5288
Boris Brezillon01389b62016-06-08 10:30:18 +02005289 /* Calc pagesize */
5290 mtd->writesize = 1024 << (extid & 0x03);
5291 extid >>= 2;
5292 /* Calc oobsize */
5293 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5294 extid >>= 2;
5295 /* Calc blocksize. Blocksize is multiples of 64KiB */
5296 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5297 extid >>= 2;
5298 /* Get buswidth information */
5299 if (extid & 0x1)
5300 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005301}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005302EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005303
5304/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005305 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5306 * decodes a matching ID table entry and assigns the MTD size parameters for
5307 * the chip.
5308 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005309static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005310{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005311 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005312
5313 mtd->erasesize = type->erasesize;
5314 mtd->writesize = type->pagesize;
5315 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005316
Huang Shijie1c195e92013-09-25 14:58:12 +08005317 /* All legacy ID NAND are small-page, SLC */
5318 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005319}
5320
5321/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005322 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5323 * heuristic patterns using various detected parameters (e.g., manufacturer,
5324 * page size, cell-type information).
5325 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005326static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005327{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005328 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005329
5330 /* Set the bad block position */
5331 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5332 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5333 else
5334 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005335}
5336
Huang Shijieec6e87e2013-03-15 11:01:00 +08005337static inline bool is_full_id_nand(struct nand_flash_dev *type)
5338{
5339 return type->id_len;
5340}
5341
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005342static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005343 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005344{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005345 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005346 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005347
Huang Shijieec6e87e2013-03-15 11:01:00 +08005348 if (!strncmp(type->id, id_data, type->id_len)) {
5349 mtd->writesize = type->pagesize;
5350 mtd->erasesize = type->erasesize;
5351 mtd->oobsize = type->oobsize;
5352
Huang Shijie7db906b2013-09-25 14:58:11 +08005353 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005354 chip->chipsize = (uint64_t)type->chipsize << 20;
5355 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005356 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5357 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005358 chip->onfi_timing_mode_default =
5359 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005360
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005361 if (!mtd->name)
5362 mtd->name = type->name;
5363
Huang Shijieec6e87e2013-03-15 11:01:00 +08005364 return true;
5365 }
5366 return false;
5367}
5368
Brian Norris7e74c2d2012-09-24 20:40:49 -07005369/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005370 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5371 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5372 * table.
5373 */
5374static void nand_manufacturer_detect(struct nand_chip *chip)
5375{
5376 /*
5377 * Try manufacturer detection if available and use
5378 * nand_decode_ext_id() otherwise.
5379 */
5380 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005381 chip->manufacturer.desc->ops->detect) {
5382 /* The 3rd id byte holds MLC / multichip data */
5383 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005384 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005385 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005386 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005387 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005388}
5389
5390/*
5391 * Manufacturer initialization. This function is called for all NANDs including
5392 * ONFI and JEDEC compliant ones.
5393 * Manufacturer drivers should put all their specific initialization code in
5394 * their ->init() hook.
5395 */
5396static int nand_manufacturer_init(struct nand_chip *chip)
5397{
5398 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5399 !chip->manufacturer.desc->ops->init)
5400 return 0;
5401
5402 return chip->manufacturer.desc->ops->init(chip);
5403}
5404
5405/*
5406 * Manufacturer cleanup. This function is called for all NANDs including
5407 * ONFI and JEDEC compliant ones.
5408 * Manufacturer drivers should put all their specific cleanup code in their
5409 * ->cleanup() hook.
5410 */
5411static void nand_manufacturer_cleanup(struct nand_chip *chip)
5412{
5413 /* Release manufacturer private data */
5414 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5415 chip->manufacturer.desc->ops->cleanup)
5416 chip->manufacturer.desc->ops->cleanup(chip);
5417}
5418
5419/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005420 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005421 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005422static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005423{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005424 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005425 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005426 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005427 u8 *id_data = chip->id.data;
5428 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
Karl Beldanef89a882008-09-15 14:37:29 +02005430 /*
5431 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005432 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005433 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005434 ret = nand_reset(chip, 0);
5435 if (ret)
5436 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005437
5438 /* Select the device */
5439 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005440
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005442 ret = nand_readid_op(chip, 0, id_data, 2);
5443 if (ret)
5444 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
5446 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005447 maf_id = id_data[0];
5448 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
Brian Norris8b6e50c2011-05-25 14:59:01 -07005450 /*
5451 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005452 * interface concerns can cause random data which looks like a
5453 * possibly credible NAND flash to appear. If the two results do
5454 * not match, ignore the device completely.
5455 */
5456
Brian Norris4aef9b72012-09-24 20:40:48 -07005457 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005458 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5459 if (ret)
5460 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005461
Boris Brezillon7f501f02016-05-24 19:20:05 +02005462 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005463 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005464 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005465 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005466 }
5467
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005468 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005469
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005470 /* Try to identify manufacturer */
5471 manufacturer = nand_get_manufacturer(maf_id);
5472 chip->manufacturer.desc = manufacturer;
5473
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005474 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005475 type = nand_flash_ids;
5476
Boris Brezillon29a198a2016-05-24 20:17:48 +02005477 /*
5478 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5479 * override it.
5480 * This is required to make sure initial NAND bus width set by the
5481 * NAND controller driver is coherent with the real NAND bus width
5482 * (extracted by auto-detection code).
5483 */
5484 busw = chip->options & NAND_BUSWIDTH_16;
5485
5486 /*
5487 * The flag is only set (never cleared), reset it to its default value
5488 * before starting auto-detection.
5489 */
5490 chip->options &= ~NAND_BUSWIDTH_16;
5491
Huang Shijieec6e87e2013-03-15 11:01:00 +08005492 for (; type->name != NULL; type++) {
5493 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005494 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005495 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005496 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005497 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005498 }
5499 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005500
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005501 chip->onfi_version = 0;
5502 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005503 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005504 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005505 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005506
5507 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005508 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08005509 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005510 }
5511
David Woodhouse5e81e882010-02-26 18:32:56 +00005512 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005513 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005514
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005515 if (!mtd->name)
5516 mtd->name = type->name;
5517
Adrian Hunter69423d92008-12-10 13:37:21 +00005518 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005519
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005520 if (!type->pagesize)
5521 nand_manufacturer_detect(chip);
5522 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005523 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005524
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005525 /* Get chip options */
5526 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005527
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005528ident_done:
5529
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005530 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005531 WARN_ON(busw & NAND_BUSWIDTH_16);
5532 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005533 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5534 /*
5535 * Check, if buswidth is correct. Hardware drivers should set
5536 * chip correct!
5537 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005538 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005539 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005540 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5541 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005542 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5543 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005544 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005545 }
5546
Boris Brezillon7f501f02016-05-24 19:20:05 +02005547 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005548
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005549 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005550 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005551 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005552 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005553
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005554 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005555 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005556 if (chip->chipsize & 0xffffffff)
5557 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005558 else {
5559 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5560 chip->chip_shift += 32 - 1;
5561 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005562
Masahiro Yamada14157f82017-09-13 11:05:50 +09005563 if (chip->chip_shift - chip->page_shift > 16)
5564 chip->options |= NAND_ROW_ADDR_3;
5565
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005566 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005567 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005568
Brian Norris8b6e50c2011-05-25 14:59:01 -07005569 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005570 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5571 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005572
Ezequiel Garcia20171642013-11-25 08:30:31 -03005573 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005574 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005575
5576 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005577 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5578 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005579 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005580 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5581 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005582 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005583 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5584 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005585
Rafał Miłecki3755a992014-10-21 00:01:04 +02005586 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005587 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005588 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005589 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005590}
5591
Boris Brezillond48f62b2016-04-01 14:54:32 +02005592static const char * const nand_ecc_modes[] = {
5593 [NAND_ECC_NONE] = "none",
5594 [NAND_ECC_SOFT] = "soft",
5595 [NAND_ECC_HW] = "hw",
5596 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5597 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005598 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005599};
5600
5601static int of_get_nand_ecc_mode(struct device_node *np)
5602{
5603 const char *pm;
5604 int err, i;
5605
5606 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5607 if (err < 0)
5608 return err;
5609
5610 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5611 if (!strcasecmp(pm, nand_ecc_modes[i]))
5612 return i;
5613
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005614 /*
5615 * For backward compatibility we support few obsoleted values that don't
5616 * have their mappings into nand_ecc_modes_t anymore (they were merged
5617 * with other enums).
5618 */
5619 if (!strcasecmp(pm, "soft_bch"))
5620 return NAND_ECC_SOFT;
5621
Boris Brezillond48f62b2016-04-01 14:54:32 +02005622 return -ENODEV;
5623}
5624
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005625static const char * const nand_ecc_algos[] = {
5626 [NAND_ECC_HAMMING] = "hamming",
5627 [NAND_ECC_BCH] = "bch",
5628};
5629
Boris Brezillond48f62b2016-04-01 14:54:32 +02005630static int of_get_nand_ecc_algo(struct device_node *np)
5631{
5632 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005633 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005634
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005635 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5636 if (!err) {
5637 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5638 if (!strcasecmp(pm, nand_ecc_algos[i]))
5639 return i;
5640 return -ENODEV;
5641 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005642
5643 /*
5644 * For backward compatibility we also read "nand-ecc-mode" checking
5645 * for some obsoleted values that were specifying ECC algorithm.
5646 */
5647 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5648 if (err < 0)
5649 return err;
5650
5651 if (!strcasecmp(pm, "soft"))
5652 return NAND_ECC_HAMMING;
5653 else if (!strcasecmp(pm, "soft_bch"))
5654 return NAND_ECC_BCH;
5655
5656 return -ENODEV;
5657}
5658
5659static int of_get_nand_ecc_step_size(struct device_node *np)
5660{
5661 int ret;
5662 u32 val;
5663
5664 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5665 return ret ? ret : val;
5666}
5667
5668static int of_get_nand_ecc_strength(struct device_node *np)
5669{
5670 int ret;
5671 u32 val;
5672
5673 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5674 return ret ? ret : val;
5675}
5676
5677static int of_get_nand_bus_width(struct device_node *np)
5678{
5679 u32 val;
5680
5681 if (of_property_read_u32(np, "nand-bus-width", &val))
5682 return 8;
5683
5684 switch (val) {
5685 case 8:
5686 case 16:
5687 return val;
5688 default:
5689 return -EIO;
5690 }
5691}
5692
5693static bool of_get_nand_on_flash_bbt(struct device_node *np)
5694{
5695 return of_property_read_bool(np, "nand-on-flash-bbt");
5696}
5697
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005698static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005699{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005700 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005701 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005702
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005703 if (!dn)
5704 return 0;
5705
Brian Norris5844fee2015-01-23 00:22:27 -08005706 if (of_get_nand_bus_width(dn) == 16)
5707 chip->options |= NAND_BUSWIDTH_16;
5708
5709 if (of_get_nand_on_flash_bbt(dn))
5710 chip->bbt_options |= NAND_BBT_USE_FLASH;
5711
5712 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005713 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005714 ecc_strength = of_get_nand_ecc_strength(dn);
5715 ecc_step = of_get_nand_ecc_step_size(dn);
5716
Brian Norris5844fee2015-01-23 00:22:27 -08005717 if (ecc_mode >= 0)
5718 chip->ecc.mode = ecc_mode;
5719
Rafał Miłecki79082452016-03-23 11:19:02 +01005720 if (ecc_algo >= 0)
5721 chip->ecc.algo = ecc_algo;
5722
Brian Norris5844fee2015-01-23 00:22:27 -08005723 if (ecc_strength >= 0)
5724 chip->ecc.strength = ecc_strength;
5725
5726 if (ecc_step > 0)
5727 chip->ecc.size = ecc_step;
5728
Boris Brezillonba78ee02016-06-08 17:04:22 +02005729 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5730 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5731
Brian Norris5844fee2015-01-23 00:22:27 -08005732 return 0;
5733}
5734
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005735/**
David Woodhouse3b85c322006-09-25 17:06:53 +01005736 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005737 * @mtd: MTD device structure
5738 * @maxchips: number of chips to scan for
5739 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005740 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005741 * This is the first phase of the normal nand_scan() function. It reads the
5742 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005743 *
5744 */
David Woodhouse5e81e882010-02-26 18:32:56 +00005745int nand_scan_ident(struct mtd_info *mtd, int maxchips,
5746 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005747{
Cai Zhiyongbb770822013-12-25 20:11:15 +08005748 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01005749 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08005750 int ret;
5751
Miquel Raynal17fa8042017-11-30 18:01:31 +01005752 /* Enforce the right timings for reset/detection */
5753 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5754
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005755 ret = nand_dt_init(chip);
5756 if (ret)
5757 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005758
Brian Norrisf7a8e382016-01-05 10:39:45 -08005759 if (!mtd->name && mtd->dev.parent)
5760 mtd->name = dev_name(mtd->dev.parent);
5761
Miquel Raynal8878b122017-11-09 14:16:45 +01005762 /*
5763 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5764 * populated.
5765 */
5766 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005767 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005768 * Default functions assigned for ->cmdfunc() and
5769 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005770 */
Miquel Raynal8878b122017-11-09 14:16:45 +01005771 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5772 pr_err("->cmd_ctrl() should be provided\n");
5773 return -EINVAL;
5774 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005775 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005776
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005777 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005778 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005779
5780 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005781 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005782 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005783 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005784 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005785 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005786 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 }
5788
Boris Brezillon7f501f02016-05-24 19:20:05 +02005789 nand_maf_id = chip->id.data[0];
5790 nand_dev_id = chip->id.data[1];
5791
Huang Shijie07300162012-11-09 16:23:45 +08005792 chip->select_chip(mtd, -1);
5793
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005794 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005795 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005796 u8 id[2];
5797
Karl Beldanef89a882008-09-15 14:37:29 +02005798 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005799 nand_reset(chip, i);
5800
5801 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005803 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005805 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08005806 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 break;
Huang Shijie07300162012-11-09 16:23:45 +08005808 }
5809 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 }
5811 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005812 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005813
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005815 chip->numchips = i;
5816 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
David Woodhouse3b85c322006-09-25 17:06:53 +01005818 return 0;
5819}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005820EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01005821
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005822static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
5823{
5824 struct nand_chip *chip = mtd_to_nand(mtd);
5825 struct nand_ecc_ctrl *ecc = &chip->ecc;
5826
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005827 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005828 return -EINVAL;
5829
5830 switch (ecc->algo) {
5831 case NAND_ECC_HAMMING:
5832 ecc->calculate = nand_calculate_ecc;
5833 ecc->correct = nand_correct_data;
5834 ecc->read_page = nand_read_page_swecc;
5835 ecc->read_subpage = nand_read_subpage;
5836 ecc->write_page = nand_write_page_swecc;
5837 ecc->read_page_raw = nand_read_page_raw;
5838 ecc->write_page_raw = nand_write_page_raw;
5839 ecc->read_oob = nand_read_oob_std;
5840 ecc->write_oob = nand_write_oob_std;
5841 if (!ecc->size)
5842 ecc->size = 256;
5843 ecc->bytes = 3;
5844 ecc->strength = 1;
5845 return 0;
5846 case NAND_ECC_BCH:
5847 if (!mtd_nand_has_bch()) {
5848 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5849 return -EINVAL;
5850 }
5851 ecc->calculate = nand_bch_calculate_ecc;
5852 ecc->correct = nand_bch_correct_data;
5853 ecc->read_page = nand_read_page_swecc;
5854 ecc->read_subpage = nand_read_subpage;
5855 ecc->write_page = nand_write_page_swecc;
5856 ecc->read_page_raw = nand_read_page_raw;
5857 ecc->write_page_raw = nand_write_page_raw;
5858 ecc->read_oob = nand_read_oob_std;
5859 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005860
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005861 /*
5862 * Board driver should supply ecc.size and ecc.strength
5863 * values to select how many bits are correctable.
5864 * Otherwise, default to 4 bits for large page devices.
5865 */
5866 if (!ecc->size && (mtd->oobsize >= 64)) {
5867 ecc->size = 512;
5868 ecc->strength = 4;
5869 }
5870
5871 /*
5872 * if no ecc placement scheme was provided pickup the default
5873 * large page one.
5874 */
5875 if (!mtd->ooblayout) {
5876 /* handle large page devices only */
5877 if (mtd->oobsize < 64) {
5878 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5879 return -EINVAL;
5880 }
5881
5882 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005883
5884 }
5885
5886 /*
5887 * We can only maximize ECC config when the default layout is
5888 * used, otherwise we don't know how many bytes can really be
5889 * used.
5890 */
5891 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5892 ecc->options & NAND_ECC_MAXIMIZE) {
5893 int steps, bytes;
5894
5895 /* Always prefer 1k blocks over 512bytes ones */
5896 ecc->size = 1024;
5897 steps = mtd->writesize / ecc->size;
5898
5899 /* Reserve 2 bytes for the BBM */
5900 bytes = (mtd->oobsize - 2) / steps;
5901 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005902 }
5903
5904 /* See nand_bch_init() for details. */
5905 ecc->bytes = 0;
5906 ecc->priv = nand_bch_init(mtd);
5907 if (!ecc->priv) {
5908 WARN(1, "BCH ECC initialization failed!\n");
5909 return -EINVAL;
5910 }
5911 return 0;
5912 default:
5913 WARN(1, "Unsupported ECC algorithm!\n");
5914 return -EINVAL;
5915 }
5916}
5917
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005918/**
5919 * nand_check_ecc_caps - check the sanity of preset ECC settings
5920 * @chip: nand chip info structure
5921 * @caps: ECC caps info structure
5922 * @oobavail: OOB size that the ECC engine can use
5923 *
5924 * When ECC step size and strength are already set, check if they are supported
5925 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5926 * On success, the calculated ECC bytes is set.
5927 */
5928int nand_check_ecc_caps(struct nand_chip *chip,
5929 const struct nand_ecc_caps *caps, int oobavail)
5930{
5931 struct mtd_info *mtd = nand_to_mtd(chip);
5932 const struct nand_ecc_step_info *stepinfo;
5933 int preset_step = chip->ecc.size;
5934 int preset_strength = chip->ecc.strength;
5935 int nsteps, ecc_bytes;
5936 int i, j;
5937
5938 if (WARN_ON(oobavail < 0))
5939 return -EINVAL;
5940
5941 if (!preset_step || !preset_strength)
5942 return -ENODATA;
5943
5944 nsteps = mtd->writesize / preset_step;
5945
5946 for (i = 0; i < caps->nstepinfos; i++) {
5947 stepinfo = &caps->stepinfos[i];
5948
5949 if (stepinfo->stepsize != preset_step)
5950 continue;
5951
5952 for (j = 0; j < stepinfo->nstrengths; j++) {
5953 if (stepinfo->strengths[j] != preset_strength)
5954 continue;
5955
5956 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5957 preset_strength);
5958 if (WARN_ON_ONCE(ecc_bytes < 0))
5959 return ecc_bytes;
5960
5961 if (ecc_bytes * nsteps > oobavail) {
5962 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5963 preset_step, preset_strength);
5964 return -ENOSPC;
5965 }
5966
5967 chip->ecc.bytes = ecc_bytes;
5968
5969 return 0;
5970 }
5971 }
5972
5973 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5974 preset_step, preset_strength);
5975
5976 return -ENOTSUPP;
5977}
5978EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
5979
5980/**
5981 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5982 * @chip: nand chip info structure
5983 * @caps: ECC engine caps info structure
5984 * @oobavail: OOB size that the ECC engine can use
5985 *
5986 * If a chip's ECC requirement is provided, try to meet it with the least
5987 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5988 * On success, the chosen ECC settings are set.
5989 */
5990int nand_match_ecc_req(struct nand_chip *chip,
5991 const struct nand_ecc_caps *caps, int oobavail)
5992{
5993 struct mtd_info *mtd = nand_to_mtd(chip);
5994 const struct nand_ecc_step_info *stepinfo;
5995 int req_step = chip->ecc_step_ds;
5996 int req_strength = chip->ecc_strength_ds;
5997 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5998 int best_step, best_strength, best_ecc_bytes;
5999 int best_ecc_bytes_total = INT_MAX;
6000 int i, j;
6001
6002 if (WARN_ON(oobavail < 0))
6003 return -EINVAL;
6004
6005 /* No information provided by the NAND chip */
6006 if (!req_step || !req_strength)
6007 return -ENOTSUPP;
6008
6009 /* number of correctable bits the chip requires in a page */
6010 req_corr = mtd->writesize / req_step * req_strength;
6011
6012 for (i = 0; i < caps->nstepinfos; i++) {
6013 stepinfo = &caps->stepinfos[i];
6014 step_size = stepinfo->stepsize;
6015
6016 for (j = 0; j < stepinfo->nstrengths; j++) {
6017 strength = stepinfo->strengths[j];
6018
6019 /*
6020 * If both step size and strength are smaller than the
6021 * chip's requirement, it is not easy to compare the
6022 * resulted reliability.
6023 */
6024 if (step_size < req_step && strength < req_strength)
6025 continue;
6026
6027 if (mtd->writesize % step_size)
6028 continue;
6029
6030 nsteps = mtd->writesize / step_size;
6031
6032 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6033 if (WARN_ON_ONCE(ecc_bytes < 0))
6034 continue;
6035 ecc_bytes_total = ecc_bytes * nsteps;
6036
6037 if (ecc_bytes_total > oobavail ||
6038 strength * nsteps < req_corr)
6039 continue;
6040
6041 /*
6042 * We assume the best is to meet the chip's requrement
6043 * with the least number of ECC bytes.
6044 */
6045 if (ecc_bytes_total < best_ecc_bytes_total) {
6046 best_ecc_bytes_total = ecc_bytes_total;
6047 best_step = step_size;
6048 best_strength = strength;
6049 best_ecc_bytes = ecc_bytes;
6050 }
6051 }
6052 }
6053
6054 if (best_ecc_bytes_total == INT_MAX)
6055 return -ENOTSUPP;
6056
6057 chip->ecc.size = best_step;
6058 chip->ecc.strength = best_strength;
6059 chip->ecc.bytes = best_ecc_bytes;
6060
6061 return 0;
6062}
6063EXPORT_SYMBOL_GPL(nand_match_ecc_req);
6064
6065/**
6066 * nand_maximize_ecc - choose the max ECC strength available
6067 * @chip: nand chip info structure
6068 * @caps: ECC engine caps info structure
6069 * @oobavail: OOB size that the ECC engine can use
6070 *
6071 * Choose the max ECC strength that is supported on the controller, and can fit
6072 * within the chip's OOB. On success, the chosen ECC settings are set.
6073 */
6074int nand_maximize_ecc(struct nand_chip *chip,
6075 const struct nand_ecc_caps *caps, int oobavail)
6076{
6077 struct mtd_info *mtd = nand_to_mtd(chip);
6078 const struct nand_ecc_step_info *stepinfo;
6079 int step_size, strength, nsteps, ecc_bytes, corr;
6080 int best_corr = 0;
6081 int best_step = 0;
6082 int best_strength, best_ecc_bytes;
6083 int i, j;
6084
6085 if (WARN_ON(oobavail < 0))
6086 return -EINVAL;
6087
6088 for (i = 0; i < caps->nstepinfos; i++) {
6089 stepinfo = &caps->stepinfos[i];
6090 step_size = stepinfo->stepsize;
6091
6092 /* If chip->ecc.size is already set, respect it */
6093 if (chip->ecc.size && step_size != chip->ecc.size)
6094 continue;
6095
6096 for (j = 0; j < stepinfo->nstrengths; j++) {
6097 strength = stepinfo->strengths[j];
6098
6099 if (mtd->writesize % step_size)
6100 continue;
6101
6102 nsteps = mtd->writesize / step_size;
6103
6104 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6105 if (WARN_ON_ONCE(ecc_bytes < 0))
6106 continue;
6107
6108 if (ecc_bytes * nsteps > oobavail)
6109 continue;
6110
6111 corr = strength * nsteps;
6112
6113 /*
6114 * If the number of correctable bits is the same,
6115 * bigger step_size has more reliability.
6116 */
6117 if (corr > best_corr ||
6118 (corr == best_corr && step_size > best_step)) {
6119 best_corr = corr;
6120 best_step = step_size;
6121 best_strength = strength;
6122 best_ecc_bytes = ecc_bytes;
6123 }
6124 }
6125 }
6126
6127 if (!best_corr)
6128 return -ENOTSUPP;
6129
6130 chip->ecc.size = best_step;
6131 chip->ecc.strength = best_strength;
6132 chip->ecc.bytes = best_ecc_bytes;
6133
6134 return 0;
6135}
6136EXPORT_SYMBOL_GPL(nand_maximize_ecc);
6137
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006138/*
6139 * Check if the chip configuration meet the datasheet requirements.
6140
6141 * If our configuration corrects A bits per B bytes and the minimum
6142 * required correction level is X bits per Y bytes, then we must ensure
6143 * both of the following are true:
6144 *
6145 * (1) A / B >= X / Y
6146 * (2) A >= X
6147 *
6148 * Requirement (1) ensures we can correct for the required bitflip density.
6149 * Requirement (2) ensures we can correct even when all bitflips are clumped
6150 * in the same sector.
6151 */
6152static bool nand_ecc_strength_good(struct mtd_info *mtd)
6153{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006154 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006155 struct nand_ecc_ctrl *ecc = &chip->ecc;
6156 int corr, ds_corr;
6157
6158 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6159 /* Not enough information */
6160 return true;
6161
6162 /*
6163 * We get the number of corrected bits per page to compare
6164 * the correction density.
6165 */
6166 corr = (mtd->writesize * ecc->strength) / ecc->size;
6167 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6168
6169 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6170}
David Woodhouse3b85c322006-09-25 17:06:53 +01006171
6172/**
6173 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006174 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01006175 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006176 * This is the second phase of the normal nand_scan() function. It fills out
6177 * all the uninitialized function pointers with the defaults and scans for a
6178 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006179 */
6180int nand_scan_tail(struct mtd_info *mtd)
6181{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006182 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08006183 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006184 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006185
Brian Norrise2414f42012-02-06 13:44:00 -08006186 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006187 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006188 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006189 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006190 }
Brian Norrise2414f42012-02-06 13:44:00 -08006191
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006192 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006193 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006194 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006195
Boris Brezillonf84674b2017-06-02 12:18:24 +02006196 /*
6197 * FIXME: some NAND manufacturer drivers expect the first die to be
6198 * selected when manufacturer->init() is called. They should be fixed
6199 * to explictly select the relevant die when interacting with the NAND
6200 * chip.
6201 */
6202 chip->select_chip(mtd, 0);
6203 ret = nand_manufacturer_init(chip);
6204 chip->select_chip(mtd, -1);
6205 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006206 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006207
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006208 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006209 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006210
6211 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006212 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006213 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006214 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006215 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006216 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006219 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 break;
6221 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006222 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006223 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006226 /*
6227 * Expose the whole OOB area to users if ECC_NONE
6228 * is passed. We could do that for all kind of
6229 * ->oobsize, but we must keep the old large/small
6230 * page with ECC layout when ->oobsize <= 128 for
6231 * compatibility reasons.
6232 */
6233 if (ecc->mode == NAND_ECC_NONE) {
6234 mtd_set_ooblayout(mtd,
6235 &nand_ooblayout_lp_ops);
6236 break;
6237 }
6238
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006239 WARN(1, "No oob scheme defined for oobsize %d\n",
6240 mtd->oobsize);
6241 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006242 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 }
6244 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006245
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006246 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006247 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006248 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006249 */
David Woodhouse956e9442006-09-25 17:12:39 +01006250
Huang Shijie97de79e02013-10-18 14:20:53 +08006251 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006252 case NAND_ECC_HW_OOB_FIRST:
6253 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006254 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006255 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6256 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006257 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006258 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006259 if (!ecc->read_page)
6260 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006261
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006262 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006263 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006264 if (!ecc->read_page)
6265 ecc->read_page = nand_read_page_hwecc;
6266 if (!ecc->write_page)
6267 ecc->write_page = nand_write_page_hwecc;
6268 if (!ecc->read_page_raw)
6269 ecc->read_page_raw = nand_read_page_raw;
6270 if (!ecc->write_page_raw)
6271 ecc->write_page_raw = nand_write_page_raw;
6272 if (!ecc->read_oob)
6273 ecc->read_oob = nand_read_oob_std;
6274 if (!ecc->write_oob)
6275 ecc->write_oob = nand_write_oob_std;
6276 if (!ecc->read_subpage)
6277 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006278 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006279 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006280
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006281 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006282 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6283 (!ecc->read_page ||
6284 ecc->read_page == nand_read_page_hwecc ||
6285 !ecc->write_page ||
6286 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006287 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6288 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006289 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006290 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006291 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006292 if (!ecc->read_page)
6293 ecc->read_page = nand_read_page_syndrome;
6294 if (!ecc->write_page)
6295 ecc->write_page = nand_write_page_syndrome;
6296 if (!ecc->read_page_raw)
6297 ecc->read_page_raw = nand_read_page_raw_syndrome;
6298 if (!ecc->write_page_raw)
6299 ecc->write_page_raw = nand_write_page_raw_syndrome;
6300 if (!ecc->read_oob)
6301 ecc->read_oob = nand_read_oob_syndrome;
6302 if (!ecc->write_oob)
6303 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006304
Huang Shijie97de79e02013-10-18 14:20:53 +08006305 if (mtd->writesize >= ecc->size) {
6306 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006307 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6308 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006309 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006310 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006311 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006312 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006313 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6314 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006315 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006316 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006318 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006319 ret = nand_set_ecc_soft_ops(mtd);
6320 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006321 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006322 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006323 }
6324 break;
6325
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006326 case NAND_ECC_ON_DIE:
6327 if (!ecc->read_page || !ecc->write_page) {
6328 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6329 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006330 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006331 }
6332 if (!ecc->read_oob)
6333 ecc->read_oob = nand_read_oob_std;
6334 if (!ecc->write_oob)
6335 ecc->write_oob = nand_write_oob_std;
6336 break;
6337
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006338 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006339 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006340 ecc->read_page = nand_read_page_raw;
6341 ecc->write_page = nand_write_page_raw;
6342 ecc->read_oob = nand_read_oob_std;
6343 ecc->read_page_raw = nand_read_page_raw;
6344 ecc->write_page_raw = nand_write_page_raw;
6345 ecc->write_oob = nand_write_oob_std;
6346 ecc->size = mtd->writesize;
6347 ecc->bytes = 0;
6348 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006350
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006352 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6353 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006354 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006357 if (ecc->correct || ecc->calculate) {
6358 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6359 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6360 if (!ecc->calc_buf || !ecc->code_buf) {
6361 ret = -ENOMEM;
6362 goto err_nand_manuf_cleanup;
6363 }
6364 }
6365
Brian Norris9ce244b2011-08-30 18:45:37 -07006366 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006367 if (!ecc->read_oob_raw)
6368 ecc->read_oob_raw = ecc->read_oob;
6369 if (!ecc->write_oob_raw)
6370 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006371
Boris Brezillon846031d2016-02-03 20:11:00 +01006372 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006373 mtd->ecc_strength = ecc->strength;
6374 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006375
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006376 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006377 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006378 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006379 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006380 ecc->steps = mtd->writesize / ecc->size;
6381 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006382 WARN(1, "Invalid ECC parameters\n");
6383 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006384 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006386 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006387 if (ecc->total > mtd->oobsize) {
6388 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6389 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006390 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006391 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006392
Boris Brezillon846031d2016-02-03 20:11:00 +01006393 /*
6394 * The number of bytes available for a client to place data into
6395 * the out of band area.
6396 */
6397 ret = mtd_ooblayout_count_freebytes(mtd);
6398 if (ret < 0)
6399 ret = 0;
6400
6401 mtd->oobavail = ret;
6402
6403 /* ECC sanity check: warn if it's too weak */
6404 if (!nand_ecc_strength_good(mtd))
6405 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6406 mtd->name);
6407
Brian Norris8b6e50c2011-05-25 14:59:01 -07006408 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006409 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006410 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006411 case 2:
6412 mtd->subpage_sft = 1;
6413 break;
6414 case 4:
6415 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006416 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006417 mtd->subpage_sft = 2;
6418 break;
6419 }
6420 }
6421 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6422
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006423 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006424 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006427 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006429 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306430 switch (ecc->mode) {
6431 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306432 if (chip->page_shift > 9)
6433 chip->options |= NAND_SUBPAGE_READ;
6434 break;
6435
6436 default:
6437 break;
6438 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006439
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006441 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006442 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6443 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006444 mtd->_erase = nand_erase;
6445 mtd->_point = NULL;
6446 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006447 mtd->_panic_write = panic_nand_write;
6448 mtd->_read_oob = nand_read_oob;
6449 mtd->_write_oob = nand_write_oob;
6450 mtd->_sync = nand_sync;
6451 mtd->_lock = NULL;
6452 mtd->_unlock = NULL;
6453 mtd->_suspend = nand_suspend;
6454 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006455 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006456 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006457 mtd->_block_isbad = nand_block_isbad;
6458 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006459 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006460 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006462 /*
6463 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6464 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6465 * properly set.
6466 */
6467 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006468 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
Boris Brezillonf84674b2017-06-02 12:18:24 +02006470 /* Initialize the ->data_interface field. */
6471 ret = nand_init_data_interface(chip);
6472 if (ret)
6473 goto err_nand_manuf_cleanup;
6474
6475 /* Enter fastest possible mode on all dies. */
6476 for (i = 0; i < chip->numchips; i++) {
6477 chip->select_chip(mtd, i);
6478 ret = nand_setup_data_interface(chip, i);
6479 chip->select_chip(mtd, -1);
6480
6481 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006482 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006483 }
6484
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006485 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006486 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488
6489 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07006490 ret = chip->scan_bbt(mtd);
6491 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006492 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006493
Brian Norris44d41822017-05-01 17:04:50 -07006494 return 0;
6495
Boris Brezillonf84674b2017-06-02 12:18:24 +02006496
6497err_nand_manuf_cleanup:
6498 nand_manufacturer_cleanup(chip);
6499
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006500err_free_buf:
6501 kfree(chip->data_buf);
6502 kfree(ecc->code_buf);
6503 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006504
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006505 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006507EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508
Brian Norris8b6e50c2011-05-25 14:59:01 -07006509/*
6510 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006511 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07006512 * to call us from in-kernel code if the core NAND support is modular.
6513 */
David Woodhouse3b85c322006-09-25 17:06:53 +01006514#ifdef MODULE
6515#define caller_is_module() (1)
6516#else
6517#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06006518 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01006519#endif
6520
6521/**
6522 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006523 * @mtd: MTD device structure
6524 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01006525 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006526 * This fills out all the uninitialized function pointers with the defaults.
6527 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006528 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006529 */
6530int nand_scan(struct mtd_info *mtd, int maxchips)
6531{
6532 int ret;
6533
David Woodhouse5e81e882010-02-26 18:32:56 +00006534 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01006535 if (!ret)
6536 ret = nand_scan_tail(mtd);
6537 return ret;
6538}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006539EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01006540
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006542 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6543 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006544 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006545void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006547 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006548 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006549 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6550
Jesper Juhlfa671642005-11-07 01:01:27 -08006551 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006552 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006553 kfree(chip->data_buf);
6554 kfree(chip->ecc.code_buf);
6555 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006556
6557 /* Free bad block descriptor memory */
6558 if (chip->badblock_pattern && chip->badblock_pattern->options
6559 & NAND_BBT_DYNAMICSTRUCT)
6560 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006561
6562 /* Free manufacturer priv data. */
6563 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006565EXPORT_SYMBOL_GPL(nand_cleanup);
6566
6567/**
6568 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6569 * held by the NAND device
6570 * @mtd: MTD device structure
6571 */
6572void nand_release(struct mtd_info *mtd)
6573{
6574 mtd_device_unregister(mtd);
6575 nand_cleanup(mtd_to_nand(mtd));
6576}
David Woodhousee0c7d762006-05-13 18:07:53 +01006577EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006578
David Woodhousee0c7d762006-05-13 18:07:53 +01006579MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006580MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6581MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006582MODULE_DESCRIPTION("Generic NAND flash driver code");