blob: 84d0a5d67e338e9b03b514e82248878502154508 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010039#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/types.h>
41#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020042#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010044#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020047#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020049#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010050
Huang Shijie6a8214a2012-11-19 14:43:30 +080051static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020053static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
Boris Brezillon41b207a2016-02-03 19:06:15 +010056/* Define default oob placement schemes for large and small page devices */
57static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59{
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020068 if (mtd->oobsize == 16)
69 oobregion->length = 4;
70 else
71 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010072 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020073 if (mtd->oobsize == 8)
74 return -ERANGE;
75
Boris Brezillon41b207a2016-02-03 19:06:15 +010076 oobregion->offset = 6;
77 oobregion->length = ecc->total - 4;
78 }
79
80 return 0;
81}
82
83static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85{
86 if (section > 1)
87 return -ERANGE;
88
89 if (mtd->oobsize == 16) {
90 if (section)
91 return -ERANGE;
92
93 oobregion->length = 8;
94 oobregion->offset = 8;
95 } else {
96 oobregion->length = 2;
97 if (!section)
98 oobregion->offset = 3;
99 else
100 oobregion->offset = 6;
101 }
102
103 return 0;
104}
105
106const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107 .ecc = nand_ooblayout_ecc_sp,
108 .free = nand_ooblayout_free_sp,
109};
110EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
111
112static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113 struct mtd_oob_region *oobregion)
114{
115 struct nand_chip *chip = mtd_to_nand(mtd);
116 struct nand_ecc_ctrl *ecc = &chip->ecc;
117
Miquel Raynal882fd152017-08-26 17:19:15 +0200118 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100119 return -ERANGE;
120
121 oobregion->length = ecc->total;
122 oobregion->offset = mtd->oobsize - oobregion->length;
123
124 return 0;
125}
126
127static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128 struct mtd_oob_region *oobregion)
129{
130 struct nand_chip *chip = mtd_to_nand(mtd);
131 struct nand_ecc_ctrl *ecc = &chip->ecc;
132
133 if (section)
134 return -ERANGE;
135
136 oobregion->length = mtd->oobsize - ecc->total - 2;
137 oobregion->offset = 2;
138
139 return 0;
140}
141
142const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143 .ecc = nand_ooblayout_ecc_lp,
144 .free = nand_ooblayout_free_lp,
145};
146EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200147
Alexander Couzens6a623e02017-05-02 12:19:00 +0200148/*
149 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
150 * are placed at a fixed offset.
151 */
152static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153 struct mtd_oob_region *oobregion)
154{
155 struct nand_chip *chip = mtd_to_nand(mtd);
156 struct nand_ecc_ctrl *ecc = &chip->ecc;
157
158 if (section)
159 return -ERANGE;
160
161 switch (mtd->oobsize) {
162 case 64:
163 oobregion->offset = 40;
164 break;
165 case 128:
166 oobregion->offset = 80;
167 break;
168 default:
169 return -EINVAL;
170 }
171
172 oobregion->length = ecc->total;
173 if (oobregion->offset + oobregion->length > mtd->oobsize)
174 return -ERANGE;
175
176 return 0;
177}
178
179static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180 struct mtd_oob_region *oobregion)
181{
182 struct nand_chip *chip = mtd_to_nand(mtd);
183 struct nand_ecc_ctrl *ecc = &chip->ecc;
184 int ecc_offset = 0;
185
186 if (section < 0 || section > 1)
187 return -ERANGE;
188
189 switch (mtd->oobsize) {
190 case 64:
191 ecc_offset = 40;
192 break;
193 case 128:
194 ecc_offset = 80;
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 if (section == 0) {
201 oobregion->offset = 2;
202 oobregion->length = ecc_offset - 2;
203 } else {
204 oobregion->offset = ecc_offset + ecc->total;
205 oobregion->length = mtd->oobsize - oobregion->offset;
206 }
207
208 return 0;
209}
210
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100211static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200212 .ecc = nand_ooblayout_ecc_lp_hamming,
213 .free = nand_ooblayout_free_lp_hamming,
214};
215
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530216static int check_offs_len(struct mtd_info *mtd,
217 loff_t ofs, uint64_t len)
218{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100219 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530220 int ret = 0;
221
222 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
228 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300229 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700230 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530231 ret = -EINVAL;
232 }
233
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530234 return ret;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700239 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800241 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100243static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100245 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200247 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 spin_lock(&chip->controller->lock);
249 chip->controller->active = NULL;
250 chip->state = FL_READY;
251 wake_up(&chip->controller->wq);
252 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700257 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700259 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200261static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100263 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200264 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/**
Masanari Iida064a7692012-11-09 23:20:58 +0900268 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700269 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700271 * Default read function for 16bit buswidth with endianness conversion.
272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200274static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100276 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700282 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700284 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286static u16 nand_read_word(struct mtd_info *mtd)
287{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100288 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200289 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700294 * @mtd: MTD device structure
295 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * Default select function for 1 chip devices.
298 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200299static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100301 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200302
303 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200305 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309
310 default:
311 BUG();
312 }
313}
314
315/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100316 * nand_write_byte - [DEFAULT] write single byte to chip
317 * @mtd: MTD device structure
318 * @byte: value to write
319 *
320 * Default function to write a byte to I/O[7:0]
321 */
322static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
323{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100324 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100325
326 chip->write_buf(mtd, &byte, 1);
327}
328
329/**
330 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
331 * @mtd: MTD device structure
332 * @byte: value to write
333 *
334 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
335 */
336static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
337{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100338 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100339 uint16_t word = byte;
340
341 /*
342 * It's not entirely clear what should happen to I/O[15:8] when writing
343 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
344 *
345 * When the host supports a 16-bit bus width, only data is
346 * transferred at the 16-bit width. All address and command line
347 * transfers shall use only the lower 8-bits of the data bus. During
348 * command transfers, the host may place any value on the upper
349 * 8-bits of the data bus. During address transfers, the host shall
350 * set the upper 8-bits of the data bus to 00h.
351 *
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);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100564 u8 status;
565 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200566
Brian Norris8b6e50c2011-05-25 14:59:01 -0700567 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200568 if (chip->options & NAND_BROKEN_XD)
569 return 0;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100572 ret = nand_status_op(chip, &status);
573 if (ret)
574 return ret;
575
576 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800580 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700581 * @mtd: MTD device structure
582 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300583 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800584 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300585 */
586static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
587{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100588 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300589
590 if (!chip->bbt)
591 return 0;
592 /* Return info from the table */
593 return nand_isreserved_bbt(mtd, ofs);
594}
595
596/**
597 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
598 * @mtd: MTD device structure
599 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700600 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
602 * Check, if the block is bad. Either by reading the bad block table or
603 * calling of the scan function.
604 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530605static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100607 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000608
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200609 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530610 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100613 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200616/**
617 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700618 * @mtd: MTD device structure
619 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200620 *
621 * Helper function for nand_wait_ready used when needing to wait in interrupt
622 * context.
623 */
624static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
625{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100626 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200627 int i;
628
629 /* Wait for the device to get ready */
630 for (i = 0; i < timeo; i++) {
631 if (chip->dev_ready(mtd))
632 break;
633 touch_softlockup_watchdog();
634 mdelay(1);
635 }
636}
637
Alex Smithb70af9b2015-10-06 14:52:07 +0100638/**
639 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
640 * @mtd: MTD device structure
641 *
642 * Wait for the ready pin after a command, and warn if a timeout occurs.
643 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100644void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000645{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100646 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100647 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000648
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200649 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100650 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200651
Brian Norris7854d3f2011-06-23 14:12:08 -0700652 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100653 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000654 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300656 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100657 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000658 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100659
Brian Norris9ebfdf52016-03-04 17:19:23 -0800660 if (!chip->dev_ready(mtd))
661 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000662}
David Woodhouse4b648b02006-09-25 17:05:24 +0100663EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200666 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
667 * @mtd: MTD device structure
668 * @timeo: Timeout in ms
669 *
670 * Wait for status ready (i.e. command done) or timeout.
671 */
672static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
673{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100674 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100675 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200676
677 timeo = jiffies + msecs_to_jiffies(timeo);
678 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100679 u8 status;
680
681 ret = nand_read_data_op(chip, &status, sizeof(status), true);
682 if (ret)
683 return;
684
685 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200686 break;
687 touch_softlockup_watchdog();
688 } while (time_before(jiffies, timeo));
689};
690
691/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700693 * @mtd: MTD device structure
694 * @command: the command to be sent
695 * @column: the column address for this command, -1 if none
696 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700698 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200699 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200701static void nand_command(struct mtd_info *mtd, unsigned int command,
702 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100704 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200705 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Brian Norris8b6e50c2011-05-25 14:59:01 -0700707 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (command == NAND_CMD_SEQIN) {
709 int readcmd;
710
Joern Engel28318772006-05-22 23:18:05 +0200711 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200713 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 readcmd = NAND_CMD_READOOB;
715 } else if (column < 256) {
716 /* First 256 bytes --> READ0 */
717 readcmd = NAND_CMD_READ0;
718 } else {
719 column -= 256;
720 readcmd = NAND_CMD_READ1;
721 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200722 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200723 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100725 if (command != NAND_CMD_NONE)
726 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Brian Norris8b6e50c2011-05-25 14:59:01 -0700728 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200729 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
730 /* Serially input address */
731 if (column != -1) {
732 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800733 if (chip->options & NAND_BUSWIDTH_16 &&
734 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200735 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200736 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200737 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200739 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200740 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200741 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200742 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900743 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200744 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200745 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200746 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000747
748 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700749 * Program and erase have their own busy handlers status and sequential
750 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000753
Miquel Raynaldf467892017-11-08 17:00:27 +0100754 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 case NAND_CMD_PAGEPROG:
756 case NAND_CMD_ERASE1:
757 case NAND_CMD_ERASE2:
758 case NAND_CMD_SEQIN:
759 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900760 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900761 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return;
763
764 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200765 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200767 udelay(chip->chip_delay);
768 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200769 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200770 chip->cmd_ctrl(mtd,
771 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200772 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
773 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return;
775
David Woodhousee0c7d762006-05-13 18:07:53 +0100776 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200777 case NAND_CMD_READ0:
778 /*
779 * READ0 is sometimes used to exit GET STATUS mode. When this
780 * is the case no address cycles are requested, and we can use
781 * this information to detect that we should not wait for the
782 * device to be ready.
783 */
784 if (column == -1 && page_addr == -1)
785 return;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000788 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * If we don't have access to the busy pin, we apply the given
790 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100791 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200792 if (!chip->dev_ready) {
793 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700797 /*
798 * Apply this short delay always to ensure that we do wait tWB in
799 * any case on any machine.
800 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100801 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000802
803 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200806static void nand_ccs_delay(struct nand_chip *chip)
807{
808 /*
809 * The controller already takes care of waiting for tCCS when the RNDIN
810 * or RNDOUT command is sent, return directly.
811 */
812 if (!(chip->options & NAND_WAIT_TCCS))
813 return;
814
815 /*
816 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
817 * (which should be safe for all NANDs).
818 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100819 if (chip->setup_data_interface)
820 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200821 else
822 ndelay(500);
823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/**
826 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700827 * @mtd: MTD device structure
828 * @command: the command to be sent
829 * @column: the column address for this command, -1 if none
830 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200832 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700833 * devices. We don't have the separate regions as we have in the small page
834 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200836static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
837 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100839 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* Emulate NAND_CMD_READOOB */
842 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200843 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 command = NAND_CMD_READ0;
845 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000846
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200847 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100848 if (command != NAND_CMD_NONE)
849 chip->cmd_ctrl(mtd, command,
850 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200853 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* Serially input address */
856 if (column != -1) {
857 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800858 if (chip->options & NAND_BUSWIDTH_16 &&
859 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200861 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200862 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200863
Brian Norrisf5b88de2016-10-03 09:49:35 -0700864 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200865 if (!nand_opcode_8bits(command))
866 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200869 chip->cmd_ctrl(mtd, page_addr, ctrl);
870 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200871 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900872 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200873 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200874 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200877 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000878
879 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700880 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100881 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000884
Miquel Raynaldf467892017-11-08 17:00:27 +0100885 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 case NAND_CMD_CACHEDPROG:
887 case NAND_CMD_PAGEPROG:
888 case NAND_CMD_ERASE1:
889 case NAND_CMD_ERASE2:
890 case NAND_CMD_SEQIN:
891 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900892 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900893 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000894 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200896 case NAND_CMD_RNDIN:
897 nand_ccs_delay(chip);
898 return;
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200901 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200903 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200904 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
905 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
906 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
907 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200908 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
909 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return;
911
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200912 case NAND_CMD_RNDOUT:
913 /* No ready / busy check necessary */
914 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
915 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
916 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
917 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200918
919 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200920 return;
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200923 /*
924 * READ0 is sometimes used to exit GET STATUS mode. When this
925 * is the case no address cycles are requested, and we can use
926 * this information to detect that READSTART should not be
927 * issued.
928 */
929 if (column == -1 && page_addr == -1)
930 return;
931
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200932 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
933 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
934 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
935 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000936
David Woodhousee0c7d762006-05-13 18:07:53 +0100937 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000939 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700941 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100942 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200943 if (!chip->dev_ready) {
944 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000948
Brian Norris8b6e50c2011-05-25 14:59:01 -0700949 /*
950 * Apply this short delay always to ensure that we do wait tWB in
951 * any case on any machine.
952 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100953 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000954
955 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200959 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700960 * @chip: the nand chip descriptor
961 * @mtd: MTD device structure
962 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200963 *
964 * Used when in panic, no locks are taken.
965 */
966static void panic_nand_get_device(struct nand_chip *chip,
967 struct mtd_info *mtd, int new_state)
968{
Brian Norris7854d3f2011-06-23 14:12:08 -0700969 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200970 chip->controller->active = chip;
971 chip->state = new_state;
972}
973
974/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700976 * @mtd: MTD device structure
977 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 *
979 * Get the device and lock it for exclusive access
980 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200981static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800982nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100984 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200985 spinlock_t *lock = &chip->controller->lock;
986 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100987 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200988retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100989 spin_lock(lock);
990
vimal singhb8b3ee92009-07-09 20:41:22 +0530991 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200992 if (!chip->controller->active)
993 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200994
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200995 if (chip->controller->active == chip && chip->state == FL_READY) {
996 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100997 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100998 return 0;
999 }
1000 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001001 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1002 chip->state = FL_PM_SUSPENDED;
1003 spin_unlock(lock);
1004 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001005 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001006 }
1007 set_current_state(TASK_UNINTERRUPTIBLE);
1008 add_wait_queue(wq, &wait);
1009 spin_unlock(lock);
1010 schedule();
1011 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto retry;
1013}
1014
1015/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001016 * panic_nand_wait - [GENERIC] wait until the command is done
1017 * @mtd: MTD device structure
1018 * @chip: NAND chip structure
1019 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001020 *
1021 * Wait for command done. This is a helper function for nand_wait used when
1022 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001023 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001024 */
1025static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1026 unsigned long timeo)
1027{
1028 int i;
1029 for (i = 0; i < timeo; i++) {
1030 if (chip->dev_ready) {
1031 if (chip->dev_ready(mtd))
1032 break;
1033 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001034 int ret;
1035 u8 status;
1036
1037 ret = nand_read_data_op(chip, &status, sizeof(status),
1038 true);
1039 if (ret)
1040 return;
1041
1042 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001043 break;
1044 }
1045 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001046 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001047}
1048
1049/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001050 * nand_wait - [DEFAULT] wait until the command is done
1051 * @mtd: MTD device structure
1052 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001054 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001055 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001056static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058
Alex Smithb70af9b2015-10-06 14:52:07 +01001059 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001060 u8 status;
1061 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Brian Norris8b6e50c2011-05-25 14:59:01 -07001063 /*
1064 * Apply this short delay always to ensure that we do wait tWB in any
1065 * case on any machine.
1066 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001067 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Boris Brezillon97d90da2017-11-30 18:01:29 +01001069 ret = nand_status_op(chip, NULL);
1070 if (ret)
1071 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001073 if (in_interrupt() || oops_in_progress)
1074 panic_nand_wait(mtd, chip, timeo);
1075 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001076 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001077 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001078 if (chip->dev_ready) {
1079 if (chip->dev_ready(mtd))
1080 break;
1081 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001082 ret = nand_read_data_op(chip, &status,
1083 sizeof(status), true);
1084 if (ret)
1085 return ret;
1086
1087 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001088 break;
1089 }
1090 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001091 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001093
Boris Brezillon97d90da2017-11-30 18:01:29 +01001094 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1095 if (ret)
1096 return ret;
1097
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001098 /* This can happen if in case of timeout or buggy dev_ready */
1099 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return status;
1101}
1102
1103/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001104 * nand_reset_data_interface - Reset data interface and timings
1105 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001106 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001107 *
1108 * Reset the Data interface and timings to ONFI mode 0.
1109 *
1110 * Returns 0 for success or negative error code otherwise.
1111 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001112static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001113{
1114 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001115 int ret;
1116
1117 if (!chip->setup_data_interface)
1118 return 0;
1119
1120 /*
1121 * The ONFI specification says:
1122 * "
1123 * To transition from NV-DDR or NV-DDR2 to the SDR data
1124 * interface, the host shall use the Reset (FFh) command
1125 * using SDR timing mode 0. A device in any timing mode is
1126 * required to recognize Reset (FFh) command issued in SDR
1127 * timing mode 0.
1128 * "
1129 *
1130 * Configure the data interface in SDR mode and set the
1131 * timings to timing mode 0.
1132 */
1133
Miquel Raynal17fa8042017-11-30 18:01:31 +01001134 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1135 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001136 if (ret)
1137 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1138
1139 return ret;
1140}
1141
1142/**
1143 * nand_setup_data_interface - Setup the best data interface and timings
1144 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001145 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001146 *
1147 * Find and configure the best data interface and NAND timings supported by
1148 * the chip and the driver.
1149 * First tries to retrieve supported timing modes from ONFI information,
1150 * and if the NAND chip does not support ONFI, relies on the
1151 * ->onfi_timing_mode_default specified in the nand_ids table.
1152 *
1153 * Returns 0 for success or negative error code otherwise.
1154 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001155static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001156{
1157 struct mtd_info *mtd = nand_to_mtd(chip);
1158 int ret;
1159
Miquel Raynal17fa8042017-11-30 18:01:31 +01001160 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001161 return 0;
1162
1163 /*
1164 * Ensure the timing mode has been changed on the chip side
1165 * before changing timings on the controller side.
1166 */
Boris Brezillona11bf5e2017-07-31 10:29:56 +02001167 if (chip->onfi_version &&
1168 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1169 ONFI_OPT_CMD_SET_GET_FEATURES)) {
Boris Brezillond8e725d2016-09-15 10:32:50 +02001170 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1171 chip->onfi_timing_mode_default,
1172 };
1173
1174 ret = chip->onfi_set_features(mtd, chip,
1175 ONFI_FEATURE_ADDR_TIMING_MODE,
1176 tmode_param);
1177 if (ret)
1178 goto err;
1179 }
1180
Miquel Raynal17fa8042017-11-30 18:01:31 +01001181 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001182err:
1183 return ret;
1184}
1185
1186/**
1187 * nand_init_data_interface - find the best data interface and timings
1188 * @chip: The NAND chip
1189 *
1190 * Find the best data interface and NAND timings supported by the chip
1191 * and the driver.
1192 * First tries to retrieve supported timing modes from ONFI information,
1193 * and if the NAND chip does not support ONFI, relies on the
1194 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1195 * function nand_chip->data_interface is initialized with the best timing mode
1196 * available.
1197 *
1198 * Returns 0 for success or negative error code otherwise.
1199 */
1200static int nand_init_data_interface(struct nand_chip *chip)
1201{
1202 struct mtd_info *mtd = nand_to_mtd(chip);
1203 int modes, mode, ret;
1204
1205 if (!chip->setup_data_interface)
1206 return 0;
1207
1208 /*
1209 * First try to identify the best timings from ONFI parameters and
1210 * if the NAND does not support ONFI, fallback to the default ONFI
1211 * timing mode.
1212 */
1213 modes = onfi_get_async_timing_mode(chip);
1214 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1215 if (!chip->onfi_timing_mode_default)
1216 return 0;
1217
1218 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1219 }
1220
Boris Brezillond8e725d2016-09-15 10:32:50 +02001221
1222 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001223 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001224 if (ret)
1225 continue;
1226
Boris Brezillon104e4422017-03-16 09:35:58 +01001227 /* Pass -1 to only */
1228 ret = chip->setup_data_interface(mtd,
1229 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001230 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001231 if (!ret) {
1232 chip->onfi_timing_mode_default = mode;
1233 break;
1234 }
1235 }
1236
1237 return 0;
1238}
1239
Boris Brezillond8e725d2016-09-15 10:32:50 +02001240/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001241 * nand_read_page_op - Do a READ PAGE operation
1242 * @chip: The NAND chip
1243 * @page: page to read
1244 * @offset_in_page: offset within the page
1245 * @buf: buffer used to store the data
1246 * @len: length of the buffer
1247 *
1248 * This function issues a READ PAGE operation.
1249 * This function does not select/unselect the CS line.
1250 *
1251 * Returns 0 on success, a negative error code otherwise.
1252 */
1253int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1254 unsigned int offset_in_page, void *buf, unsigned int len)
1255{
1256 struct mtd_info *mtd = nand_to_mtd(chip);
1257
1258 if (len && !buf)
1259 return -EINVAL;
1260
1261 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1262 return -EINVAL;
1263
1264 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1265 if (len)
1266 chip->read_buf(mtd, buf, len);
1267
1268 return 0;
1269}
1270EXPORT_SYMBOL_GPL(nand_read_page_op);
1271
1272/**
1273 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1274 * @chip: The NAND chip
1275 * @page: parameter page to read
1276 * @buf: buffer used to store the data
1277 * @len: length of the buffer
1278 *
1279 * This function issues a READ PARAMETER PAGE operation.
1280 * This function does not select/unselect the CS line.
1281 *
1282 * Returns 0 on success, a negative error code otherwise.
1283 */
1284static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1285 unsigned int len)
1286{
1287 struct mtd_info *mtd = nand_to_mtd(chip);
1288 unsigned int i;
1289 u8 *p = buf;
1290
1291 if (len && !buf)
1292 return -EINVAL;
1293
1294 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1295 for (i = 0; i < len; i++)
1296 p[i] = chip->read_byte(mtd);
1297
1298 return 0;
1299}
1300
1301/**
1302 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1303 * @chip: The NAND chip
1304 * @offset_in_page: offset within the page
1305 * @buf: buffer used to store the data
1306 * @len: length of the buffer
1307 * @force_8bit: force 8-bit bus access
1308 *
1309 * This function issues a CHANGE READ COLUMN operation.
1310 * This function does not select/unselect the CS line.
1311 *
1312 * Returns 0 on success, a negative error code otherwise.
1313 */
1314int nand_change_read_column_op(struct nand_chip *chip,
1315 unsigned int offset_in_page, void *buf,
1316 unsigned int len, bool force_8bit)
1317{
1318 struct mtd_info *mtd = nand_to_mtd(chip);
1319
1320 if (len && !buf)
1321 return -EINVAL;
1322
1323 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1324 return -EINVAL;
1325
1326 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1327 if (len)
1328 chip->read_buf(mtd, buf, len);
1329
1330 return 0;
1331}
1332EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1333
1334/**
1335 * nand_read_oob_op - Do a READ OOB operation
1336 * @chip: The NAND chip
1337 * @page: page to read
1338 * @offset_in_oob: offset within the OOB area
1339 * @buf: buffer used to store the data
1340 * @len: length of the buffer
1341 *
1342 * This function issues a READ OOB operation.
1343 * This function does not select/unselect the CS line.
1344 *
1345 * Returns 0 on success, a negative error code otherwise.
1346 */
1347int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1348 unsigned int offset_in_oob, void *buf, unsigned int len)
1349{
1350 struct mtd_info *mtd = nand_to_mtd(chip);
1351
1352 if (len && !buf)
1353 return -EINVAL;
1354
1355 if (offset_in_oob + len > mtd->oobsize)
1356 return -EINVAL;
1357
1358 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1359 if (len)
1360 chip->read_buf(mtd, buf, len);
1361
1362 return 0;
1363}
1364EXPORT_SYMBOL_GPL(nand_read_oob_op);
1365
1366/**
1367 * nand_prog_page_begin_op - starts a PROG PAGE operation
1368 * @chip: The NAND chip
1369 * @page: page to write
1370 * @offset_in_page: offset within the page
1371 * @buf: buffer containing the data to write to the page
1372 * @len: length of the buffer
1373 *
1374 * This function issues the first half of a PROG PAGE operation.
1375 * This function does not select/unselect the CS line.
1376 *
1377 * Returns 0 on success, a negative error code otherwise.
1378 */
1379int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1380 unsigned int offset_in_page, const void *buf,
1381 unsigned int len)
1382{
1383 struct mtd_info *mtd = nand_to_mtd(chip);
1384
1385 if (len && !buf)
1386 return -EINVAL;
1387
1388 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1389 return -EINVAL;
1390
1391 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1392
1393 if (buf)
1394 chip->write_buf(mtd, buf, len);
1395
1396 return 0;
1397}
1398EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1399
1400/**
1401 * nand_prog_page_end_op - ends a PROG PAGE operation
1402 * @chip: The NAND chip
1403 *
1404 * This function issues the second half of a PROG PAGE operation.
1405 * This function does not select/unselect the CS line.
1406 *
1407 * Returns 0 on success, a negative error code otherwise.
1408 */
1409int nand_prog_page_end_op(struct nand_chip *chip)
1410{
1411 struct mtd_info *mtd = nand_to_mtd(chip);
1412 int status;
1413
1414 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1415
1416 status = chip->waitfunc(mtd, chip);
1417 if (status & NAND_STATUS_FAIL)
1418 return -EIO;
1419
1420 return 0;
1421}
1422EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1423
1424/**
1425 * nand_prog_page_op - Do a full PROG PAGE operation
1426 * @chip: The NAND chip
1427 * @page: page to write
1428 * @offset_in_page: offset within the page
1429 * @buf: buffer containing the data to write to the page
1430 * @len: length of the buffer
1431 *
1432 * This function issues a full PROG PAGE operation.
1433 * This function does not select/unselect the CS line.
1434 *
1435 * Returns 0 on success, a negative error code otherwise.
1436 */
1437int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1438 unsigned int offset_in_page, const void *buf,
1439 unsigned int len)
1440{
1441 struct mtd_info *mtd = nand_to_mtd(chip);
1442 int status;
1443
1444 if (!len || !buf)
1445 return -EINVAL;
1446
1447 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1448 return -EINVAL;
1449
1450 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1451 chip->write_buf(mtd, buf, len);
1452 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1453
1454 status = chip->waitfunc(mtd, chip);
1455 if (status & NAND_STATUS_FAIL)
1456 return -EIO;
1457
1458 return 0;
1459}
1460EXPORT_SYMBOL_GPL(nand_prog_page_op);
1461
1462/**
1463 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1464 * @chip: The NAND chip
1465 * @offset_in_page: offset within the page
1466 * @buf: buffer containing the data to send to the NAND
1467 * @len: length of the buffer
1468 * @force_8bit: force 8-bit bus access
1469 *
1470 * This function issues a CHANGE WRITE COLUMN operation.
1471 * This function does not select/unselect the CS line.
1472 *
1473 * Returns 0 on success, a negative error code otherwise.
1474 */
1475int nand_change_write_column_op(struct nand_chip *chip,
1476 unsigned int offset_in_page,
1477 const void *buf, unsigned int len,
1478 bool force_8bit)
1479{
1480 struct mtd_info *mtd = nand_to_mtd(chip);
1481
1482 if (len && !buf)
1483 return -EINVAL;
1484
1485 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1486 return -EINVAL;
1487
1488 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1489 if (len)
1490 chip->write_buf(mtd, buf, len);
1491
1492 return 0;
1493}
1494EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1495
1496/**
1497 * nand_readid_op - Do a READID operation
1498 * @chip: The NAND chip
1499 * @addr: address cycle to pass after the READID command
1500 * @buf: buffer used to store the ID
1501 * @len: length of the buffer
1502 *
1503 * This function sends a READID command and reads back the ID returned by the
1504 * NAND.
1505 * This function does not select/unselect the CS line.
1506 *
1507 * Returns 0 on success, a negative error code otherwise.
1508 */
1509int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1510 unsigned int len)
1511{
1512 struct mtd_info *mtd = nand_to_mtd(chip);
1513 unsigned int i;
1514 u8 *id = buf;
1515
1516 if (len && !buf)
1517 return -EINVAL;
1518
1519 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1520
1521 for (i = 0; i < len; i++)
1522 id[i] = chip->read_byte(mtd);
1523
1524 return 0;
1525}
1526EXPORT_SYMBOL_GPL(nand_readid_op);
1527
1528/**
1529 * nand_status_op - Do a STATUS operation
1530 * @chip: The NAND chip
1531 * @status: out variable to store the NAND status
1532 *
1533 * This function sends a STATUS command and reads back the status returned by
1534 * the NAND.
1535 * This function does not select/unselect the CS line.
1536 *
1537 * Returns 0 on success, a negative error code otherwise.
1538 */
1539int nand_status_op(struct nand_chip *chip, u8 *status)
1540{
1541 struct mtd_info *mtd = nand_to_mtd(chip);
1542
1543 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1544 if (status)
1545 *status = chip->read_byte(mtd);
1546
1547 return 0;
1548}
1549EXPORT_SYMBOL_GPL(nand_status_op);
1550
1551/**
1552 * nand_exit_status_op - Exit a STATUS operation
1553 * @chip: The NAND chip
1554 *
1555 * This function sends a READ0 command to cancel the effect of the STATUS
1556 * command to avoid reading only the status until a new read command is sent.
1557 *
1558 * This function does not select/unselect the CS line.
1559 *
1560 * Returns 0 on success, a negative error code otherwise.
1561 */
1562int nand_exit_status_op(struct nand_chip *chip)
1563{
1564 struct mtd_info *mtd = nand_to_mtd(chip);
1565
1566 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1567
1568 return 0;
1569}
1570EXPORT_SYMBOL_GPL(nand_exit_status_op);
1571
1572/**
1573 * nand_erase_op - Do an erase operation
1574 * @chip: The NAND chip
1575 * @eraseblock: block to erase
1576 *
1577 * This function sends an ERASE command and waits for the NAND to be ready
1578 * before returning.
1579 * This function does not select/unselect the CS line.
1580 *
1581 * Returns 0 on success, a negative error code otherwise.
1582 */
1583int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1584{
1585 struct mtd_info *mtd = nand_to_mtd(chip);
1586 unsigned int page = eraseblock <<
1587 (chip->phys_erase_shift - chip->page_shift);
1588 int status;
1589
1590 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1591 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1592
1593 status = chip->waitfunc(mtd, chip);
1594 if (status < 0)
1595 return status;
1596
1597 if (status & NAND_STATUS_FAIL)
1598 return -EIO;
1599
1600 return 0;
1601}
1602EXPORT_SYMBOL_GPL(nand_erase_op);
1603
1604/**
1605 * nand_set_features_op - Do a SET FEATURES operation
1606 * @chip: The NAND chip
1607 * @feature: feature id
1608 * @data: 4 bytes of data
1609 *
1610 * This function sends a SET FEATURES command and waits for the NAND to be
1611 * ready before returning.
1612 * This function does not select/unselect the CS line.
1613 *
1614 * Returns 0 on success, a negative error code otherwise.
1615 */
1616static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1617 const void *data)
1618{
1619 struct mtd_info *mtd = nand_to_mtd(chip);
1620 const u8 *params = data;
1621 int i, status;
1622
1623 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1624 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1625 chip->write_byte(mtd, params[i]);
1626
1627 status = chip->waitfunc(mtd, chip);
1628 if (status & NAND_STATUS_FAIL)
1629 return -EIO;
1630
1631 return 0;
1632}
1633
1634/**
1635 * nand_get_features_op - Do a GET FEATURES operation
1636 * @chip: The NAND chip
1637 * @feature: feature id
1638 * @data: 4 bytes of data
1639 *
1640 * This function sends a GET FEATURES command and waits for the NAND to be
1641 * ready before returning.
1642 * This function does not select/unselect the CS line.
1643 *
1644 * Returns 0 on success, a negative error code otherwise.
1645 */
1646static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1647 void *data)
1648{
1649 struct mtd_info *mtd = nand_to_mtd(chip);
1650 u8 *params = data;
1651 int i;
1652
1653 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1654 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1655 params[i] = chip->read_byte(mtd);
1656
1657 return 0;
1658}
1659
1660/**
1661 * nand_reset_op - Do a reset operation
1662 * @chip: The NAND chip
1663 *
1664 * This function sends a RESET command and waits for the NAND to be ready
1665 * before returning.
1666 * This function does not select/unselect the CS line.
1667 *
1668 * Returns 0 on success, a negative error code otherwise.
1669 */
1670int nand_reset_op(struct nand_chip *chip)
1671{
1672 struct mtd_info *mtd = nand_to_mtd(chip);
1673
1674 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1675
1676 return 0;
1677}
1678EXPORT_SYMBOL_GPL(nand_reset_op);
1679
1680/**
1681 * nand_read_data_op - Read data from the NAND
1682 * @chip: The NAND chip
1683 * @buf: buffer used to store the data
1684 * @len: length of the buffer
1685 * @force_8bit: force 8-bit bus access
1686 *
1687 * This function does a raw data read on the bus. Usually used after launching
1688 * another NAND operation like nand_read_page_op().
1689 * This function does not select/unselect the CS line.
1690 *
1691 * Returns 0 on success, a negative error code otherwise.
1692 */
1693int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1694 bool force_8bit)
1695{
1696 struct mtd_info *mtd = nand_to_mtd(chip);
1697
1698 if (!len || !buf)
1699 return -EINVAL;
1700
1701 if (force_8bit) {
1702 u8 *p = buf;
1703 unsigned int i;
1704
1705 for (i = 0; i < len; i++)
1706 p[i] = chip->read_byte(mtd);
1707 } else {
1708 chip->read_buf(mtd, buf, len);
1709 }
1710
1711 return 0;
1712}
1713EXPORT_SYMBOL_GPL(nand_read_data_op);
1714
1715/**
1716 * nand_write_data_op - Write data from the NAND
1717 * @chip: The NAND chip
1718 * @buf: buffer containing the data to send on the bus
1719 * @len: length of the buffer
1720 * @force_8bit: force 8-bit bus access
1721 *
1722 * This function does a raw data write on the bus. Usually used after launching
1723 * another NAND operation like nand_write_page_begin_op().
1724 * This function does not select/unselect the CS line.
1725 *
1726 * Returns 0 on success, a negative error code otherwise.
1727 */
1728int nand_write_data_op(struct nand_chip *chip, const void *buf,
1729 unsigned int len, bool force_8bit)
1730{
1731 struct mtd_info *mtd = nand_to_mtd(chip);
1732
1733 if (!len || !buf)
1734 return -EINVAL;
1735
1736 if (force_8bit) {
1737 const u8 *p = buf;
1738 unsigned int i;
1739
1740 for (i = 0; i < len; i++)
1741 chip->write_byte(mtd, p[i]);
1742 } else {
1743 chip->write_buf(mtd, buf, len);
1744 }
1745
1746 return 0;
1747}
1748EXPORT_SYMBOL_GPL(nand_write_data_op);
1749
1750/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001751 * nand_reset - Reset and initialize a NAND device
1752 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001753 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001754 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01001755 * Save the timings data structure, then apply SDR timings mode 0 (see
1756 * nand_reset_data_interface for details), do the reset operation, and
1757 * apply back the previous timings.
1758 *
1759 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001760 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001761int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001762{
1763 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal17fa8042017-11-30 18:01:31 +01001764 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001765 int ret;
1766
Boris Brezillon104e4422017-03-16 09:35:58 +01001767 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001768 if (ret)
1769 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001770
Boris Brezillon73f907f2016-10-24 16:46:20 +02001771 /*
1772 * The CS line has to be released before we can apply the new NAND
1773 * interface settings, hence this weird ->select_chip() dance.
1774 */
1775 chip->select_chip(mtd, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001776 ret = nand_reset_op(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001777 chip->select_chip(mtd, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001778 if (ret)
1779 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001780
Boris Brezillon73f907f2016-10-24 16:46:20 +02001781 chip->select_chip(mtd, chipnr);
Miquel Raynal17fa8042017-11-30 18:01:31 +01001782 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01001783 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001784 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001785 if (ret)
1786 return ret;
1787
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001788 return 0;
1789}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02001790EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001791
1792/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001793 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1794 * @buf: buffer to test
1795 * @len: buffer length
1796 * @bitflips_threshold: maximum number of bitflips
1797 *
1798 * Check if a buffer contains only 0xff, which means the underlying region
1799 * has been erased and is ready to be programmed.
1800 * The bitflips_threshold specify the maximum number of bitflips before
1801 * considering the region is not erased.
1802 * Note: The logic of this function has been extracted from the memweight
1803 * implementation, except that nand_check_erased_buf function exit before
1804 * testing the whole buffer if the number of bitflips exceed the
1805 * bitflips_threshold value.
1806 *
1807 * Returns a positive number of bitflips less than or equal to
1808 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1809 * threshold.
1810 */
1811static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1812{
1813 const unsigned char *bitmap = buf;
1814 int bitflips = 0;
1815 int weight;
1816
1817 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1818 len--, bitmap++) {
1819 weight = hweight8(*bitmap);
1820 bitflips += BITS_PER_BYTE - weight;
1821 if (unlikely(bitflips > bitflips_threshold))
1822 return -EBADMSG;
1823 }
1824
1825 for (; len >= sizeof(long);
1826 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001827 unsigned long d = *((unsigned long *)bitmap);
1828 if (d == ~0UL)
1829 continue;
1830 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001831 bitflips += BITS_PER_LONG - weight;
1832 if (unlikely(bitflips > bitflips_threshold))
1833 return -EBADMSG;
1834 }
1835
1836 for (; len > 0; len--, bitmap++) {
1837 weight = hweight8(*bitmap);
1838 bitflips += BITS_PER_BYTE - weight;
1839 if (unlikely(bitflips > bitflips_threshold))
1840 return -EBADMSG;
1841 }
1842
1843 return bitflips;
1844}
1845
1846/**
1847 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1848 * 0xff data
1849 * @data: data buffer to test
1850 * @datalen: data length
1851 * @ecc: ECC buffer
1852 * @ecclen: ECC length
1853 * @extraoob: extra OOB buffer
1854 * @extraooblen: extra OOB length
1855 * @bitflips_threshold: maximum number of bitflips
1856 *
1857 * Check if a data buffer and its associated ECC and OOB data contains only
1858 * 0xff pattern, which means the underlying region has been erased and is
1859 * ready to be programmed.
1860 * The bitflips_threshold specify the maximum number of bitflips before
1861 * considering the region as not erased.
1862 *
1863 * Note:
1864 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1865 * different from the NAND page size. When fixing bitflips, ECC engines will
1866 * report the number of errors per chunk, and the NAND core infrastructure
1867 * expect you to return the maximum number of bitflips for the whole page.
1868 * This is why you should always use this function on a single chunk and
1869 * not on the whole page. After checking each chunk you should update your
1870 * max_bitflips value accordingly.
1871 * 2/ When checking for bitflips in erased pages you should not only check
1872 * the payload data but also their associated ECC data, because a user might
1873 * have programmed almost all bits to 1 but a few. In this case, we
1874 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1875 * this case.
1876 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1877 * data are protected by the ECC engine.
1878 * It could also be used if you support subpages and want to attach some
1879 * extra OOB data to an ECC chunk.
1880 *
1881 * Returns a positive number of bitflips less than or equal to
1882 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1883 * threshold. In case of success, the passed buffers are filled with 0xff.
1884 */
1885int nand_check_erased_ecc_chunk(void *data, int datalen,
1886 void *ecc, int ecclen,
1887 void *extraoob, int extraooblen,
1888 int bitflips_threshold)
1889{
1890 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1891
1892 data_bitflips = nand_check_erased_buf(data, datalen,
1893 bitflips_threshold);
1894 if (data_bitflips < 0)
1895 return data_bitflips;
1896
1897 bitflips_threshold -= data_bitflips;
1898
1899 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1900 if (ecc_bitflips < 0)
1901 return ecc_bitflips;
1902
1903 bitflips_threshold -= ecc_bitflips;
1904
1905 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1906 bitflips_threshold);
1907 if (extraoob_bitflips < 0)
1908 return extraoob_bitflips;
1909
1910 if (data_bitflips)
1911 memset(data, 0xff, datalen);
1912
1913 if (ecc_bitflips)
1914 memset(ecc, 0xff, ecclen);
1915
1916 if (extraoob_bitflips)
1917 memset(extraoob, 0xff, extraooblen);
1918
1919 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1920}
1921EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1922
1923/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001924 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001925 * @mtd: mtd info structure
1926 * @chip: nand chip info structure
1927 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001928 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001929 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001930 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001931 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001932 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001933int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1934 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001935{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001936 int ret;
1937
Boris Brezillon25f815f2017-11-30 18:01:30 +01001938 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001939 if (ret)
1940 return ret;
1941
1942 if (oob_required) {
1943 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
1944 false);
1945 if (ret)
1946 return ret;
1947 }
1948
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001949 return 0;
1950}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001951EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001952
1953/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001954 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001955 * @mtd: mtd info structure
1956 * @chip: nand chip info structure
1957 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001958 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001959 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001960 *
1961 * We need a special oob layout and handling even when OOB isn't used.
1962 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001963static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001964 struct nand_chip *chip, uint8_t *buf,
1965 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001966{
1967 int eccsize = chip->ecc.size;
1968 int eccbytes = chip->ecc.bytes;
1969 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001970 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08001971
Boris Brezillon25f815f2017-11-30 18:01:30 +01001972 ret = nand_read_page_op(chip, page, 0, NULL, 0);
1973 if (ret)
1974 return ret;
1975
David Brownell52ff49d2009-03-04 12:01:36 -08001976 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001977 ret = nand_read_data_op(chip, buf, eccsize, false);
1978 if (ret)
1979 return ret;
1980
David Brownell52ff49d2009-03-04 12:01:36 -08001981 buf += eccsize;
1982
1983 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001984 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
1985 false);
1986 if (ret)
1987 return ret;
1988
David Brownell52ff49d2009-03-04 12:01:36 -08001989 oob += chip->ecc.prepad;
1990 }
1991
Boris Brezillon97d90da2017-11-30 18:01:29 +01001992 ret = nand_read_data_op(chip, oob, eccbytes, false);
1993 if (ret)
1994 return ret;
1995
David Brownell52ff49d2009-03-04 12:01:36 -08001996 oob += eccbytes;
1997
1998 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001999 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2000 false);
2001 if (ret)
2002 return ret;
2003
David Brownell52ff49d2009-03-04 12:01:36 -08002004 oob += chip->ecc.postpad;
2005 }
2006 }
2007
2008 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002009 if (size) {
2010 ret = nand_read_data_op(chip, oob, size, false);
2011 if (ret)
2012 return ret;
2013 }
David Brownell52ff49d2009-03-04 12:01:36 -08002014
2015 return 0;
2016}
2017
2018/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002019 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002020 * @mtd: mtd info structure
2021 * @chip: nand chip info structure
2022 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002023 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002024 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002025 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002026static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002027 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Boris Brezillon846031d2016-02-03 20:11:00 +01002029 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002030 int eccbytes = chip->ecc.bytes;
2031 int eccsteps = chip->ecc.steps;
2032 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002033 uint8_t *ecc_calc = chip->ecc.calc_buf;
2034 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002035 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002036
Brian Norris1fbb9382012-05-02 10:14:55 -07002037 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002038
2039 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2040 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2041
Boris Brezillon846031d2016-02-03 20:11:00 +01002042 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2043 chip->ecc.total);
2044 if (ret)
2045 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002046
2047 eccsteps = chip->ecc.steps;
2048 p = buf;
2049
2050 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2051 int stat;
2052
2053 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07002054 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002055 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002056 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002057 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002058 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2059 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002060 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002061 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01002062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302065 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002066 * @mtd: mtd info structure
2067 * @chip: nand chip info structure
2068 * @data_offs: offset of requested data within the page
2069 * @readlen: data length
2070 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08002071 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01002072 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002073static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08002074 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
2075 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01002076{
Boris Brezillon846031d2016-02-03 20:11:00 +01002077 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002078 uint8_t *p;
2079 int data_col_addr, i, gaps = 0;
2080 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
2081 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01002082 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07002083 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01002084 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01002085
Brian Norris7854d3f2011-06-23 14:12:08 -07002086 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01002087 start_step = data_offs / chip->ecc.size;
2088 end_step = (data_offs + readlen - 1) / chip->ecc.size;
2089 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10302090 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01002091
Brian Norris8b6e50c2011-05-25 14:59:01 -07002092 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01002093 datafrag_len = num_steps * chip->ecc.size;
2094 eccfrag_len = num_steps * chip->ecc.bytes;
2095
2096 data_col_addr = start_step * chip->ecc.size;
2097 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01002098 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01002099 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002100 if (ret)
2101 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002102
Brian Norris8b6e50c2011-05-25 14:59:01 -07002103 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01002104 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002105 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01002106
Brian Norris8b6e50c2011-05-25 14:59:01 -07002107 /*
2108 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07002109 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07002110 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002111 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
2112 if (ret)
2113 return ret;
2114
2115 if (oobregion.length < eccfrag_len)
2116 gaps = 1;
2117
Alexey Korolev3d459552008-05-15 17:23:18 +01002118 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002119 ret = nand_change_read_column_op(chip, mtd->writesize,
2120 chip->oob_poi, mtd->oobsize,
2121 false);
2122 if (ret)
2123 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002124 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002125 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002126 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07002127 * about buswidth alignment in read_buf.
2128 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002129 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01002130 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01002131 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002132 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01002133 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
2134 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002135 aligned_len++;
2136
Boris Brezillon97d90da2017-11-30 18:01:29 +01002137 ret = nand_change_read_column_op(chip,
2138 mtd->writesize + aligned_pos,
2139 &chip->oob_poi[aligned_pos],
2140 aligned_len, false);
2141 if (ret)
2142 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002143 }
2144
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002145 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01002146 chip->oob_poi, index, eccfrag_len);
2147 if (ret)
2148 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002149
2150 p = bufpoi + data_col_addr;
2151 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
2152 int stat;
2153
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002154 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
2155 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002156 if (stat == -EBADMSG &&
2157 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2158 /* check for empty pages with bitflips */
2159 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002160 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002161 chip->ecc.bytes,
2162 NULL, 0,
2163 chip->ecc.strength);
2164 }
2165
Mike Dunn3f91e942012-04-25 12:06:09 -07002166 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002167 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002168 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01002169 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002170 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2171 }
Alexey Korolev3d459552008-05-15 17:23:18 +01002172 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002173 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01002174}
2175
2176/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002177 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002178 * @mtd: mtd info structure
2179 * @chip: nand chip info structure
2180 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002181 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002182 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002183 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002184 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002185 */
2186static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002187 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002188{
Boris Brezillon846031d2016-02-03 20:11:00 +01002189 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002190 int eccbytes = chip->ecc.bytes;
2191 int eccsteps = chip->ecc.steps;
2192 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002193 uint8_t *ecc_calc = chip->ecc.calc_buf;
2194 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002195 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002196
Boris Brezillon25f815f2017-11-30 18:01:30 +01002197 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2198 if (ret)
2199 return ret;
2200
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002201 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2202 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002203
2204 ret = nand_read_data_op(chip, p, eccsize, false);
2205 if (ret)
2206 return ret;
2207
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002208 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2209 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002210
2211 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2212 if (ret)
2213 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002214
Boris Brezillon846031d2016-02-03 20:11:00 +01002215 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2216 chip->ecc.total);
2217 if (ret)
2218 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002219
2220 eccsteps = chip->ecc.steps;
2221 p = buf;
2222
2223 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2224 int stat;
2225
2226 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002227 if (stat == -EBADMSG &&
2228 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2229 /* check for empty pages with bitflips */
2230 stat = nand_check_erased_ecc_chunk(p, eccsize,
2231 &ecc_code[i], eccbytes,
2232 NULL, 0,
2233 chip->ecc.strength);
2234 }
2235
Mike Dunn3f91e942012-04-25 12:06:09 -07002236 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002237 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002238 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002239 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002240 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2241 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002242 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002243 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002244}
2245
2246/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002247 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07002248 * @mtd: mtd info structure
2249 * @chip: nand chip info structure
2250 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002251 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002252 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002253 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002254 * Hardware ECC for large page chips, require OOB to be read first. For this
2255 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2256 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2257 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2258 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002259 */
2260static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002261 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002262{
Boris Brezillon846031d2016-02-03 20:11:00 +01002263 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002264 int eccbytes = chip->ecc.bytes;
2265 int eccsteps = chip->ecc.steps;
2266 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002267 uint8_t *ecc_code = chip->ecc.code_buf;
2268 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07002269 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002270
2271 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01002272 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2273 if (ret)
2274 return ret;
2275
2276 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2277 if (ret)
2278 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002279
Boris Brezillon846031d2016-02-03 20:11:00 +01002280 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2281 chip->ecc.total);
2282 if (ret)
2283 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002284
2285 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2286 int stat;
2287
2288 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002289
2290 ret = nand_read_data_op(chip, p, eccsize, false);
2291 if (ret)
2292 return ret;
2293
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002294 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2295
2296 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002297 if (stat == -EBADMSG &&
2298 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2299 /* check for empty pages with bitflips */
2300 stat = nand_check_erased_ecc_chunk(p, eccsize,
2301 &ecc_code[i], eccbytes,
2302 NULL, 0,
2303 chip->ecc.strength);
2304 }
2305
Mike Dunn3f91e942012-04-25 12:06:09 -07002306 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002307 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002308 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002309 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002310 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2311 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002312 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002313 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002314}
2315
2316/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002317 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07002318 * @mtd: mtd info structure
2319 * @chip: nand chip info structure
2320 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002321 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002322 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002323 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002324 * The hw generator calculates the error syndrome automatically. Therefore we
2325 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002326 */
2327static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002328 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002329{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002330 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002331 int eccbytes = chip->ecc.bytes;
2332 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002333 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002334 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002335 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07002336 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002337
Boris Brezillon25f815f2017-11-30 18:01:30 +01002338 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2339 if (ret)
2340 return ret;
2341
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002342 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2343 int stat;
2344
2345 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002346
2347 ret = nand_read_data_op(chip, p, eccsize, false);
2348 if (ret)
2349 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002350
2351 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002352 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2353 false);
2354 if (ret)
2355 return ret;
2356
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002357 oob += chip->ecc.prepad;
2358 }
2359
2360 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002361
2362 ret = nand_read_data_op(chip, oob, eccbytes, false);
2363 if (ret)
2364 return ret;
2365
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002366 stat = chip->ecc.correct(mtd, p, oob, NULL);
2367
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002368 oob += eccbytes;
2369
2370 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002371 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2372 false);
2373 if (ret)
2374 return ret;
2375
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002376 oob += chip->ecc.postpad;
2377 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002378
2379 if (stat == -EBADMSG &&
2380 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2381 /* check for empty pages with bitflips */
2382 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2383 oob - eccpadbytes,
2384 eccpadbytes,
2385 NULL, 0,
2386 chip->ecc.strength);
2387 }
2388
2389 if (stat < 0) {
2390 mtd->ecc_stats.failed++;
2391 } else {
2392 mtd->ecc_stats.corrected += stat;
2393 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2394 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002395 }
2396
2397 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002398 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002399 if (i) {
2400 ret = nand_read_data_op(chip, oob, i, false);
2401 if (ret)
2402 return ret;
2403 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002404
Mike Dunn3f91e942012-04-25 12:06:09 -07002405 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002406}
2407
2408/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002409 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01002410 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002411 * @oob: oob destination address
2412 * @ops: oob ops structure
2413 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002414 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002415static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03002416 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002417{
Boris Brezillon846031d2016-02-03 20:11:00 +01002418 struct nand_chip *chip = mtd_to_nand(mtd);
2419 int ret;
2420
Florian Fainellif8ac0412010-09-07 13:23:43 +02002421 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002422
Brian Norris0612b9d2011-08-30 18:45:40 -07002423 case MTD_OPS_PLACE_OOB:
2424 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002425 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2426 return oob + len;
2427
Boris Brezillon846031d2016-02-03 20:11:00 +01002428 case MTD_OPS_AUTO_OOB:
2429 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
2430 ops->ooboffs, len);
2431 BUG_ON(ret);
2432 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002433
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002434 default:
2435 BUG();
2436 }
2437 return NULL;
2438}
2439
2440/**
Brian Norrisba84fb52014-01-03 15:13:33 -08002441 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
2442 * @mtd: MTD device structure
2443 * @retry_mode: the retry mode to use
2444 *
2445 * Some vendors supply a special command to shift the Vt threshold, to be used
2446 * when there are too many bitflips in a page (i.e., ECC error). After setting
2447 * a new threshold, the host should retry reading the page.
2448 */
2449static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2450{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002451 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08002452
2453 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2454
2455 if (retry_mode >= chip->read_retries)
2456 return -EINVAL;
2457
2458 if (!chip->setup_read_retry)
2459 return -EOPNOTSUPP;
2460
2461 return chip->setup_read_retry(mtd, retry_mode);
2462}
2463
2464/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002465 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002466 * @mtd: MTD device structure
2467 * @from: offset to read from
2468 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00002469 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002470 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00002471 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002472static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2473 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00002474{
Brian Norrise47f3db2012-05-02 10:14:56 -07002475 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002476 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002477 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002478 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03002479 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002480 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02002481
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002482 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002483 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07002484 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08002485 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08002486 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002488 chipnr = (int)(from >> chip->chip_shift);
2489 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002491 realpage = (int)(from >> chip->page_shift);
2492 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002494 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002496 buf = ops->datbuf;
2497 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07002498 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002499
Florian Fainellif8ac0412010-09-07 13:23:43 +02002500 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08002501 unsigned int ecc_failures = mtd->ecc_stats.failed;
2502
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002503 bytes = min(mtd->writesize - col, readlen);
2504 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002505
Kamal Dasu66507c72014-05-01 20:51:19 -04002506 if (!aligned)
2507 use_bufpoi = 1;
2508 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002509 use_bufpoi = !virt_addr_valid(buf) ||
2510 !IS_ALIGNED((unsigned long)buf,
2511 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002512 else
2513 use_bufpoi = 0;
2514
Brian Norris8b6e50c2011-05-25 14:59:01 -07002515 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002516 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002517 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002518
2519 if (use_bufpoi && aligned)
2520 pr_debug("%s: using read bounce buffer for buf@%p\n",
2521 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Brian Norrisba84fb52014-01-03 15:13:33 -08002523read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07002524 /*
2525 * Now read the page into the buffer. Absent an error,
2526 * the read methods return max bitflips per ecc step.
2527 */
Brian Norris0612b9d2011-08-30 18:45:40 -07002528 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07002529 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002530 oob_required,
2531 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002532 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
2533 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002534 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08002535 col, bytes, bufpoi,
2536 page);
David Woodhouse956e9442006-09-25 17:12:39 +01002537 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07002538 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002539 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07002540 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002541 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07002542 /* Invalidate page cache */
2543 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01002544 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07002545 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002546
2547 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04002548 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002549 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08002550 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07002551 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002552 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07002553 chip->pagebuf_bitflips = ret;
2554 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07002555 /* Invalidate page cache */
2556 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07002557 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002558 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002560
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002561 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002562 int toread = min(oobreadlen, max_oobsize);
2563
2564 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01002565 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002566 oob, ops, toread);
2567 oobreadlen -= toread;
2568 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002569 }
Brian Norris5bc7c332013-03-13 09:51:31 -07002570
2571 if (chip->options & NAND_NEED_READRDY) {
2572 /* Apply delay or wait for ready/busy pin */
2573 if (!chip->dev_ready)
2574 udelay(chip->chip_delay);
2575 else
2576 nand_wait_ready(mtd);
2577 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002578
Brian Norrisba84fb52014-01-03 15:13:33 -08002579 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002580 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002581 retry_mode++;
2582 ret = nand_setup_read_retry(mtd,
2583 retry_mode);
2584 if (ret < 0)
2585 break;
2586
2587 /* Reset failures; retry */
2588 mtd->ecc_stats.failed = ecc_failures;
2589 goto read_retry;
2590 } else {
2591 /* No more retry modes; real failure */
2592 ecc_fail = true;
2593 }
2594 }
2595
2596 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09002597 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002598 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09002599 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002600 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002601 max_bitflips = max_t(unsigned int, max_bitflips,
2602 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002605 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002606
Brian Norrisba84fb52014-01-03 15:13:33 -08002607 /* Reset to retry mode 0 */
2608 if (retry_mode) {
2609 ret = nand_setup_read_retry(mtd, 0);
2610 if (ret < 0)
2611 break;
2612 retry_mode = 0;
2613 }
2614
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002615 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002616 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Brian Norris8b6e50c2011-05-25 14:59:01 -07002618 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 col = 0;
2620 /* Increment page address */
2621 realpage++;
2622
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002623 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 /* Check, if we cross a chip boundary */
2625 if (!page) {
2626 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002627 chip->select_chip(mtd, -1);
2628 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002631 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002633 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002634 if (oob)
2635 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Mike Dunn3f91e942012-04-25 12:06:09 -07002637 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002638 return ret;
2639
Brian Norrisb72f3df2013-12-03 11:04:14 -08002640 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002641 return -EBADMSG;
2642
Mike Dunnedbc45402012-04-25 12:06:11 -07002643 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002644}
2645
2646/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002647 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002648 * @mtd: MTD device structure
2649 * @from: offset to read from
2650 * @len: number of bytes to read
2651 * @retlen: pointer to variable to store the number of read bytes
2652 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002653 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002654 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002655 */
2656static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2657 size_t *retlen, uint8_t *buf)
2658{
Brian Norris4a89ff82011-08-30 18:45:45 -07002659 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002660 int ret;
2661
Huang Shijie6a8214a2012-11-19 14:43:30 +08002662 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002663 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002664 ops.len = len;
2665 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002666 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002667 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002668 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002669 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002670 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
2673/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002674 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002675 * @mtd: mtd info structure
2676 * @chip: nand chip info structure
2677 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002678 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002679int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002680{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002681 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002682}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002683EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002684
2685/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002686 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002687 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002688 * @mtd: mtd info structure
2689 * @chip: nand chip info structure
2690 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002691 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002692int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2693 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002694{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002695 int length = mtd->oobsize;
2696 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2697 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002698 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002699 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002700
Boris Brezillon97d90da2017-11-30 18:01:29 +01002701 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
2702 if (ret)
2703 return ret;
2704
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002705 for (i = 0; i < chip->ecc.steps; i++) {
2706 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002707 int ret;
2708
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002709 pos = eccsize + i * (eccsize + chunk);
2710 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002711 ret = nand_change_read_column_op(chip, pos,
2712 NULL, 0,
2713 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002714 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01002715 ret = nand_read_page_op(chip, page, pos, NULL,
2716 0);
2717
2718 if (ret)
2719 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002720 } else
2721 sndrnd = 1;
2722 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002723
2724 ret = nand_read_data_op(chip, bufpoi, toread, false);
2725 if (ret)
2726 return ret;
2727
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002728 bufpoi += toread;
2729 length -= toread;
2730 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002731 if (length > 0) {
2732 ret = nand_read_data_op(chip, bufpoi, length, false);
2733 if (ret)
2734 return ret;
2735 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002736
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002737 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002738}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002739EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002740
2741/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002742 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002743 * @mtd: mtd info structure
2744 * @chip: nand chip info structure
2745 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002746 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002747int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002748{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002749 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
2750 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002751}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002752EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002753
2754/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002755 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002756 * with syndrome - only for large page flash
2757 * @mtd: mtd info structure
2758 * @chip: nand chip info structure
2759 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002760 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002761int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2762 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002763{
2764 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2765 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002766 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002767 const uint8_t *bufpoi = chip->oob_poi;
2768
2769 /*
2770 * data-ecc-data-ecc ... ecc-oob
2771 * or
2772 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2773 */
2774 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2775 pos = steps * (eccsize + chunk);
2776 steps = 0;
2777 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002778 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002779
Boris Brezillon97d90da2017-11-30 18:01:29 +01002780 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
2781 if (ret)
2782 return ret;
2783
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002784 for (i = 0; i < steps; i++) {
2785 if (sndcmd) {
2786 if (mtd->writesize <= 512) {
2787 uint32_t fill = 0xFFFFFFFF;
2788
2789 len = eccsize;
2790 while (len > 0) {
2791 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002792
2793 ret = nand_write_data_op(chip, &fill,
2794 num, false);
2795 if (ret)
2796 return ret;
2797
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002798 len -= num;
2799 }
2800 } else {
2801 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002802 ret = nand_change_write_column_op(chip, pos,
2803 NULL, 0,
2804 false);
2805 if (ret)
2806 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002807 }
2808 } else
2809 sndcmd = 1;
2810 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002811
2812 ret = nand_write_data_op(chip, bufpoi, len, false);
2813 if (ret)
2814 return ret;
2815
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002816 bufpoi += len;
2817 length -= len;
2818 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002819 if (length > 0) {
2820 ret = nand_write_data_op(chip, bufpoi, length, false);
2821 if (ret)
2822 return ret;
2823 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002824
Boris Brezillon97d90da2017-11-30 18:01:29 +01002825 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002826}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002827EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002828
2829/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002830 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002831 * @mtd: MTD device structure
2832 * @from: offset to read from
2833 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002835 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002837static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2838 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Brian Norrisc00a0992012-05-01 17:12:54 -07002840 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002841 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002842 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002843 int readlen = ops->ooblen;
2844 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002845 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002846 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Brian Norris289c0522011-07-19 10:06:09 -07002848 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302849 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Brian Norris041e4572011-06-23 16:45:24 -07002851 stats = mtd->ecc_stats;
2852
Boris BREZILLON29f10582016-03-07 10:46:52 +01002853 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002854
2855 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002856 pr_debug("%s: attempt to start read outside oob\n",
2857 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002858 return -EINVAL;
2859 }
2860
2861 /* Do not allow reads past end of device */
2862 if (unlikely(from >= mtd->size ||
2863 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2864 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002865 pr_debug("%s: attempt to read beyond end of device\n",
2866 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002867 return -EINVAL;
2868 }
Vitaly Wool70145682006-11-03 18:20:38 +03002869
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002870 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002871 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002873 /* Shift to get page */
2874 realpage = (int)(from >> chip->page_shift);
2875 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Florian Fainellif8ac0412010-09-07 13:23:43 +02002877 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002878 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002879 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002880 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002881 ret = chip->ecc.read_oob(mtd, chip, page);
2882
2883 if (ret < 0)
2884 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002885
2886 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002887 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002888
Brian Norris5bc7c332013-03-13 09:51:31 -07002889 if (chip->options & NAND_NEED_READRDY) {
2890 /* Apply delay or wait for ready/busy pin */
2891 if (!chip->dev_ready)
2892 udelay(chip->chip_delay);
2893 else
2894 nand_wait_ready(mtd);
2895 }
2896
Vitaly Wool70145682006-11-03 18:20:38 +03002897 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002898 if (!readlen)
2899 break;
2900
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002901 /* Increment page address */
2902 realpage++;
2903
2904 page = realpage & chip->pagemask;
2905 /* Check, if we cross a chip boundary */
2906 if (!page) {
2907 chipnr++;
2908 chip->select_chip(mtd, -1);
2909 chip->select_chip(mtd, chipnr);
2910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002912 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002914 ops->oobretlen = ops->ooblen - readlen;
2915
2916 if (ret < 0)
2917 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002918
2919 if (mtd->ecc_stats.failed - stats.failed)
2920 return -EBADMSG;
2921
2922 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923}
2924
2925/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002926 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002927 * @mtd: MTD device structure
2928 * @from: offset to read from
2929 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002931 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002933static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2934 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002936 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002937
2938 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002941 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002942 pr_debug("%s: attempt to read beyond end of device\n",
2943 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return -EINVAL;
2945 }
2946
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002947 if (ops->mode != MTD_OPS_PLACE_OOB &&
2948 ops->mode != MTD_OPS_AUTO_OOB &&
2949 ops->mode != MTD_OPS_RAW)
2950 return -ENOTSUPP;
2951
Huang Shijie6a8214a2012-11-19 14:43:30 +08002952 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002954 if (!ops->datbuf)
2955 ret = nand_do_read_oob(mtd, from, ops);
2956 else
2957 ret = nand_do_read_ops(mtd, from, ops);
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002960 return ret;
2961}
2962
2963
2964/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002965 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002966 * @mtd: mtd info structure
2967 * @chip: nand chip info structure
2968 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002969 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002970 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002971 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002972 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002973 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002974int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2975 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002976{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002977 int ret;
2978
Boris Brezillon25f815f2017-11-30 18:01:30 +01002979 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002980 if (ret)
2981 return ret;
2982
2983 if (oob_required) {
2984 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
2985 false);
2986 if (ret)
2987 return ret;
2988 }
Josh Wufdbad98d2012-06-25 18:07:45 +08002989
Boris Brezillon25f815f2017-11-30 18:01:30 +01002990 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002992EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002994/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002995 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002996 * @mtd: mtd info structure
2997 * @chip: nand chip info structure
2998 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002999 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003000 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003001 *
3002 * We need a special oob layout and handling even when ECC isn't checked.
3003 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003004static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003005 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003006 const uint8_t *buf, int oob_required,
3007 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003008{
3009 int eccsize = chip->ecc.size;
3010 int eccbytes = chip->ecc.bytes;
3011 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003012 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003013
Boris Brezillon25f815f2017-11-30 18:01:30 +01003014 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3015 if (ret)
3016 return ret;
3017
David Brownell52ff49d2009-03-04 12:01:36 -08003018 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003019 ret = nand_write_data_op(chip, buf, eccsize, false);
3020 if (ret)
3021 return ret;
3022
David Brownell52ff49d2009-03-04 12:01:36 -08003023 buf += eccsize;
3024
3025 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003026 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3027 false);
3028 if (ret)
3029 return ret;
3030
David Brownell52ff49d2009-03-04 12:01:36 -08003031 oob += chip->ecc.prepad;
3032 }
3033
Boris Brezillon97d90da2017-11-30 18:01:29 +01003034 ret = nand_write_data_op(chip, oob, eccbytes, false);
3035 if (ret)
3036 return ret;
3037
David Brownell52ff49d2009-03-04 12:01:36 -08003038 oob += eccbytes;
3039
3040 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003041 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3042 false);
3043 if (ret)
3044 return ret;
3045
David Brownell52ff49d2009-03-04 12:01:36 -08003046 oob += chip->ecc.postpad;
3047 }
3048 }
3049
3050 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003051 if (size) {
3052 ret = nand_write_data_op(chip, oob, size, false);
3053 if (ret)
3054 return ret;
3055 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003056
Boris Brezillon25f815f2017-11-30 18:01:30 +01003057 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003058}
3059/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003060 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003061 * @mtd: mtd info structure
3062 * @chip: nand chip info structure
3063 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003064 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003065 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003066 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003067static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003068 const uint8_t *buf, int oob_required,
3069 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003070{
Boris Brezillon846031d2016-02-03 20:11:00 +01003071 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003072 int eccbytes = chip->ecc.bytes;
3073 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003074 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003075 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003076
Brian Norris7854d3f2011-06-23 14:12:08 -07003077 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003078 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3079 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003080
Boris Brezillon846031d2016-02-03 20:11:00 +01003081 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3082 chip->ecc.total);
3083 if (ret)
3084 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003085
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003086 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003087}
3088
3089/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003090 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003091 * @mtd: mtd info structure
3092 * @chip: nand chip info structure
3093 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003094 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003095 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003096 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003097static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003098 const uint8_t *buf, int oob_required,
3099 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003100{
Boris Brezillon846031d2016-02-03 20:11:00 +01003101 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003102 int eccbytes = chip->ecc.bytes;
3103 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003104 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003105 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003106
Boris Brezillon25f815f2017-11-30 18:01:30 +01003107 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3108 if (ret)
3109 return ret;
3110
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003111 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3112 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003113
3114 ret = nand_write_data_op(chip, p, eccsize, false);
3115 if (ret)
3116 return ret;
3117
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003118 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3119 }
3120
Boris Brezillon846031d2016-02-03 20:11:00 +01003121 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3122 chip->ecc.total);
3123 if (ret)
3124 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003125
Boris Brezillon97d90da2017-11-30 18:01:29 +01003126 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3127 if (ret)
3128 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003129
Boris Brezillon25f815f2017-11-30 18:01:30 +01003130 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003131}
3132
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303133
3134/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08003135 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303136 * @mtd: mtd info structure
3137 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07003138 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303139 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07003140 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303141 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003142 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303143 */
3144static int nand_write_subpage_hwecc(struct mtd_info *mtd,
3145 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07003146 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003147 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303148{
3149 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003150 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303151 int ecc_size = chip->ecc.size;
3152 int ecc_bytes = chip->ecc.bytes;
3153 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303154 uint32_t start_step = offset / ecc_size;
3155 uint32_t end_step = (offset + data_len - 1) / ecc_size;
3156 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01003157 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303158
Boris Brezillon25f815f2017-11-30 18:01:30 +01003159 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3160 if (ret)
3161 return ret;
3162
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303163 for (step = 0; step < ecc_steps; step++) {
3164 /* configure controller for WRITE access */
3165 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
3166
3167 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003168 ret = nand_write_data_op(chip, buf, ecc_size, false);
3169 if (ret)
3170 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303171
3172 /* mask ECC of un-touched subpages by padding 0xFF */
3173 if ((step < start_step) || (step > end_step))
3174 memset(ecc_calc, 0xff, ecc_bytes);
3175 else
Brian Norrisd6a950802013-08-08 17:16:36 -07003176 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303177
3178 /* mask OOB of un-touched subpages by padding 0xFF */
3179 /* if oob_required, preserve OOB metadata of written subpage */
3180 if (!oob_required || (step < start_step) || (step > end_step))
3181 memset(oob_buf, 0xff, oob_bytes);
3182
Brian Norrisd6a950802013-08-08 17:16:36 -07003183 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303184 ecc_calc += ecc_bytes;
3185 oob_buf += oob_bytes;
3186 }
3187
3188 /* copy calculated ECC for whole page to chip->buffer->oob */
3189 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003190 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01003191 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3192 chip->ecc.total);
3193 if (ret)
3194 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303195
3196 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003197 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3198 if (ret)
3199 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303200
Boris Brezillon25f815f2017-11-30 18:01:30 +01003201 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303202}
3203
3204
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003205/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003206 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003207 * @mtd: mtd info structure
3208 * @chip: nand chip info structure
3209 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003210 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003211 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003212 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003213 * The hw generator calculates the error syndrome automatically. Therefore we
3214 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003215 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003216static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003217 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003218 const uint8_t *buf, int oob_required,
3219 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003220{
3221 int i, eccsize = chip->ecc.size;
3222 int eccbytes = chip->ecc.bytes;
3223 int eccsteps = chip->ecc.steps;
3224 const uint8_t *p = buf;
3225 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003226 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003227
Boris Brezillon25f815f2017-11-30 18:01:30 +01003228 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3229 if (ret)
3230 return ret;
3231
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003232 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003233 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003234
3235 ret = nand_write_data_op(chip, p, eccsize, false);
3236 if (ret)
3237 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003238
3239 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003240 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3241 false);
3242 if (ret)
3243 return ret;
3244
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003245 oob += chip->ecc.prepad;
3246 }
3247
3248 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003249
3250 ret = nand_write_data_op(chip, oob, eccbytes, false);
3251 if (ret)
3252 return ret;
3253
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003254 oob += eccbytes;
3255
3256 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003257 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3258 false);
3259 if (ret)
3260 return ret;
3261
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003262 oob += chip->ecc.postpad;
3263 }
3264 }
3265
3266 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003267 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003268 if (i) {
3269 ret = nand_write_data_op(chip, oob, i, false);
3270 if (ret)
3271 return ret;
3272 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003273
Boris Brezillon25f815f2017-11-30 18:01:30 +01003274 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003275}
3276
3277/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003278 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07003279 * @mtd: MTD device structure
3280 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303281 * @offset: address offset within the page
3282 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07003283 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07003284 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07003285 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003286 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003287 */
3288static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303289 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02003290 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003291{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303292 int status, subpage;
3293
3294 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3295 chip->ecc.write_subpage)
3296 subpage = offset || (data_len < mtd->writesize);
3297 else
3298 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003299
David Woodhouse956e9442006-09-25 17:12:39 +01003300 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303301 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003302 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303303 else if (subpage)
3304 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003305 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003306 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003307 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
3308 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08003309
3310 if (status < 0)
3311 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003312
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003313 return 0;
3314}
3315
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003316/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003317 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003318 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003319 * @oob: oob data buffer
3320 * @len: oob data write length
3321 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003322 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003323static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3324 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003325{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003326 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01003327 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003328
3329 /*
3330 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3331 * data from a previous OOB read.
3332 */
3333 memset(chip->oob_poi, 0xff, mtd->oobsize);
3334
Florian Fainellif8ac0412010-09-07 13:23:43 +02003335 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003336
Brian Norris0612b9d2011-08-30 18:45:40 -07003337 case MTD_OPS_PLACE_OOB:
3338 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003339 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3340 return oob + len;
3341
Boris Brezillon846031d2016-02-03 20:11:00 +01003342 case MTD_OPS_AUTO_OOB:
3343 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
3344 ops->ooboffs, len);
3345 BUG_ON(ret);
3346 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003347
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003348 default:
3349 BUG();
3350 }
3351 return NULL;
3352}
3353
Florian Fainellif8ac0412010-09-07 13:23:43 +02003354#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003355
3356/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003357 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003358 * @mtd: MTD device structure
3359 * @to: offset to write to
3360 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003361 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003362 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003363 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003364static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3365 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003366{
Corentin Labbe73600b62017-09-02 10:49:38 +02003367 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003368 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003369 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02003370
3371 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003372 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003373
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003374 uint8_t *oob = ops->oobbuf;
3375 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303376 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07003377 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003378
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003379 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003380 if (!writelen)
3381 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003382
Brian Norris8b6e50c2011-05-25 14:59:01 -07003383 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003384 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003385 pr_notice("%s: attempt to write non page aligned data\n",
3386 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003387 return -EINVAL;
3388 }
3389
Thomas Gleixner29072b92006-09-28 15:38:36 +02003390 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003391
Thomas Gleixner6a930962006-06-28 00:11:45 +02003392 chipnr = (int)(to >> chip->chip_shift);
3393 chip->select_chip(mtd, chipnr);
3394
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003395 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003396 if (nand_check_wp(mtd)) {
3397 ret = -EIO;
3398 goto err_out;
3399 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003400
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003401 realpage = (int)(to >> chip->page_shift);
3402 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003403
3404 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07003405 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3406 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003407 chip->pagebuf = -1;
3408
Maxim Levitsky782ce792010-02-22 20:39:36 +02003409 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003410 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3411 ret = -EINVAL;
3412 goto err_out;
3413 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02003414
Florian Fainellif8ac0412010-09-07 13:23:43 +02003415 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003416 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003417 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003418 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02003419 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02003420
Kamal Dasu66507c72014-05-01 20:51:19 -04003421 if (part_pagewr)
3422 use_bufpoi = 1;
3423 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003424 use_bufpoi = !virt_addr_valid(buf) ||
3425 !IS_ALIGNED((unsigned long)buf,
3426 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003427 else
3428 use_bufpoi = 0;
3429
3430 /* Partial page write?, or need to use bounce buffer */
3431 if (use_bufpoi) {
3432 pr_debug("%s: using write bounce buffer for buf@%p\n",
3433 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04003434 if (part_pagewr)
3435 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02003436 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003437 memset(chip->data_buf, 0xff, mtd->writesize);
3438 memcpy(&chip->data_buf[column], buf, bytes);
3439 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003440 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003441
Maxim Levitsky782ce792010-02-22 20:39:36 +02003442 if (unlikely(oob)) {
3443 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003444 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003445 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003446 } else {
3447 /* We still need to erase leftover OOB data */
3448 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003449 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003450
3451 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02003452 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003453 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003454 if (ret)
3455 break;
3456
3457 writelen -= bytes;
3458 if (!writelen)
3459 break;
3460
Thomas Gleixner29072b92006-09-28 15:38:36 +02003461 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003462 buf += bytes;
3463 realpage++;
3464
3465 page = realpage & chip->pagemask;
3466 /* Check, if we cross a chip boundary */
3467 if (!page) {
3468 chipnr++;
3469 chip->select_chip(mtd, -1);
3470 chip->select_chip(mtd, chipnr);
3471 }
3472 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003473
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003474 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03003475 if (unlikely(oob))
3476 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003477
3478err_out:
3479 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003480 return ret;
3481}
3482
3483/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003484 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003485 * @mtd: MTD device structure
3486 * @to: offset to write to
3487 * @len: number of bytes to write
3488 * @retlen: pointer to variable to store the number of written bytes
3489 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003490 *
3491 * NAND write with ECC. Used when performing writes in interrupt context, this
3492 * may for example be called by mtdoops when writing an oops while in panic.
3493 */
3494static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3495 size_t *retlen, const uint8_t *buf)
3496{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003497 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05003498 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07003499 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003500 int ret;
3501
Brian Norris8b6e50c2011-05-25 14:59:01 -07003502 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003503 panic_nand_get_device(chip, mtd, FL_WRITING);
3504
Brent Taylor30863e382017-10-30 22:32:45 -05003505 chip->select_chip(mtd, chipnr);
3506
3507 /* Wait for the device to get ready */
3508 panic_nand_wait(mtd, chip, 400);
3509
Brian Norris0ec56dc2015-02-28 02:02:30 -08003510 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07003511 ops.len = len;
3512 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08003513 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003514
Brian Norris4a89ff82011-08-30 18:45:45 -07003515 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003516
Brian Norris4a89ff82011-08-30 18:45:45 -07003517 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003518 return ret;
3519}
3520
3521/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003522 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003523 * @mtd: MTD device structure
3524 * @to: offset to write to
3525 * @len: number of bytes to write
3526 * @retlen: pointer to variable to store the number of written bytes
3527 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003529 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003531static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003532 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
Brian Norris4a89ff82011-08-30 18:45:45 -07003534 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003535 int ret;
3536
Huang Shijie6a8214a2012-11-19 14:43:30 +08003537 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08003538 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07003539 ops.len = len;
3540 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08003541 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07003542 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07003543 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003544 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003545 return ret;
3546}
3547
3548/**
3549 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003550 * @mtd: MTD device structure
3551 * @to: offset to write to
3552 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003553 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003554 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003555 */
3556static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3557 struct mtd_oob_ops *ops)
3558{
Adrian Hunter03736152007-01-31 17:58:29 +02003559 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003560 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Brian Norris289c0522011-07-19 10:06:09 -07003562 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303563 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Boris BREZILLON29f10582016-03-07 10:46:52 +01003565 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02003568 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07003569 pr_debug("%s: attempt to write past end of page\n",
3570 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return -EINVAL;
3572 }
3573
Adrian Hunter03736152007-01-31 17:58:29 +02003574 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003575 pr_debug("%s: attempt to start write outside oob\n",
3576 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003577 return -EINVAL;
3578 }
3579
Jason Liu775adc3d42011-02-25 13:06:18 +08003580 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02003581 if (unlikely(to >= mtd->size ||
3582 ops->ooboffs + ops->ooblen >
3583 ((mtd->size >> chip->page_shift) -
3584 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003585 pr_debug("%s: attempt to write beyond end of device\n",
3586 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003587 return -EINVAL;
3588 }
3589
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003590 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003591
3592 /*
3593 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3594 * of my DiskOnChip 2000 test units) will clear the whole data page too
3595 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3596 * it in the doc2000 driver in August 1999. dwmw2.
3597 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003598 nand_reset(chip, chipnr);
3599
3600 chip->select_chip(mtd, chipnr);
3601
3602 /* Shift to get page */
3603 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
3605 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003606 if (nand_check_wp(mtd)) {
3607 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003608 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003609 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003610
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003612 if (page == chip->pagebuf)
3613 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003615 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07003616
Brian Norris0612b9d2011-08-30 18:45:40 -07003617 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07003618 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3619 else
3620 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003621
Huang Shijieb0bb6902012-11-19 14:43:29 +08003622 chip->select_chip(mtd, -1);
3623
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003624 if (status)
3625 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Vitaly Wool70145682006-11-03 18:20:38 +03003627 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003629 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003630}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003632/**
3633 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003634 * @mtd: MTD device structure
3635 * @to: offset to write to
3636 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003637 */
3638static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3639 struct mtd_oob_ops *ops)
3640{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003641 int ret = -ENOTSUPP;
3642
3643 ops->retlen = 0;
3644
3645 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003646 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003647 pr_debug("%s: attempt to write beyond end of device\n",
3648 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003649 return -EINVAL;
3650 }
3651
Huang Shijie6a8214a2012-11-19 14:43:30 +08003652 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003653
Florian Fainellif8ac0412010-09-07 13:23:43 +02003654 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003655 case MTD_OPS_PLACE_OOB:
3656 case MTD_OPS_AUTO_OOB:
3657 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003658 break;
3659
3660 default:
3661 goto out;
3662 }
3663
3664 if (!ops->datbuf)
3665 ret = nand_do_write_oob(mtd, to, ops);
3666 else
3667 ret = nand_do_write_ops(mtd, to, ops);
3668
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003669out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003670 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return ret;
3672}
3673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674/**
Brian Norris49c50b92014-05-06 16:02:19 -07003675 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003676 * @mtd: MTD device structure
3677 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 *
Brian Norris49c50b92014-05-06 16:02:19 -07003679 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 */
Brian Norris49c50b92014-05-06 16:02:19 -07003681static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003683 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003684 unsigned int eraseblock;
Miquel Raynaleb945552017-11-30 18:01:28 +01003685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003687 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07003688
Boris Brezillon97d90da2017-11-30 18:01:29 +01003689 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690}
3691
3692/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003694 * @mtd: MTD device structure
3695 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003697 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003699static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
David Woodhousee0c7d762006-05-13 18:07:53 +01003701 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003705 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003706 * @mtd: MTD device structure
3707 * @instr: erase instruction
3708 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003710 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003712int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3713 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
Adrian Hunter69423d92008-12-10 13:37:21 +00003715 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003716 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003717 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Brian Norris289c0522011-07-19 10:06:09 -07003719 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3720 __func__, (unsigned long long)instr->addr,
3721 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303723 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003727 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
3729 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003730 page = (int)(instr->addr >> chip->page_shift);
3731 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
3733 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003734 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
3736 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003737 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 /* Check, if it is write protected */
3740 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003741 pr_debug("%s: device is write protected!\n",
3742 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 instr->state = MTD_ERASE_FAILED;
3744 goto erase_exit;
3745 }
3746
3747 /* Loop through the pages */
3748 len = instr->len;
3749
3750 instr->state = MTD_ERASING;
3751
3752 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003753 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003754 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303755 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003756 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3757 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 instr->state = MTD_ERASE_FAILED;
3759 goto erase_exit;
3760 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003761
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003762 /*
3763 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003764 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003765 */
3766 if (page <= chip->pagebuf && chip->pagebuf <
3767 (page + pages_per_block))
3768 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Brian Norris49c50b92014-05-06 16:02:19 -07003770 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
3772 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01003773 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07003774 pr_debug("%s: failed erase, page 0x%08x\n",
3775 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003777 instr->fail_addr =
3778 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 goto erase_exit;
3780 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003783 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 page += pages_per_block;
3785
3786 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003787 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003789 chip->select_chip(mtd, -1);
3790 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 }
3792 }
3793 instr->state = MTD_ERASE_DONE;
3794
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003795erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
3797 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003800 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 nand_release_device(mtd);
3802
David Woodhouse49defc02007-10-06 15:01:59 -04003803 /* Do call back function */
3804 if (!ret)
3805 mtd_erase_callback(instr);
3806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 /* Return more or less happy */
3808 return ret;
3809}
3810
3811/**
3812 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003813 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003815 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003817static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
Brian Norris289c0522011-07-19 10:06:09 -07003819 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
3821 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003822 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003824 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825}
3826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003828 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003829 * @mtd: MTD device structure
3830 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003832static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303834 struct nand_chip *chip = mtd_to_nand(mtd);
3835 int chipnr = (int)(offs >> chip->chip_shift);
3836 int ret;
3837
3838 /* Select the NAND device */
3839 nand_get_device(mtd, FL_READING);
3840 chip->select_chip(mtd, chipnr);
3841
3842 ret = nand_block_checkbad(mtd, offs, 0);
3843
3844 chip->select_chip(mtd, -1);
3845 nand_release_device(mtd);
3846
3847 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848}
3849
3850/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003851 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003852 * @mtd: MTD device structure
3853 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003855static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 int ret;
3858
Florian Fainellif8ac0412010-09-07 13:23:43 +02003859 ret = nand_block_isbad(mtd, ofs);
3860 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003861 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 if (ret > 0)
3863 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003864 return ret;
3865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Brian Norris5a0edb22013-07-30 17:52:58 -07003867 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868}
3869
3870/**
Zach Brown56718422017-01-10 13:30:20 -06003871 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3872 * @mtd: MTD device structure
3873 * @ofs: offset relative to mtd start
3874 * @len: length of mtd
3875 */
3876static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3877{
3878 struct nand_chip *chip = mtd_to_nand(mtd);
3879 u32 part_start_block;
3880 u32 part_end_block;
3881 u32 part_start_die;
3882 u32 part_end_die;
3883
3884 /*
3885 * max_bb_per_die and blocks_per_die used to determine
3886 * the maximum bad block count.
3887 */
3888 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3889 return -ENOTSUPP;
3890
3891 /* Get the start and end of the partition in erase blocks. */
3892 part_start_block = mtd_div_by_eb(ofs, mtd);
3893 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3894
3895 /* Get the start and end LUNs of the partition. */
3896 part_start_die = part_start_block / chip->blocks_per_die;
3897 part_end_die = part_end_block / chip->blocks_per_die;
3898
3899 /*
3900 * Look up the bad blocks per unit and multiply by the number of units
3901 * that the partition spans.
3902 */
3903 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3904}
3905
3906/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003907 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3908 * @mtd: MTD device structure
3909 * @chip: nand chip info structure
3910 * @addr: feature address.
3911 * @subfeature_param: the subfeature parameters, a four bytes array.
3912 */
3913static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3914 int addr, uint8_t *subfeature_param)
3915{
David Mosbergerd914c932013-05-29 15:30:13 +03003916 if (!chip->onfi_version ||
3917 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3918 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003919 return -EINVAL;
3920
Boris Brezillon97d90da2017-11-30 18:01:29 +01003921 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003922}
3923
3924/**
3925 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3926 * @mtd: MTD device structure
3927 * @chip: nand chip info structure
3928 * @addr: feature address.
3929 * @subfeature_param: the subfeature parameters, a four bytes array.
3930 */
3931static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3932 int addr, uint8_t *subfeature_param)
3933{
David Mosbergerd914c932013-05-29 15:30:13 +03003934 if (!chip->onfi_version ||
3935 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3936 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003937 return -EINVAL;
3938
Boris Brezillon97d90da2017-11-30 18:01:29 +01003939 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003940}
3941
3942/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003943 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3944 * -ENOTSUPP
3945 * @mtd: MTD device structure
3946 * @chip: nand chip info structure
3947 * @addr: feature address.
3948 * @subfeature_param: the subfeature parameters, a four bytes array.
3949 *
3950 * Should be used by NAND controller drivers that do not support the SET/GET
3951 * FEATURES operations.
3952 */
3953int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3954 struct nand_chip *chip, int addr,
3955 u8 *subfeature_param)
3956{
3957 return -ENOTSUPP;
3958}
3959EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3960
3961/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003962 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003963 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003964 */
3965static int nand_suspend(struct mtd_info *mtd)
3966{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003967 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003968}
3969
3970/**
3971 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003972 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003973 */
3974static void nand_resume(struct mtd_info *mtd)
3975{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003976 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003977
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003978 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003979 nand_release_device(mtd);
3980 else
Brian Norrisd0370212011-07-19 10:06:08 -07003981 pr_err("%s called for a chip which is not in suspended state\n",
3982 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003983}
3984
Scott Branden72ea4032014-11-20 11:18:05 -08003985/**
3986 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3987 * prevent further operations
3988 * @mtd: MTD device structure
3989 */
3990static void nand_shutdown(struct mtd_info *mtd)
3991{
Brian Norris9ca641b2015-11-09 16:37:28 -08003992 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003993}
3994
Brian Norris8b6e50c2011-05-25 14:59:01 -07003995/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003996static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003997{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003998 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004001 if (!chip->chip_delay)
4002 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
4004 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004005 if (chip->cmdfunc == NULL)
4006 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
4008 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004009 if (chip->waitfunc == NULL)
4010 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004012 if (!chip->select_chip)
4013 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004014
Huang Shijie4204ccc2013-08-16 10:10:07 +08004015 /* set for ONFI nand */
4016 if (!chip->onfi_set_features)
4017 chip->onfi_set_features = nand_onfi_set_features;
4018 if (!chip->onfi_get_features)
4019 chip->onfi_get_features = nand_onfi_get_features;
4020
Brian Norris68e80782013-07-18 01:17:02 -07004021 /* If called twice, pointers that depend on busw may need to be reset */
4022 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004023 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4024 if (!chip->read_word)
4025 chip->read_word = nand_read_word;
4026 if (!chip->block_bad)
4027 chip->block_bad = nand_block_bad;
4028 if (!chip->block_markbad)
4029 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004030 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004031 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004032 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4033 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004034 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004035 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004036 if (!chip->scan_bbt)
4037 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004038
4039 if (!chip->controller) {
4040 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02004041 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004042 }
4043
Masahiro Yamada477544c2017-03-30 17:15:05 +09004044 if (!chip->buf_align)
4045 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004046}
4047
Brian Norris8b6e50c2011-05-25 14:59:01 -07004048/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004049static void sanitize_string(uint8_t *s, size_t len)
4050{
4051 ssize_t i;
4052
Brian Norris8b6e50c2011-05-25 14:59:01 -07004053 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004054 s[len - 1] = 0;
4055
Brian Norris8b6e50c2011-05-25 14:59:01 -07004056 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004057 for (i = 0; i < len - 1; i++) {
4058 if (s[i] < ' ' || s[i] > 127)
4059 s[i] = '?';
4060 }
4061
Brian Norris8b6e50c2011-05-25 14:59:01 -07004062 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004063 strim(s);
4064}
4065
4066static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4067{
4068 int i;
4069 while (len--) {
4070 crc ^= *p++ << 8;
4071 for (i = 0; i < 8; i++)
4072 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
4073 }
4074
4075 return crc;
4076}
4077
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004078/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004079static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
4080 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004081{
4082 struct onfi_ext_param_page *ep;
4083 struct onfi_ext_section *s;
4084 struct onfi_ext_ecc_info *ecc;
4085 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004086 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004087 int len;
4088 int i;
4089
4090 len = le16_to_cpu(p->ext_param_page_length) * 16;
4091 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07004092 if (!ep)
4093 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004094
4095 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004096 ret = nand_read_param_page_op(chip, 0, NULL, 0);
4097 if (ret)
4098 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004099
4100 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004101 ret = nand_change_read_column_op(chip,
4102 sizeof(*p) * p->num_of_param_pages,
4103 ep, len, true);
4104 if (ret)
4105 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004106
Boris Brezillon97d90da2017-11-30 18:01:29 +01004107 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004108 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
4109 != le16_to_cpu(ep->crc))) {
4110 pr_debug("fail in the CRC.\n");
4111 goto ext_out;
4112 }
4113
4114 /*
4115 * Check the signature.
4116 * Do not strictly follow the ONFI spec, maybe changed in future.
4117 */
4118 if (strncmp(ep->sig, "EPPS", 4)) {
4119 pr_debug("The signature is invalid.\n");
4120 goto ext_out;
4121 }
4122
4123 /* find the ECC section. */
4124 cursor = (uint8_t *)(ep + 1);
4125 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
4126 s = ep->sections + i;
4127 if (s->type == ONFI_SECTION_TYPE_2)
4128 break;
4129 cursor += s->length * 16;
4130 }
4131 if (i == ONFI_EXT_SECTION_MAX) {
4132 pr_debug("We can not find the ECC section.\n");
4133 goto ext_out;
4134 }
4135
4136 /* get the info we want. */
4137 ecc = (struct onfi_ext_ecc_info *)cursor;
4138
Brian Norris4ae7d222013-09-16 18:20:21 -07004139 if (!ecc->codeword_size) {
4140 pr_debug("Invalid codeword size\n");
4141 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004142 }
4143
Brian Norris4ae7d222013-09-16 18:20:21 -07004144 chip->ecc_strength_ds = ecc->ecc_bits;
4145 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07004146 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004147
4148ext_out:
4149 kfree(ep);
4150 return ret;
4151}
4152
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004153/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004154 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004155 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004156static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004157{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004158 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004159 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004160 char id[4];
4161 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004162
Brian Norris7854d3f2011-06-23 14:12:08 -07004163 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004164 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
4165 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004166 return 0;
4167
Boris Brezillon97d90da2017-11-30 18:01:29 +01004168 ret = nand_read_param_page_op(chip, 0, NULL, 0);
4169 if (ret)
4170 return 0;
4171
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004172 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004173 ret = nand_read_data_op(chip, p, sizeof(*p), true);
4174 if (ret)
4175 return 0;
4176
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004177 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
4178 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004179 break;
4180 }
4181 }
4182
Brian Norrisc7f23a72013-08-13 10:51:55 -07004183 if (i == 3) {
4184 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004185 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07004186 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004187
Brian Norris8b6e50c2011-05-25 14:59:01 -07004188 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004189 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08004190 if (val & (1 << 5))
4191 chip->onfi_version = 23;
4192 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004193 chip->onfi_version = 22;
4194 else if (val & (1 << 3))
4195 chip->onfi_version = 21;
4196 else if (val & (1 << 2))
4197 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08004198 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004199 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08004200
4201 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004202 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08004203 return 0;
4204 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004205
4206 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4207 sanitize_string(p->model, sizeof(p->model));
4208 if (!mtd->name)
4209 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07004210
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004211 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07004212
4213 /*
4214 * pages_per_block and blocks_per_lun may not be a power-of-2 size
4215 * (don't ask me who thought of this...). MTD assumes that these
4216 * dimensions will be power-of-2, so just truncate the remaining area.
4217 */
4218 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4219 mtd->erasesize *= mtd->writesize;
4220
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004221 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07004222
4223 /* See erasesize comment */
4224 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01004225 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08004226 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08004227
Zach Brown34da5f52017-01-10 13:30:21 -06004228 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
4229 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
4230
Huang Shijiee2985fc2013-05-17 11:17:30 +08004231 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02004232 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004233
Huang Shijie10c86ba2013-05-17 11:17:26 +08004234 if (p->ecc_bits != 0xff) {
4235 chip->ecc_strength_ds = p->ecc_bits;
4236 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004237 } else if (chip->onfi_version >= 21 &&
4238 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
4239
4240 /*
4241 * The nand_flash_detect_ext_param_page() uses the
4242 * Change Read Column command which maybe not supported
4243 * by the chip->cmdfunc. So try to update the chip->cmdfunc
4244 * now. We do not replace user supplied command function.
4245 */
4246 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4247 chip->cmdfunc = nand_command_lp;
4248
4249 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004250 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07004251 pr_warn("Failed to detect ONFI extended param page\n");
4252 } else {
4253 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08004254 }
4255
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004256 return 1;
4257}
4258
4259/*
Huang Shijie91361812014-02-21 13:39:40 +08004260 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
4261 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004262static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08004263{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004264 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08004265 struct nand_jedec_params *p = &chip->jedec_params;
4266 struct jedec_ecc_info *ecc;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004267 char id[5];
4268 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08004269
4270 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004271 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
4272 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08004273 return 0;
4274
Boris Brezillon97d90da2017-11-30 18:01:29 +01004275 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
4276 if (ret)
4277 return 0;
4278
Huang Shijie91361812014-02-21 13:39:40 +08004279 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004280 ret = nand_read_data_op(chip, p, sizeof(*p), true);
4281 if (ret)
4282 return 0;
Huang Shijie91361812014-02-21 13:39:40 +08004283
4284 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
4285 le16_to_cpu(p->crc))
4286 break;
4287 }
4288
4289 if (i == 3) {
4290 pr_err("Could not find valid JEDEC parameter page; aborting\n");
4291 return 0;
4292 }
4293
4294 /* Check version */
4295 val = le16_to_cpu(p->revision);
4296 if (val & (1 << 2))
4297 chip->jedec_version = 10;
4298 else if (val & (1 << 1))
4299 chip->jedec_version = 1; /* vendor specific version */
4300
4301 if (!chip->jedec_version) {
4302 pr_info("unsupported JEDEC version: %d\n", val);
4303 return 0;
4304 }
4305
4306 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4307 sanitize_string(p->model, sizeof(p->model));
4308 if (!mtd->name)
4309 mtd->name = p->model;
4310
4311 mtd->writesize = le32_to_cpu(p->byte_per_page);
4312
4313 /* Please reference to the comment for nand_flash_detect_onfi. */
4314 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4315 mtd->erasesize *= mtd->writesize;
4316
4317 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4318
4319 /* Please reference to the comment for nand_flash_detect_onfi. */
4320 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4321 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4322 chip->bits_per_cell = p->bits_per_cell;
4323
4324 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02004325 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08004326
4327 /* ECC info */
4328 ecc = &p->ecc_info[0];
4329
4330 if (ecc->codeword_size >= 9) {
4331 chip->ecc_strength_ds = ecc->ecc_bits;
4332 chip->ecc_step_ds = 1 << ecc->codeword_size;
4333 } else {
4334 pr_warn("Invalid codeword size\n");
4335 }
4336
4337 return 1;
4338}
4339
4340/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07004341 * nand_id_has_period - Check if an ID string has a given wraparound period
4342 * @id_data: the ID string
4343 * @arrlen: the length of the @id_data array
4344 * @period: the period of repitition
4345 *
4346 * Check if an ID string is repeated within a given sequence of bytes at
4347 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08004348 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07004349 * if the repetition has a period of @period; otherwise, returns zero.
4350 */
4351static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4352{
4353 int i, j;
4354 for (i = 0; i < period; i++)
4355 for (j = i + period; j < arrlen; j += period)
4356 if (id_data[i] != id_data[j])
4357 return 0;
4358 return 1;
4359}
4360
4361/*
4362 * nand_id_len - Get the length of an ID string returned by CMD_READID
4363 * @id_data: the ID string
4364 * @arrlen: the length of the @id_data array
4365
4366 * Returns the length of the ID string, according to known wraparound/trailing
4367 * zero patterns. If no pattern exists, returns the length of the array.
4368 */
4369static int nand_id_len(u8 *id_data, int arrlen)
4370{
4371 int last_nonzero, period;
4372
4373 /* Find last non-zero byte */
4374 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4375 if (id_data[last_nonzero])
4376 break;
4377
4378 /* All zeros */
4379 if (last_nonzero < 0)
4380 return 0;
4381
4382 /* Calculate wraparound period */
4383 for (period = 1; period < arrlen; period++)
4384 if (nand_id_has_period(id_data, arrlen, period))
4385 break;
4386
4387 /* There's a repeated pattern */
4388 if (period < arrlen)
4389 return period;
4390
4391 /* There are trailing zeros */
4392 if (last_nonzero < arrlen - 1)
4393 return last_nonzero + 1;
4394
4395 /* No pattern detected */
4396 return arrlen;
4397}
4398
Huang Shijie7db906b2013-09-25 14:58:11 +08004399/* Extract the bits of per cell from the 3rd byte of the extended ID */
4400static int nand_get_bits_per_cell(u8 cellinfo)
4401{
4402 int bits;
4403
4404 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4405 bits >>= NAND_CI_CELLTYPE_SHIFT;
4406 return bits + 1;
4407}
4408
Brian Norrise3b88bd2012-09-24 20:40:52 -07004409/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004410 * Many new NAND share similar device ID codes, which represent the size of the
4411 * chip. The rest of the parameters must be decoded according to generic or
4412 * manufacturer-specific "extended ID" decoding patterns.
4413 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004414void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004415{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004416 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02004417 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004418 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004419 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08004420 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004421 /* The 4th id byte is the important one */
4422 extid = id_data[3];
4423
Boris Brezillon01389b62016-06-08 10:30:18 +02004424 /* Calc pagesize */
4425 mtd->writesize = 1024 << (extid & 0x03);
4426 extid >>= 2;
4427 /* Calc oobsize */
4428 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4429 extid >>= 2;
4430 /* Calc blocksize. Blocksize is multiples of 64KiB */
4431 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4432 extid >>= 2;
4433 /* Get buswidth information */
4434 if (extid & 0x1)
4435 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004436}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004437EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004438
4439/*
Brian Norrisf23a4812012-09-24 20:40:51 -07004440 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4441 * decodes a matching ID table entry and assigns the MTD size parameters for
4442 * the chip.
4443 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004444static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07004445{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004446 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07004447
4448 mtd->erasesize = type->erasesize;
4449 mtd->writesize = type->pagesize;
4450 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07004451
Huang Shijie1c195e92013-09-25 14:58:12 +08004452 /* All legacy ID NAND are small-page, SLC */
4453 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07004454}
4455
4456/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004457 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4458 * heuristic patterns using various detected parameters (e.g., manufacturer,
4459 * page size, cell-type information).
4460 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004461static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07004462{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004463 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004464
4465 /* Set the bad block position */
4466 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4467 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4468 else
4469 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004470}
4471
Huang Shijieec6e87e2013-03-15 11:01:00 +08004472static inline bool is_full_id_nand(struct nand_flash_dev *type)
4473{
4474 return type->id_len;
4475}
4476
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004477static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02004478 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004479{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004480 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02004481 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004482
Huang Shijieec6e87e2013-03-15 11:01:00 +08004483 if (!strncmp(type->id, id_data, type->id_len)) {
4484 mtd->writesize = type->pagesize;
4485 mtd->erasesize = type->erasesize;
4486 mtd->oobsize = type->oobsize;
4487
Huang Shijie7db906b2013-09-25 14:58:11 +08004488 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004489 chip->chipsize = (uint64_t)type->chipsize << 20;
4490 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004491 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4492 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004493 chip->onfi_timing_mode_default =
4494 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004495
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004496 if (!mtd->name)
4497 mtd->name = type->name;
4498
Huang Shijieec6e87e2013-03-15 11:01:00 +08004499 return true;
4500 }
4501 return false;
4502}
4503
Brian Norris7e74c2d2012-09-24 20:40:49 -07004504/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004505 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4506 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4507 * table.
4508 */
4509static void nand_manufacturer_detect(struct nand_chip *chip)
4510{
4511 /*
4512 * Try manufacturer detection if available and use
4513 * nand_decode_ext_id() otherwise.
4514 */
4515 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004516 chip->manufacturer.desc->ops->detect) {
4517 /* The 3rd id byte holds MLC / multichip data */
4518 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004519 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004520 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004521 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004522 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004523}
4524
4525/*
4526 * Manufacturer initialization. This function is called for all NANDs including
4527 * ONFI and JEDEC compliant ones.
4528 * Manufacturer drivers should put all their specific initialization code in
4529 * their ->init() hook.
4530 */
4531static int nand_manufacturer_init(struct nand_chip *chip)
4532{
4533 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4534 !chip->manufacturer.desc->ops->init)
4535 return 0;
4536
4537 return chip->manufacturer.desc->ops->init(chip);
4538}
4539
4540/*
4541 * Manufacturer cleanup. This function is called for all NANDs including
4542 * ONFI and JEDEC compliant ones.
4543 * Manufacturer drivers should put all their specific cleanup code in their
4544 * ->cleanup() hook.
4545 */
4546static void nand_manufacturer_cleanup(struct nand_chip *chip)
4547{
4548 /* Release manufacturer private data */
4549 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4550 chip->manufacturer.desc->ops->cleanup)
4551 chip->manufacturer.desc->ops->cleanup(chip);
4552}
4553
4554/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004555 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004556 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004557static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004558{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004559 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004560 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004561 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004562 u8 *id_data = chip->id.data;
4563 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Karl Beldanef89a882008-09-15 14:37:29 +02004565 /*
4566 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004567 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004568 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004569 ret = nand_reset(chip, 0);
4570 if (ret)
4571 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004572
4573 /* Select the device */
4574 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004577 ret = nand_readid_op(chip, 0, id_data, 2);
4578 if (ret)
4579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
4581 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004582 maf_id = id_data[0];
4583 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
Brian Norris8b6e50c2011-05-25 14:59:01 -07004585 /*
4586 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004587 * interface concerns can cause random data which looks like a
4588 * possibly credible NAND flash to appear. If the two results do
4589 * not match, ignore the device completely.
4590 */
4591
Brian Norris4aef9b72012-09-24 20:40:48 -07004592 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004593 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
4594 if (ret)
4595 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01004596
Boris Brezillon7f501f02016-05-24 19:20:05 +02004597 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004598 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004599 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004600 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004601 }
4602
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02004603 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02004604
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004605 /* Try to identify manufacturer */
4606 manufacturer = nand_get_manufacturer(maf_id);
4607 chip->manufacturer.desc = manufacturer;
4608
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004609 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004610 type = nand_flash_ids;
4611
Boris Brezillon29a198a2016-05-24 20:17:48 +02004612 /*
4613 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4614 * override it.
4615 * This is required to make sure initial NAND bus width set by the
4616 * NAND controller driver is coherent with the real NAND bus width
4617 * (extracted by auto-detection code).
4618 */
4619 busw = chip->options & NAND_BUSWIDTH_16;
4620
4621 /*
4622 * The flag is only set (never cleared), reset it to its default value
4623 * before starting auto-detection.
4624 */
4625 chip->options &= ~NAND_BUSWIDTH_16;
4626
Huang Shijieec6e87e2013-03-15 11:01:00 +08004627 for (; type->name != NULL; type++) {
4628 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004629 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004630 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004631 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004632 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004633 }
4634 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004635
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004636 chip->onfi_version = 0;
4637 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004638 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004639 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004640 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004641
4642 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004643 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08004644 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004645 }
4646
David Woodhouse5e81e882010-02-26 18:32:56 +00004647 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004648 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004649
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02004650 if (!mtd->name)
4651 mtd->name = type->name;
4652
Adrian Hunter69423d92008-12-10 13:37:21 +00004653 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004654
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004655 if (!type->pagesize)
4656 nand_manufacturer_detect(chip);
4657 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004658 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004659
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004660 /* Get chip options */
4661 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004662
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004663ident_done:
4664
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004665 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004666 WARN_ON(busw & NAND_BUSWIDTH_16);
4667 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004668 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4669 /*
4670 * Check, if buswidth is correct. Hardware drivers should set
4671 * chip correct!
4672 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004673 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004674 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004675 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4676 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004677 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4678 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004679 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004680 }
4681
Boris Brezillon7f501f02016-05-24 19:20:05 +02004682 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004683
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004684 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004685 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004686 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004687 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004688
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004689 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004690 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004691 if (chip->chipsize & 0xffffffff)
4692 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004693 else {
4694 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4695 chip->chip_shift += 32 - 1;
4696 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004697
Masahiro Yamada14157f82017-09-13 11:05:50 +09004698 if (chip->chip_shift - chip->page_shift > 16)
4699 chip->options |= NAND_ROW_ADDR_3;
4700
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004701 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004702 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004703
Brian Norris8b6e50c2011-05-25 14:59:01 -07004704 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004705 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4706 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004707
Ezequiel Garcia20171642013-11-25 08:30:31 -03004708 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004709 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004710
4711 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004712 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4713 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004714 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004715 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4716 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004717 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004718 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4719 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004720
Rafał Miłecki3755a992014-10-21 00:01:04 +02004721 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004722 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004723 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004724 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004725}
4726
Boris Brezillond48f62b2016-04-01 14:54:32 +02004727static const char * const nand_ecc_modes[] = {
4728 [NAND_ECC_NONE] = "none",
4729 [NAND_ECC_SOFT] = "soft",
4730 [NAND_ECC_HW] = "hw",
4731 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4732 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004733 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004734};
4735
4736static int of_get_nand_ecc_mode(struct device_node *np)
4737{
4738 const char *pm;
4739 int err, i;
4740
4741 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4742 if (err < 0)
4743 return err;
4744
4745 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4746 if (!strcasecmp(pm, nand_ecc_modes[i]))
4747 return i;
4748
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004749 /*
4750 * For backward compatibility we support few obsoleted values that don't
4751 * have their mappings into nand_ecc_modes_t anymore (they were merged
4752 * with other enums).
4753 */
4754 if (!strcasecmp(pm, "soft_bch"))
4755 return NAND_ECC_SOFT;
4756
Boris Brezillond48f62b2016-04-01 14:54:32 +02004757 return -ENODEV;
4758}
4759
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004760static const char * const nand_ecc_algos[] = {
4761 [NAND_ECC_HAMMING] = "hamming",
4762 [NAND_ECC_BCH] = "bch",
4763};
4764
Boris Brezillond48f62b2016-04-01 14:54:32 +02004765static int of_get_nand_ecc_algo(struct device_node *np)
4766{
4767 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004768 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004769
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004770 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4771 if (!err) {
4772 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4773 if (!strcasecmp(pm, nand_ecc_algos[i]))
4774 return i;
4775 return -ENODEV;
4776 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004777
4778 /*
4779 * For backward compatibility we also read "nand-ecc-mode" checking
4780 * for some obsoleted values that were specifying ECC algorithm.
4781 */
4782 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4783 if (err < 0)
4784 return err;
4785
4786 if (!strcasecmp(pm, "soft"))
4787 return NAND_ECC_HAMMING;
4788 else if (!strcasecmp(pm, "soft_bch"))
4789 return NAND_ECC_BCH;
4790
4791 return -ENODEV;
4792}
4793
4794static int of_get_nand_ecc_step_size(struct device_node *np)
4795{
4796 int ret;
4797 u32 val;
4798
4799 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4800 return ret ? ret : val;
4801}
4802
4803static int of_get_nand_ecc_strength(struct device_node *np)
4804{
4805 int ret;
4806 u32 val;
4807
4808 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4809 return ret ? ret : val;
4810}
4811
4812static int of_get_nand_bus_width(struct device_node *np)
4813{
4814 u32 val;
4815
4816 if (of_property_read_u32(np, "nand-bus-width", &val))
4817 return 8;
4818
4819 switch (val) {
4820 case 8:
4821 case 16:
4822 return val;
4823 default:
4824 return -EIO;
4825 }
4826}
4827
4828static bool of_get_nand_on_flash_bbt(struct device_node *np)
4829{
4830 return of_property_read_bool(np, "nand-on-flash-bbt");
4831}
4832
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004833static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004834{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004835 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004836 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004837
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004838 if (!dn)
4839 return 0;
4840
Brian Norris5844fee2015-01-23 00:22:27 -08004841 if (of_get_nand_bus_width(dn) == 16)
4842 chip->options |= NAND_BUSWIDTH_16;
4843
4844 if (of_get_nand_on_flash_bbt(dn))
4845 chip->bbt_options |= NAND_BBT_USE_FLASH;
4846
4847 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004848 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004849 ecc_strength = of_get_nand_ecc_strength(dn);
4850 ecc_step = of_get_nand_ecc_step_size(dn);
4851
Brian Norris5844fee2015-01-23 00:22:27 -08004852 if (ecc_mode >= 0)
4853 chip->ecc.mode = ecc_mode;
4854
Rafał Miłecki79082452016-03-23 11:19:02 +01004855 if (ecc_algo >= 0)
4856 chip->ecc.algo = ecc_algo;
4857
Brian Norris5844fee2015-01-23 00:22:27 -08004858 if (ecc_strength >= 0)
4859 chip->ecc.strength = ecc_strength;
4860
4861 if (ecc_step > 0)
4862 chip->ecc.size = ecc_step;
4863
Boris Brezillonba78ee02016-06-08 17:04:22 +02004864 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4865 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4866
Brian Norris5844fee2015-01-23 00:22:27 -08004867 return 0;
4868}
4869
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004870/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004871 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004872 * @mtd: MTD device structure
4873 * @maxchips: number of chips to scan for
4874 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004875 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004876 * This is the first phase of the normal nand_scan() function. It reads the
4877 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004878 *
4879 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004880int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4881 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004882{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004883 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004884 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004885 int ret;
4886
Miquel Raynal17fa8042017-11-30 18:01:31 +01004887 /* Enforce the right timings for reset/detection */
4888 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
4889
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004890 ret = nand_dt_init(chip);
4891 if (ret)
4892 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004893
Brian Norrisf7a8e382016-01-05 10:39:45 -08004894 if (!mtd->name && mtd->dev.parent)
4895 mtd->name = dev_name(mtd->dev.parent);
4896
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004897 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4898 /*
4899 * Default functions assigned for chip_select() and
4900 * cmdfunc() both expect cmd_ctrl() to be populated,
4901 * so we need to check that that's the case
4902 */
4903 pr_err("chip.cmd_ctrl() callback is not provided");
4904 return -EINVAL;
4905 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004906 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004907 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004908
4909 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004910 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004911 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004912 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004913 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004914 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004915 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 }
4917
Boris Brezillon7f501f02016-05-24 19:20:05 +02004918 nand_maf_id = chip->id.data[0];
4919 nand_dev_id = chip->id.data[1];
4920
Huang Shijie07300162012-11-09 16:23:45 +08004921 chip->select_chip(mtd, -1);
4922
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004923 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004924 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004925 u8 id[2];
4926
Karl Beldanef89a882008-09-15 14:37:29 +02004927 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004928 nand_reset(chip, i);
4929
4930 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004932 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004934 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08004935 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 break;
Huang Shijie07300162012-11-09 16:23:45 +08004937 }
4938 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 }
4940 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004941 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004942
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004944 chip->numchips = i;
4945 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
David Woodhouse3b85c322006-09-25 17:06:53 +01004947 return 0;
4948}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004949EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004950
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004951static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4952{
4953 struct nand_chip *chip = mtd_to_nand(mtd);
4954 struct nand_ecc_ctrl *ecc = &chip->ecc;
4955
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004956 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004957 return -EINVAL;
4958
4959 switch (ecc->algo) {
4960 case NAND_ECC_HAMMING:
4961 ecc->calculate = nand_calculate_ecc;
4962 ecc->correct = nand_correct_data;
4963 ecc->read_page = nand_read_page_swecc;
4964 ecc->read_subpage = nand_read_subpage;
4965 ecc->write_page = nand_write_page_swecc;
4966 ecc->read_page_raw = nand_read_page_raw;
4967 ecc->write_page_raw = nand_write_page_raw;
4968 ecc->read_oob = nand_read_oob_std;
4969 ecc->write_oob = nand_write_oob_std;
4970 if (!ecc->size)
4971 ecc->size = 256;
4972 ecc->bytes = 3;
4973 ecc->strength = 1;
4974 return 0;
4975 case NAND_ECC_BCH:
4976 if (!mtd_nand_has_bch()) {
4977 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4978 return -EINVAL;
4979 }
4980 ecc->calculate = nand_bch_calculate_ecc;
4981 ecc->correct = nand_bch_correct_data;
4982 ecc->read_page = nand_read_page_swecc;
4983 ecc->read_subpage = nand_read_subpage;
4984 ecc->write_page = nand_write_page_swecc;
4985 ecc->read_page_raw = nand_read_page_raw;
4986 ecc->write_page_raw = nand_write_page_raw;
4987 ecc->read_oob = nand_read_oob_std;
4988 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004989
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004990 /*
4991 * Board driver should supply ecc.size and ecc.strength
4992 * values to select how many bits are correctable.
4993 * Otherwise, default to 4 bits for large page devices.
4994 */
4995 if (!ecc->size && (mtd->oobsize >= 64)) {
4996 ecc->size = 512;
4997 ecc->strength = 4;
4998 }
4999
5000 /*
5001 * if no ecc placement scheme was provided pickup the default
5002 * large page one.
5003 */
5004 if (!mtd->ooblayout) {
5005 /* handle large page devices only */
5006 if (mtd->oobsize < 64) {
5007 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5008 return -EINVAL;
5009 }
5010
5011 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005012
5013 }
5014
5015 /*
5016 * We can only maximize ECC config when the default layout is
5017 * used, otherwise we don't know how many bytes can really be
5018 * used.
5019 */
5020 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5021 ecc->options & NAND_ECC_MAXIMIZE) {
5022 int steps, bytes;
5023
5024 /* Always prefer 1k blocks over 512bytes ones */
5025 ecc->size = 1024;
5026 steps = mtd->writesize / ecc->size;
5027
5028 /* Reserve 2 bytes for the BBM */
5029 bytes = (mtd->oobsize - 2) / steps;
5030 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005031 }
5032
5033 /* See nand_bch_init() for details. */
5034 ecc->bytes = 0;
5035 ecc->priv = nand_bch_init(mtd);
5036 if (!ecc->priv) {
5037 WARN(1, "BCH ECC initialization failed!\n");
5038 return -EINVAL;
5039 }
5040 return 0;
5041 default:
5042 WARN(1, "Unsupported ECC algorithm!\n");
5043 return -EINVAL;
5044 }
5045}
5046
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005047/**
5048 * nand_check_ecc_caps - check the sanity of preset ECC settings
5049 * @chip: nand chip info structure
5050 * @caps: ECC caps info structure
5051 * @oobavail: OOB size that the ECC engine can use
5052 *
5053 * When ECC step size and strength are already set, check if they are supported
5054 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5055 * On success, the calculated ECC bytes is set.
5056 */
5057int nand_check_ecc_caps(struct nand_chip *chip,
5058 const struct nand_ecc_caps *caps, int oobavail)
5059{
5060 struct mtd_info *mtd = nand_to_mtd(chip);
5061 const struct nand_ecc_step_info *stepinfo;
5062 int preset_step = chip->ecc.size;
5063 int preset_strength = chip->ecc.strength;
5064 int nsteps, ecc_bytes;
5065 int i, j;
5066
5067 if (WARN_ON(oobavail < 0))
5068 return -EINVAL;
5069
5070 if (!preset_step || !preset_strength)
5071 return -ENODATA;
5072
5073 nsteps = mtd->writesize / preset_step;
5074
5075 for (i = 0; i < caps->nstepinfos; i++) {
5076 stepinfo = &caps->stepinfos[i];
5077
5078 if (stepinfo->stepsize != preset_step)
5079 continue;
5080
5081 for (j = 0; j < stepinfo->nstrengths; j++) {
5082 if (stepinfo->strengths[j] != preset_strength)
5083 continue;
5084
5085 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5086 preset_strength);
5087 if (WARN_ON_ONCE(ecc_bytes < 0))
5088 return ecc_bytes;
5089
5090 if (ecc_bytes * nsteps > oobavail) {
5091 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5092 preset_step, preset_strength);
5093 return -ENOSPC;
5094 }
5095
5096 chip->ecc.bytes = ecc_bytes;
5097
5098 return 0;
5099 }
5100 }
5101
5102 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5103 preset_step, preset_strength);
5104
5105 return -ENOTSUPP;
5106}
5107EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
5108
5109/**
5110 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5111 * @chip: nand chip info structure
5112 * @caps: ECC engine caps info structure
5113 * @oobavail: OOB size that the ECC engine can use
5114 *
5115 * If a chip's ECC requirement is provided, try to meet it with the least
5116 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5117 * On success, the chosen ECC settings are set.
5118 */
5119int nand_match_ecc_req(struct nand_chip *chip,
5120 const struct nand_ecc_caps *caps, int oobavail)
5121{
5122 struct mtd_info *mtd = nand_to_mtd(chip);
5123 const struct nand_ecc_step_info *stepinfo;
5124 int req_step = chip->ecc_step_ds;
5125 int req_strength = chip->ecc_strength_ds;
5126 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5127 int best_step, best_strength, best_ecc_bytes;
5128 int best_ecc_bytes_total = INT_MAX;
5129 int i, j;
5130
5131 if (WARN_ON(oobavail < 0))
5132 return -EINVAL;
5133
5134 /* No information provided by the NAND chip */
5135 if (!req_step || !req_strength)
5136 return -ENOTSUPP;
5137
5138 /* number of correctable bits the chip requires in a page */
5139 req_corr = mtd->writesize / req_step * req_strength;
5140
5141 for (i = 0; i < caps->nstepinfos; i++) {
5142 stepinfo = &caps->stepinfos[i];
5143 step_size = stepinfo->stepsize;
5144
5145 for (j = 0; j < stepinfo->nstrengths; j++) {
5146 strength = stepinfo->strengths[j];
5147
5148 /*
5149 * If both step size and strength are smaller than the
5150 * chip's requirement, it is not easy to compare the
5151 * resulted reliability.
5152 */
5153 if (step_size < req_step && strength < req_strength)
5154 continue;
5155
5156 if (mtd->writesize % step_size)
5157 continue;
5158
5159 nsteps = mtd->writesize / step_size;
5160
5161 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5162 if (WARN_ON_ONCE(ecc_bytes < 0))
5163 continue;
5164 ecc_bytes_total = ecc_bytes * nsteps;
5165
5166 if (ecc_bytes_total > oobavail ||
5167 strength * nsteps < req_corr)
5168 continue;
5169
5170 /*
5171 * We assume the best is to meet the chip's requrement
5172 * with the least number of ECC bytes.
5173 */
5174 if (ecc_bytes_total < best_ecc_bytes_total) {
5175 best_ecc_bytes_total = ecc_bytes_total;
5176 best_step = step_size;
5177 best_strength = strength;
5178 best_ecc_bytes = ecc_bytes;
5179 }
5180 }
5181 }
5182
5183 if (best_ecc_bytes_total == INT_MAX)
5184 return -ENOTSUPP;
5185
5186 chip->ecc.size = best_step;
5187 chip->ecc.strength = best_strength;
5188 chip->ecc.bytes = best_ecc_bytes;
5189
5190 return 0;
5191}
5192EXPORT_SYMBOL_GPL(nand_match_ecc_req);
5193
5194/**
5195 * nand_maximize_ecc - choose the max ECC strength available
5196 * @chip: nand chip info structure
5197 * @caps: ECC engine caps info structure
5198 * @oobavail: OOB size that the ECC engine can use
5199 *
5200 * Choose the max ECC strength that is supported on the controller, and can fit
5201 * within the chip's OOB. On success, the chosen ECC settings are set.
5202 */
5203int nand_maximize_ecc(struct nand_chip *chip,
5204 const struct nand_ecc_caps *caps, int oobavail)
5205{
5206 struct mtd_info *mtd = nand_to_mtd(chip);
5207 const struct nand_ecc_step_info *stepinfo;
5208 int step_size, strength, nsteps, ecc_bytes, corr;
5209 int best_corr = 0;
5210 int best_step = 0;
5211 int best_strength, best_ecc_bytes;
5212 int i, j;
5213
5214 if (WARN_ON(oobavail < 0))
5215 return -EINVAL;
5216
5217 for (i = 0; i < caps->nstepinfos; i++) {
5218 stepinfo = &caps->stepinfos[i];
5219 step_size = stepinfo->stepsize;
5220
5221 /* If chip->ecc.size is already set, respect it */
5222 if (chip->ecc.size && step_size != chip->ecc.size)
5223 continue;
5224
5225 for (j = 0; j < stepinfo->nstrengths; j++) {
5226 strength = stepinfo->strengths[j];
5227
5228 if (mtd->writesize % step_size)
5229 continue;
5230
5231 nsteps = mtd->writesize / step_size;
5232
5233 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5234 if (WARN_ON_ONCE(ecc_bytes < 0))
5235 continue;
5236
5237 if (ecc_bytes * nsteps > oobavail)
5238 continue;
5239
5240 corr = strength * nsteps;
5241
5242 /*
5243 * If the number of correctable bits is the same,
5244 * bigger step_size has more reliability.
5245 */
5246 if (corr > best_corr ||
5247 (corr == best_corr && step_size > best_step)) {
5248 best_corr = corr;
5249 best_step = step_size;
5250 best_strength = strength;
5251 best_ecc_bytes = ecc_bytes;
5252 }
5253 }
5254 }
5255
5256 if (!best_corr)
5257 return -ENOTSUPP;
5258
5259 chip->ecc.size = best_step;
5260 chip->ecc.strength = best_strength;
5261 chip->ecc.bytes = best_ecc_bytes;
5262
5263 return 0;
5264}
5265EXPORT_SYMBOL_GPL(nand_maximize_ecc);
5266
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005267/*
5268 * Check if the chip configuration meet the datasheet requirements.
5269
5270 * If our configuration corrects A bits per B bytes and the minimum
5271 * required correction level is X bits per Y bytes, then we must ensure
5272 * both of the following are true:
5273 *
5274 * (1) A / B >= X / Y
5275 * (2) A >= X
5276 *
5277 * Requirement (1) ensures we can correct for the required bitflip density.
5278 * Requirement (2) ensures we can correct even when all bitflips are clumped
5279 * in the same sector.
5280 */
5281static bool nand_ecc_strength_good(struct mtd_info *mtd)
5282{
Boris BREZILLON862eba52015-12-01 12:03:03 +01005283 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005284 struct nand_ecc_ctrl *ecc = &chip->ecc;
5285 int corr, ds_corr;
5286
5287 if (ecc->size == 0 || chip->ecc_step_ds == 0)
5288 /* Not enough information */
5289 return true;
5290
5291 /*
5292 * We get the number of corrected bits per page to compare
5293 * the correction density.
5294 */
5295 corr = (mtd->writesize * ecc->strength) / ecc->size;
5296 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
5297
5298 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
5299}
David Woodhouse3b85c322006-09-25 17:06:53 +01005300
5301/**
5302 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005303 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01005304 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005305 * This is the second phase of the normal nand_scan() function. It fills out
5306 * all the uninitialized function pointers with the defaults and scans for a
5307 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01005308 */
5309int nand_scan_tail(struct mtd_info *mtd)
5310{
Boris BREZILLON862eba52015-12-01 12:03:03 +01005311 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08005312 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005313 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01005314
Brian Norrise2414f42012-02-06 13:44:00 -08005315 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005316 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07005317 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005318 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07005319 }
Brian Norrise2414f42012-02-06 13:44:00 -08005320
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005321 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005322 if (!chip->data_buf)
5323 return -ENOMEM;
Masahiro Yamada8b311ea2017-12-05 17:47:15 +09005324
Boris Brezillonf84674b2017-06-02 12:18:24 +02005325 /*
5326 * FIXME: some NAND manufacturer drivers expect the first die to be
5327 * selected when manufacturer->init() is called. They should be fixed
5328 * to explictly select the relevant die when interacting with the NAND
5329 * chip.
5330 */
5331 chip->select_chip(mtd, 0);
5332 ret = nand_manufacturer_init(chip);
5333 chip->select_chip(mtd, -1);
5334 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005335 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005336
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01005337 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005338 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005339
5340 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005341 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005342 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005343 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005344 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005345 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01005348 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 break;
5350 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005351 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02005352 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005353 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02005355 /*
5356 * Expose the whole OOB area to users if ECC_NONE
5357 * is passed. We could do that for all kind of
5358 * ->oobsize, but we must keep the old large/small
5359 * page with ECC layout when ->oobsize <= 128 for
5360 * compatibility reasons.
5361 */
5362 if (ecc->mode == NAND_ECC_NONE) {
5363 mtd_set_ooblayout(mtd,
5364 &nand_ooblayout_lp_ops);
5365 break;
5366 }
5367
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005368 WARN(1, "No oob scheme defined for oobsize %d\n",
5369 mtd->oobsize);
5370 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005371 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 }
5373 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005374
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005375 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005376 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005377 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01005378 */
David Woodhouse956e9442006-09-25 17:12:39 +01005379
Huang Shijie97de79e02013-10-18 14:20:53 +08005380 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005381 case NAND_ECC_HW_OOB_FIRST:
5382 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08005383 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005384 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5385 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005386 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005387 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005388 if (!ecc->read_page)
5389 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005390
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005391 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07005392 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005393 if (!ecc->read_page)
5394 ecc->read_page = nand_read_page_hwecc;
5395 if (!ecc->write_page)
5396 ecc->write_page = nand_write_page_hwecc;
5397 if (!ecc->read_page_raw)
5398 ecc->read_page_raw = nand_read_page_raw;
5399 if (!ecc->write_page_raw)
5400 ecc->write_page_raw = nand_write_page_raw;
5401 if (!ecc->read_oob)
5402 ecc->read_oob = nand_read_oob_std;
5403 if (!ecc->write_oob)
5404 ecc->write_oob = nand_write_oob_std;
5405 if (!ecc->read_subpage)
5406 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02005407 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08005408 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005409
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005410 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08005411 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5412 (!ecc->read_page ||
5413 ecc->read_page == nand_read_page_hwecc ||
5414 !ecc->write_page ||
5415 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005416 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5417 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005418 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005419 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07005420 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005421 if (!ecc->read_page)
5422 ecc->read_page = nand_read_page_syndrome;
5423 if (!ecc->write_page)
5424 ecc->write_page = nand_write_page_syndrome;
5425 if (!ecc->read_page_raw)
5426 ecc->read_page_raw = nand_read_page_raw_syndrome;
5427 if (!ecc->write_page_raw)
5428 ecc->write_page_raw = nand_write_page_raw_syndrome;
5429 if (!ecc->read_oob)
5430 ecc->read_oob = nand_read_oob_syndrome;
5431 if (!ecc->write_oob)
5432 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005433
Huang Shijie97de79e02013-10-18 14:20:53 +08005434 if (mtd->writesize >= ecc->size) {
5435 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005436 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
5437 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005438 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07005439 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005440 break;
Mike Dunne2788c92012-04-25 12:06:10 -07005441 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005442 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5443 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08005444 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02005445 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005447 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005448 ret = nand_set_ecc_soft_ops(mtd);
5449 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005450 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005451 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01005452 }
5453 break;
5454
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005455 case NAND_ECC_ON_DIE:
5456 if (!ecc->read_page || !ecc->write_page) {
5457 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
5458 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005459 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005460 }
5461 if (!ecc->read_oob)
5462 ecc->read_oob = nand_read_oob_std;
5463 if (!ecc->write_oob)
5464 ecc->write_oob = nand_write_oob_std;
5465 break;
5466
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005467 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005468 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08005469 ecc->read_page = nand_read_page_raw;
5470 ecc->write_page = nand_write_page_raw;
5471 ecc->read_oob = nand_read_oob_std;
5472 ecc->read_page_raw = nand_read_page_raw;
5473 ecc->write_page_raw = nand_write_page_raw;
5474 ecc->write_oob = nand_write_oob_std;
5475 ecc->size = mtd->writesize;
5476 ecc->bytes = 0;
5477 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005479
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005481 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5482 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005483 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
Boris Brezillonaeb93af2017-12-05 12:09:29 +01005486 if (ecc->correct || ecc->calculate) {
5487 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5488 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
5489 if (!ecc->calc_buf || !ecc->code_buf) {
5490 ret = -ENOMEM;
5491 goto err_nand_manuf_cleanup;
5492 }
5493 }
5494
Brian Norris9ce244b2011-08-30 18:45:37 -07005495 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005496 if (!ecc->read_oob_raw)
5497 ecc->read_oob_raw = ecc->read_oob;
5498 if (!ecc->write_oob_raw)
5499 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005500
Boris Brezillon846031d2016-02-03 20:11:00 +01005501 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005502 mtd->ecc_strength = ecc->strength;
5503 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005504
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005505 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005506 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005507 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005508 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005509 ecc->steps = mtd->writesize / ecc->size;
5510 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005511 WARN(1, "Invalid ECC parameters\n");
5512 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005513 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005515 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005516 if (ecc->total > mtd->oobsize) {
5517 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5518 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005519 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005520 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005521
Boris Brezillon846031d2016-02-03 20:11:00 +01005522 /*
5523 * The number of bytes available for a client to place data into
5524 * the out of band area.
5525 */
5526 ret = mtd_ooblayout_count_freebytes(mtd);
5527 if (ret < 0)
5528 ret = 0;
5529
5530 mtd->oobavail = ret;
5531
5532 /* ECC sanity check: warn if it's too weak */
5533 if (!nand_ecc_strength_good(mtd))
5534 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5535 mtd->name);
5536
Brian Norris8b6e50c2011-05-25 14:59:01 -07005537 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005538 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005539 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005540 case 2:
5541 mtd->subpage_sft = 1;
5542 break;
5543 case 4:
5544 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005545 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005546 mtd->subpage_sft = 2;
5547 break;
5548 }
5549 }
5550 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5551
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02005552 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005553 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005556 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005558 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305559 switch (ecc->mode) {
5560 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305561 if (chip->page_shift > 9)
5562 chip->options |= NAND_SUBPAGE_READ;
5563 break;
5564
5565 default:
5566 break;
5567 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08005570 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02005571 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5572 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005573 mtd->_erase = nand_erase;
5574 mtd->_point = NULL;
5575 mtd->_unpoint = NULL;
5576 mtd->_read = nand_read;
5577 mtd->_write = nand_write;
5578 mtd->_panic_write = panic_nand_write;
5579 mtd->_read_oob = nand_read_oob;
5580 mtd->_write_oob = nand_write_oob;
5581 mtd->_sync = nand_sync;
5582 mtd->_lock = NULL;
5583 mtd->_unlock = NULL;
5584 mtd->_suspend = nand_suspend;
5585 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005586 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005587 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005588 mtd->_block_isbad = nand_block_isbad;
5589 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06005590 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01005591 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005593 /*
5594 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5595 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5596 * properly set.
5597 */
5598 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005599 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Boris Brezillonf84674b2017-06-02 12:18:24 +02005601 /* Initialize the ->data_interface field. */
5602 ret = nand_init_data_interface(chip);
5603 if (ret)
5604 goto err_nand_manuf_cleanup;
5605
5606 /* Enter fastest possible mode on all dies. */
5607 for (i = 0; i < chip->numchips; i++) {
5608 chip->select_chip(mtd, i);
5609 ret = nand_setup_data_interface(chip, i);
5610 chip->select_chip(mtd, -1);
5611
5612 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005613 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005614 }
5615
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005616 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005617 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
5620 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07005621 ret = chip->scan_bbt(mtd);
5622 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01005623 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005624
Brian Norris44d41822017-05-01 17:04:50 -07005625 return 0;
5626
Boris Brezillonf84674b2017-06-02 12:18:24 +02005627
5628err_nand_manuf_cleanup:
5629 nand_manufacturer_cleanup(chip);
5630
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005631err_free_buf:
5632 kfree(chip->data_buf);
5633 kfree(ecc->code_buf);
5634 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07005635
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005636 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005638EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
Brian Norris8b6e50c2011-05-25 14:59:01 -07005640/*
5641 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005642 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07005643 * to call us from in-kernel code if the core NAND support is modular.
5644 */
David Woodhouse3b85c322006-09-25 17:06:53 +01005645#ifdef MODULE
5646#define caller_is_module() (1)
5647#else
5648#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06005649 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01005650#endif
5651
5652/**
5653 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005654 * @mtd: MTD device structure
5655 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01005656 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005657 * This fills out all the uninitialized function pointers with the defaults.
5658 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005659 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005660 */
5661int nand_scan(struct mtd_info *mtd, int maxchips)
5662{
5663 int ret;
5664
David Woodhouse5e81e882010-02-26 18:32:56 +00005665 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01005666 if (!ret)
5667 ret = nand_scan_tail(mtd);
5668 return ret;
5669}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005670EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01005671
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005673 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5674 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005675 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005676void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005678 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005679 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005680 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5681
Jesper Juhlfa671642005-11-07 01:01:27 -08005682 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005683 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09005684 kfree(chip->data_buf);
5685 kfree(chip->ecc.code_buf);
5686 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07005687
5688 /* Free bad block descriptor memory */
5689 if (chip->badblock_pattern && chip->badblock_pattern->options
5690 & NAND_BBT_DYNAMICSTRUCT)
5691 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005692
5693 /* Free manufacturer priv data. */
5694 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005696EXPORT_SYMBOL_GPL(nand_cleanup);
5697
5698/**
5699 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5700 * held by the NAND device
5701 * @mtd: MTD device structure
5702 */
5703void nand_release(struct mtd_info *mtd)
5704{
5705 mtd_device_unregister(mtd);
5706 nand_cleanup(mtd_to_nand(mtd));
5707}
David Woodhousee0c7d762006-05-13 18:07:53 +01005708EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005709
David Woodhousee0c7d762006-05-13 18:07:53 +01005710MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005711MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5712MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005713MODULE_DESCRIPTION("Generic NAND flash driver code");