blob: c6c18b82f8f4eade18561edb24439a9d4737efa5 [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>
42#include <linux/mtd/nand.h>
43#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
118 if (section)
119 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 *
352 * One user of the write_byte callback is nand_onfi_set_features. The
353 * 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);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200564
Brian Norris8b6e50c2011-05-25 14:59:01 -0700565 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200566 if (chip->options & NAND_BROKEN_XD)
567 return 0;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200570 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
571 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800575 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700576 * @mtd: MTD device structure
577 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300578 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800579 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300580 */
581static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
582{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100583 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300584
585 if (!chip->bbt)
586 return 0;
587 /* Return info from the table */
588 return nand_isreserved_bbt(mtd, ofs);
589}
590
591/**
592 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
593 * @mtd: MTD device structure
594 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700595 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
597 * Check, if the block is bad. Either by reading the bad block table or
598 * calling of the scan function.
599 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530600static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100602 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000603
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200604 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530605 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100608 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200611/**
612 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700613 * @mtd: MTD device structure
614 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200615 *
616 * Helper function for nand_wait_ready used when needing to wait in interrupt
617 * context.
618 */
619static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
620{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100621 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200622 int i;
623
624 /* Wait for the device to get ready */
625 for (i = 0; i < timeo; i++) {
626 if (chip->dev_ready(mtd))
627 break;
628 touch_softlockup_watchdog();
629 mdelay(1);
630 }
631}
632
Alex Smithb70af9b2015-10-06 14:52:07 +0100633/**
634 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
635 * @mtd: MTD device structure
636 *
637 * Wait for the ready pin after a command, and warn if a timeout occurs.
638 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100639void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000640{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100641 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100642 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000643
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200644 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100645 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200646
Brian Norris7854d3f2011-06-23 14:12:08 -0700647 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100648 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000649 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200650 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300651 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100652 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000653 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100654
Brian Norris9ebfdf52016-03-04 17:19:23 -0800655 if (!chip->dev_ready(mtd))
656 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000657}
David Woodhouse4b648b02006-09-25 17:05:24 +0100658EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200661 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
662 * @mtd: MTD device structure
663 * @timeo: Timeout in ms
664 *
665 * Wait for status ready (i.e. command done) or timeout.
666 */
667static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
668{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100669 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200670
671 timeo = jiffies + msecs_to_jiffies(timeo);
672 do {
673 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
674 break;
675 touch_softlockup_watchdog();
676 } while (time_before(jiffies, timeo));
677};
678
679/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700681 * @mtd: MTD device structure
682 * @command: the command to be sent
683 * @column: the column address for this command, -1 if none
684 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700686 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200687 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200689static void nand_command(struct mtd_info *mtd, unsigned int command,
690 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100692 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200693 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Brian Norris8b6e50c2011-05-25 14:59:01 -0700695 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (command == NAND_CMD_SEQIN) {
697 int readcmd;
698
Joern Engel28318772006-05-22 23:18:05 +0200699 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200701 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 readcmd = NAND_CMD_READOOB;
703 } else if (column < 256) {
704 /* First 256 bytes --> READ0 */
705 readcmd = NAND_CMD_READ0;
706 } else {
707 column -= 256;
708 readcmd = NAND_CMD_READ1;
709 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200710 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200711 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200713 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Brian Norris8b6e50c2011-05-25 14:59:01 -0700715 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200716 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
717 /* Serially input address */
718 if (column != -1) {
719 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800720 if (chip->options & NAND_BUSWIDTH_16 &&
721 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200722 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200723 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200724 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200726 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200727 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200728 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200729 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200730 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 if (chip->chipsize > (32 << 20))
732 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200733 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200734 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000735
736 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700737 * Program and erase have their own busy handlers status and sequential
738 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100739 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 case NAND_CMD_PAGEPROG:
743 case NAND_CMD_ERASE1:
744 case NAND_CMD_ERASE2:
745 case NAND_CMD_SEQIN:
746 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900747 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900748 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return;
750
751 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200752 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200754 udelay(chip->chip_delay);
755 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200756 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200757 chip->cmd_ctrl(mtd,
758 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200759 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
760 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return;
762
David Woodhousee0c7d762006-05-13 18:07:53 +0100763 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200764 case NAND_CMD_READ0:
765 /*
766 * READ0 is sometimes used to exit GET STATUS mode. When this
767 * is the case no address cycles are requested, and we can use
768 * this information to detect that we should not wait for the
769 * device to be ready.
770 */
771 if (column == -1 && page_addr == -1)
772 return;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000775 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 * If we don't have access to the busy pin, we apply the given
777 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100778 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200779 if (!chip->dev_ready) {
780 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700784 /*
785 * Apply this short delay always to ensure that we do wait tWB in
786 * any case on any machine.
787 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100788 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000789
790 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200793static void nand_ccs_delay(struct nand_chip *chip)
794{
795 /*
796 * The controller already takes care of waiting for tCCS when the RNDIN
797 * or RNDOUT command is sent, return directly.
798 */
799 if (!(chip->options & NAND_WAIT_TCCS))
800 return;
801
802 /*
803 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
804 * (which should be safe for all NANDs).
805 */
806 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
807 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
808 else
809 ndelay(500);
810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/**
813 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700814 * @mtd: MTD device structure
815 * @command: the command to be sent
816 * @column: the column address for this command, -1 if none
817 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200819 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700820 * devices. We don't have the separate regions as we have in the small page
821 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200823static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
824 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100826 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 /* Emulate NAND_CMD_READOOB */
829 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200830 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 command = NAND_CMD_READ0;
832 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000833
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200834 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400835 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200838 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /* Serially input address */
841 if (column != -1) {
842 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800843 if (chip->options & NAND_BUSWIDTH_16 &&
844 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200846 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200847 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200848
Brian Norrisf5b88de2016-10-03 09:49:35 -0700849 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200850 if (!nand_opcode_8bits(command))
851 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200854 chip->cmd_ctrl(mtd, page_addr, ctrl);
855 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200856 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200858 if (chip->chipsize > (128 << 20))
859 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200860 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200863 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000864
865 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700866 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100867 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000868 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 case NAND_CMD_CACHEDPROG:
872 case NAND_CMD_PAGEPROG:
873 case NAND_CMD_ERASE1:
874 case NAND_CMD_ERASE2:
875 case NAND_CMD_SEQIN:
876 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900877 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900878 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000879 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200881 case NAND_CMD_RNDIN:
882 nand_ccs_delay(chip);
883 return;
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200886 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200888 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200889 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
890 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
891 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
892 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200893 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
894 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return;
896
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200897 case NAND_CMD_RNDOUT:
898 /* No ready / busy check necessary */
899 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
900 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
901 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
902 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200903
904 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200905 return;
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200908 /*
909 * READ0 is sometimes used to exit GET STATUS mode. When this
910 * is the case no address cycles are requested, and we can use
911 * this information to detect that READSTART should not be
912 * issued.
913 */
914 if (column == -1 && page_addr == -1)
915 return;
916
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200917 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
918 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
919 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
920 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000921
David Woodhousee0c7d762006-05-13 18:07:53 +0100922 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000924 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700926 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100927 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200928 if (!chip->dev_ready) {
929 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000933
Brian Norris8b6e50c2011-05-25 14:59:01 -0700934 /*
935 * Apply this short delay always to ensure that we do wait tWB in
936 * any case on any machine.
937 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100938 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000939
940 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200944 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700945 * @chip: the nand chip descriptor
946 * @mtd: MTD device structure
947 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200948 *
949 * Used when in panic, no locks are taken.
950 */
951static void panic_nand_get_device(struct nand_chip *chip,
952 struct mtd_info *mtd, int new_state)
953{
Brian Norris7854d3f2011-06-23 14:12:08 -0700954 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200955 chip->controller->active = chip;
956 chip->state = new_state;
957}
958
959/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700961 * @mtd: MTD device structure
962 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 *
964 * Get the device and lock it for exclusive access
965 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200966static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800967nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100969 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200970 spinlock_t *lock = &chip->controller->lock;
971 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100972 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200973retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100974 spin_lock(lock);
975
vimal singhb8b3ee92009-07-09 20:41:22 +0530976 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200977 if (!chip->controller->active)
978 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200979
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200980 if (chip->controller->active == chip && chip->state == FL_READY) {
981 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100982 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100983 return 0;
984 }
985 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800986 if (chip->controller->active->state == FL_PM_SUSPENDED) {
987 chip->state = FL_PM_SUSPENDED;
988 spin_unlock(lock);
989 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800990 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100991 }
992 set_current_state(TASK_UNINTERRUPTIBLE);
993 add_wait_queue(wq, &wait);
994 spin_unlock(lock);
995 schedule();
996 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto retry;
998}
999
1000/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001001 * panic_nand_wait - [GENERIC] wait until the command is done
1002 * @mtd: MTD device structure
1003 * @chip: NAND chip structure
1004 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001005 *
1006 * Wait for command done. This is a helper function for nand_wait used when
1007 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001008 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001009 */
1010static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1011 unsigned long timeo)
1012{
1013 int i;
1014 for (i = 0; i < timeo; i++) {
1015 if (chip->dev_ready) {
1016 if (chip->dev_ready(mtd))
1017 break;
1018 } else {
1019 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1020 break;
1021 }
1022 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001023 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001024}
1025
1026/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001027 * nand_wait - [DEFAULT] wait until the command is done
1028 * @mtd: MTD device structure
1029 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001031 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001032 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001033static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035
Alex Smithb70af9b2015-10-06 14:52:07 +01001036 int status;
1037 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Brian Norris8b6e50c2011-05-25 14:59:01 -07001039 /*
1040 * Apply this short delay always to ensure that we do wait tWB in any
1041 * case on any machine.
1042 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001043 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Artem Bityutskiy14c65782013-03-04 14:21:34 +02001045 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001047 if (in_interrupt() || oops_in_progress)
1048 panic_nand_wait(mtd, chip, timeo);
1049 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001050 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001051 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001052 if (chip->dev_ready) {
1053 if (chip->dev_ready(mtd))
1054 break;
1055 } else {
1056 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1057 break;
1058 }
1059 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001060 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001062
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001063 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001064 /* This can happen if in case of timeout or buggy dev_ready */
1065 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return status;
1067}
1068
1069/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001070 * nand_reset_data_interface - Reset data interface and timings
1071 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001072 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001073 *
1074 * Reset the Data interface and timings to ONFI mode 0.
1075 *
1076 * Returns 0 for success or negative error code otherwise.
1077 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001078static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001079{
1080 struct mtd_info *mtd = nand_to_mtd(chip);
1081 const struct nand_data_interface *conf;
1082 int ret;
1083
1084 if (!chip->setup_data_interface)
1085 return 0;
1086
1087 /*
1088 * The ONFI specification says:
1089 * "
1090 * To transition from NV-DDR or NV-DDR2 to the SDR data
1091 * interface, the host shall use the Reset (FFh) command
1092 * using SDR timing mode 0. A device in any timing mode is
1093 * required to recognize Reset (FFh) command issued in SDR
1094 * timing mode 0.
1095 * "
1096 *
1097 * Configure the data interface in SDR mode and set the
1098 * timings to timing mode 0.
1099 */
1100
1101 conf = nand_get_default_data_interface();
Boris Brezillon104e4422017-03-16 09:35:58 +01001102 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001103 if (ret)
1104 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1105
1106 return ret;
1107}
1108
1109/**
1110 * nand_setup_data_interface - Setup the best data interface and timings
1111 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001112 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001113 *
1114 * Find and configure the best data interface and NAND timings supported by
1115 * the chip and the driver.
1116 * First tries to retrieve supported timing modes from ONFI information,
1117 * and if the NAND chip does not support ONFI, relies on the
1118 * ->onfi_timing_mode_default specified in the nand_ids table.
1119 *
1120 * Returns 0 for success or negative error code otherwise.
1121 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001122static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001123{
1124 struct mtd_info *mtd = nand_to_mtd(chip);
1125 int ret;
1126
1127 if (!chip->setup_data_interface || !chip->data_interface)
1128 return 0;
1129
1130 /*
1131 * Ensure the timing mode has been changed on the chip side
1132 * before changing timings on the controller side.
1133 */
Boris Brezillona11bf5e2017-07-31 10:29:56 +02001134 if (chip->onfi_version &&
1135 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1136 ONFI_OPT_CMD_SET_GET_FEATURES)) {
Boris Brezillond8e725d2016-09-15 10:32:50 +02001137 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1138 chip->onfi_timing_mode_default,
1139 };
1140
1141 ret = chip->onfi_set_features(mtd, chip,
1142 ONFI_FEATURE_ADDR_TIMING_MODE,
1143 tmode_param);
1144 if (ret)
1145 goto err;
1146 }
1147
Boris Brezillon104e4422017-03-16 09:35:58 +01001148 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001149err:
1150 return ret;
1151}
1152
1153/**
1154 * nand_init_data_interface - find the best data interface and timings
1155 * @chip: The NAND chip
1156 *
1157 * Find the best data interface and NAND timings supported by the chip
1158 * and the driver.
1159 * First tries to retrieve supported timing modes from ONFI information,
1160 * and if the NAND chip does not support ONFI, relies on the
1161 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1162 * function nand_chip->data_interface is initialized with the best timing mode
1163 * available.
1164 *
1165 * Returns 0 for success or negative error code otherwise.
1166 */
1167static int nand_init_data_interface(struct nand_chip *chip)
1168{
1169 struct mtd_info *mtd = nand_to_mtd(chip);
1170 int modes, mode, ret;
1171
1172 if (!chip->setup_data_interface)
1173 return 0;
1174
1175 /*
1176 * First try to identify the best timings from ONFI parameters and
1177 * if the NAND does not support ONFI, fallback to the default ONFI
1178 * timing mode.
1179 */
1180 modes = onfi_get_async_timing_mode(chip);
1181 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1182 if (!chip->onfi_timing_mode_default)
1183 return 0;
1184
1185 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1186 }
1187
1188 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1189 GFP_KERNEL);
1190 if (!chip->data_interface)
1191 return -ENOMEM;
1192
1193 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1194 ret = onfi_init_data_interface(chip, chip->data_interface,
1195 NAND_SDR_IFACE, mode);
1196 if (ret)
1197 continue;
1198
Boris Brezillon104e4422017-03-16 09:35:58 +01001199 /* Pass -1 to only */
1200 ret = chip->setup_data_interface(mtd,
1201 NAND_DATA_IFACE_CHECK_ONLY,
1202 chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001203 if (!ret) {
1204 chip->onfi_timing_mode_default = mode;
1205 break;
1206 }
1207 }
1208
1209 return 0;
1210}
1211
1212static void nand_release_data_interface(struct nand_chip *chip)
1213{
1214 kfree(chip->data_interface);
1215}
1216
1217/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001218 * nand_reset - Reset and initialize a NAND device
1219 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001220 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001221 *
1222 * Returns 0 for success or negative error code otherwise
1223 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001224int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001225{
1226 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001227 int ret;
1228
Boris Brezillon104e4422017-03-16 09:35:58 +01001229 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001230 if (ret)
1231 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001232
Boris Brezillon73f907f2016-10-24 16:46:20 +02001233 /*
1234 * The CS line has to be released before we can apply the new NAND
1235 * interface settings, hence this weird ->select_chip() dance.
1236 */
1237 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001238 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001239 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001240
Boris Brezillon73f907f2016-10-24 16:46:20 +02001241 chip->select_chip(mtd, chipnr);
Boris Brezillon104e4422017-03-16 09:35:58 +01001242 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001243 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001244 if (ret)
1245 return ret;
1246
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001247 return 0;
1248}
1249
1250/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001251 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001252 * @mtd: mtd info
1253 * @ofs: offset to start unlock from
1254 * @len: length to unlock
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -03001255 * @invert:
1256 * - when = 0, unlock the range of blocks within the lower and
Brian Norris8b6e50c2011-05-25 14:59:01 -07001257 * upper boundary address
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -03001258 * - when = 1, unlock the range of blocks outside the boundaries
Brian Norris8b6e50c2011-05-25 14:59:01 -07001259 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301260 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001261 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301262 */
1263static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1264 uint64_t len, int invert)
1265{
1266 int ret = 0;
1267 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001268 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301269
1270 /* Submit address of first page to unlock */
1271 page = ofs >> chip->page_shift;
1272 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1273
1274 /* Submit address of last page to unlock */
1275 page = (ofs + len) >> chip->page_shift;
1276 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1277 (page | invert) & chip->pagemask);
1278
1279 /* Call wait ready function */
1280 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301281 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001282 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001283 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301284 __func__, status);
1285 ret = -EIO;
1286 }
1287
1288 return ret;
1289}
1290
1291/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001292 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001293 * @mtd: mtd info
1294 * @ofs: offset to start unlock from
1295 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301296 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001297 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301298 */
1299int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1300{
1301 int ret = 0;
1302 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001303 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301304
Brian Norris289c0522011-07-19 10:06:09 -07001305 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301306 __func__, (unsigned long long)ofs, len);
1307
1308 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001309 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301310
1311 /* Align to last block address if size addresses end of the device */
1312 if (ofs + len == mtd->size)
1313 len -= mtd->erasesize;
1314
Huang Shijie6a8214a2012-11-19 14:43:30 +08001315 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301316
1317 /* Shift to get chip number */
1318 chipnr = ofs >> chip->chip_shift;
1319
White Ding57d3a9a2014-07-24 00:10:45 +08001320 /*
1321 * Reset the chip.
1322 * If we want to check the WP through READ STATUS and check the bit 7
1323 * we must reset the chip
1324 * some operation can also clear the bit 7 of status register
1325 * eg. erase/program a locked block
1326 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001327 nand_reset(chip, chipnr);
1328
1329 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001330
Vimal Singh7d70f332010-02-08 15:50:49 +05301331 /* Check, if it is write protected */
1332 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001333 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301334 __func__);
1335 ret = -EIO;
1336 goto out;
1337 }
1338
1339 ret = __nand_unlock(mtd, ofs, len, 0);
1340
1341out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001342 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301343 nand_release_device(mtd);
1344
1345 return ret;
1346}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001347EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301348
1349/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001350 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001351 * @mtd: mtd info
1352 * @ofs: offset to start unlock from
1353 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301354 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001355 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1356 * have this feature, but it allows only to lock all blocks, not for specified
1357 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1358 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301359 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001360 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301361 */
1362int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1363{
1364 int ret = 0;
1365 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001366 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301367
Brian Norris289c0522011-07-19 10:06:09 -07001368 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301369 __func__, (unsigned long long)ofs, len);
1370
1371 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001372 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301373
Huang Shijie6a8214a2012-11-19 14:43:30 +08001374 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301375
1376 /* Shift to get chip number */
1377 chipnr = ofs >> chip->chip_shift;
1378
White Ding57d3a9a2014-07-24 00:10:45 +08001379 /*
1380 * Reset the chip.
1381 * If we want to check the WP through READ STATUS and check the bit 7
1382 * we must reset the chip
1383 * some operation can also clear the bit 7 of status register
1384 * eg. erase/program a locked block
1385 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001386 nand_reset(chip, chipnr);
1387
1388 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001389
Vimal Singh7d70f332010-02-08 15:50:49 +05301390 /* Check, if it is write protected */
1391 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001392 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301393 __func__);
1394 status = MTD_ERASE_FAILED;
1395 ret = -EIO;
1396 goto out;
1397 }
1398
1399 /* Submit address of first page to lock */
1400 page = ofs >> chip->page_shift;
1401 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1402
1403 /* Call wait ready function */
1404 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301405 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001406 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001407 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301408 __func__, status);
1409 ret = -EIO;
1410 goto out;
1411 }
1412
1413 ret = __nand_unlock(mtd, ofs, len, 0x1);
1414
1415out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001416 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301417 nand_release_device(mtd);
1418
1419 return ret;
1420}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001421EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301422
1423/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001424 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1425 * @buf: buffer to test
1426 * @len: buffer length
1427 * @bitflips_threshold: maximum number of bitflips
1428 *
1429 * Check if a buffer contains only 0xff, which means the underlying region
1430 * has been erased and is ready to be programmed.
1431 * The bitflips_threshold specify the maximum number of bitflips before
1432 * considering the region is not erased.
1433 * Note: The logic of this function has been extracted from the memweight
1434 * implementation, except that nand_check_erased_buf function exit before
1435 * testing the whole buffer if the number of bitflips exceed the
1436 * bitflips_threshold value.
1437 *
1438 * Returns a positive number of bitflips less than or equal to
1439 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1440 * threshold.
1441 */
1442static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1443{
1444 const unsigned char *bitmap = buf;
1445 int bitflips = 0;
1446 int weight;
1447
1448 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1449 len--, bitmap++) {
1450 weight = hweight8(*bitmap);
1451 bitflips += BITS_PER_BYTE - weight;
1452 if (unlikely(bitflips > bitflips_threshold))
1453 return -EBADMSG;
1454 }
1455
1456 for (; len >= sizeof(long);
1457 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001458 unsigned long d = *((unsigned long *)bitmap);
1459 if (d == ~0UL)
1460 continue;
1461 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001462 bitflips += BITS_PER_LONG - weight;
1463 if (unlikely(bitflips > bitflips_threshold))
1464 return -EBADMSG;
1465 }
1466
1467 for (; len > 0; len--, bitmap++) {
1468 weight = hweight8(*bitmap);
1469 bitflips += BITS_PER_BYTE - weight;
1470 if (unlikely(bitflips > bitflips_threshold))
1471 return -EBADMSG;
1472 }
1473
1474 return bitflips;
1475}
1476
1477/**
1478 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1479 * 0xff data
1480 * @data: data buffer to test
1481 * @datalen: data length
1482 * @ecc: ECC buffer
1483 * @ecclen: ECC length
1484 * @extraoob: extra OOB buffer
1485 * @extraooblen: extra OOB length
1486 * @bitflips_threshold: maximum number of bitflips
1487 *
1488 * Check if a data buffer and its associated ECC and OOB data contains only
1489 * 0xff pattern, which means the underlying region has been erased and is
1490 * ready to be programmed.
1491 * The bitflips_threshold specify the maximum number of bitflips before
1492 * considering the region as not erased.
1493 *
1494 * Note:
1495 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1496 * different from the NAND page size. When fixing bitflips, ECC engines will
1497 * report the number of errors per chunk, and the NAND core infrastructure
1498 * expect you to return the maximum number of bitflips for the whole page.
1499 * This is why you should always use this function on a single chunk and
1500 * not on the whole page. After checking each chunk you should update your
1501 * max_bitflips value accordingly.
1502 * 2/ When checking for bitflips in erased pages you should not only check
1503 * the payload data but also their associated ECC data, because a user might
1504 * have programmed almost all bits to 1 but a few. In this case, we
1505 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1506 * this case.
1507 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1508 * data are protected by the ECC engine.
1509 * It could also be used if you support subpages and want to attach some
1510 * extra OOB data to an ECC chunk.
1511 *
1512 * Returns a positive number of bitflips less than or equal to
1513 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1514 * threshold. In case of success, the passed buffers are filled with 0xff.
1515 */
1516int nand_check_erased_ecc_chunk(void *data, int datalen,
1517 void *ecc, int ecclen,
1518 void *extraoob, int extraooblen,
1519 int bitflips_threshold)
1520{
1521 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1522
1523 data_bitflips = nand_check_erased_buf(data, datalen,
1524 bitflips_threshold);
1525 if (data_bitflips < 0)
1526 return data_bitflips;
1527
1528 bitflips_threshold -= data_bitflips;
1529
1530 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1531 if (ecc_bitflips < 0)
1532 return ecc_bitflips;
1533
1534 bitflips_threshold -= ecc_bitflips;
1535
1536 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1537 bitflips_threshold);
1538 if (extraoob_bitflips < 0)
1539 return extraoob_bitflips;
1540
1541 if (data_bitflips)
1542 memset(data, 0xff, datalen);
1543
1544 if (ecc_bitflips)
1545 memset(ecc, 0xff, ecclen);
1546
1547 if (extraoob_bitflips)
1548 memset(extraoob, 0xff, extraooblen);
1549
1550 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1551}
1552EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1553
1554/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001555 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001556 * @mtd: mtd info structure
1557 * @chip: nand chip info structure
1558 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001559 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001560 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001561 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001562 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001563 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001564int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1565 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001566{
1567 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001568 if (oob_required)
1569 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001570 return 0;
1571}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001572EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001573
1574/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001575 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001576 * @mtd: mtd info structure
1577 * @chip: nand chip info structure
1578 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001579 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001580 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001581 *
1582 * We need a special oob layout and handling even when OOB isn't used.
1583 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001584static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001585 struct nand_chip *chip, uint8_t *buf,
1586 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001587{
1588 int eccsize = chip->ecc.size;
1589 int eccbytes = chip->ecc.bytes;
1590 uint8_t *oob = chip->oob_poi;
1591 int steps, size;
1592
1593 for (steps = chip->ecc.steps; steps > 0; steps--) {
1594 chip->read_buf(mtd, buf, eccsize);
1595 buf += eccsize;
1596
1597 if (chip->ecc.prepad) {
1598 chip->read_buf(mtd, oob, chip->ecc.prepad);
1599 oob += chip->ecc.prepad;
1600 }
1601
1602 chip->read_buf(mtd, oob, eccbytes);
1603 oob += eccbytes;
1604
1605 if (chip->ecc.postpad) {
1606 chip->read_buf(mtd, oob, chip->ecc.postpad);
1607 oob += chip->ecc.postpad;
1608 }
1609 }
1610
1611 size = mtd->oobsize - (oob - chip->oob_poi);
1612 if (size)
1613 chip->read_buf(mtd, oob, size);
1614
1615 return 0;
1616}
1617
1618/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001619 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001620 * @mtd: mtd info structure
1621 * @chip: nand chip info structure
1622 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001623 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001624 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001625 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001626static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001627 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Boris Brezillon846031d2016-02-03 20:11:00 +01001629 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001630 int eccbytes = chip->ecc.bytes;
1631 int eccsteps = chip->ecc.steps;
1632 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001633 uint8_t *ecc_calc = chip->buffers->ecccalc;
1634 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001635 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001636
Brian Norris1fbb9382012-05-02 10:14:55 -07001637 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001638
1639 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1640 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1641
Boris Brezillon846031d2016-02-03 20:11:00 +01001642 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1643 chip->ecc.total);
1644 if (ret)
1645 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001646
1647 eccsteps = chip->ecc.steps;
1648 p = buf;
1649
1650 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1651 int stat;
1652
1653 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001654 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001655 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001656 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001657 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001658 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1659 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001660 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001661 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301665 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001666 * @mtd: mtd info structure
1667 * @chip: nand chip info structure
1668 * @data_offs: offset of requested data within the page
1669 * @readlen: data length
1670 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001671 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001672 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001673static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001674 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1675 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001676{
Boris Brezillon846031d2016-02-03 20:11:00 +01001677 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001678 uint8_t *p;
1679 int data_col_addr, i, gaps = 0;
1680 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1681 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001682 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001683 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001684 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001685
Brian Norris7854d3f2011-06-23 14:12:08 -07001686 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001687 start_step = data_offs / chip->ecc.size;
1688 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1689 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301690 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001691
Brian Norris8b6e50c2011-05-25 14:59:01 -07001692 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001693 datafrag_len = num_steps * chip->ecc.size;
1694 eccfrag_len = num_steps * chip->ecc.bytes;
1695
1696 data_col_addr = start_step * chip->ecc.size;
1697 /* If we read not a page aligned data */
1698 if (data_col_addr != 0)
1699 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1700
1701 p = bufpoi + data_col_addr;
1702 chip->read_buf(mtd, p, datafrag_len);
1703
Brian Norris8b6e50c2011-05-25 14:59:01 -07001704 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001705 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1706 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1707
Brian Norris8b6e50c2011-05-25 14:59:01 -07001708 /*
1709 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001710 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001711 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001712 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1713 if (ret)
1714 return ret;
1715
1716 if (oobregion.length < eccfrag_len)
1717 gaps = 1;
1718
Alexey Korolev3d459552008-05-15 17:23:18 +01001719 if (gaps) {
1720 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1721 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1722 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001723 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001724 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001725 * about buswidth alignment in read_buf.
1726 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001727 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001728 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001729 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001730 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001731 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1732 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001733 aligned_len++;
1734
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001735 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001736 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001737 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1738 }
1739
Boris Brezillon846031d2016-02-03 20:11:00 +01001740 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1741 chip->oob_poi, index, eccfrag_len);
1742 if (ret)
1743 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001744
1745 p = bufpoi + data_col_addr;
1746 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1747 int stat;
1748
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001749 stat = chip->ecc.correct(mtd, p,
1750 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001751 if (stat == -EBADMSG &&
1752 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1753 /* check for empty pages with bitflips */
1754 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1755 &chip->buffers->ecccode[i],
1756 chip->ecc.bytes,
1757 NULL, 0,
1758 chip->ecc.strength);
1759 }
1760
Mike Dunn3f91e942012-04-25 12:06:09 -07001761 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001762 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001763 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001764 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001765 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1766 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001767 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001768 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001769}
1770
1771/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001772 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001773 * @mtd: mtd info structure
1774 * @chip: nand chip info structure
1775 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001776 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001777 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001778 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001779 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001780 */
1781static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001782 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001783{
Boris Brezillon846031d2016-02-03 20:11:00 +01001784 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001785 int eccbytes = chip->ecc.bytes;
1786 int eccsteps = chip->ecc.steps;
1787 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001788 uint8_t *ecc_calc = chip->buffers->ecccalc;
1789 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001790 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001791
1792 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1793 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1794 chip->read_buf(mtd, p, eccsize);
1795 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1796 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001797 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001798
Boris Brezillon846031d2016-02-03 20:11:00 +01001799 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1800 chip->ecc.total);
1801 if (ret)
1802 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001803
1804 eccsteps = chip->ecc.steps;
1805 p = buf;
1806
1807 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1808 int stat;
1809
1810 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001811 if (stat == -EBADMSG &&
1812 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1813 /* check for empty pages with bitflips */
1814 stat = nand_check_erased_ecc_chunk(p, eccsize,
1815 &ecc_code[i], eccbytes,
1816 NULL, 0,
1817 chip->ecc.strength);
1818 }
1819
Mike Dunn3f91e942012-04-25 12:06:09 -07001820 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001821 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001822 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001823 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001824 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1825 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001826 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001827 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001828}
1829
1830/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001831 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001832 * @mtd: mtd info structure
1833 * @chip: nand chip info structure
1834 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001835 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001836 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001837 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001838 * Hardware ECC for large page chips, require OOB to be read first. For this
1839 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1840 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1841 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1842 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001843 */
1844static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001845 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001846{
Boris Brezillon846031d2016-02-03 20:11:00 +01001847 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001848 int eccbytes = chip->ecc.bytes;
1849 int eccsteps = chip->ecc.steps;
1850 uint8_t *p = buf;
1851 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001852 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001853 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001854
1855 /* Read the OOB area first */
1856 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1857 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1858 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1859
Boris Brezillon846031d2016-02-03 20:11:00 +01001860 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1861 chip->ecc.total);
1862 if (ret)
1863 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001864
1865 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1866 int stat;
1867
1868 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1869 chip->read_buf(mtd, p, eccsize);
1870 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1871
1872 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001873 if (stat == -EBADMSG &&
1874 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1875 /* check for empty pages with bitflips */
1876 stat = nand_check_erased_ecc_chunk(p, eccsize,
1877 &ecc_code[i], eccbytes,
1878 NULL, 0,
1879 chip->ecc.strength);
1880 }
1881
Mike Dunn3f91e942012-04-25 12:06:09 -07001882 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001883 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001884 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001885 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001886 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1887 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001888 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001889 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001890}
1891
1892/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001893 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001894 * @mtd: mtd info structure
1895 * @chip: nand chip info structure
1896 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001897 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001898 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001899 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001900 * The hw generator calculates the error syndrome automatically. Therefore we
1901 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001902 */
1903static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001904 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001905{
1906 int i, eccsize = chip->ecc.size;
1907 int eccbytes = chip->ecc.bytes;
1908 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001909 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001910 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001911 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001912 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001913
1914 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1915 int stat;
1916
1917 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1918 chip->read_buf(mtd, p, eccsize);
1919
1920 if (chip->ecc.prepad) {
1921 chip->read_buf(mtd, oob, chip->ecc.prepad);
1922 oob += chip->ecc.prepad;
1923 }
1924
1925 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1926 chip->read_buf(mtd, oob, eccbytes);
1927 stat = chip->ecc.correct(mtd, p, oob, NULL);
1928
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001929 oob += eccbytes;
1930
1931 if (chip->ecc.postpad) {
1932 chip->read_buf(mtd, oob, chip->ecc.postpad);
1933 oob += chip->ecc.postpad;
1934 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001935
1936 if (stat == -EBADMSG &&
1937 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1938 /* check for empty pages with bitflips */
1939 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1940 oob - eccpadbytes,
1941 eccpadbytes,
1942 NULL, 0,
1943 chip->ecc.strength);
1944 }
1945
1946 if (stat < 0) {
1947 mtd->ecc_stats.failed++;
1948 } else {
1949 mtd->ecc_stats.corrected += stat;
1950 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1951 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001952 }
1953
1954 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001955 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001956 if (i)
1957 chip->read_buf(mtd, oob, i);
1958
Mike Dunn3f91e942012-04-25 12:06:09 -07001959 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001960}
1961
1962/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001963 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001964 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001965 * @oob: oob destination address
1966 * @ops: oob ops structure
1967 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001968 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001969static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001970 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001971{
Boris Brezillon846031d2016-02-03 20:11:00 +01001972 struct nand_chip *chip = mtd_to_nand(mtd);
1973 int ret;
1974
Florian Fainellif8ac0412010-09-07 13:23:43 +02001975 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001976
Brian Norris0612b9d2011-08-30 18:45:40 -07001977 case MTD_OPS_PLACE_OOB:
1978 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001979 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1980 return oob + len;
1981
Boris Brezillon846031d2016-02-03 20:11:00 +01001982 case MTD_OPS_AUTO_OOB:
1983 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1984 ops->ooboffs, len);
1985 BUG_ON(ret);
1986 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001987
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001988 default:
1989 BUG();
1990 }
1991 return NULL;
1992}
1993
1994/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001995 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1996 * @mtd: MTD device structure
1997 * @retry_mode: the retry mode to use
1998 *
1999 * Some vendors supply a special command to shift the Vt threshold, to be used
2000 * when there are too many bitflips in a page (i.e., ECC error). After setting
2001 * a new threshold, the host should retry reading the page.
2002 */
2003static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2004{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002005 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08002006
2007 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2008
2009 if (retry_mode >= chip->read_retries)
2010 return -EINVAL;
2011
2012 if (!chip->setup_read_retry)
2013 return -EOPNOTSUPP;
2014
2015 return chip->setup_read_retry(mtd, retry_mode);
2016}
2017
2018/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002019 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002020 * @mtd: MTD device structure
2021 * @from: offset to read from
2022 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00002023 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002024 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00002025 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002026static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2027 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00002028{
Brian Norrise47f3db2012-05-02 10:14:56 -07002029 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002030 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002031 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002032 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03002033 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002034 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02002035
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002036 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002037 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07002038 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08002039 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08002040 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002042 chipnr = (int)(from >> chip->chip_shift);
2043 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002045 realpage = (int)(from >> chip->page_shift);
2046 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002048 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002050 buf = ops->datbuf;
2051 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07002052 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002053
Florian Fainellif8ac0412010-09-07 13:23:43 +02002054 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08002055 unsigned int ecc_failures = mtd->ecc_stats.failed;
2056
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002057 bytes = min(mtd->writesize - col, readlen);
2058 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002059
Kamal Dasu66507c72014-05-01 20:51:19 -04002060 if (!aligned)
2061 use_bufpoi = 1;
2062 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002063 use_bufpoi = !virt_addr_valid(buf) ||
2064 !IS_ALIGNED((unsigned long)buf,
2065 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002066 else
2067 use_bufpoi = 0;
2068
Brian Norris8b6e50c2011-05-25 14:59:01 -07002069 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002070 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002071 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2072
2073 if (use_bufpoi && aligned)
2074 pr_debug("%s: using read bounce buffer for buf@%p\n",
2075 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Brian Norrisba84fb52014-01-03 15:13:33 -08002077read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01002078 if (nand_standard_page_accessors(&chip->ecc))
2079 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Mike Dunnedbc45402012-04-25 12:06:11 -07002081 /*
2082 * Now read the page into the buffer. Absent an error,
2083 * the read methods return max bitflips per ecc step.
2084 */
Brian Norris0612b9d2011-08-30 18:45:40 -07002085 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07002086 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002087 oob_required,
2088 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002089 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
2090 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002091 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08002092 col, bytes, bufpoi,
2093 page);
David Woodhouse956e9442006-09-25 17:12:39 +01002094 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07002095 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002096 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07002097 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002098 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07002099 /* Invalidate page cache */
2100 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01002101 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07002102 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002103
2104 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04002105 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002106 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08002107 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07002108 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002109 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07002110 chip->pagebuf_bitflips = ret;
2111 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07002112 /* Invalidate page cache */
2113 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07002114 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002115 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002117
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002118 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002119 int toread = min(oobreadlen, max_oobsize);
2120
2121 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01002122 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002123 oob, ops, toread);
2124 oobreadlen -= toread;
2125 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002126 }
Brian Norris5bc7c332013-03-13 09:51:31 -07002127
2128 if (chip->options & NAND_NEED_READRDY) {
2129 /* Apply delay or wait for ready/busy pin */
2130 if (!chip->dev_ready)
2131 udelay(chip->chip_delay);
2132 else
2133 nand_wait_ready(mtd);
2134 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002135
Brian Norrisba84fb52014-01-03 15:13:33 -08002136 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002137 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002138 retry_mode++;
2139 ret = nand_setup_read_retry(mtd,
2140 retry_mode);
2141 if (ret < 0)
2142 break;
2143
2144 /* Reset failures; retry */
2145 mtd->ecc_stats.failed = ecc_failures;
2146 goto read_retry;
2147 } else {
2148 /* No more retry modes; real failure */
2149 ecc_fail = true;
2150 }
2151 }
2152
2153 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09002154 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002155 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002156 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002157 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002158 max_bitflips = max_t(unsigned int, max_bitflips,
2159 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002162 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002163
Brian Norrisba84fb52014-01-03 15:13:33 -08002164 /* Reset to retry mode 0 */
2165 if (retry_mode) {
2166 ret = nand_setup_read_retry(mtd, 0);
2167 if (ret < 0)
2168 break;
2169 retry_mode = 0;
2170 }
2171
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002172 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Brian Norris8b6e50c2011-05-25 14:59:01 -07002175 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 col = 0;
2177 /* Increment page address */
2178 realpage++;
2179
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002180 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /* Check, if we cross a chip boundary */
2182 if (!page) {
2183 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002184 chip->select_chip(mtd, -1);
2185 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002188 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002190 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002191 if (oob)
2192 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Mike Dunn3f91e942012-04-25 12:06:09 -07002194 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002195 return ret;
2196
Brian Norrisb72f3df2013-12-03 11:04:14 -08002197 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002198 return -EBADMSG;
2199
Mike Dunnedbc45402012-04-25 12:06:11 -07002200 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002201}
2202
2203/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002204 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002205 * @mtd: MTD device structure
2206 * @from: offset to read from
2207 * @len: number of bytes to read
2208 * @retlen: pointer to variable to store the number of read bytes
2209 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002210 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002211 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002212 */
2213static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2214 size_t *retlen, uint8_t *buf)
2215{
Brian Norris4a89ff82011-08-30 18:45:45 -07002216 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002217 int ret;
2218
Huang Shijie6a8214a2012-11-19 14:43:30 +08002219 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002220 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002221 ops.len = len;
2222 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002223 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002224 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002225 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002226 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002227 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
2230/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002231 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002232 * @mtd: mtd info structure
2233 * @chip: nand chip info structure
2234 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002235 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002236int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002237{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002238 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002239 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002240 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002241}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002242EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002243
2244/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002245 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002246 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002247 * @mtd: mtd info structure
2248 * @chip: nand chip info structure
2249 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002250 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002251int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2252 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002253{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002254 int length = mtd->oobsize;
2255 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2256 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002257 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002258 int i, toread, sndrnd = 0, pos;
2259
2260 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2261 for (i = 0; i < chip->ecc.steps; i++) {
2262 if (sndrnd) {
2263 pos = eccsize + i * (eccsize + chunk);
2264 if (mtd->writesize > 512)
2265 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2266 else
2267 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2268 } else
2269 sndrnd = 1;
2270 toread = min_t(int, length, chunk);
2271 chip->read_buf(mtd, bufpoi, toread);
2272 bufpoi += toread;
2273 length -= toread;
2274 }
2275 if (length > 0)
2276 chip->read_buf(mtd, bufpoi, length);
2277
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002278 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002279}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002280EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002281
2282/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002283 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002284 * @mtd: mtd info structure
2285 * @chip: nand chip info structure
2286 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002287 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002288int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002289{
2290 int status = 0;
2291 const uint8_t *buf = chip->oob_poi;
2292 int length = mtd->oobsize;
2293
2294 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2295 chip->write_buf(mtd, buf, length);
2296 /* Send command to program the OOB data */
2297 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2298
2299 status = chip->waitfunc(mtd, chip);
2300
Savin Zlobec0d420f92006-06-21 11:51:20 +02002301 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002302}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002303EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002304
2305/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002306 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002307 * with syndrome - only for large page flash
2308 * @mtd: mtd info structure
2309 * @chip: nand chip info structure
2310 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002311 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002312int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2313 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002314{
2315 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2316 int eccsize = chip->ecc.size, length = mtd->oobsize;
2317 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2318 const uint8_t *bufpoi = chip->oob_poi;
2319
2320 /*
2321 * data-ecc-data-ecc ... ecc-oob
2322 * or
2323 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2324 */
2325 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2326 pos = steps * (eccsize + chunk);
2327 steps = 0;
2328 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002329 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002330
2331 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2332 for (i = 0; i < steps; i++) {
2333 if (sndcmd) {
2334 if (mtd->writesize <= 512) {
2335 uint32_t fill = 0xFFFFFFFF;
2336
2337 len = eccsize;
2338 while (len > 0) {
2339 int num = min_t(int, len, 4);
2340 chip->write_buf(mtd, (uint8_t *)&fill,
2341 num);
2342 len -= num;
2343 }
2344 } else {
2345 pos = eccsize + i * (eccsize + chunk);
2346 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2347 }
2348 } else
2349 sndcmd = 1;
2350 len = min_t(int, length, chunk);
2351 chip->write_buf(mtd, bufpoi, len);
2352 bufpoi += len;
2353 length -= len;
2354 }
2355 if (length > 0)
2356 chip->write_buf(mtd, bufpoi, length);
2357
2358 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2359 status = chip->waitfunc(mtd, chip);
2360
2361 return status & NAND_STATUS_FAIL ? -EIO : 0;
2362}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002363EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002364
2365/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002366 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002367 * @mtd: MTD device structure
2368 * @from: offset to read from
2369 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002371 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002373static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2374 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Brian Norrisc00a0992012-05-01 17:12:54 -07002376 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002377 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002378 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002379 int readlen = ops->ooblen;
2380 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002381 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002382 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Brian Norris289c0522011-07-19 10:06:09 -07002384 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302385 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Brian Norris041e4572011-06-23 16:45:24 -07002387 stats = mtd->ecc_stats;
2388
Boris BREZILLON29f10582016-03-07 10:46:52 +01002389 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002390
2391 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002392 pr_debug("%s: attempt to start read outside oob\n",
2393 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002394 return -EINVAL;
2395 }
2396
2397 /* Do not allow reads past end of device */
2398 if (unlikely(from >= mtd->size ||
2399 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2400 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002401 pr_debug("%s: attempt to read beyond end of device\n",
2402 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002403 return -EINVAL;
2404 }
Vitaly Wool70145682006-11-03 18:20:38 +03002405
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002406 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002407 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002409 /* Shift to get page */
2410 realpage = (int)(from >> chip->page_shift);
2411 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Florian Fainellif8ac0412010-09-07 13:23:43 +02002413 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002414 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002415 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002416 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002417 ret = chip->ecc.read_oob(mtd, chip, page);
2418
2419 if (ret < 0)
2420 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002421
2422 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002423 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002424
Brian Norris5bc7c332013-03-13 09:51:31 -07002425 if (chip->options & NAND_NEED_READRDY) {
2426 /* Apply delay or wait for ready/busy pin */
2427 if (!chip->dev_ready)
2428 udelay(chip->chip_delay);
2429 else
2430 nand_wait_ready(mtd);
2431 }
2432
Vitaly Wool70145682006-11-03 18:20:38 +03002433 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002434 if (!readlen)
2435 break;
2436
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002437 /* Increment page address */
2438 realpage++;
2439
2440 page = realpage & chip->pagemask;
2441 /* Check, if we cross a chip boundary */
2442 if (!page) {
2443 chipnr++;
2444 chip->select_chip(mtd, -1);
2445 chip->select_chip(mtd, chipnr);
2446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002448 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002450 ops->oobretlen = ops->ooblen - readlen;
2451
2452 if (ret < 0)
2453 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002454
2455 if (mtd->ecc_stats.failed - stats.failed)
2456 return -EBADMSG;
2457
2458 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
2461/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002462 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002463 * @mtd: MTD device structure
2464 * @from: offset to read from
2465 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002467 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002469static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2470 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002472 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002473
2474 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002477 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002478 pr_debug("%s: attempt to read beyond end of device\n",
2479 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return -EINVAL;
2481 }
2482
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002483 if (ops->mode != MTD_OPS_PLACE_OOB &&
2484 ops->mode != MTD_OPS_AUTO_OOB &&
2485 ops->mode != MTD_OPS_RAW)
2486 return -ENOTSUPP;
2487
Huang Shijie6a8214a2012-11-19 14:43:30 +08002488 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002490 if (!ops->datbuf)
2491 ret = nand_do_read_oob(mtd, from, ops);
2492 else
2493 ret = nand_do_read_ops(mtd, from, ops);
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002496 return ret;
2497}
2498
2499
2500/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002501 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002502 * @mtd: mtd info structure
2503 * @chip: nand chip info structure
2504 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002505 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002506 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002507 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002508 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002509 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002510int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2511 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002512{
2513 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002514 if (oob_required)
2515 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002516
2517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002519EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002521/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002522 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002523 * @mtd: mtd info structure
2524 * @chip: nand chip info structure
2525 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002526 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002527 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002528 *
2529 * We need a special oob layout and handling even when ECC isn't checked.
2530 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002531static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002532 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002533 const uint8_t *buf, int oob_required,
2534 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002535{
2536 int eccsize = chip->ecc.size;
2537 int eccbytes = chip->ecc.bytes;
2538 uint8_t *oob = chip->oob_poi;
2539 int steps, size;
2540
2541 for (steps = chip->ecc.steps; steps > 0; steps--) {
2542 chip->write_buf(mtd, buf, eccsize);
2543 buf += eccsize;
2544
2545 if (chip->ecc.prepad) {
2546 chip->write_buf(mtd, oob, chip->ecc.prepad);
2547 oob += chip->ecc.prepad;
2548 }
2549
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002550 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002551 oob += eccbytes;
2552
2553 if (chip->ecc.postpad) {
2554 chip->write_buf(mtd, oob, chip->ecc.postpad);
2555 oob += chip->ecc.postpad;
2556 }
2557 }
2558
2559 size = mtd->oobsize - (oob - chip->oob_poi);
2560 if (size)
2561 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002562
2563 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002564}
2565/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002566 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002567 * @mtd: mtd info structure
2568 * @chip: nand chip info structure
2569 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002570 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002571 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002572 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002573static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002574 const uint8_t *buf, int oob_required,
2575 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002576{
Boris Brezillon846031d2016-02-03 20:11:00 +01002577 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002578 int eccbytes = chip->ecc.bytes;
2579 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002580 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002581 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002582
Brian Norris7854d3f2011-06-23 14:12:08 -07002583 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002584 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2585 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002586
Boris Brezillon846031d2016-02-03 20:11:00 +01002587 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2588 chip->ecc.total);
2589 if (ret)
2590 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002591
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002592 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002593}
2594
2595/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002596 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002597 * @mtd: mtd info structure
2598 * @chip: nand chip info structure
2599 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002600 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002601 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002602 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002603static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002604 const uint8_t *buf, int oob_required,
2605 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002606{
Boris Brezillon846031d2016-02-03 20:11:00 +01002607 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002608 int eccbytes = chip->ecc.bytes;
2609 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002610 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002611 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002612
2613 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2614 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002615 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002616 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2617 }
2618
Boris Brezillon846031d2016-02-03 20:11:00 +01002619 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2620 chip->ecc.total);
2621 if (ret)
2622 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002623
2624 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002625
2626 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002627}
2628
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302629
2630/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002631 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302632 * @mtd: mtd info structure
2633 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002634 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302635 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002636 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302637 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002638 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302639 */
2640static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2641 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002642 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002643 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302644{
2645 uint8_t *oob_buf = chip->oob_poi;
2646 uint8_t *ecc_calc = chip->buffers->ecccalc;
2647 int ecc_size = chip->ecc.size;
2648 int ecc_bytes = chip->ecc.bytes;
2649 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302650 uint32_t start_step = offset / ecc_size;
2651 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2652 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002653 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302654
2655 for (step = 0; step < ecc_steps; step++) {
2656 /* configure controller for WRITE access */
2657 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2658
2659 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002660 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302661
2662 /* mask ECC of un-touched subpages by padding 0xFF */
2663 if ((step < start_step) || (step > end_step))
2664 memset(ecc_calc, 0xff, ecc_bytes);
2665 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002666 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302667
2668 /* mask OOB of un-touched subpages by padding 0xFF */
2669 /* if oob_required, preserve OOB metadata of written subpage */
2670 if (!oob_required || (step < start_step) || (step > end_step))
2671 memset(oob_buf, 0xff, oob_bytes);
2672
Brian Norrisd6a950802013-08-08 17:16:36 -07002673 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302674 ecc_calc += ecc_bytes;
2675 oob_buf += oob_bytes;
2676 }
2677
2678 /* copy calculated ECC for whole page to chip->buffer->oob */
2679 /* this include masked-value(0xFF) for unwritten subpages */
2680 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002681 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2682 chip->ecc.total);
2683 if (ret)
2684 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302685
2686 /* write OOB buffer to NAND device */
2687 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2688
2689 return 0;
2690}
2691
2692
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002693/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002694 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002695 * @mtd: mtd info structure
2696 * @chip: nand chip info structure
2697 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002698 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002699 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002700 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002701 * The hw generator calculates the error syndrome automatically. Therefore we
2702 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002703 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002704static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002705 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002706 const uint8_t *buf, int oob_required,
2707 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002708{
2709 int i, eccsize = chip->ecc.size;
2710 int eccbytes = chip->ecc.bytes;
2711 int eccsteps = chip->ecc.steps;
2712 const uint8_t *p = buf;
2713 uint8_t *oob = chip->oob_poi;
2714
2715 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2716
2717 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2718 chip->write_buf(mtd, p, eccsize);
2719
2720 if (chip->ecc.prepad) {
2721 chip->write_buf(mtd, oob, chip->ecc.prepad);
2722 oob += chip->ecc.prepad;
2723 }
2724
2725 chip->ecc.calculate(mtd, p, oob);
2726 chip->write_buf(mtd, oob, eccbytes);
2727 oob += eccbytes;
2728
2729 if (chip->ecc.postpad) {
2730 chip->write_buf(mtd, oob, chip->ecc.postpad);
2731 oob += chip->ecc.postpad;
2732 }
2733 }
2734
2735 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002736 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002737 if (i)
2738 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002739
2740 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002741}
2742
2743/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002744 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002745 * @mtd: MTD device structure
2746 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302747 * @offset: address offset within the page
2748 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002749 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002750 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002751 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002752 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002753 */
2754static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302755 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002756 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002757{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302758 int status, subpage;
2759
2760 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2761 chip->ecc.write_subpage)
2762 subpage = offset || (data_len < mtd->writesize);
2763 else
2764 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002765
Marc Gonzalez3371d662016-11-15 10:56:20 +01002766 if (nand_standard_page_accessors(&chip->ecc))
2767 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002768
David Woodhouse956e9442006-09-25 17:12:39 +01002769 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302770 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002771 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302772 else if (subpage)
2773 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002774 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002775 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002776 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2777 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002778
2779 if (status < 0)
2780 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002781
Boris Brezillon41145642017-05-16 18:27:49 +02002782 if (nand_standard_page_accessors(&chip->ecc)) {
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002783 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002784
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002785 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002786 if (status & NAND_STATUS_FAIL)
2787 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002788 }
2789
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002790 return 0;
2791}
2792
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002793/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002794 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002795 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002796 * @oob: oob data buffer
2797 * @len: oob data write length
2798 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002799 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002800static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2801 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002802{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002803 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002804 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002805
2806 /*
2807 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2808 * data from a previous OOB read.
2809 */
2810 memset(chip->oob_poi, 0xff, mtd->oobsize);
2811
Florian Fainellif8ac0412010-09-07 13:23:43 +02002812 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002813
Brian Norris0612b9d2011-08-30 18:45:40 -07002814 case MTD_OPS_PLACE_OOB:
2815 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002816 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2817 return oob + len;
2818
Boris Brezillon846031d2016-02-03 20:11:00 +01002819 case MTD_OPS_AUTO_OOB:
2820 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2821 ops->ooboffs, len);
2822 BUG_ON(ret);
2823 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002824
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002825 default:
2826 BUG();
2827 }
2828 return NULL;
2829}
2830
Florian Fainellif8ac0412010-09-07 13:23:43 +02002831#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002832
2833/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002834 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002835 * @mtd: MTD device structure
2836 * @to: offset to write to
2837 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002838 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002839 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002840 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002841static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2842 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002843{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002844 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002845 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002846 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002847
2848 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002849 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002850
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002851 uint8_t *oob = ops->oobbuf;
2852 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302853 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002854 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002855
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002856 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002857 if (!writelen)
2858 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002859
Brian Norris8b6e50c2011-05-25 14:59:01 -07002860 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002861 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002862 pr_notice("%s: attempt to write non page aligned data\n",
2863 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002864 return -EINVAL;
2865 }
2866
Thomas Gleixner29072b92006-09-28 15:38:36 +02002867 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002868
Thomas Gleixner6a930962006-06-28 00:11:45 +02002869 chipnr = (int)(to >> chip->chip_shift);
2870 chip->select_chip(mtd, chipnr);
2871
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002872 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002873 if (nand_check_wp(mtd)) {
2874 ret = -EIO;
2875 goto err_out;
2876 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002877
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002878 realpage = (int)(to >> chip->page_shift);
2879 page = realpage & chip->pagemask;
2880 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2881
2882 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002883 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2884 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002885 chip->pagebuf = -1;
2886
Maxim Levitsky782ce792010-02-22 20:39:36 +02002887 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002888 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2889 ret = -EINVAL;
2890 goto err_out;
2891 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002892
Florian Fainellif8ac0412010-09-07 13:23:43 +02002893 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002894 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002895 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002896 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002897 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002898
Kamal Dasu66507c72014-05-01 20:51:19 -04002899 if (part_pagewr)
2900 use_bufpoi = 1;
2901 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002902 use_bufpoi = !virt_addr_valid(buf) ||
2903 !IS_ALIGNED((unsigned long)buf,
2904 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002905 else
2906 use_bufpoi = 0;
2907
2908 /* Partial page write?, or need to use bounce buffer */
2909 if (use_bufpoi) {
2910 pr_debug("%s: using write bounce buffer for buf@%p\n",
2911 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04002912 if (part_pagewr)
2913 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002914 chip->pagebuf = -1;
2915 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2916 memcpy(&chip->buffers->databuf[column], buf, bytes);
2917 wbuf = chip->buffers->databuf;
2918 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002919
Maxim Levitsky782ce792010-02-22 20:39:36 +02002920 if (unlikely(oob)) {
2921 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002922 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002923 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002924 } else {
2925 /* We still need to erase leftover OOB data */
2926 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002927 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002928
2929 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002930 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002931 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002932 if (ret)
2933 break;
2934
2935 writelen -= bytes;
2936 if (!writelen)
2937 break;
2938
Thomas Gleixner29072b92006-09-28 15:38:36 +02002939 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002940 buf += bytes;
2941 realpage++;
2942
2943 page = realpage & chip->pagemask;
2944 /* Check, if we cross a chip boundary */
2945 if (!page) {
2946 chipnr++;
2947 chip->select_chip(mtd, -1);
2948 chip->select_chip(mtd, chipnr);
2949 }
2950 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002951
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002952 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002953 if (unlikely(oob))
2954 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002955
2956err_out:
2957 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002958 return ret;
2959}
2960
2961/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002962 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002963 * @mtd: MTD device structure
2964 * @to: offset to write to
2965 * @len: number of bytes to write
2966 * @retlen: pointer to variable to store the number of written bytes
2967 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002968 *
2969 * NAND write with ECC. Used when performing writes in interrupt context, this
2970 * may for example be called by mtdoops when writing an oops while in panic.
2971 */
2972static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2973 size_t *retlen, const uint8_t *buf)
2974{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002975 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002976 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002977 int ret;
2978
Brian Norris8b6e50c2011-05-25 14:59:01 -07002979 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002980 panic_nand_wait(mtd, chip, 400);
2981
Brian Norris8b6e50c2011-05-25 14:59:01 -07002982 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002983 panic_nand_get_device(chip, mtd, FL_WRITING);
2984
Brian Norris0ec56dc2015-02-28 02:02:30 -08002985 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002986 ops.len = len;
2987 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002988 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002989
Brian Norris4a89ff82011-08-30 18:45:45 -07002990 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002991
Brian Norris4a89ff82011-08-30 18:45:45 -07002992 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002993 return ret;
2994}
2995
2996/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002997 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002998 * @mtd: MTD device structure
2999 * @to: offset to write to
3000 * @len: number of bytes to write
3001 * @retlen: pointer to variable to store the number of written bytes
3002 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003004 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003006static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003007 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Brian Norris4a89ff82011-08-30 18:45:45 -07003009 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003010 int ret;
3011
Huang Shijie6a8214a2012-11-19 14:43:30 +08003012 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08003013 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07003014 ops.len = len;
3015 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08003016 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07003017 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07003018 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003019 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003020 return ret;
3021}
3022
3023/**
3024 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003025 * @mtd: MTD device structure
3026 * @to: offset to write to
3027 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003028 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003029 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003030 */
3031static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3032 struct mtd_oob_ops *ops)
3033{
Adrian Hunter03736152007-01-31 17:58:29 +02003034 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003035 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Brian Norris289c0522011-07-19 10:06:09 -07003037 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303038 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Boris BREZILLON29f10582016-03-07 10:46:52 +01003040 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02003043 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07003044 pr_debug("%s: attempt to write past end of page\n",
3045 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return -EINVAL;
3047 }
3048
Adrian Hunter03736152007-01-31 17:58:29 +02003049 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003050 pr_debug("%s: attempt to start write outside oob\n",
3051 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003052 return -EINVAL;
3053 }
3054
Jason Liu775adc3d42011-02-25 13:06:18 +08003055 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02003056 if (unlikely(to >= mtd->size ||
3057 ops->ooboffs + ops->ooblen >
3058 ((mtd->size >> chip->page_shift) -
3059 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003060 pr_debug("%s: attempt to write beyond end of device\n",
3061 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003062 return -EINVAL;
3063 }
3064
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003065 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003066
3067 /*
3068 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3069 * of my DiskOnChip 2000 test units) will clear the whole data page too
3070 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3071 * it in the doc2000 driver in August 1999. dwmw2.
3072 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003073 nand_reset(chip, chipnr);
3074
3075 chip->select_chip(mtd, chipnr);
3076
3077 /* Shift to get page */
3078 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003081 if (nand_check_wp(mtd)) {
3082 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003083 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003084 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003087 if (page == chip->pagebuf)
3088 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003090 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07003091
Brian Norris0612b9d2011-08-30 18:45:40 -07003092 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07003093 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3094 else
3095 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003096
Huang Shijieb0bb6902012-11-19 14:43:29 +08003097 chip->select_chip(mtd, -1);
3098
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003099 if (status)
3100 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Vitaly Wool70145682006-11-03 18:20:38 +03003102 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003104 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003105}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003107/**
3108 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003109 * @mtd: MTD device structure
3110 * @to: offset to write to
3111 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003112 */
3113static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3114 struct mtd_oob_ops *ops)
3115{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003116 int ret = -ENOTSUPP;
3117
3118 ops->retlen = 0;
3119
3120 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003121 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003122 pr_debug("%s: attempt to write beyond end of device\n",
3123 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003124 return -EINVAL;
3125 }
3126
Huang Shijie6a8214a2012-11-19 14:43:30 +08003127 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003128
Florian Fainellif8ac0412010-09-07 13:23:43 +02003129 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003130 case MTD_OPS_PLACE_OOB:
3131 case MTD_OPS_AUTO_OOB:
3132 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003133 break;
3134
3135 default:
3136 goto out;
3137 }
3138
3139 if (!ops->datbuf)
3140 ret = nand_do_write_oob(mtd, to, ops);
3141 else
3142 ret = nand_do_write_ops(mtd, to, ops);
3143
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003144out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003145 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return ret;
3147}
3148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149/**
Brian Norris49c50b92014-05-06 16:02:19 -07003150 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003151 * @mtd: MTD device structure
3152 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 *
Brian Norris49c50b92014-05-06 16:02:19 -07003154 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 */
Brian Norris49c50b92014-05-06 16:02:19 -07003156static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003158 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003160 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3161 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003162
3163 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
3166/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003168 * @mtd: MTD device structure
3169 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003171 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003173static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
David Woodhousee0c7d762006-05-13 18:07:53 +01003175 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003179 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003180 * @mtd: MTD device structure
3181 * @instr: erase instruction
3182 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003184 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003186int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3187 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Adrian Hunter69423d92008-12-10 13:37:21 +00003189 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003190 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003191 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Brian Norris289c0522011-07-19 10:06:09 -07003193 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3194 __func__, (unsigned long long)instr->addr,
3195 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303197 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003201 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003204 page = (int)(instr->addr >> chip->page_shift);
3205 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003208 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003211 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 /* Check, if it is write protected */
3214 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003215 pr_debug("%s: device is write protected!\n",
3216 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 instr->state = MTD_ERASE_FAILED;
3218 goto erase_exit;
3219 }
3220
3221 /* Loop through the pages */
3222 len = instr->len;
3223
3224 instr->state = MTD_ERASING;
3225
3226 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003227 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003228 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303229 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003230 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3231 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 instr->state = MTD_ERASE_FAILED;
3233 goto erase_exit;
3234 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003235
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003236 /*
3237 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003238 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003239 */
3240 if (page <= chip->pagebuf && chip->pagebuf <
3241 (page + pages_per_block))
3242 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Brian Norris49c50b92014-05-06 16:02:19 -07003244 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003247 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003248 pr_debug("%s: failed erase, page 0x%08x\n",
3249 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003251 instr->fail_addr =
3252 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 goto erase_exit;
3254 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003257 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 page += pages_per_block;
3259
3260 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003261 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003263 chip->select_chip(mtd, -1);
3264 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
3266 }
3267 instr->state = MTD_ERASE_DONE;
3268
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003269erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003274 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 nand_release_device(mtd);
3276
David Woodhouse49defc02007-10-06 15:01:59 -04003277 /* Do call back function */
3278 if (!ret)
3279 mtd_erase_callback(instr);
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 /* Return more or less happy */
3282 return ret;
3283}
3284
3285/**
3286 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003287 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003289 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003291static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
Brian Norris289c0522011-07-19 10:06:09 -07003293 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003296 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003298 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299}
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003302 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003303 * @mtd: MTD device structure
3304 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003306static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303308 struct nand_chip *chip = mtd_to_nand(mtd);
3309 int chipnr = (int)(offs >> chip->chip_shift);
3310 int ret;
3311
3312 /* Select the NAND device */
3313 nand_get_device(mtd, FL_READING);
3314 chip->select_chip(mtd, chipnr);
3315
3316 ret = nand_block_checkbad(mtd, offs, 0);
3317
3318 chip->select_chip(mtd, -1);
3319 nand_release_device(mtd);
3320
3321 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
3323
3324/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003325 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003326 * @mtd: MTD device structure
3327 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003329static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 int ret;
3332
Florian Fainellif8ac0412010-09-07 13:23:43 +02003333 ret = nand_block_isbad(mtd, ofs);
3334 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003335 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (ret > 0)
3337 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003338 return ret;
3339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Brian Norris5a0edb22013-07-30 17:52:58 -07003341 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
3343
3344/**
Zach Brown56718422017-01-10 13:30:20 -06003345 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3346 * @mtd: MTD device structure
3347 * @ofs: offset relative to mtd start
3348 * @len: length of mtd
3349 */
3350static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3351{
3352 struct nand_chip *chip = mtd_to_nand(mtd);
3353 u32 part_start_block;
3354 u32 part_end_block;
3355 u32 part_start_die;
3356 u32 part_end_die;
3357
3358 /*
3359 * max_bb_per_die and blocks_per_die used to determine
3360 * the maximum bad block count.
3361 */
3362 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3363 return -ENOTSUPP;
3364
3365 /* Get the start and end of the partition in erase blocks. */
3366 part_start_block = mtd_div_by_eb(ofs, mtd);
3367 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3368
3369 /* Get the start and end LUNs of the partition. */
3370 part_start_die = part_start_block / chip->blocks_per_die;
3371 part_end_die = part_end_block / chip->blocks_per_die;
3372
3373 /*
3374 * Look up the bad blocks per unit and multiply by the number of units
3375 * that the partition spans.
3376 */
3377 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3378}
3379
3380/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003381 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3382 * @mtd: MTD device structure
3383 * @chip: nand chip info structure
3384 * @addr: feature address.
3385 * @subfeature_param: the subfeature parameters, a four bytes array.
3386 */
3387static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3388 int addr, uint8_t *subfeature_param)
3389{
3390 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003391 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003392
David Mosbergerd914c932013-05-29 15:30:13 +03003393 if (!chip->onfi_version ||
3394 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3395 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003396 return -EINVAL;
3397
3398 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003399 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3400 chip->write_byte(mtd, subfeature_param[i]);
3401
Huang Shijie7db03ec2012-09-13 14:57:52 +08003402 status = chip->waitfunc(mtd, chip);
3403 if (status & NAND_STATUS_FAIL)
3404 return -EIO;
3405 return 0;
3406}
3407
3408/**
3409 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3410 * @mtd: MTD device structure
3411 * @chip: nand chip info structure
3412 * @addr: feature address.
3413 * @subfeature_param: the subfeature parameters, a four bytes array.
3414 */
3415static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3416 int addr, uint8_t *subfeature_param)
3417{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003418 int i;
3419
David Mosbergerd914c932013-05-29 15:30:13 +03003420 if (!chip->onfi_version ||
3421 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3422 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003423 return -EINVAL;
3424
Huang Shijie7db03ec2012-09-13 14:57:52 +08003425 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003426 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3427 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003428 return 0;
3429}
3430
3431/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003432 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3433 * -ENOTSUPP
3434 * @mtd: MTD device structure
3435 * @chip: nand chip info structure
3436 * @addr: feature address.
3437 * @subfeature_param: the subfeature parameters, a four bytes array.
3438 *
3439 * Should be used by NAND controller drivers that do not support the SET/GET
3440 * FEATURES operations.
3441 */
3442int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3443 struct nand_chip *chip, int addr,
3444 u8 *subfeature_param)
3445{
3446 return -ENOTSUPP;
3447}
3448EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3449
3450/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003451 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003452 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003453 */
3454static int nand_suspend(struct mtd_info *mtd)
3455{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003456 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003457}
3458
3459/**
3460 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003461 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003462 */
3463static void nand_resume(struct mtd_info *mtd)
3464{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003465 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003466
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003467 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003468 nand_release_device(mtd);
3469 else
Brian Norrisd0370212011-07-19 10:06:08 -07003470 pr_err("%s called for a chip which is not in suspended state\n",
3471 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003472}
3473
Scott Branden72ea4032014-11-20 11:18:05 -08003474/**
3475 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3476 * prevent further operations
3477 * @mtd: MTD device structure
3478 */
3479static void nand_shutdown(struct mtd_info *mtd)
3480{
Brian Norris9ca641b2015-11-09 16:37:28 -08003481 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003482}
3483
Brian Norris8b6e50c2011-05-25 14:59:01 -07003484/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003485static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003486{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003487 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003490 if (!chip->chip_delay)
3491 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
3493 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003494 if (chip->cmdfunc == NULL)
3495 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
3497 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003498 if (chip->waitfunc == NULL)
3499 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003501 if (!chip->select_chip)
3502 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003503
Huang Shijie4204ccc2013-08-16 10:10:07 +08003504 /* set for ONFI nand */
3505 if (!chip->onfi_set_features)
3506 chip->onfi_set_features = nand_onfi_set_features;
3507 if (!chip->onfi_get_features)
3508 chip->onfi_get_features = nand_onfi_get_features;
3509
Brian Norris68e80782013-07-18 01:17:02 -07003510 /* If called twice, pointers that depend on busw may need to be reset */
3511 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003512 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3513 if (!chip->read_word)
3514 chip->read_word = nand_read_word;
3515 if (!chip->block_bad)
3516 chip->block_bad = nand_block_bad;
3517 if (!chip->block_markbad)
3518 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003519 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003520 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003521 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3522 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003523 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003524 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003525 if (!chip->scan_bbt)
3526 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003527
3528 if (!chip->controller) {
3529 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003530 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003531 }
3532
Masahiro Yamada477544c2017-03-30 17:15:05 +09003533 if (!chip->buf_align)
3534 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003535}
3536
Brian Norris8b6e50c2011-05-25 14:59:01 -07003537/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003538static void sanitize_string(uint8_t *s, size_t len)
3539{
3540 ssize_t i;
3541
Brian Norris8b6e50c2011-05-25 14:59:01 -07003542 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003543 s[len - 1] = 0;
3544
Brian Norris8b6e50c2011-05-25 14:59:01 -07003545 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003546 for (i = 0; i < len - 1; i++) {
3547 if (s[i] < ' ' || s[i] > 127)
3548 s[i] = '?';
3549 }
3550
Brian Norris8b6e50c2011-05-25 14:59:01 -07003551 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003552 strim(s);
3553}
3554
3555static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3556{
3557 int i;
3558 while (len--) {
3559 crc ^= *p++ << 8;
3560 for (i = 0; i < 8; i++)
3561 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3562 }
3563
3564 return crc;
3565}
3566
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003567/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003568static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3569 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003570{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003571 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003572 struct onfi_ext_param_page *ep;
3573 struct onfi_ext_section *s;
3574 struct onfi_ext_ecc_info *ecc;
3575 uint8_t *cursor;
3576 int ret = -EINVAL;
3577 int len;
3578 int i;
3579
3580 len = le16_to_cpu(p->ext_param_page_length) * 16;
3581 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003582 if (!ep)
3583 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003584
3585 /* Send our own NAND_CMD_PARAM. */
3586 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3587
3588 /* Use the Change Read Column command to skip the ONFI param pages. */
3589 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3590 sizeof(*p) * p->num_of_param_pages , -1);
3591
3592 /* Read out the Extended Parameter Page. */
3593 chip->read_buf(mtd, (uint8_t *)ep, len);
3594 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3595 != le16_to_cpu(ep->crc))) {
3596 pr_debug("fail in the CRC.\n");
3597 goto ext_out;
3598 }
3599
3600 /*
3601 * Check the signature.
3602 * Do not strictly follow the ONFI spec, maybe changed in future.
3603 */
3604 if (strncmp(ep->sig, "EPPS", 4)) {
3605 pr_debug("The signature is invalid.\n");
3606 goto ext_out;
3607 }
3608
3609 /* find the ECC section. */
3610 cursor = (uint8_t *)(ep + 1);
3611 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3612 s = ep->sections + i;
3613 if (s->type == ONFI_SECTION_TYPE_2)
3614 break;
3615 cursor += s->length * 16;
3616 }
3617 if (i == ONFI_EXT_SECTION_MAX) {
3618 pr_debug("We can not find the ECC section.\n");
3619 goto ext_out;
3620 }
3621
3622 /* get the info we want. */
3623 ecc = (struct onfi_ext_ecc_info *)cursor;
3624
Brian Norris4ae7d222013-09-16 18:20:21 -07003625 if (!ecc->codeword_size) {
3626 pr_debug("Invalid codeword size\n");
3627 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003628 }
3629
Brian Norris4ae7d222013-09-16 18:20:21 -07003630 chip->ecc_strength_ds = ecc->ecc_bits;
3631 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003632 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003633
3634ext_out:
3635 kfree(ep);
3636 return ret;
3637}
3638
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003639/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003640 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003641 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003642static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003643{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003644 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003645 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003646 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003647 int val;
3648
Brian Norris7854d3f2011-06-23 14:12:08 -07003649 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003650 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3651 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3652 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3653 return 0;
3654
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003655 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3656 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003657 for (j = 0; j < sizeof(*p); j++)
3658 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003659 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3660 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003661 break;
3662 }
3663 }
3664
Brian Norrisc7f23a72013-08-13 10:51:55 -07003665 if (i == 3) {
3666 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003667 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003668 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003669
Brian Norris8b6e50c2011-05-25 14:59:01 -07003670 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003671 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003672 if (val & (1 << 5))
3673 chip->onfi_version = 23;
3674 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003675 chip->onfi_version = 22;
3676 else if (val & (1 << 3))
3677 chip->onfi_version = 21;
3678 else if (val & (1 << 2))
3679 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003680 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003681 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003682
3683 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003684 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003685 return 0;
3686 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003687
3688 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3689 sanitize_string(p->model, sizeof(p->model));
3690 if (!mtd->name)
3691 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003692
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003693 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003694
3695 /*
3696 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3697 * (don't ask me who thought of this...). MTD assumes that these
3698 * dimensions will be power-of-2, so just truncate the remaining area.
3699 */
3700 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3701 mtd->erasesize *= mtd->writesize;
3702
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003703 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003704
3705 /* See erasesize comment */
3706 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003707 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003708 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003709
Zach Brown34da5f52017-01-10 13:30:21 -06003710 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3711 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3712
Huang Shijiee2985fc2013-05-17 11:17:30 +08003713 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003714 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003715
Huang Shijie10c86ba2013-05-17 11:17:26 +08003716 if (p->ecc_bits != 0xff) {
3717 chip->ecc_strength_ds = p->ecc_bits;
3718 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003719 } else if (chip->onfi_version >= 21 &&
3720 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3721
3722 /*
3723 * The nand_flash_detect_ext_param_page() uses the
3724 * Change Read Column command which maybe not supported
3725 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3726 * now. We do not replace user supplied command function.
3727 */
3728 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3729 chip->cmdfunc = nand_command_lp;
3730
3731 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003732 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003733 pr_warn("Failed to detect ONFI extended param page\n");
3734 } else {
3735 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003736 }
3737
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003738 return 1;
3739}
3740
3741/*
Huang Shijie91361812014-02-21 13:39:40 +08003742 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3743 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003744static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003745{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003746 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003747 struct nand_jedec_params *p = &chip->jedec_params;
3748 struct jedec_ecc_info *ecc;
3749 int val;
3750 int i, j;
3751
3752 /* Try JEDEC for unknown chip or LP */
3753 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3754 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3755 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3756 chip->read_byte(mtd) != 'C')
3757 return 0;
3758
3759 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3760 for (i = 0; i < 3; i++) {
3761 for (j = 0; j < sizeof(*p); j++)
3762 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3763
3764 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3765 le16_to_cpu(p->crc))
3766 break;
3767 }
3768
3769 if (i == 3) {
3770 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3771 return 0;
3772 }
3773
3774 /* Check version */
3775 val = le16_to_cpu(p->revision);
3776 if (val & (1 << 2))
3777 chip->jedec_version = 10;
3778 else if (val & (1 << 1))
3779 chip->jedec_version = 1; /* vendor specific version */
3780
3781 if (!chip->jedec_version) {
3782 pr_info("unsupported JEDEC version: %d\n", val);
3783 return 0;
3784 }
3785
3786 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3787 sanitize_string(p->model, sizeof(p->model));
3788 if (!mtd->name)
3789 mtd->name = p->model;
3790
3791 mtd->writesize = le32_to_cpu(p->byte_per_page);
3792
3793 /* Please reference to the comment for nand_flash_detect_onfi. */
3794 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3795 mtd->erasesize *= mtd->writesize;
3796
3797 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3798
3799 /* Please reference to the comment for nand_flash_detect_onfi. */
3800 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3801 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3802 chip->bits_per_cell = p->bits_per_cell;
3803
3804 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003805 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003806
3807 /* ECC info */
3808 ecc = &p->ecc_info[0];
3809
3810 if (ecc->codeword_size >= 9) {
3811 chip->ecc_strength_ds = ecc->ecc_bits;
3812 chip->ecc_step_ds = 1 << ecc->codeword_size;
3813 } else {
3814 pr_warn("Invalid codeword size\n");
3815 }
3816
3817 return 1;
3818}
3819
3820/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003821 * nand_id_has_period - Check if an ID string has a given wraparound period
3822 * @id_data: the ID string
3823 * @arrlen: the length of the @id_data array
3824 * @period: the period of repitition
3825 *
3826 * Check if an ID string is repeated within a given sequence of bytes at
3827 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003828 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003829 * if the repetition has a period of @period; otherwise, returns zero.
3830 */
3831static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3832{
3833 int i, j;
3834 for (i = 0; i < period; i++)
3835 for (j = i + period; j < arrlen; j += period)
3836 if (id_data[i] != id_data[j])
3837 return 0;
3838 return 1;
3839}
3840
3841/*
3842 * nand_id_len - Get the length of an ID string returned by CMD_READID
3843 * @id_data: the ID string
3844 * @arrlen: the length of the @id_data array
3845
3846 * Returns the length of the ID string, according to known wraparound/trailing
3847 * zero patterns. If no pattern exists, returns the length of the array.
3848 */
3849static int nand_id_len(u8 *id_data, int arrlen)
3850{
3851 int last_nonzero, period;
3852
3853 /* Find last non-zero byte */
3854 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3855 if (id_data[last_nonzero])
3856 break;
3857
3858 /* All zeros */
3859 if (last_nonzero < 0)
3860 return 0;
3861
3862 /* Calculate wraparound period */
3863 for (period = 1; period < arrlen; period++)
3864 if (nand_id_has_period(id_data, arrlen, period))
3865 break;
3866
3867 /* There's a repeated pattern */
3868 if (period < arrlen)
3869 return period;
3870
3871 /* There are trailing zeros */
3872 if (last_nonzero < arrlen - 1)
3873 return last_nonzero + 1;
3874
3875 /* No pattern detected */
3876 return arrlen;
3877}
3878
Huang Shijie7db906b2013-09-25 14:58:11 +08003879/* Extract the bits of per cell from the 3rd byte of the extended ID */
3880static int nand_get_bits_per_cell(u8 cellinfo)
3881{
3882 int bits;
3883
3884 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3885 bits >>= NAND_CI_CELLTYPE_SHIFT;
3886 return bits + 1;
3887}
3888
Brian Norrise3b88bd2012-09-24 20:40:52 -07003889/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003890 * Many new NAND share similar device ID codes, which represent the size of the
3891 * chip. The rest of the parameters must be decoded according to generic or
3892 * manufacturer-specific "extended ID" decoding patterns.
3893 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003894void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003895{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003896 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003897 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003898 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003899 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003900 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003901 /* The 4th id byte is the important one */
3902 extid = id_data[3];
3903
Boris Brezillon01389b62016-06-08 10:30:18 +02003904 /* Calc pagesize */
3905 mtd->writesize = 1024 << (extid & 0x03);
3906 extid >>= 2;
3907 /* Calc oobsize */
3908 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3909 extid >>= 2;
3910 /* Calc blocksize. Blocksize is multiples of 64KiB */
3911 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3912 extid >>= 2;
3913 /* Get buswidth information */
3914 if (extid & 0x1)
3915 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003916}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003917EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003918
3919/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003920 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3921 * decodes a matching ID table entry and assigns the MTD size parameters for
3922 * the chip.
3923 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003924static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003925{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003926 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003927
3928 mtd->erasesize = type->erasesize;
3929 mtd->writesize = type->pagesize;
3930 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003931
Huang Shijie1c195e92013-09-25 14:58:12 +08003932 /* All legacy ID NAND are small-page, SLC */
3933 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003934}
3935
3936/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003937 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3938 * heuristic patterns using various detected parameters (e.g., manufacturer,
3939 * page size, cell-type information).
3940 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003941static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003942{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003943 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003944
3945 /* Set the bad block position */
3946 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3947 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3948 else
3949 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003950}
3951
Huang Shijieec6e87e2013-03-15 11:01:00 +08003952static inline bool is_full_id_nand(struct nand_flash_dev *type)
3953{
3954 return type->id_len;
3955}
3956
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003957static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003958 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003959{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003960 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003961 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003962
Huang Shijieec6e87e2013-03-15 11:01:00 +08003963 if (!strncmp(type->id, id_data, type->id_len)) {
3964 mtd->writesize = type->pagesize;
3965 mtd->erasesize = type->erasesize;
3966 mtd->oobsize = type->oobsize;
3967
Huang Shijie7db906b2013-09-25 14:58:11 +08003968 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003969 chip->chipsize = (uint64_t)type->chipsize << 20;
3970 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003971 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3972 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003973 chip->onfi_timing_mode_default =
3974 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003975
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003976 if (!mtd->name)
3977 mtd->name = type->name;
3978
Huang Shijieec6e87e2013-03-15 11:01:00 +08003979 return true;
3980 }
3981 return false;
3982}
3983
Brian Norris7e74c2d2012-09-24 20:40:49 -07003984/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003985 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3986 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3987 * table.
3988 */
3989static void nand_manufacturer_detect(struct nand_chip *chip)
3990{
3991 /*
3992 * Try manufacturer detection if available and use
3993 * nand_decode_ext_id() otherwise.
3994 */
3995 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3996 chip->manufacturer.desc->ops->detect)
3997 chip->manufacturer.desc->ops->detect(chip);
3998 else
3999 nand_decode_ext_id(chip);
4000}
4001
4002/*
4003 * Manufacturer initialization. This function is called for all NANDs including
4004 * ONFI and JEDEC compliant ones.
4005 * Manufacturer drivers should put all their specific initialization code in
4006 * their ->init() hook.
4007 */
4008static int nand_manufacturer_init(struct nand_chip *chip)
4009{
4010 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4011 !chip->manufacturer.desc->ops->init)
4012 return 0;
4013
4014 return chip->manufacturer.desc->ops->init(chip);
4015}
4016
4017/*
4018 * Manufacturer cleanup. This function is called for all NANDs including
4019 * ONFI and JEDEC compliant ones.
4020 * Manufacturer drivers should put all their specific cleanup code in their
4021 * ->cleanup() hook.
4022 */
4023static void nand_manufacturer_cleanup(struct nand_chip *chip)
4024{
4025 /* Release manufacturer private data */
4026 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4027 chip->manufacturer.desc->ops->cleanup)
4028 chip->manufacturer.desc->ops->cleanup(chip);
4029}
4030
4031/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004032 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004033 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004034static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004035{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004036 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004037 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08004038 int busw;
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004039 int i, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004040 u8 *id_data = chip->id.data;
4041 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Karl Beldanef89a882008-09-15 14:37:29 +02004043 /*
4044 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004045 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004046 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004047 nand_reset(chip, 0);
4048
4049 /* Select the device */
4050 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004053 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
4055 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004056 maf_id = chip->read_byte(mtd);
4057 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
Brian Norris8b6e50c2011-05-25 14:59:01 -07004059 /*
4060 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004061 * interface concerns can cause random data which looks like a
4062 * possibly credible NAND flash to appear. If the two results do
4063 * not match, ignore the device completely.
4064 */
4065
4066 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4067
Brian Norris4aef9b72012-09-24 20:40:48 -07004068 /* Read entire ID string */
4069 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07004070 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01004071
Boris Brezillon7f501f02016-05-24 19:20:05 +02004072 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004073 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004074 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004075 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004076 }
4077
Boris Brezillon7f501f02016-05-24 19:20:05 +02004078 chip->id.len = nand_id_len(id_data, 8);
4079
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004080 /* Try to identify manufacturer */
4081 manufacturer = nand_get_manufacturer(maf_id);
4082 chip->manufacturer.desc = manufacturer;
4083
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004084 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004085 type = nand_flash_ids;
4086
Boris Brezillon29a198a2016-05-24 20:17:48 +02004087 /*
4088 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4089 * override it.
4090 * This is required to make sure initial NAND bus width set by the
4091 * NAND controller driver is coherent with the real NAND bus width
4092 * (extracted by auto-detection code).
4093 */
4094 busw = chip->options & NAND_BUSWIDTH_16;
4095
4096 /*
4097 * The flag is only set (never cleared), reset it to its default value
4098 * before starting auto-detection.
4099 */
4100 chip->options &= ~NAND_BUSWIDTH_16;
4101
Huang Shijieec6e87e2013-03-15 11:01:00 +08004102 for (; type->name != NULL; type++) {
4103 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004104 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004105 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004106 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004107 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004108 }
4109 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004110
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004111 chip->onfi_version = 0;
4112 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004113 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004114 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004115 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004116
4117 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004118 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08004119 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004120 }
4121
David Woodhouse5e81e882010-02-26 18:32:56 +00004122 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004123 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004124
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004125 if (!mtd->name)
4126 mtd->name = type->name;
4127
Adrian Hunter69423d92008-12-10 13:37:21 +00004128 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004129
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004130 if (!type->pagesize)
4131 nand_manufacturer_detect(chip);
4132 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004133 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004134
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004135 /* Get chip options */
4136 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004137
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004138ident_done:
4139
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004140 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004141 WARN_ON(busw & NAND_BUSWIDTH_16);
4142 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004143 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4144 /*
4145 * Check, if buswidth is correct. Hardware drivers should set
4146 * chip correct!
4147 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004148 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004149 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004150 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4151 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004152 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4153 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004154 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004155 }
4156
Boris Brezillon7f501f02016-05-24 19:20:05 +02004157 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004158
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004159 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004160 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004161 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004162 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004163
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004164 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004165 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004166 if (chip->chipsize & 0xffffffff)
4167 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004168 else {
4169 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4170 chip->chip_shift += 32 - 1;
4171 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004172
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004173 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004174 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004175
Brian Norris8b6e50c2011-05-25 14:59:01 -07004176 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004177 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4178 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004179
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004180 ret = nand_manufacturer_init(chip);
4181 if (ret)
4182 return ret;
4183
Ezequiel Garcia20171642013-11-25 08:30:31 -03004184 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004185 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004186
4187 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004188 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4189 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004190 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004191 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4192 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004193 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004194 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4195 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004196
Rafał Miłecki3755a992014-10-21 00:01:04 +02004197 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004198 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004199 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004200 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004201}
4202
Boris Brezillond48f62b2016-04-01 14:54:32 +02004203static const char * const nand_ecc_modes[] = {
4204 [NAND_ECC_NONE] = "none",
4205 [NAND_ECC_SOFT] = "soft",
4206 [NAND_ECC_HW] = "hw",
4207 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4208 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004209 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004210};
4211
4212static int of_get_nand_ecc_mode(struct device_node *np)
4213{
4214 const char *pm;
4215 int err, i;
4216
4217 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4218 if (err < 0)
4219 return err;
4220
4221 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4222 if (!strcasecmp(pm, nand_ecc_modes[i]))
4223 return i;
4224
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004225 /*
4226 * For backward compatibility we support few obsoleted values that don't
4227 * have their mappings into nand_ecc_modes_t anymore (they were merged
4228 * with other enums).
4229 */
4230 if (!strcasecmp(pm, "soft_bch"))
4231 return NAND_ECC_SOFT;
4232
Boris Brezillond48f62b2016-04-01 14:54:32 +02004233 return -ENODEV;
4234}
4235
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004236static const char * const nand_ecc_algos[] = {
4237 [NAND_ECC_HAMMING] = "hamming",
4238 [NAND_ECC_BCH] = "bch",
4239};
4240
Boris Brezillond48f62b2016-04-01 14:54:32 +02004241static int of_get_nand_ecc_algo(struct device_node *np)
4242{
4243 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004244 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004245
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004246 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4247 if (!err) {
4248 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4249 if (!strcasecmp(pm, nand_ecc_algos[i]))
4250 return i;
4251 return -ENODEV;
4252 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004253
4254 /*
4255 * For backward compatibility we also read "nand-ecc-mode" checking
4256 * for some obsoleted values that were specifying ECC algorithm.
4257 */
4258 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4259 if (err < 0)
4260 return err;
4261
4262 if (!strcasecmp(pm, "soft"))
4263 return NAND_ECC_HAMMING;
4264 else if (!strcasecmp(pm, "soft_bch"))
4265 return NAND_ECC_BCH;
4266
4267 return -ENODEV;
4268}
4269
4270static int of_get_nand_ecc_step_size(struct device_node *np)
4271{
4272 int ret;
4273 u32 val;
4274
4275 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4276 return ret ? ret : val;
4277}
4278
4279static int of_get_nand_ecc_strength(struct device_node *np)
4280{
4281 int ret;
4282 u32 val;
4283
4284 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4285 return ret ? ret : val;
4286}
4287
4288static int of_get_nand_bus_width(struct device_node *np)
4289{
4290 u32 val;
4291
4292 if (of_property_read_u32(np, "nand-bus-width", &val))
4293 return 8;
4294
4295 switch (val) {
4296 case 8:
4297 case 16:
4298 return val;
4299 default:
4300 return -EIO;
4301 }
4302}
4303
4304static bool of_get_nand_on_flash_bbt(struct device_node *np)
4305{
4306 return of_property_read_bool(np, "nand-on-flash-bbt");
4307}
4308
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004309static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004310{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004311 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004312 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004313
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004314 if (!dn)
4315 return 0;
4316
Brian Norris5844fee2015-01-23 00:22:27 -08004317 if (of_get_nand_bus_width(dn) == 16)
4318 chip->options |= NAND_BUSWIDTH_16;
4319
4320 if (of_get_nand_on_flash_bbt(dn))
4321 chip->bbt_options |= NAND_BBT_USE_FLASH;
4322
4323 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004324 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004325 ecc_strength = of_get_nand_ecc_strength(dn);
4326 ecc_step = of_get_nand_ecc_step_size(dn);
4327
Brian Norris5844fee2015-01-23 00:22:27 -08004328 if (ecc_mode >= 0)
4329 chip->ecc.mode = ecc_mode;
4330
Rafał Miłecki79082452016-03-23 11:19:02 +01004331 if (ecc_algo >= 0)
4332 chip->ecc.algo = ecc_algo;
4333
Brian Norris5844fee2015-01-23 00:22:27 -08004334 if (ecc_strength >= 0)
4335 chip->ecc.strength = ecc_strength;
4336
4337 if (ecc_step > 0)
4338 chip->ecc.size = ecc_step;
4339
Boris Brezillonba78ee02016-06-08 17:04:22 +02004340 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4341 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4342
Brian Norris5844fee2015-01-23 00:22:27 -08004343 return 0;
4344}
4345
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004346/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004347 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004348 * @mtd: MTD device structure
4349 * @maxchips: number of chips to scan for
4350 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004351 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004352 * This is the first phase of the normal nand_scan() function. It reads the
4353 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004354 *
4355 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004356int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4357 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004358{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004359 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004360 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004361 int ret;
4362
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004363 ret = nand_dt_init(chip);
4364 if (ret)
4365 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004366
Brian Norrisf7a8e382016-01-05 10:39:45 -08004367 if (!mtd->name && mtd->dev.parent)
4368 mtd->name = dev_name(mtd->dev.parent);
4369
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004370 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4371 /*
4372 * Default functions assigned for chip_select() and
4373 * cmdfunc() both expect cmd_ctrl() to be populated,
4374 * so we need to check that that's the case
4375 */
4376 pr_err("chip.cmd_ctrl() callback is not provided");
4377 return -EINVAL;
4378 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004379 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004380 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004381
4382 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004383 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004384 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004385 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004386 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004387 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004388 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 }
4390
Boris Brezillon73f907f2016-10-24 16:46:20 +02004391 /* Initialize the ->data_interface field. */
Boris Brezillond8e725d2016-09-15 10:32:50 +02004392 ret = nand_init_data_interface(chip);
4393 if (ret)
Brian Norris78771042017-05-01 17:04:53 -07004394 goto err_nand_init;
Boris Brezillond8e725d2016-09-15 10:32:50 +02004395
Boris Brezillon73f907f2016-10-24 16:46:20 +02004396 /*
4397 * Setup the data interface correctly on the chip and controller side.
4398 * This explicit call to nand_setup_data_interface() is only required
4399 * for the first die, because nand_reset() has been called before
4400 * ->data_interface and ->default_onfi_timing_mode were set.
4401 * For the other dies, nand_reset() will automatically switch to the
4402 * best mode for us.
4403 */
Boris Brezillon104e4422017-03-16 09:35:58 +01004404 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004405 if (ret)
Brian Norris78771042017-05-01 17:04:53 -07004406 goto err_nand_init;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004407
Boris Brezillon7f501f02016-05-24 19:20:05 +02004408 nand_maf_id = chip->id.data[0];
4409 nand_dev_id = chip->id.data[1];
4410
Huang Shijie07300162012-11-09 16:23:45 +08004411 chip->select_chip(mtd, -1);
4412
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004413 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004414 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004415 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004416 nand_reset(chip, i);
4417
4418 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004420 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004422 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004423 nand_dev_id != chip->read_byte(mtd)) {
4424 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 break;
Huang Shijie07300162012-11-09 16:23:45 +08004426 }
4427 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 }
4429 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004430 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004431
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004433 chip->numchips = i;
4434 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
David Woodhouse3b85c322006-09-25 17:06:53 +01004436 return 0;
Brian Norris78771042017-05-01 17:04:53 -07004437
4438err_nand_init:
4439 /* Free manufacturer priv data. */
4440 nand_manufacturer_cleanup(chip);
4441
4442 return ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004443}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004444EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004445
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004446static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4447{
4448 struct nand_chip *chip = mtd_to_nand(mtd);
4449 struct nand_ecc_ctrl *ecc = &chip->ecc;
4450
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004451 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004452 return -EINVAL;
4453
4454 switch (ecc->algo) {
4455 case NAND_ECC_HAMMING:
4456 ecc->calculate = nand_calculate_ecc;
4457 ecc->correct = nand_correct_data;
4458 ecc->read_page = nand_read_page_swecc;
4459 ecc->read_subpage = nand_read_subpage;
4460 ecc->write_page = nand_write_page_swecc;
4461 ecc->read_page_raw = nand_read_page_raw;
4462 ecc->write_page_raw = nand_write_page_raw;
4463 ecc->read_oob = nand_read_oob_std;
4464 ecc->write_oob = nand_write_oob_std;
4465 if (!ecc->size)
4466 ecc->size = 256;
4467 ecc->bytes = 3;
4468 ecc->strength = 1;
4469 return 0;
4470 case NAND_ECC_BCH:
4471 if (!mtd_nand_has_bch()) {
4472 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4473 return -EINVAL;
4474 }
4475 ecc->calculate = nand_bch_calculate_ecc;
4476 ecc->correct = nand_bch_correct_data;
4477 ecc->read_page = nand_read_page_swecc;
4478 ecc->read_subpage = nand_read_subpage;
4479 ecc->write_page = nand_write_page_swecc;
4480 ecc->read_page_raw = nand_read_page_raw;
4481 ecc->write_page_raw = nand_write_page_raw;
4482 ecc->read_oob = nand_read_oob_std;
4483 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004484
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004485 /*
4486 * Board driver should supply ecc.size and ecc.strength
4487 * values to select how many bits are correctable.
4488 * Otherwise, default to 4 bits for large page devices.
4489 */
4490 if (!ecc->size && (mtd->oobsize >= 64)) {
4491 ecc->size = 512;
4492 ecc->strength = 4;
4493 }
4494
4495 /*
4496 * if no ecc placement scheme was provided pickup the default
4497 * large page one.
4498 */
4499 if (!mtd->ooblayout) {
4500 /* handle large page devices only */
4501 if (mtd->oobsize < 64) {
4502 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4503 return -EINVAL;
4504 }
4505
4506 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004507
4508 }
4509
4510 /*
4511 * We can only maximize ECC config when the default layout is
4512 * used, otherwise we don't know how many bytes can really be
4513 * used.
4514 */
4515 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4516 ecc->options & NAND_ECC_MAXIMIZE) {
4517 int steps, bytes;
4518
4519 /* Always prefer 1k blocks over 512bytes ones */
4520 ecc->size = 1024;
4521 steps = mtd->writesize / ecc->size;
4522
4523 /* Reserve 2 bytes for the BBM */
4524 bytes = (mtd->oobsize - 2) / steps;
4525 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004526 }
4527
4528 /* See nand_bch_init() for details. */
4529 ecc->bytes = 0;
4530 ecc->priv = nand_bch_init(mtd);
4531 if (!ecc->priv) {
4532 WARN(1, "BCH ECC initialization failed!\n");
4533 return -EINVAL;
4534 }
4535 return 0;
4536 default:
4537 WARN(1, "Unsupported ECC algorithm!\n");
4538 return -EINVAL;
4539 }
4540}
4541
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09004542/**
4543 * nand_check_ecc_caps - check the sanity of preset ECC settings
4544 * @chip: nand chip info structure
4545 * @caps: ECC caps info structure
4546 * @oobavail: OOB size that the ECC engine can use
4547 *
4548 * When ECC step size and strength are already set, check if they are supported
4549 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4550 * On success, the calculated ECC bytes is set.
4551 */
4552int nand_check_ecc_caps(struct nand_chip *chip,
4553 const struct nand_ecc_caps *caps, int oobavail)
4554{
4555 struct mtd_info *mtd = nand_to_mtd(chip);
4556 const struct nand_ecc_step_info *stepinfo;
4557 int preset_step = chip->ecc.size;
4558 int preset_strength = chip->ecc.strength;
4559 int nsteps, ecc_bytes;
4560 int i, j;
4561
4562 if (WARN_ON(oobavail < 0))
4563 return -EINVAL;
4564
4565 if (!preset_step || !preset_strength)
4566 return -ENODATA;
4567
4568 nsteps = mtd->writesize / preset_step;
4569
4570 for (i = 0; i < caps->nstepinfos; i++) {
4571 stepinfo = &caps->stepinfos[i];
4572
4573 if (stepinfo->stepsize != preset_step)
4574 continue;
4575
4576 for (j = 0; j < stepinfo->nstrengths; j++) {
4577 if (stepinfo->strengths[j] != preset_strength)
4578 continue;
4579
4580 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4581 preset_strength);
4582 if (WARN_ON_ONCE(ecc_bytes < 0))
4583 return ecc_bytes;
4584
4585 if (ecc_bytes * nsteps > oobavail) {
4586 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4587 preset_step, preset_strength);
4588 return -ENOSPC;
4589 }
4590
4591 chip->ecc.bytes = ecc_bytes;
4592
4593 return 0;
4594 }
4595 }
4596
4597 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4598 preset_step, preset_strength);
4599
4600 return -ENOTSUPP;
4601}
4602EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4603
4604/**
4605 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4606 * @chip: nand chip info structure
4607 * @caps: ECC engine caps info structure
4608 * @oobavail: OOB size that the ECC engine can use
4609 *
4610 * If a chip's ECC requirement is provided, try to meet it with the least
4611 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4612 * On success, the chosen ECC settings are set.
4613 */
4614int nand_match_ecc_req(struct nand_chip *chip,
4615 const struct nand_ecc_caps *caps, int oobavail)
4616{
4617 struct mtd_info *mtd = nand_to_mtd(chip);
4618 const struct nand_ecc_step_info *stepinfo;
4619 int req_step = chip->ecc_step_ds;
4620 int req_strength = chip->ecc_strength_ds;
4621 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4622 int best_step, best_strength, best_ecc_bytes;
4623 int best_ecc_bytes_total = INT_MAX;
4624 int i, j;
4625
4626 if (WARN_ON(oobavail < 0))
4627 return -EINVAL;
4628
4629 /* No information provided by the NAND chip */
4630 if (!req_step || !req_strength)
4631 return -ENOTSUPP;
4632
4633 /* number of correctable bits the chip requires in a page */
4634 req_corr = mtd->writesize / req_step * req_strength;
4635
4636 for (i = 0; i < caps->nstepinfos; i++) {
4637 stepinfo = &caps->stepinfos[i];
4638 step_size = stepinfo->stepsize;
4639
4640 for (j = 0; j < stepinfo->nstrengths; j++) {
4641 strength = stepinfo->strengths[j];
4642
4643 /*
4644 * If both step size and strength are smaller than the
4645 * chip's requirement, it is not easy to compare the
4646 * resulted reliability.
4647 */
4648 if (step_size < req_step && strength < req_strength)
4649 continue;
4650
4651 if (mtd->writesize % step_size)
4652 continue;
4653
4654 nsteps = mtd->writesize / step_size;
4655
4656 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4657 if (WARN_ON_ONCE(ecc_bytes < 0))
4658 continue;
4659 ecc_bytes_total = ecc_bytes * nsteps;
4660
4661 if (ecc_bytes_total > oobavail ||
4662 strength * nsteps < req_corr)
4663 continue;
4664
4665 /*
4666 * We assume the best is to meet the chip's requrement
4667 * with the least number of ECC bytes.
4668 */
4669 if (ecc_bytes_total < best_ecc_bytes_total) {
4670 best_ecc_bytes_total = ecc_bytes_total;
4671 best_step = step_size;
4672 best_strength = strength;
4673 best_ecc_bytes = ecc_bytes;
4674 }
4675 }
4676 }
4677
4678 if (best_ecc_bytes_total == INT_MAX)
4679 return -ENOTSUPP;
4680
4681 chip->ecc.size = best_step;
4682 chip->ecc.strength = best_strength;
4683 chip->ecc.bytes = best_ecc_bytes;
4684
4685 return 0;
4686}
4687EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4688
4689/**
4690 * nand_maximize_ecc - choose the max ECC strength available
4691 * @chip: nand chip info structure
4692 * @caps: ECC engine caps info structure
4693 * @oobavail: OOB size that the ECC engine can use
4694 *
4695 * Choose the max ECC strength that is supported on the controller, and can fit
4696 * within the chip's OOB. On success, the chosen ECC settings are set.
4697 */
4698int nand_maximize_ecc(struct nand_chip *chip,
4699 const struct nand_ecc_caps *caps, int oobavail)
4700{
4701 struct mtd_info *mtd = nand_to_mtd(chip);
4702 const struct nand_ecc_step_info *stepinfo;
4703 int step_size, strength, nsteps, ecc_bytes, corr;
4704 int best_corr = 0;
4705 int best_step = 0;
4706 int best_strength, best_ecc_bytes;
4707 int i, j;
4708
4709 if (WARN_ON(oobavail < 0))
4710 return -EINVAL;
4711
4712 for (i = 0; i < caps->nstepinfos; i++) {
4713 stepinfo = &caps->stepinfos[i];
4714 step_size = stepinfo->stepsize;
4715
4716 /* If chip->ecc.size is already set, respect it */
4717 if (chip->ecc.size && step_size != chip->ecc.size)
4718 continue;
4719
4720 for (j = 0; j < stepinfo->nstrengths; j++) {
4721 strength = stepinfo->strengths[j];
4722
4723 if (mtd->writesize % step_size)
4724 continue;
4725
4726 nsteps = mtd->writesize / step_size;
4727
4728 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4729 if (WARN_ON_ONCE(ecc_bytes < 0))
4730 continue;
4731
4732 if (ecc_bytes * nsteps > oobavail)
4733 continue;
4734
4735 corr = strength * nsteps;
4736
4737 /*
4738 * If the number of correctable bits is the same,
4739 * bigger step_size has more reliability.
4740 */
4741 if (corr > best_corr ||
4742 (corr == best_corr && step_size > best_step)) {
4743 best_corr = corr;
4744 best_step = step_size;
4745 best_strength = strength;
4746 best_ecc_bytes = ecc_bytes;
4747 }
4748 }
4749 }
4750
4751 if (!best_corr)
4752 return -ENOTSUPP;
4753
4754 chip->ecc.size = best_step;
4755 chip->ecc.strength = best_strength;
4756 chip->ecc.bytes = best_ecc_bytes;
4757
4758 return 0;
4759}
4760EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4761
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004762/*
4763 * Check if the chip configuration meet the datasheet requirements.
4764
4765 * If our configuration corrects A bits per B bytes and the minimum
4766 * required correction level is X bits per Y bytes, then we must ensure
4767 * both of the following are true:
4768 *
4769 * (1) A / B >= X / Y
4770 * (2) A >= X
4771 *
4772 * Requirement (1) ensures we can correct for the required bitflip density.
4773 * Requirement (2) ensures we can correct even when all bitflips are clumped
4774 * in the same sector.
4775 */
4776static bool nand_ecc_strength_good(struct mtd_info *mtd)
4777{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004778 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004779 struct nand_ecc_ctrl *ecc = &chip->ecc;
4780 int corr, ds_corr;
4781
4782 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4783 /* Not enough information */
4784 return true;
4785
4786 /*
4787 * We get the number of corrected bits per page to compare
4788 * the correction density.
4789 */
4790 corr = (mtd->writesize * ecc->strength) / ecc->size;
4791 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4792
4793 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4794}
David Woodhouse3b85c322006-09-25 17:06:53 +01004795
Marc Gonzalez3371d662016-11-15 10:56:20 +01004796static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4797{
4798 struct nand_ecc_ctrl *ecc = &chip->ecc;
4799
4800 if (nand_standard_page_accessors(ecc))
4801 return false;
4802
4803 /*
4804 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4805 * controller driver implements all the page accessors because
4806 * default helpers are not suitable when the core does not
4807 * send the READ0/PAGEPROG commands.
4808 */
4809 return (!ecc->read_page || !ecc->write_page ||
4810 !ecc->read_page_raw || !ecc->write_page_raw ||
4811 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4812 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4813 ecc->hwctl && ecc->calculate));
4814}
4815
David Woodhouse3b85c322006-09-25 17:06:53 +01004816/**
4817 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004818 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004819 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004820 * This is the second phase of the normal nand_scan() function. It fills out
4821 * all the uninitialized function pointers with the defaults and scans for a
4822 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004823 */
4824int nand_scan_tail(struct mtd_info *mtd)
4825{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004826 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004827 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004828 struct nand_buffers *nbuf = NULL;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004829 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004830
Brian Norrise2414f42012-02-06 13:44:00 -08004831 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004832 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07004833 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
4834 ret = -EINVAL;
4835 goto err_ident;
4836 }
Brian Norrise2414f42012-02-06 13:44:00 -08004837
Marc Gonzalez3371d662016-11-15 10:56:20 +01004838 if (invalid_ecc_page_accessors(chip)) {
4839 pr_err("Invalid ECC page accessors setup\n");
Brian Norris78771042017-05-01 17:04:53 -07004840 ret = -EINVAL;
4841 goto err_ident;
Marc Gonzalez3371d662016-11-15 10:56:20 +01004842 }
4843
Huang Shijief02ea4e2014-01-13 14:27:12 +08004844 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004845 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Brian Norris78771042017-05-01 17:04:53 -07004846 if (!nbuf) {
4847 ret = -ENOMEM;
4848 goto err_ident;
4849 }
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004850
4851 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4852 if (!nbuf->ecccalc) {
4853 ret = -ENOMEM;
4854 goto err_free;
4855 }
4856
4857 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4858 if (!nbuf->ecccode) {
4859 ret = -ENOMEM;
4860 goto err_free;
4861 }
4862
4863 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4864 GFP_KERNEL);
4865 if (!nbuf->databuf) {
4866 ret = -ENOMEM;
4867 goto err_free;
4868 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004869
4870 chip->buffers = nbuf;
4871 } else {
Brian Norris78771042017-05-01 17:04:53 -07004872 if (!chip->buffers) {
4873 ret = -ENOMEM;
4874 goto err_ident;
4875 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004876 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004877
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004878 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004879 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004880
4881 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004882 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004883 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004884 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004885 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004886 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004889 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 break;
4891 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004892 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02004893 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004896 WARN(1, "No oob scheme defined for oobsize %d\n",
4897 mtd->oobsize);
4898 ret = -EINVAL;
4899 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 }
4901 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004902
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004903 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004904 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004905 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004906 */
David Woodhouse956e9442006-09-25 17:12:39 +01004907
Huang Shijie97de79e02013-10-18 14:20:53 +08004908 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004909 case NAND_ECC_HW_OOB_FIRST:
4910 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004911 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004912 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4913 ret = -EINVAL;
4914 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004915 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004916 if (!ecc->read_page)
4917 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004918
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004919 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004920 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004921 if (!ecc->read_page)
4922 ecc->read_page = nand_read_page_hwecc;
4923 if (!ecc->write_page)
4924 ecc->write_page = nand_write_page_hwecc;
4925 if (!ecc->read_page_raw)
4926 ecc->read_page_raw = nand_read_page_raw;
4927 if (!ecc->write_page_raw)
4928 ecc->write_page_raw = nand_write_page_raw;
4929 if (!ecc->read_oob)
4930 ecc->read_oob = nand_read_oob_std;
4931 if (!ecc->write_oob)
4932 ecc->write_oob = nand_write_oob_std;
4933 if (!ecc->read_subpage)
4934 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004935 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004936 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004937
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004938 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004939 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4940 (!ecc->read_page ||
4941 ecc->read_page == nand_read_page_hwecc ||
4942 !ecc->write_page ||
4943 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004944 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4945 ret = -EINVAL;
4946 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004947 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004948 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004949 if (!ecc->read_page)
4950 ecc->read_page = nand_read_page_syndrome;
4951 if (!ecc->write_page)
4952 ecc->write_page = nand_write_page_syndrome;
4953 if (!ecc->read_page_raw)
4954 ecc->read_page_raw = nand_read_page_raw_syndrome;
4955 if (!ecc->write_page_raw)
4956 ecc->write_page_raw = nand_write_page_raw_syndrome;
4957 if (!ecc->read_oob)
4958 ecc->read_oob = nand_read_oob_syndrome;
4959 if (!ecc->write_oob)
4960 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004961
Huang Shijie97de79e02013-10-18 14:20:53 +08004962 if (mtd->writesize >= ecc->size) {
4963 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004964 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4965 ret = -EINVAL;
4966 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004967 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004968 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004969 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004970 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4971 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004972 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004973 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004975 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004976 ret = nand_set_ecc_soft_ops(mtd);
4977 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004978 ret = -EINVAL;
4979 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004980 }
4981 break;
4982
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004983 case NAND_ECC_ON_DIE:
4984 if (!ecc->read_page || !ecc->write_page) {
4985 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4986 ret = -EINVAL;
4987 goto err_free;
4988 }
4989 if (!ecc->read_oob)
4990 ecc->read_oob = nand_read_oob_std;
4991 if (!ecc->write_oob)
4992 ecc->write_oob = nand_write_oob_std;
4993 break;
4994
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004995 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004996 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004997 ecc->read_page = nand_read_page_raw;
4998 ecc->write_page = nand_write_page_raw;
4999 ecc->read_oob = nand_read_oob_std;
5000 ecc->read_page_raw = nand_read_page_raw;
5001 ecc->write_page_raw = nand_write_page_raw;
5002 ecc->write_oob = nand_write_oob_std;
5003 ecc->size = mtd->writesize;
5004 ecc->bytes = 0;
5005 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005007
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005009 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5010 ret = -EINVAL;
5011 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013
Brian Norris9ce244b2011-08-30 18:45:37 -07005014 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005015 if (!ecc->read_oob_raw)
5016 ecc->read_oob_raw = ecc->read_oob;
5017 if (!ecc->write_oob_raw)
5018 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005019
Boris Brezillon846031d2016-02-03 20:11:00 +01005020 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005021 mtd->ecc_strength = ecc->strength;
5022 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005023
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005024 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005025 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005026 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005027 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005028 ecc->steps = mtd->writesize / ecc->size;
5029 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005030 WARN(1, "Invalid ECC parameters\n");
5031 ret = -EINVAL;
5032 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005034 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005035 if (ecc->total > mtd->oobsize) {
5036 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5037 ret = -EINVAL;
5038 goto err_free;
5039 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005040
Boris Brezillon846031d2016-02-03 20:11:00 +01005041 /*
5042 * The number of bytes available for a client to place data into
5043 * the out of band area.
5044 */
5045 ret = mtd_ooblayout_count_freebytes(mtd);
5046 if (ret < 0)
5047 ret = 0;
5048
5049 mtd->oobavail = ret;
5050
5051 /* ECC sanity check: warn if it's too weak */
5052 if (!nand_ecc_strength_good(mtd))
5053 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5054 mtd->name);
5055
Brian Norris8b6e50c2011-05-25 14:59:01 -07005056 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005057 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005058 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005059 case 2:
5060 mtd->subpage_sft = 1;
5061 break;
5062 case 4:
5063 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005064 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005065 mtd->subpage_sft = 2;
5066 break;
5067 }
5068 }
5069 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5070
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02005071 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005072 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005075 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005077 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305078 switch (ecc->mode) {
5079 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305080 if (chip->page_shift > 9)
5081 chip->options |= NAND_SUBPAGE_READ;
5082 break;
5083
5084 default:
5085 break;
5086 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005087
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08005089 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02005090 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5091 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005092 mtd->_erase = nand_erase;
5093 mtd->_point = NULL;
5094 mtd->_unpoint = NULL;
5095 mtd->_read = nand_read;
5096 mtd->_write = nand_write;
5097 mtd->_panic_write = panic_nand_write;
5098 mtd->_read_oob = nand_read_oob;
5099 mtd->_write_oob = nand_write_oob;
5100 mtd->_sync = nand_sync;
5101 mtd->_lock = NULL;
5102 mtd->_unlock = NULL;
5103 mtd->_suspend = nand_suspend;
5104 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005105 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005106 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005107 mtd->_block_isbad = nand_block_isbad;
5108 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06005109 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01005110 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005112 /*
5113 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5114 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5115 * properly set.
5116 */
5117 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005118 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005120 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005121 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
5124 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07005125 ret = chip->scan_bbt(mtd);
5126 if (ret)
5127 goto err_free;
5128 return 0;
5129
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005130err_free:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005131 if (nbuf) {
5132 kfree(nbuf->databuf);
5133 kfree(nbuf->ecccode);
5134 kfree(nbuf->ecccalc);
5135 kfree(nbuf);
5136 }
Brian Norris78771042017-05-01 17:04:53 -07005137
5138err_ident:
5139 /* Clean up nand_scan_ident(). */
5140
5141 /* Free manufacturer priv data. */
5142 nand_manufacturer_cleanup(chip);
5143
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005144 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005146EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Brian Norris8b6e50c2011-05-25 14:59:01 -07005148/*
5149 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005150 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07005151 * to call us from in-kernel code if the core NAND support is modular.
5152 */
David Woodhouse3b85c322006-09-25 17:06:53 +01005153#ifdef MODULE
5154#define caller_is_module() (1)
5155#else
5156#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06005157 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01005158#endif
5159
5160/**
5161 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005162 * @mtd: MTD device structure
5163 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01005164 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005165 * This fills out all the uninitialized function pointers with the defaults.
5166 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005167 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005168 */
5169int nand_scan(struct mtd_info *mtd, int maxchips)
5170{
5171 int ret;
5172
David Woodhouse5e81e882010-02-26 18:32:56 +00005173 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01005174 if (!ret)
5175 ret = nand_scan_tail(mtd);
5176 return ret;
5177}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005178EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01005179
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005181 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5182 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005183 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005184void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005186 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005187 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005188 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5189
Boris Brezillond8e725d2016-09-15 10:32:50 +02005190 nand_release_data_interface(chip);
5191
Jesper Juhlfa671642005-11-07 01:01:27 -08005192 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005193 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005194 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
5195 kfree(chip->buffers->databuf);
5196 kfree(chip->buffers->ecccode);
5197 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005198 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005199 }
Brian Norris58373ff2010-07-15 12:15:44 -07005200
5201 /* Free bad block descriptor memory */
5202 if (chip->badblock_pattern && chip->badblock_pattern->options
5203 & NAND_BBT_DYNAMICSTRUCT)
5204 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005205
5206 /* Free manufacturer priv data. */
5207 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005209EXPORT_SYMBOL_GPL(nand_cleanup);
5210
5211/**
5212 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5213 * held by the NAND device
5214 * @mtd: MTD device structure
5215 */
5216void nand_release(struct mtd_info *mtd)
5217{
5218 mtd_device_unregister(mtd);
5219 nand_cleanup(mtd_to_nand(mtd));
5220}
David Woodhousee0c7d762006-05-13 18:07:53 +01005221EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005222
David Woodhousee0c7d762006-05-13 18:07:53 +01005223MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005224MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5225MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005226MODULE_DESCRIPTION("Generic NAND flash driver code");