blob: c63e4a88a653116cef7e9ccdd15f821c92211fad [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);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200564
Brian Norris8b6e50c2011-05-25 14:59:01 -0700565 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200566 if (chip->options & NAND_BROKEN_XD)
567 return 0;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200570 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
571 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800575 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700576 * @mtd: MTD device structure
577 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300578 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800579 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300580 */
581static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
582{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100583 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300584
585 if (!chip->bbt)
586 return 0;
587 /* Return info from the table */
588 return nand_isreserved_bbt(mtd, ofs);
589}
590
591/**
592 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
593 * @mtd: MTD device structure
594 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700595 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
597 * Check, if the block is bad. Either by reading the bad block table or
598 * calling of the scan function.
599 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530600static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100602 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000603
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200604 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530605 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100608 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200611/**
612 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700613 * @mtd: MTD device structure
614 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200615 *
616 * Helper function for nand_wait_ready used when needing to wait in interrupt
617 * context.
618 */
619static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
620{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100621 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200622 int i;
623
624 /* Wait for the device to get ready */
625 for (i = 0; i < timeo; i++) {
626 if (chip->dev_ready(mtd))
627 break;
628 touch_softlockup_watchdog();
629 mdelay(1);
630 }
631}
632
Alex Smithb70af9b2015-10-06 14:52:07 +0100633/**
634 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
635 * @mtd: MTD device structure
636 *
637 * Wait for the ready pin after a command, and warn if a timeout occurs.
638 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100639void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000640{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100641 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100642 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000643
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200644 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100645 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200646
Brian Norris7854d3f2011-06-23 14:12:08 -0700647 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100648 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000649 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200650 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300651 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100652 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000653 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100654
Brian Norris9ebfdf52016-03-04 17:19:23 -0800655 if (!chip->dev_ready(mtd))
656 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000657}
David Woodhouse4b648b02006-09-25 17:05:24 +0100658EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200661 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
662 * @mtd: MTD device structure
663 * @timeo: Timeout in ms
664 *
665 * Wait for status ready (i.e. command done) or timeout.
666 */
667static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
668{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100669 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200670
671 timeo = jiffies + msecs_to_jiffies(timeo);
672 do {
673 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
674 break;
675 touch_softlockup_watchdog();
676 } while (time_before(jiffies, timeo));
677};
678
679/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700681 * @mtd: MTD device structure
682 * @command: the command to be sent
683 * @column: the column address for this command, -1 if none
684 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700686 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200687 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200689static void nand_command(struct mtd_info *mtd, unsigned int command,
690 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100692 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200693 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Brian Norris8b6e50c2011-05-25 14:59:01 -0700695 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (command == NAND_CMD_SEQIN) {
697 int readcmd;
698
Joern Engel28318772006-05-22 23:18:05 +0200699 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200701 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 readcmd = NAND_CMD_READOOB;
703 } else if (column < 256) {
704 /* First 256 bytes --> READ0 */
705 readcmd = NAND_CMD_READ0;
706 } else {
707 column -= 256;
708 readcmd = NAND_CMD_READ1;
709 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200710 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200711 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200713 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Brian Norris8b6e50c2011-05-25 14:59:01 -0700715 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200716 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
717 /* Serially input address */
718 if (column != -1) {
719 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800720 if (chip->options & NAND_BUSWIDTH_16 &&
721 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200722 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200723 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200724 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200726 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200727 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200728 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200729 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900730 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200732 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200733 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000734
735 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700736 * Program and erase have their own busy handlers status and sequential
737 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 case NAND_CMD_PAGEPROG:
742 case NAND_CMD_ERASE1:
743 case NAND_CMD_ERASE2:
744 case NAND_CMD_SEQIN:
745 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900746 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900747 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return;
749
750 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200751 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200753 udelay(chip->chip_delay);
754 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200755 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200756 chip->cmd_ctrl(mtd,
757 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200758 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
759 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return;
761
David Woodhousee0c7d762006-05-13 18:07:53 +0100762 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200763 case NAND_CMD_READ0:
764 /*
765 * READ0 is sometimes used to exit GET STATUS mode. When this
766 * is the case no address cycles are requested, and we can use
767 * this information to detect that we should not wait for the
768 * device to be ready.
769 */
770 if (column == -1 && page_addr == -1)
771 return;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000774 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * If we don't have access to the busy pin, we apply the given
776 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100777 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200778 if (!chip->dev_ready) {
779 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700783 /*
784 * Apply this short delay always to ensure that we do wait tWB in
785 * any case on any machine.
786 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100787 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000788
789 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200792static void nand_ccs_delay(struct nand_chip *chip)
793{
794 /*
795 * The controller already takes care of waiting for tCCS when the RNDIN
796 * or RNDOUT command is sent, return directly.
797 */
798 if (!(chip->options & NAND_WAIT_TCCS))
799 return;
800
801 /*
802 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
803 * (which should be safe for all NANDs).
804 */
805 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
806 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
807 else
808 ndelay(500);
809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/**
812 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700813 * @mtd: MTD device structure
814 * @command: the command to be sent
815 * @column: the column address for this command, -1 if none
816 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200818 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700819 * devices. We don't have the separate regions as we have in the small page
820 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200822static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
823 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100825 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /* Emulate NAND_CMD_READOOB */
828 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200829 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 command = NAND_CMD_READ0;
831 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000832
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200833 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400834 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200837 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /* Serially input address */
840 if (column != -1) {
841 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800842 if (chip->options & NAND_BUSWIDTH_16 &&
843 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200845 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200846 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200847
Brian Norrisf5b88de2016-10-03 09:49:35 -0700848 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200849 if (!nand_opcode_8bits(command))
850 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200853 chip->cmd_ctrl(mtd, page_addr, ctrl);
854 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200855 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900856 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200857 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200858 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200861 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000862
863 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700864 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100865 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 case NAND_CMD_CACHEDPROG:
870 case NAND_CMD_PAGEPROG:
871 case NAND_CMD_ERASE1:
872 case NAND_CMD_ERASE2:
873 case NAND_CMD_SEQIN:
874 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900875 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900876 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000877 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200879 case NAND_CMD_RNDIN:
880 nand_ccs_delay(chip);
881 return;
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200884 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200886 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200887 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
888 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
889 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
890 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200891 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
892 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return;
894
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200895 case NAND_CMD_RNDOUT:
896 /* No ready / busy check necessary */
897 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
898 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
899 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
900 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200901
902 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200903 return;
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200906 /*
907 * READ0 is sometimes used to exit GET STATUS mode. When this
908 * is the case no address cycles are requested, and we can use
909 * this information to detect that READSTART should not be
910 * issued.
911 */
912 if (column == -1 && page_addr == -1)
913 return;
914
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200915 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
916 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
917 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
918 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000919
David Woodhousee0c7d762006-05-13 18:07:53 +0100920 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000922 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700924 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100925 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200926 if (!chip->dev_ready) {
927 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000931
Brian Norris8b6e50c2011-05-25 14:59:01 -0700932 /*
933 * Apply this short delay always to ensure that we do wait tWB in
934 * any case on any machine.
935 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100936 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000937
938 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200942 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700943 * @chip: the nand chip descriptor
944 * @mtd: MTD device structure
945 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200946 *
947 * Used when in panic, no locks are taken.
948 */
949static void panic_nand_get_device(struct nand_chip *chip,
950 struct mtd_info *mtd, int new_state)
951{
Brian Norris7854d3f2011-06-23 14:12:08 -0700952 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200953 chip->controller->active = chip;
954 chip->state = new_state;
955}
956
957/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700959 * @mtd: MTD device structure
960 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 *
962 * Get the device and lock it for exclusive access
963 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200964static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800965nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100967 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200968 spinlock_t *lock = &chip->controller->lock;
969 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100970 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200971retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100972 spin_lock(lock);
973
vimal singhb8b3ee92009-07-09 20:41:22 +0530974 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200975 if (!chip->controller->active)
976 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200977
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200978 if (chip->controller->active == chip && chip->state == FL_READY) {
979 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100980 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100981 return 0;
982 }
983 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800984 if (chip->controller->active->state == FL_PM_SUSPENDED) {
985 chip->state = FL_PM_SUSPENDED;
986 spin_unlock(lock);
987 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800988 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100989 }
990 set_current_state(TASK_UNINTERRUPTIBLE);
991 add_wait_queue(wq, &wait);
992 spin_unlock(lock);
993 schedule();
994 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto retry;
996}
997
998/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700999 * panic_nand_wait - [GENERIC] wait until the command is done
1000 * @mtd: MTD device structure
1001 * @chip: NAND chip structure
1002 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001003 *
1004 * Wait for command done. This is a helper function for nand_wait used when
1005 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001006 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001007 */
1008static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1009 unsigned long timeo)
1010{
1011 int i;
1012 for (i = 0; i < timeo; i++) {
1013 if (chip->dev_ready) {
1014 if (chip->dev_ready(mtd))
1015 break;
1016 } else {
1017 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1018 break;
1019 }
1020 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001021 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001022}
1023
1024/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001025 * nand_wait - [DEFAULT] wait until the command is done
1026 * @mtd: MTD device structure
1027 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001029 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001030 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001031static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033
Alex Smithb70af9b2015-10-06 14:52:07 +01001034 int status;
1035 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Brian Norris8b6e50c2011-05-25 14:59:01 -07001037 /*
1038 * Apply this short delay always to ensure that we do wait tWB in any
1039 * case on any machine.
1040 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001041 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Artem Bityutskiy14c65782013-03-04 14:21:34 +02001043 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001045 if (in_interrupt() || oops_in_progress)
1046 panic_nand_wait(mtd, chip, timeo);
1047 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001048 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001049 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001050 if (chip->dev_ready) {
1051 if (chip->dev_ready(mtd))
1052 break;
1053 } else {
1054 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1055 break;
1056 }
1057 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001058 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001060
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001061 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001062 /* This can happen if in case of timeout or buggy dev_ready */
1063 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return status;
1065}
1066
1067/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001068 * nand_reset_data_interface - Reset data interface and timings
1069 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001070 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001071 *
1072 * Reset the Data interface and timings to ONFI mode 0.
1073 *
1074 * Returns 0 for success or negative error code otherwise.
1075 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001076static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001077{
1078 struct mtd_info *mtd = nand_to_mtd(chip);
1079 const struct nand_data_interface *conf;
1080 int ret;
1081
1082 if (!chip->setup_data_interface)
1083 return 0;
1084
1085 /*
1086 * The ONFI specification says:
1087 * "
1088 * To transition from NV-DDR or NV-DDR2 to the SDR data
1089 * interface, the host shall use the Reset (FFh) command
1090 * using SDR timing mode 0. A device in any timing mode is
1091 * required to recognize Reset (FFh) command issued in SDR
1092 * timing mode 0.
1093 * "
1094 *
1095 * Configure the data interface in SDR mode and set the
1096 * timings to timing mode 0.
1097 */
1098
1099 conf = nand_get_default_data_interface();
Boris Brezillon104e4422017-03-16 09:35:58 +01001100 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001101 if (ret)
1102 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1103
1104 return ret;
1105}
1106
1107/**
1108 * nand_setup_data_interface - Setup the best data interface and timings
1109 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001110 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001111 *
1112 * Find and configure the best data interface and NAND timings supported by
1113 * the chip and the driver.
1114 * First tries to retrieve supported timing modes from ONFI information,
1115 * and if the NAND chip does not support ONFI, relies on the
1116 * ->onfi_timing_mode_default specified in the nand_ids table.
1117 *
1118 * Returns 0 for success or negative error code otherwise.
1119 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001120static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001121{
1122 struct mtd_info *mtd = nand_to_mtd(chip);
1123 int ret;
1124
1125 if (!chip->setup_data_interface || !chip->data_interface)
1126 return 0;
1127
1128 /*
1129 * Ensure the timing mode has been changed on the chip side
1130 * before changing timings on the controller side.
1131 */
Boris Brezillona11bf5e2017-07-31 10:29:56 +02001132 if (chip->onfi_version &&
1133 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1134 ONFI_OPT_CMD_SET_GET_FEATURES)) {
Boris Brezillond8e725d2016-09-15 10:32:50 +02001135 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1136 chip->onfi_timing_mode_default,
1137 };
1138
1139 ret = chip->onfi_set_features(mtd, chip,
1140 ONFI_FEATURE_ADDR_TIMING_MODE,
1141 tmode_param);
1142 if (ret)
1143 goto err;
1144 }
1145
Boris Brezillon104e4422017-03-16 09:35:58 +01001146 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001147err:
1148 return ret;
1149}
1150
1151/**
1152 * nand_init_data_interface - find the best data interface and timings
1153 * @chip: The NAND chip
1154 *
1155 * Find the best data interface and NAND timings supported by the chip
1156 * and the driver.
1157 * First tries to retrieve supported timing modes from ONFI information,
1158 * and if the NAND chip does not support ONFI, relies on the
1159 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1160 * function nand_chip->data_interface is initialized with the best timing mode
1161 * available.
1162 *
1163 * Returns 0 for success or negative error code otherwise.
1164 */
1165static int nand_init_data_interface(struct nand_chip *chip)
1166{
1167 struct mtd_info *mtd = nand_to_mtd(chip);
1168 int modes, mode, ret;
1169
1170 if (!chip->setup_data_interface)
1171 return 0;
1172
1173 /*
1174 * First try to identify the best timings from ONFI parameters and
1175 * if the NAND does not support ONFI, fallback to the default ONFI
1176 * timing mode.
1177 */
1178 modes = onfi_get_async_timing_mode(chip);
1179 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1180 if (!chip->onfi_timing_mode_default)
1181 return 0;
1182
1183 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1184 }
1185
1186 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1187 GFP_KERNEL);
1188 if (!chip->data_interface)
1189 return -ENOMEM;
1190
1191 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1192 ret = onfi_init_data_interface(chip, chip->data_interface,
1193 NAND_SDR_IFACE, mode);
1194 if (ret)
1195 continue;
1196
Boris Brezillon104e4422017-03-16 09:35:58 +01001197 /* Pass -1 to only */
1198 ret = chip->setup_data_interface(mtd,
1199 NAND_DATA_IFACE_CHECK_ONLY,
1200 chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001201 if (!ret) {
1202 chip->onfi_timing_mode_default = mode;
1203 break;
1204 }
1205 }
1206
1207 return 0;
1208}
1209
1210static void nand_release_data_interface(struct nand_chip *chip)
1211{
1212 kfree(chip->data_interface);
1213}
1214
1215/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001216 * nand_reset - Reset and initialize a NAND device
1217 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001218 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001219 *
1220 * Returns 0 for success or negative error code otherwise
1221 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001222int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001223{
1224 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001225 int ret;
1226
Boris Brezillon104e4422017-03-16 09:35:58 +01001227 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001228 if (ret)
1229 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001230
Boris Brezillon73f907f2016-10-24 16:46:20 +02001231 /*
1232 * The CS line has to be released before we can apply the new NAND
1233 * interface settings, hence this weird ->select_chip() dance.
1234 */
1235 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001236 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001237 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001238
Boris Brezillon73f907f2016-10-24 16:46:20 +02001239 chip->select_chip(mtd, chipnr);
Boris Brezillon104e4422017-03-16 09:35:58 +01001240 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001241 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001242 if (ret)
1243 return ret;
1244
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001245 return 0;
1246}
1247
1248/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001249 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1250 * @buf: buffer to test
1251 * @len: buffer length
1252 * @bitflips_threshold: maximum number of bitflips
1253 *
1254 * Check if a buffer contains only 0xff, which means the underlying region
1255 * has been erased and is ready to be programmed.
1256 * The bitflips_threshold specify the maximum number of bitflips before
1257 * considering the region is not erased.
1258 * Note: The logic of this function has been extracted from the memweight
1259 * implementation, except that nand_check_erased_buf function exit before
1260 * testing the whole buffer if the number of bitflips exceed the
1261 * bitflips_threshold value.
1262 *
1263 * Returns a positive number of bitflips less than or equal to
1264 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1265 * threshold.
1266 */
1267static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1268{
1269 const unsigned char *bitmap = buf;
1270 int bitflips = 0;
1271 int weight;
1272
1273 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1274 len--, bitmap++) {
1275 weight = hweight8(*bitmap);
1276 bitflips += BITS_PER_BYTE - weight;
1277 if (unlikely(bitflips > bitflips_threshold))
1278 return -EBADMSG;
1279 }
1280
1281 for (; len >= sizeof(long);
1282 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001283 unsigned long d = *((unsigned long *)bitmap);
1284 if (d == ~0UL)
1285 continue;
1286 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001287 bitflips += BITS_PER_LONG - weight;
1288 if (unlikely(bitflips > bitflips_threshold))
1289 return -EBADMSG;
1290 }
1291
1292 for (; len > 0; len--, bitmap++) {
1293 weight = hweight8(*bitmap);
1294 bitflips += BITS_PER_BYTE - weight;
1295 if (unlikely(bitflips > bitflips_threshold))
1296 return -EBADMSG;
1297 }
1298
1299 return bitflips;
1300}
1301
1302/**
1303 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1304 * 0xff data
1305 * @data: data buffer to test
1306 * @datalen: data length
1307 * @ecc: ECC buffer
1308 * @ecclen: ECC length
1309 * @extraoob: extra OOB buffer
1310 * @extraooblen: extra OOB length
1311 * @bitflips_threshold: maximum number of bitflips
1312 *
1313 * Check if a data buffer and its associated ECC and OOB data contains only
1314 * 0xff pattern, which means the underlying region has been erased and is
1315 * ready to be programmed.
1316 * The bitflips_threshold specify the maximum number of bitflips before
1317 * considering the region as not erased.
1318 *
1319 * Note:
1320 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1321 * different from the NAND page size. When fixing bitflips, ECC engines will
1322 * report the number of errors per chunk, and the NAND core infrastructure
1323 * expect you to return the maximum number of bitflips for the whole page.
1324 * This is why you should always use this function on a single chunk and
1325 * not on the whole page. After checking each chunk you should update your
1326 * max_bitflips value accordingly.
1327 * 2/ When checking for bitflips in erased pages you should not only check
1328 * the payload data but also their associated ECC data, because a user might
1329 * have programmed almost all bits to 1 but a few. In this case, we
1330 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1331 * this case.
1332 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1333 * data are protected by the ECC engine.
1334 * It could also be used if you support subpages and want to attach some
1335 * extra OOB data to an ECC chunk.
1336 *
1337 * Returns a positive number of bitflips less than or equal to
1338 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1339 * threshold. In case of success, the passed buffers are filled with 0xff.
1340 */
1341int nand_check_erased_ecc_chunk(void *data, int datalen,
1342 void *ecc, int ecclen,
1343 void *extraoob, int extraooblen,
1344 int bitflips_threshold)
1345{
1346 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1347
1348 data_bitflips = nand_check_erased_buf(data, datalen,
1349 bitflips_threshold);
1350 if (data_bitflips < 0)
1351 return data_bitflips;
1352
1353 bitflips_threshold -= data_bitflips;
1354
1355 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1356 if (ecc_bitflips < 0)
1357 return ecc_bitflips;
1358
1359 bitflips_threshold -= ecc_bitflips;
1360
1361 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1362 bitflips_threshold);
1363 if (extraoob_bitflips < 0)
1364 return extraoob_bitflips;
1365
1366 if (data_bitflips)
1367 memset(data, 0xff, datalen);
1368
1369 if (ecc_bitflips)
1370 memset(ecc, 0xff, ecclen);
1371
1372 if (extraoob_bitflips)
1373 memset(extraoob, 0xff, extraooblen);
1374
1375 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1376}
1377EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1378
1379/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001380 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001381 * @mtd: mtd info structure
1382 * @chip: nand chip info structure
1383 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001384 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001385 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001386 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001387 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001388 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001389int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1390 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001391{
1392 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001393 if (oob_required)
1394 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001395 return 0;
1396}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001397EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001398
1399/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001400 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001401 * @mtd: mtd info structure
1402 * @chip: nand chip info structure
1403 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001404 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001405 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001406 *
1407 * We need a special oob layout and handling even when OOB isn't used.
1408 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001409static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001410 struct nand_chip *chip, uint8_t *buf,
1411 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001412{
1413 int eccsize = chip->ecc.size;
1414 int eccbytes = chip->ecc.bytes;
1415 uint8_t *oob = chip->oob_poi;
1416 int steps, size;
1417
1418 for (steps = chip->ecc.steps; steps > 0; steps--) {
1419 chip->read_buf(mtd, buf, eccsize);
1420 buf += eccsize;
1421
1422 if (chip->ecc.prepad) {
1423 chip->read_buf(mtd, oob, chip->ecc.prepad);
1424 oob += chip->ecc.prepad;
1425 }
1426
1427 chip->read_buf(mtd, oob, eccbytes);
1428 oob += eccbytes;
1429
1430 if (chip->ecc.postpad) {
1431 chip->read_buf(mtd, oob, chip->ecc.postpad);
1432 oob += chip->ecc.postpad;
1433 }
1434 }
1435
1436 size = mtd->oobsize - (oob - chip->oob_poi);
1437 if (size)
1438 chip->read_buf(mtd, oob, size);
1439
1440 return 0;
1441}
1442
1443/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001444 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001445 * @mtd: mtd info structure
1446 * @chip: nand chip info structure
1447 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001448 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001449 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001450 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001451static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001452 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Boris Brezillon846031d2016-02-03 20:11:00 +01001454 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001455 int eccbytes = chip->ecc.bytes;
1456 int eccsteps = chip->ecc.steps;
1457 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001458 uint8_t *ecc_calc = chip->buffers->ecccalc;
1459 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001460 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001461
Brian Norris1fbb9382012-05-02 10:14:55 -07001462 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001463
1464 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1465 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1466
Boris Brezillon846031d2016-02-03 20:11:00 +01001467 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1468 chip->ecc.total);
1469 if (ret)
1470 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001471
1472 eccsteps = chip->ecc.steps;
1473 p = buf;
1474
1475 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1476 int stat;
1477
1478 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001479 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001480 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001481 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001482 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001483 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1484 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001485 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001486 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001487}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301490 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001491 * @mtd: mtd info structure
1492 * @chip: nand chip info structure
1493 * @data_offs: offset of requested data within the page
1494 * @readlen: data length
1495 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001496 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001497 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001498static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001499 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1500 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001501{
Boris Brezillon846031d2016-02-03 20:11:00 +01001502 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001503 uint8_t *p;
1504 int data_col_addr, i, gaps = 0;
1505 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1506 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001507 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001508 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001509 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001510
Brian Norris7854d3f2011-06-23 14:12:08 -07001511 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001512 start_step = data_offs / chip->ecc.size;
1513 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1514 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301515 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001516
Brian Norris8b6e50c2011-05-25 14:59:01 -07001517 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001518 datafrag_len = num_steps * chip->ecc.size;
1519 eccfrag_len = num_steps * chip->ecc.bytes;
1520
1521 data_col_addr = start_step * chip->ecc.size;
1522 /* If we read not a page aligned data */
1523 if (data_col_addr != 0)
1524 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1525
1526 p = bufpoi + data_col_addr;
1527 chip->read_buf(mtd, p, datafrag_len);
1528
Brian Norris8b6e50c2011-05-25 14:59:01 -07001529 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001530 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1531 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1532
Brian Norris8b6e50c2011-05-25 14:59:01 -07001533 /*
1534 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001535 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001536 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001537 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1538 if (ret)
1539 return ret;
1540
1541 if (oobregion.length < eccfrag_len)
1542 gaps = 1;
1543
Alexey Korolev3d459552008-05-15 17:23:18 +01001544 if (gaps) {
1545 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1546 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1547 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001548 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001549 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001550 * about buswidth alignment in read_buf.
1551 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001552 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001553 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001554 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001555 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001556 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1557 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001558 aligned_len++;
1559
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001560 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001561 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001562 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1563 }
1564
Boris Brezillon846031d2016-02-03 20:11:00 +01001565 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1566 chip->oob_poi, index, eccfrag_len);
1567 if (ret)
1568 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001569
1570 p = bufpoi + data_col_addr;
1571 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1572 int stat;
1573
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001574 stat = chip->ecc.correct(mtd, p,
1575 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001576 if (stat == -EBADMSG &&
1577 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1578 /* check for empty pages with bitflips */
1579 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1580 &chip->buffers->ecccode[i],
1581 chip->ecc.bytes,
1582 NULL, 0,
1583 chip->ecc.strength);
1584 }
1585
Mike Dunn3f91e942012-04-25 12:06:09 -07001586 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001587 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001588 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001589 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001590 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1591 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001592 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001593 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001594}
1595
1596/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001597 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001598 * @mtd: mtd info structure
1599 * @chip: nand chip info structure
1600 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001601 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001602 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001603 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001604 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001605 */
1606static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001607 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001608{
Boris Brezillon846031d2016-02-03 20:11:00 +01001609 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001610 int eccbytes = chip->ecc.bytes;
1611 int eccsteps = chip->ecc.steps;
1612 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001613 uint8_t *ecc_calc = chip->buffers->ecccalc;
1614 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001615 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001616
1617 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1618 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1619 chip->read_buf(mtd, p, eccsize);
1620 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1621 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001622 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001623
Boris Brezillon846031d2016-02-03 20:11:00 +01001624 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1625 chip->ecc.total);
1626 if (ret)
1627 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001628
1629 eccsteps = chip->ecc.steps;
1630 p = buf;
1631
1632 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1633 int stat;
1634
1635 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001636 if (stat == -EBADMSG &&
1637 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1638 /* check for empty pages with bitflips */
1639 stat = nand_check_erased_ecc_chunk(p, eccsize,
1640 &ecc_code[i], eccbytes,
1641 NULL, 0,
1642 chip->ecc.strength);
1643 }
1644
Mike Dunn3f91e942012-04-25 12:06:09 -07001645 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001646 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001647 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001648 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001649 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1650 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001651 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001652 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001653}
1654
1655/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001656 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001657 * @mtd: mtd info structure
1658 * @chip: nand chip info structure
1659 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001660 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001661 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001662 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001663 * Hardware ECC for large page chips, require OOB to be read first. For this
1664 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1665 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1666 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1667 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001668 */
1669static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001670 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001671{
Boris Brezillon846031d2016-02-03 20:11:00 +01001672 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001673 int eccbytes = chip->ecc.bytes;
1674 int eccsteps = chip->ecc.steps;
1675 uint8_t *p = buf;
1676 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001677 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001678 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001679
1680 /* Read the OOB area first */
1681 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1682 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1683 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1684
Boris Brezillon846031d2016-02-03 20:11:00 +01001685 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1686 chip->ecc.total);
1687 if (ret)
1688 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001689
1690 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1691 int stat;
1692
1693 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1694 chip->read_buf(mtd, p, eccsize);
1695 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1696
1697 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001698 if (stat == -EBADMSG &&
1699 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1700 /* check for empty pages with bitflips */
1701 stat = nand_check_erased_ecc_chunk(p, eccsize,
1702 &ecc_code[i], eccbytes,
1703 NULL, 0,
1704 chip->ecc.strength);
1705 }
1706
Mike Dunn3f91e942012-04-25 12:06:09 -07001707 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001708 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001709 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001710 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001711 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1712 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001713 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001714 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001715}
1716
1717/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001718 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001719 * @mtd: mtd info structure
1720 * @chip: nand chip info structure
1721 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001722 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001723 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001724 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001725 * The hw generator calculates the error syndrome automatically. Therefore we
1726 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001727 */
1728static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001729 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001730{
1731 int i, eccsize = chip->ecc.size;
1732 int eccbytes = chip->ecc.bytes;
1733 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001734 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001735 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001736 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001737 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001738
1739 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1740 int stat;
1741
1742 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1743 chip->read_buf(mtd, p, eccsize);
1744
1745 if (chip->ecc.prepad) {
1746 chip->read_buf(mtd, oob, chip->ecc.prepad);
1747 oob += chip->ecc.prepad;
1748 }
1749
1750 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1751 chip->read_buf(mtd, oob, eccbytes);
1752 stat = chip->ecc.correct(mtd, p, oob, NULL);
1753
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001754 oob += eccbytes;
1755
1756 if (chip->ecc.postpad) {
1757 chip->read_buf(mtd, oob, chip->ecc.postpad);
1758 oob += chip->ecc.postpad;
1759 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001760
1761 if (stat == -EBADMSG &&
1762 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1763 /* check for empty pages with bitflips */
1764 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1765 oob - eccpadbytes,
1766 eccpadbytes,
1767 NULL, 0,
1768 chip->ecc.strength);
1769 }
1770
1771 if (stat < 0) {
1772 mtd->ecc_stats.failed++;
1773 } else {
1774 mtd->ecc_stats.corrected += stat;
1775 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1776 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001777 }
1778
1779 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001780 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001781 if (i)
1782 chip->read_buf(mtd, oob, i);
1783
Mike Dunn3f91e942012-04-25 12:06:09 -07001784 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001785}
1786
1787/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001788 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001789 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001790 * @oob: oob destination address
1791 * @ops: oob ops structure
1792 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001793 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001794static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001795 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001796{
Boris Brezillon846031d2016-02-03 20:11:00 +01001797 struct nand_chip *chip = mtd_to_nand(mtd);
1798 int ret;
1799
Florian Fainellif8ac0412010-09-07 13:23:43 +02001800 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001801
Brian Norris0612b9d2011-08-30 18:45:40 -07001802 case MTD_OPS_PLACE_OOB:
1803 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001804 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1805 return oob + len;
1806
Boris Brezillon846031d2016-02-03 20:11:00 +01001807 case MTD_OPS_AUTO_OOB:
1808 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1809 ops->ooboffs, len);
1810 BUG_ON(ret);
1811 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001812
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001813 default:
1814 BUG();
1815 }
1816 return NULL;
1817}
1818
1819/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001820 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1821 * @mtd: MTD device structure
1822 * @retry_mode: the retry mode to use
1823 *
1824 * Some vendors supply a special command to shift the Vt threshold, to be used
1825 * when there are too many bitflips in a page (i.e., ECC error). After setting
1826 * a new threshold, the host should retry reading the page.
1827 */
1828static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1829{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001830 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001831
1832 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1833
1834 if (retry_mode >= chip->read_retries)
1835 return -EINVAL;
1836
1837 if (!chip->setup_read_retry)
1838 return -EOPNOTSUPP;
1839
1840 return chip->setup_read_retry(mtd, retry_mode);
1841}
1842
1843/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001844 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001845 * @mtd: MTD device structure
1846 * @from: offset to read from
1847 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001848 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001849 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001850 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001851static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1852 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001853{
Brian Norrise47f3db2012-05-02 10:14:56 -07001854 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001855 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001856 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001857 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001858 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001859 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001860
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001861 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001862 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001863 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001864 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001865 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001867 chipnr = (int)(from >> chip->chip_shift);
1868 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001870 realpage = (int)(from >> chip->page_shift);
1871 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001873 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001875 buf = ops->datbuf;
1876 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001877 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001878
Florian Fainellif8ac0412010-09-07 13:23:43 +02001879 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001880 unsigned int ecc_failures = mtd->ecc_stats.failed;
1881
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001882 bytes = min(mtd->writesize - col, readlen);
1883 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001884
Kamal Dasu66507c72014-05-01 20:51:19 -04001885 if (!aligned)
1886 use_bufpoi = 1;
1887 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09001888 use_bufpoi = !virt_addr_valid(buf) ||
1889 !IS_ALIGNED((unsigned long)buf,
1890 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04001891 else
1892 use_bufpoi = 0;
1893
Brian Norris8b6e50c2011-05-25 14:59:01 -07001894 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001895 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001896 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1897
1898 if (use_bufpoi && aligned)
1899 pr_debug("%s: using read bounce buffer for buf@%p\n",
1900 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Brian Norrisba84fb52014-01-03 15:13:33 -08001902read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01001903 if (nand_standard_page_accessors(&chip->ecc))
1904 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Mike Dunnedbc45402012-04-25 12:06:11 -07001906 /*
1907 * Now read the page into the buffer. Absent an error,
1908 * the read methods return max bitflips per ecc step.
1909 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001910 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001911 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001912 oob_required,
1913 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001914 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1915 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001916 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001917 col, bytes, bufpoi,
1918 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001919 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001920 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001921 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001922 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001923 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001924 /* Invalidate page cache */
1925 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001926 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001927 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001928
1929 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001930 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001931 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001932 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001933 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001934 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001935 chip->pagebuf_bitflips = ret;
1936 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001937 /* Invalidate page cache */
1938 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001939 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001940 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001942
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001943 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001944 int toread = min(oobreadlen, max_oobsize);
1945
1946 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01001947 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001948 oob, ops, toread);
1949 oobreadlen -= toread;
1950 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001951 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001952
1953 if (chip->options & NAND_NEED_READRDY) {
1954 /* Apply delay or wait for ready/busy pin */
1955 if (!chip->dev_ready)
1956 udelay(chip->chip_delay);
1957 else
1958 nand_wait_ready(mtd);
1959 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001960
Brian Norrisba84fb52014-01-03 15:13:33 -08001961 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001962 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001963 retry_mode++;
1964 ret = nand_setup_read_retry(mtd,
1965 retry_mode);
1966 if (ret < 0)
1967 break;
1968
1969 /* Reset failures; retry */
1970 mtd->ecc_stats.failed = ecc_failures;
1971 goto read_retry;
1972 } else {
1973 /* No more retry modes; real failure */
1974 ecc_fail = true;
1975 }
1976 }
1977
1978 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09001979 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001980 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001981 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001982 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001983 max_bitflips = max_t(unsigned int, max_bitflips,
1984 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001987 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001988
Brian Norrisba84fb52014-01-03 15:13:33 -08001989 /* Reset to retry mode 0 */
1990 if (retry_mode) {
1991 ret = nand_setup_read_retry(mtd, 0);
1992 if (ret < 0)
1993 break;
1994 retry_mode = 0;
1995 }
1996
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001997 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001998 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Brian Norris8b6e50c2011-05-25 14:59:01 -07002000 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 col = 0;
2002 /* Increment page address */
2003 realpage++;
2004
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002005 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /* Check, if we cross a chip boundary */
2007 if (!page) {
2008 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002009 chip->select_chip(mtd, -1);
2010 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002013 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002015 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002016 if (oob)
2017 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Mike Dunn3f91e942012-04-25 12:06:09 -07002019 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002020 return ret;
2021
Brian Norrisb72f3df2013-12-03 11:04:14 -08002022 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002023 return -EBADMSG;
2024
Mike Dunnedbc45402012-04-25 12:06:11 -07002025 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002026}
2027
2028/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002029 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002030 * @mtd: MTD device structure
2031 * @from: offset to read from
2032 * @len: number of bytes to read
2033 * @retlen: pointer to variable to store the number of read bytes
2034 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002035 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002036 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002037 */
2038static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2039 size_t *retlen, uint8_t *buf)
2040{
Brian Norris4a89ff82011-08-30 18:45:45 -07002041 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002042 int ret;
2043
Huang Shijie6a8214a2012-11-19 14:43:30 +08002044 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002045 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002046 ops.len = len;
2047 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002048 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002049 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002050 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002051 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002052 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
2055/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002056 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002057 * @mtd: mtd info structure
2058 * @chip: nand chip info structure
2059 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002060 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002061int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002062{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002063 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002064 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002065 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002066}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002067EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002068
2069/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002070 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002071 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002072 * @mtd: mtd info structure
2073 * @chip: nand chip info structure
2074 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002075 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002076int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2077 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002078{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002079 int length = mtd->oobsize;
2080 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2081 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002082 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002083 int i, toread, sndrnd = 0, pos;
2084
2085 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2086 for (i = 0; i < chip->ecc.steps; i++) {
2087 if (sndrnd) {
2088 pos = eccsize + i * (eccsize + chunk);
2089 if (mtd->writesize > 512)
2090 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2091 else
2092 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2093 } else
2094 sndrnd = 1;
2095 toread = min_t(int, length, chunk);
2096 chip->read_buf(mtd, bufpoi, toread);
2097 bufpoi += toread;
2098 length -= toread;
2099 }
2100 if (length > 0)
2101 chip->read_buf(mtd, bufpoi, length);
2102
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002103 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002104}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002105EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002106
2107/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002108 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002109 * @mtd: mtd info structure
2110 * @chip: nand chip info structure
2111 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002112 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002113int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002114{
2115 int status = 0;
2116 const uint8_t *buf = chip->oob_poi;
2117 int length = mtd->oobsize;
2118
2119 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2120 chip->write_buf(mtd, buf, length);
2121 /* Send command to program the OOB data */
2122 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2123
2124 status = chip->waitfunc(mtd, chip);
2125
Savin Zlobec0d420f92006-06-21 11:51:20 +02002126 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002127}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002128EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002129
2130/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002131 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002132 * with syndrome - only for large page flash
2133 * @mtd: mtd info structure
2134 * @chip: nand chip info structure
2135 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002136 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002137int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2138 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002139{
2140 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2141 int eccsize = chip->ecc.size, length = mtd->oobsize;
2142 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2143 const uint8_t *bufpoi = chip->oob_poi;
2144
2145 /*
2146 * data-ecc-data-ecc ... ecc-oob
2147 * or
2148 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2149 */
2150 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2151 pos = steps * (eccsize + chunk);
2152 steps = 0;
2153 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002154 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002155
2156 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2157 for (i = 0; i < steps; i++) {
2158 if (sndcmd) {
2159 if (mtd->writesize <= 512) {
2160 uint32_t fill = 0xFFFFFFFF;
2161
2162 len = eccsize;
2163 while (len > 0) {
2164 int num = min_t(int, len, 4);
2165 chip->write_buf(mtd, (uint8_t *)&fill,
2166 num);
2167 len -= num;
2168 }
2169 } else {
2170 pos = eccsize + i * (eccsize + chunk);
2171 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2172 }
2173 } else
2174 sndcmd = 1;
2175 len = min_t(int, length, chunk);
2176 chip->write_buf(mtd, bufpoi, len);
2177 bufpoi += len;
2178 length -= len;
2179 }
2180 if (length > 0)
2181 chip->write_buf(mtd, bufpoi, length);
2182
2183 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2184 status = chip->waitfunc(mtd, chip);
2185
2186 return status & NAND_STATUS_FAIL ? -EIO : 0;
2187}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002188EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002189
2190/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002191 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002192 * @mtd: MTD device structure
2193 * @from: offset to read from
2194 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002196 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002198static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2199 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Brian Norrisc00a0992012-05-01 17:12:54 -07002201 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002202 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002203 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002204 int readlen = ops->ooblen;
2205 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002206 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002207 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Brian Norris289c0522011-07-19 10:06:09 -07002209 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302210 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Brian Norris041e4572011-06-23 16:45:24 -07002212 stats = mtd->ecc_stats;
2213
Boris BREZILLON29f10582016-03-07 10:46:52 +01002214 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002215
2216 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002217 pr_debug("%s: attempt to start read outside oob\n",
2218 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002219 return -EINVAL;
2220 }
2221
2222 /* Do not allow reads past end of device */
2223 if (unlikely(from >= mtd->size ||
2224 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2225 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002226 pr_debug("%s: attempt to read beyond end of device\n",
2227 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002228 return -EINVAL;
2229 }
Vitaly Wool70145682006-11-03 18:20:38 +03002230
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002231 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002232 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002234 /* Shift to get page */
2235 realpage = (int)(from >> chip->page_shift);
2236 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Florian Fainellif8ac0412010-09-07 13:23:43 +02002238 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002239 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002240 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002241 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002242 ret = chip->ecc.read_oob(mtd, chip, page);
2243
2244 if (ret < 0)
2245 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002246
2247 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002248 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002249
Brian Norris5bc7c332013-03-13 09:51:31 -07002250 if (chip->options & NAND_NEED_READRDY) {
2251 /* Apply delay or wait for ready/busy pin */
2252 if (!chip->dev_ready)
2253 udelay(chip->chip_delay);
2254 else
2255 nand_wait_ready(mtd);
2256 }
2257
Vitaly Wool70145682006-11-03 18:20:38 +03002258 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002259 if (!readlen)
2260 break;
2261
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002262 /* Increment page address */
2263 realpage++;
2264
2265 page = realpage & chip->pagemask;
2266 /* Check, if we cross a chip boundary */
2267 if (!page) {
2268 chipnr++;
2269 chip->select_chip(mtd, -1);
2270 chip->select_chip(mtd, chipnr);
2271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002273 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002275 ops->oobretlen = ops->ooblen - readlen;
2276
2277 if (ret < 0)
2278 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002279
2280 if (mtd->ecc_stats.failed - stats.failed)
2281 return -EBADMSG;
2282
2283 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
2286/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002287 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002288 * @mtd: MTD device structure
2289 * @from: offset to read from
2290 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002292 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002294static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2295 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002297 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002298
2299 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002302 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002303 pr_debug("%s: attempt to read beyond end of device\n",
2304 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return -EINVAL;
2306 }
2307
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002308 if (ops->mode != MTD_OPS_PLACE_OOB &&
2309 ops->mode != MTD_OPS_AUTO_OOB &&
2310 ops->mode != MTD_OPS_RAW)
2311 return -ENOTSUPP;
2312
Huang Shijie6a8214a2012-11-19 14:43:30 +08002313 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002315 if (!ops->datbuf)
2316 ret = nand_do_read_oob(mtd, from, ops);
2317 else
2318 ret = nand_do_read_ops(mtd, from, ops);
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002321 return ret;
2322}
2323
2324
2325/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002326 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002327 * @mtd: mtd info structure
2328 * @chip: nand chip info structure
2329 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002330 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002331 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002332 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002333 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002334 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002335int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2336 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002337{
2338 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002339 if (oob_required)
2340 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002341
2342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002344EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002346/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002347 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002348 * @mtd: mtd info structure
2349 * @chip: nand chip info structure
2350 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002351 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002352 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002353 *
2354 * We need a special oob layout and handling even when ECC isn't checked.
2355 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002356static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002357 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002358 const uint8_t *buf, int oob_required,
2359 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002360{
2361 int eccsize = chip->ecc.size;
2362 int eccbytes = chip->ecc.bytes;
2363 uint8_t *oob = chip->oob_poi;
2364 int steps, size;
2365
2366 for (steps = chip->ecc.steps; steps > 0; steps--) {
2367 chip->write_buf(mtd, buf, eccsize);
2368 buf += eccsize;
2369
2370 if (chip->ecc.prepad) {
2371 chip->write_buf(mtd, oob, chip->ecc.prepad);
2372 oob += chip->ecc.prepad;
2373 }
2374
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002375 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002376 oob += eccbytes;
2377
2378 if (chip->ecc.postpad) {
2379 chip->write_buf(mtd, oob, chip->ecc.postpad);
2380 oob += chip->ecc.postpad;
2381 }
2382 }
2383
2384 size = mtd->oobsize - (oob - chip->oob_poi);
2385 if (size)
2386 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002387
2388 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002389}
2390/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002391 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002392 * @mtd: mtd info structure
2393 * @chip: nand chip info structure
2394 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002395 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002396 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002397 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002398static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002399 const uint8_t *buf, int oob_required,
2400 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002401{
Boris Brezillon846031d2016-02-03 20:11:00 +01002402 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002403 int eccbytes = chip->ecc.bytes;
2404 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002405 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002406 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002407
Brian Norris7854d3f2011-06-23 14:12:08 -07002408 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002409 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2410 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002411
Boris Brezillon846031d2016-02-03 20:11:00 +01002412 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2413 chip->ecc.total);
2414 if (ret)
2415 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002416
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002417 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002418}
2419
2420/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002421 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002422 * @mtd: mtd info structure
2423 * @chip: nand chip info structure
2424 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002425 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002426 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002427 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002428static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002429 const uint8_t *buf, int oob_required,
2430 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002431{
Boris Brezillon846031d2016-02-03 20:11:00 +01002432 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002433 int eccbytes = chip->ecc.bytes;
2434 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002435 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002436 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002437
2438 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2439 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002440 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002441 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2442 }
2443
Boris Brezillon846031d2016-02-03 20:11:00 +01002444 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2445 chip->ecc.total);
2446 if (ret)
2447 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002448
2449 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002450
2451 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002452}
2453
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302454
2455/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002456 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302457 * @mtd: mtd info structure
2458 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002459 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302460 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002461 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302462 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002463 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302464 */
2465static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2466 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002467 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002468 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302469{
2470 uint8_t *oob_buf = chip->oob_poi;
2471 uint8_t *ecc_calc = chip->buffers->ecccalc;
2472 int ecc_size = chip->ecc.size;
2473 int ecc_bytes = chip->ecc.bytes;
2474 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302475 uint32_t start_step = offset / ecc_size;
2476 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2477 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002478 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302479
2480 for (step = 0; step < ecc_steps; step++) {
2481 /* configure controller for WRITE access */
2482 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2483
2484 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002485 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302486
2487 /* mask ECC of un-touched subpages by padding 0xFF */
2488 if ((step < start_step) || (step > end_step))
2489 memset(ecc_calc, 0xff, ecc_bytes);
2490 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002491 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302492
2493 /* mask OOB of un-touched subpages by padding 0xFF */
2494 /* if oob_required, preserve OOB metadata of written subpage */
2495 if (!oob_required || (step < start_step) || (step > end_step))
2496 memset(oob_buf, 0xff, oob_bytes);
2497
Brian Norrisd6a950802013-08-08 17:16:36 -07002498 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302499 ecc_calc += ecc_bytes;
2500 oob_buf += oob_bytes;
2501 }
2502
2503 /* copy calculated ECC for whole page to chip->buffer->oob */
2504 /* this include masked-value(0xFF) for unwritten subpages */
2505 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002506 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2507 chip->ecc.total);
2508 if (ret)
2509 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302510
2511 /* write OOB buffer to NAND device */
2512 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2513
2514 return 0;
2515}
2516
2517
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002518/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002519 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002520 * @mtd: mtd info structure
2521 * @chip: nand chip info structure
2522 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002523 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002524 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002525 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002526 * The hw generator calculates the error syndrome automatically. Therefore we
2527 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002528 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002529static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002530 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002531 const uint8_t *buf, int oob_required,
2532 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002533{
2534 int i, eccsize = chip->ecc.size;
2535 int eccbytes = chip->ecc.bytes;
2536 int eccsteps = chip->ecc.steps;
2537 const uint8_t *p = buf;
2538 uint8_t *oob = chip->oob_poi;
2539
2540 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2541
2542 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2543 chip->write_buf(mtd, p, eccsize);
2544
2545 if (chip->ecc.prepad) {
2546 chip->write_buf(mtd, oob, chip->ecc.prepad);
2547 oob += chip->ecc.prepad;
2548 }
2549
2550 chip->ecc.calculate(mtd, p, oob);
2551 chip->write_buf(mtd, oob, eccbytes);
2552 oob += eccbytes;
2553
2554 if (chip->ecc.postpad) {
2555 chip->write_buf(mtd, oob, chip->ecc.postpad);
2556 oob += chip->ecc.postpad;
2557 }
2558 }
2559
2560 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002561 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002562 if (i)
2563 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002564
2565 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002566}
2567
2568/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002569 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002570 * @mtd: MTD device structure
2571 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302572 * @offset: address offset within the page
2573 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002574 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002575 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002576 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002577 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002578 */
2579static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302580 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002581 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002582{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302583 int status, subpage;
2584
2585 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2586 chip->ecc.write_subpage)
2587 subpage = offset || (data_len < mtd->writesize);
2588 else
2589 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002590
Marc Gonzalez3371d662016-11-15 10:56:20 +01002591 if (nand_standard_page_accessors(&chip->ecc))
2592 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002593
David Woodhouse956e9442006-09-25 17:12:39 +01002594 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302595 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002596 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302597 else if (subpage)
2598 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002599 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002600 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002601 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2602 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002603
2604 if (status < 0)
2605 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002606
Boris Brezillon41145642017-05-16 18:27:49 +02002607 if (nand_standard_page_accessors(&chip->ecc)) {
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002608 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002609
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002610 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002611 if (status & NAND_STATUS_FAIL)
2612 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002613 }
2614
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002615 return 0;
2616}
2617
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002618/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002619 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002620 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002621 * @oob: oob data buffer
2622 * @len: oob data write length
2623 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002624 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002625static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2626 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002627{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002628 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002629 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002630
2631 /*
2632 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2633 * data from a previous OOB read.
2634 */
2635 memset(chip->oob_poi, 0xff, mtd->oobsize);
2636
Florian Fainellif8ac0412010-09-07 13:23:43 +02002637 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002638
Brian Norris0612b9d2011-08-30 18:45:40 -07002639 case MTD_OPS_PLACE_OOB:
2640 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002641 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2642 return oob + len;
2643
Boris Brezillon846031d2016-02-03 20:11:00 +01002644 case MTD_OPS_AUTO_OOB:
2645 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2646 ops->ooboffs, len);
2647 BUG_ON(ret);
2648 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002649
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002650 default:
2651 BUG();
2652 }
2653 return NULL;
2654}
2655
Florian Fainellif8ac0412010-09-07 13:23:43 +02002656#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002657
2658/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002659 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002660 * @mtd: MTD device structure
2661 * @to: offset to write to
2662 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002663 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002664 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002665 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002666static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2667 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002668{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002669 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002670 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002671 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002672
2673 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002674 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002675
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002676 uint8_t *oob = ops->oobbuf;
2677 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302678 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002679 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002680
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002681 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002682 if (!writelen)
2683 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002684
Brian Norris8b6e50c2011-05-25 14:59:01 -07002685 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002686 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002687 pr_notice("%s: attempt to write non page aligned data\n",
2688 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002689 return -EINVAL;
2690 }
2691
Thomas Gleixner29072b92006-09-28 15:38:36 +02002692 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002693
Thomas Gleixner6a930962006-06-28 00:11:45 +02002694 chipnr = (int)(to >> chip->chip_shift);
2695 chip->select_chip(mtd, chipnr);
2696
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002697 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002698 if (nand_check_wp(mtd)) {
2699 ret = -EIO;
2700 goto err_out;
2701 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002702
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002703 realpage = (int)(to >> chip->page_shift);
2704 page = realpage & chip->pagemask;
2705 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2706
2707 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002708 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2709 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002710 chip->pagebuf = -1;
2711
Maxim Levitsky782ce792010-02-22 20:39:36 +02002712 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002713 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2714 ret = -EINVAL;
2715 goto err_out;
2716 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002717
Florian Fainellif8ac0412010-09-07 13:23:43 +02002718 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002719 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002720 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002721 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002722 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002723
Kamal Dasu66507c72014-05-01 20:51:19 -04002724 if (part_pagewr)
2725 use_bufpoi = 1;
2726 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002727 use_bufpoi = !virt_addr_valid(buf) ||
2728 !IS_ALIGNED((unsigned long)buf,
2729 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002730 else
2731 use_bufpoi = 0;
2732
2733 /* Partial page write?, or need to use bounce buffer */
2734 if (use_bufpoi) {
2735 pr_debug("%s: using write bounce buffer for buf@%p\n",
2736 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04002737 if (part_pagewr)
2738 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002739 chip->pagebuf = -1;
2740 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2741 memcpy(&chip->buffers->databuf[column], buf, bytes);
2742 wbuf = chip->buffers->databuf;
2743 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002744
Maxim Levitsky782ce792010-02-22 20:39:36 +02002745 if (unlikely(oob)) {
2746 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002747 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002748 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002749 } else {
2750 /* We still need to erase leftover OOB data */
2751 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002752 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002753
2754 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002755 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002756 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002757 if (ret)
2758 break;
2759
2760 writelen -= bytes;
2761 if (!writelen)
2762 break;
2763
Thomas Gleixner29072b92006-09-28 15:38:36 +02002764 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002765 buf += bytes;
2766 realpage++;
2767
2768 page = realpage & chip->pagemask;
2769 /* Check, if we cross a chip boundary */
2770 if (!page) {
2771 chipnr++;
2772 chip->select_chip(mtd, -1);
2773 chip->select_chip(mtd, chipnr);
2774 }
2775 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002776
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002777 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002778 if (unlikely(oob))
2779 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002780
2781err_out:
2782 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002783 return ret;
2784}
2785
2786/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002787 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002788 * @mtd: MTD device structure
2789 * @to: offset to write to
2790 * @len: number of bytes to write
2791 * @retlen: pointer to variable to store the number of written bytes
2792 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002793 *
2794 * NAND write with ECC. Used when performing writes in interrupt context, this
2795 * may for example be called by mtdoops when writing an oops while in panic.
2796 */
2797static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2798 size_t *retlen, const uint8_t *buf)
2799{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002800 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002801 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002802 int ret;
2803
Brian Norris8b6e50c2011-05-25 14:59:01 -07002804 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002805 panic_nand_wait(mtd, chip, 400);
2806
Brian Norris8b6e50c2011-05-25 14:59:01 -07002807 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002808 panic_nand_get_device(chip, mtd, FL_WRITING);
2809
Brian Norris0ec56dc2015-02-28 02:02:30 -08002810 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002811 ops.len = len;
2812 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002813 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002814
Brian Norris4a89ff82011-08-30 18:45:45 -07002815 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002816
Brian Norris4a89ff82011-08-30 18:45:45 -07002817 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002818 return ret;
2819}
2820
2821/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002822 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002823 * @mtd: MTD device structure
2824 * @to: offset to write to
2825 * @len: number of bytes to write
2826 * @retlen: pointer to variable to store the number of written bytes
2827 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002829 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002831static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002832 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
Brian Norris4a89ff82011-08-30 18:45:45 -07002834 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002835 int ret;
2836
Huang Shijie6a8214a2012-11-19 14:43:30 +08002837 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002838 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002839 ops.len = len;
2840 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002841 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002842 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002843 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002844 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002845 return ret;
2846}
2847
2848/**
2849 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002850 * @mtd: MTD device structure
2851 * @to: offset to write to
2852 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002853 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002854 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002855 */
2856static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2857 struct mtd_oob_ops *ops)
2858{
Adrian Hunter03736152007-01-31 17:58:29 +02002859 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002860 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Brian Norris289c0522011-07-19 10:06:09 -07002862 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302863 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Boris BREZILLON29f10582016-03-07 10:46:52 +01002865 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002868 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002869 pr_debug("%s: attempt to write past end of page\n",
2870 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return -EINVAL;
2872 }
2873
Adrian Hunter03736152007-01-31 17:58:29 +02002874 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002875 pr_debug("%s: attempt to start write outside oob\n",
2876 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002877 return -EINVAL;
2878 }
2879
Jason Liu775adc3d42011-02-25 13:06:18 +08002880 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002881 if (unlikely(to >= mtd->size ||
2882 ops->ooboffs + ops->ooblen >
2883 ((mtd->size >> chip->page_shift) -
2884 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002885 pr_debug("%s: attempt to write beyond end of device\n",
2886 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002887 return -EINVAL;
2888 }
2889
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002890 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002891
2892 /*
2893 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2894 * of my DiskOnChip 2000 test units) will clear the whole data page too
2895 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2896 * it in the doc2000 driver in August 1999. dwmw2.
2897 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002898 nand_reset(chip, chipnr);
2899
2900 chip->select_chip(mtd, chipnr);
2901
2902 /* Shift to get page */
2903 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
2905 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002906 if (nand_check_wp(mtd)) {
2907 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002908 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002909 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002912 if (page == chip->pagebuf)
2913 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002915 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002916
Brian Norris0612b9d2011-08-30 18:45:40 -07002917 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002918 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2919 else
2920 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002921
Huang Shijieb0bb6902012-11-19 14:43:29 +08002922 chip->select_chip(mtd, -1);
2923
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002924 if (status)
2925 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Vitaly Wool70145682006-11-03 18:20:38 +03002927 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002929 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002930}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002932/**
2933 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002934 * @mtd: MTD device structure
2935 * @to: offset to write to
2936 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002937 */
2938static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2939 struct mtd_oob_ops *ops)
2940{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002941 int ret = -ENOTSUPP;
2942
2943 ops->retlen = 0;
2944
2945 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002946 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002947 pr_debug("%s: attempt to write beyond end of device\n",
2948 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002949 return -EINVAL;
2950 }
2951
Huang Shijie6a8214a2012-11-19 14:43:30 +08002952 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002953
Florian Fainellif8ac0412010-09-07 13:23:43 +02002954 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002955 case MTD_OPS_PLACE_OOB:
2956 case MTD_OPS_AUTO_OOB:
2957 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002958 break;
2959
2960 default:
2961 goto out;
2962 }
2963
2964 if (!ops->datbuf)
2965 ret = nand_do_write_oob(mtd, to, ops);
2966 else
2967 ret = nand_do_write_ops(mtd, to, ops);
2968
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002969out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002970 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 return ret;
2972}
2973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974/**
Brian Norris49c50b92014-05-06 16:02:19 -07002975 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002976 * @mtd: MTD device structure
2977 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 *
Brian Norris49c50b92014-05-06 16:02:19 -07002979 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 */
Brian Norris49c50b92014-05-06 16:02:19 -07002981static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002983 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002985 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2986 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002987
2988 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
2991/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002993 * @mtd: MTD device structure
2994 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002996 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002998static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
David Woodhousee0c7d762006-05-13 18:07:53 +01003000 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003004 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003005 * @mtd: MTD device structure
3006 * @instr: erase instruction
3007 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003009 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003011int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3012 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
Adrian Hunter69423d92008-12-10 13:37:21 +00003014 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003015 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003016 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Brian Norris289c0522011-07-19 10:06:09 -07003018 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3019 __func__, (unsigned long long)instr->addr,
3020 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303022 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003026 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003029 page = (int)(instr->addr >> chip->page_shift);
3030 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003033 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003036 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 /* Check, if it is write protected */
3039 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003040 pr_debug("%s: device is write protected!\n",
3041 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 instr->state = MTD_ERASE_FAILED;
3043 goto erase_exit;
3044 }
3045
3046 /* Loop through the pages */
3047 len = instr->len;
3048
3049 instr->state = MTD_ERASING;
3050
3051 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003052 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003053 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303054 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003055 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3056 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 instr->state = MTD_ERASE_FAILED;
3058 goto erase_exit;
3059 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003060
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003061 /*
3062 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003063 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003064 */
3065 if (page <= chip->pagebuf && chip->pagebuf <
3066 (page + pages_per_block))
3067 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Brian Norris49c50b92014-05-06 16:02:19 -07003069 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003072 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003073 pr_debug("%s: failed erase, page 0x%08x\n",
3074 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003076 instr->fail_addr =
3077 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 goto erase_exit;
3079 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003082 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 page += pages_per_block;
3084
3085 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003086 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003088 chip->select_chip(mtd, -1);
3089 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
3091 }
3092 instr->state = MTD_ERASE_DONE;
3093
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003094erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
3096 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003099 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 nand_release_device(mtd);
3101
David Woodhouse49defc02007-10-06 15:01:59 -04003102 /* Do call back function */
3103 if (!ret)
3104 mtd_erase_callback(instr);
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 /* Return more or less happy */
3107 return ret;
3108}
3109
3110/**
3111 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003112 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003114 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003116static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
Brian Norris289c0522011-07-19 10:06:09 -07003118 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003121 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003123 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003127 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003128 * @mtd: MTD device structure
3129 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003131static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303133 struct nand_chip *chip = mtd_to_nand(mtd);
3134 int chipnr = (int)(offs >> chip->chip_shift);
3135 int ret;
3136
3137 /* Select the NAND device */
3138 nand_get_device(mtd, FL_READING);
3139 chip->select_chip(mtd, chipnr);
3140
3141 ret = nand_block_checkbad(mtd, offs, 0);
3142
3143 chip->select_chip(mtd, -1);
3144 nand_release_device(mtd);
3145
3146 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147}
3148
3149/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003150 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003151 * @mtd: MTD device structure
3152 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003154static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 int ret;
3157
Florian Fainellif8ac0412010-09-07 13:23:43 +02003158 ret = nand_block_isbad(mtd, ofs);
3159 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003160 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (ret > 0)
3162 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003163 return ret;
3164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Brian Norris5a0edb22013-07-30 17:52:58 -07003166 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
3169/**
Zach Brown56718422017-01-10 13:30:20 -06003170 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3171 * @mtd: MTD device structure
3172 * @ofs: offset relative to mtd start
3173 * @len: length of mtd
3174 */
3175static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3176{
3177 struct nand_chip *chip = mtd_to_nand(mtd);
3178 u32 part_start_block;
3179 u32 part_end_block;
3180 u32 part_start_die;
3181 u32 part_end_die;
3182
3183 /*
3184 * max_bb_per_die and blocks_per_die used to determine
3185 * the maximum bad block count.
3186 */
3187 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3188 return -ENOTSUPP;
3189
3190 /* Get the start and end of the partition in erase blocks. */
3191 part_start_block = mtd_div_by_eb(ofs, mtd);
3192 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3193
3194 /* Get the start and end LUNs of the partition. */
3195 part_start_die = part_start_block / chip->blocks_per_die;
3196 part_end_die = part_end_block / chip->blocks_per_die;
3197
3198 /*
3199 * Look up the bad blocks per unit and multiply by the number of units
3200 * that the partition spans.
3201 */
3202 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3203}
3204
3205/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003206 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3207 * @mtd: MTD device structure
3208 * @chip: nand chip info structure
3209 * @addr: feature address.
3210 * @subfeature_param: the subfeature parameters, a four bytes array.
3211 */
3212static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3213 int addr, uint8_t *subfeature_param)
3214{
3215 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003216 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003217
David Mosbergerd914c932013-05-29 15:30:13 +03003218 if (!chip->onfi_version ||
3219 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3220 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003221 return -EINVAL;
3222
3223 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003224 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3225 chip->write_byte(mtd, subfeature_param[i]);
3226
Huang Shijie7db03ec2012-09-13 14:57:52 +08003227 status = chip->waitfunc(mtd, chip);
3228 if (status & NAND_STATUS_FAIL)
3229 return -EIO;
3230 return 0;
3231}
3232
3233/**
3234 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3235 * @mtd: MTD device structure
3236 * @chip: nand chip info structure
3237 * @addr: feature address.
3238 * @subfeature_param: the subfeature parameters, a four bytes array.
3239 */
3240static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3241 int addr, uint8_t *subfeature_param)
3242{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003243 int i;
3244
David Mosbergerd914c932013-05-29 15:30:13 +03003245 if (!chip->onfi_version ||
3246 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3247 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003248 return -EINVAL;
3249
Huang Shijie7db03ec2012-09-13 14:57:52 +08003250 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003251 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3252 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003253 return 0;
3254}
3255
3256/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003257 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3258 * -ENOTSUPP
3259 * @mtd: MTD device structure
3260 * @chip: nand chip info structure
3261 * @addr: feature address.
3262 * @subfeature_param: the subfeature parameters, a four bytes array.
3263 *
3264 * Should be used by NAND controller drivers that do not support the SET/GET
3265 * FEATURES operations.
3266 */
3267int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3268 struct nand_chip *chip, int addr,
3269 u8 *subfeature_param)
3270{
3271 return -ENOTSUPP;
3272}
3273EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3274
3275/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003276 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003277 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003278 */
3279static int nand_suspend(struct mtd_info *mtd)
3280{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003281 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003282}
3283
3284/**
3285 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003286 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003287 */
3288static void nand_resume(struct mtd_info *mtd)
3289{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003290 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003291
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003292 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003293 nand_release_device(mtd);
3294 else
Brian Norrisd0370212011-07-19 10:06:08 -07003295 pr_err("%s called for a chip which is not in suspended state\n",
3296 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003297}
3298
Scott Branden72ea4032014-11-20 11:18:05 -08003299/**
3300 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3301 * prevent further operations
3302 * @mtd: MTD device structure
3303 */
3304static void nand_shutdown(struct mtd_info *mtd)
3305{
Brian Norris9ca641b2015-11-09 16:37:28 -08003306 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003307}
3308
Brian Norris8b6e50c2011-05-25 14:59:01 -07003309/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003310static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003311{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003312 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003315 if (!chip->chip_delay)
3316 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
3318 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003319 if (chip->cmdfunc == NULL)
3320 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003323 if (chip->waitfunc == NULL)
3324 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003326 if (!chip->select_chip)
3327 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003328
Huang Shijie4204ccc2013-08-16 10:10:07 +08003329 /* set for ONFI nand */
3330 if (!chip->onfi_set_features)
3331 chip->onfi_set_features = nand_onfi_set_features;
3332 if (!chip->onfi_get_features)
3333 chip->onfi_get_features = nand_onfi_get_features;
3334
Brian Norris68e80782013-07-18 01:17:02 -07003335 /* If called twice, pointers that depend on busw may need to be reset */
3336 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003337 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3338 if (!chip->read_word)
3339 chip->read_word = nand_read_word;
3340 if (!chip->block_bad)
3341 chip->block_bad = nand_block_bad;
3342 if (!chip->block_markbad)
3343 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003344 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003345 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003346 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3347 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003348 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003349 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003350 if (!chip->scan_bbt)
3351 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003352
3353 if (!chip->controller) {
3354 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003355 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003356 }
3357
Masahiro Yamada477544c2017-03-30 17:15:05 +09003358 if (!chip->buf_align)
3359 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003360}
3361
Brian Norris8b6e50c2011-05-25 14:59:01 -07003362/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003363static void sanitize_string(uint8_t *s, size_t len)
3364{
3365 ssize_t i;
3366
Brian Norris8b6e50c2011-05-25 14:59:01 -07003367 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003368 s[len - 1] = 0;
3369
Brian Norris8b6e50c2011-05-25 14:59:01 -07003370 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003371 for (i = 0; i < len - 1; i++) {
3372 if (s[i] < ' ' || s[i] > 127)
3373 s[i] = '?';
3374 }
3375
Brian Norris8b6e50c2011-05-25 14:59:01 -07003376 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003377 strim(s);
3378}
3379
3380static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3381{
3382 int i;
3383 while (len--) {
3384 crc ^= *p++ << 8;
3385 for (i = 0; i < 8; i++)
3386 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3387 }
3388
3389 return crc;
3390}
3391
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003392/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003393static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3394 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003395{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003396 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003397 struct onfi_ext_param_page *ep;
3398 struct onfi_ext_section *s;
3399 struct onfi_ext_ecc_info *ecc;
3400 uint8_t *cursor;
3401 int ret = -EINVAL;
3402 int len;
3403 int i;
3404
3405 len = le16_to_cpu(p->ext_param_page_length) * 16;
3406 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003407 if (!ep)
3408 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003409
3410 /* Send our own NAND_CMD_PARAM. */
3411 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3412
3413 /* Use the Change Read Column command to skip the ONFI param pages. */
3414 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3415 sizeof(*p) * p->num_of_param_pages , -1);
3416
3417 /* Read out the Extended Parameter Page. */
3418 chip->read_buf(mtd, (uint8_t *)ep, len);
3419 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3420 != le16_to_cpu(ep->crc))) {
3421 pr_debug("fail in the CRC.\n");
3422 goto ext_out;
3423 }
3424
3425 /*
3426 * Check the signature.
3427 * Do not strictly follow the ONFI spec, maybe changed in future.
3428 */
3429 if (strncmp(ep->sig, "EPPS", 4)) {
3430 pr_debug("The signature is invalid.\n");
3431 goto ext_out;
3432 }
3433
3434 /* find the ECC section. */
3435 cursor = (uint8_t *)(ep + 1);
3436 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3437 s = ep->sections + i;
3438 if (s->type == ONFI_SECTION_TYPE_2)
3439 break;
3440 cursor += s->length * 16;
3441 }
3442 if (i == ONFI_EXT_SECTION_MAX) {
3443 pr_debug("We can not find the ECC section.\n");
3444 goto ext_out;
3445 }
3446
3447 /* get the info we want. */
3448 ecc = (struct onfi_ext_ecc_info *)cursor;
3449
Brian Norris4ae7d222013-09-16 18:20:21 -07003450 if (!ecc->codeword_size) {
3451 pr_debug("Invalid codeword size\n");
3452 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003453 }
3454
Brian Norris4ae7d222013-09-16 18:20:21 -07003455 chip->ecc_strength_ds = ecc->ecc_bits;
3456 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003457 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003458
3459ext_out:
3460 kfree(ep);
3461 return ret;
3462}
3463
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003464/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003465 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003466 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003467static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003468{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003469 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003470 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003471 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003472 int val;
3473
Brian Norris7854d3f2011-06-23 14:12:08 -07003474 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003475 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3476 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3477 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3478 return 0;
3479
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003480 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3481 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003482 for (j = 0; j < sizeof(*p); j++)
3483 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003484 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3485 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003486 break;
3487 }
3488 }
3489
Brian Norrisc7f23a72013-08-13 10:51:55 -07003490 if (i == 3) {
3491 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003492 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003493 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003494
Brian Norris8b6e50c2011-05-25 14:59:01 -07003495 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003496 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003497 if (val & (1 << 5))
3498 chip->onfi_version = 23;
3499 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003500 chip->onfi_version = 22;
3501 else if (val & (1 << 3))
3502 chip->onfi_version = 21;
3503 else if (val & (1 << 2))
3504 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003505 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003506 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003507
3508 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003509 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003510 return 0;
3511 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003512
3513 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3514 sanitize_string(p->model, sizeof(p->model));
3515 if (!mtd->name)
3516 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003517
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003518 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003519
3520 /*
3521 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3522 * (don't ask me who thought of this...). MTD assumes that these
3523 * dimensions will be power-of-2, so just truncate the remaining area.
3524 */
3525 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3526 mtd->erasesize *= mtd->writesize;
3527
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003528 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003529
3530 /* See erasesize comment */
3531 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003532 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003533 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003534
Zach Brown34da5f52017-01-10 13:30:21 -06003535 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3536 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3537
Huang Shijiee2985fc2013-05-17 11:17:30 +08003538 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003539 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003540
Huang Shijie10c86ba2013-05-17 11:17:26 +08003541 if (p->ecc_bits != 0xff) {
3542 chip->ecc_strength_ds = p->ecc_bits;
3543 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003544 } else if (chip->onfi_version >= 21 &&
3545 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3546
3547 /*
3548 * The nand_flash_detect_ext_param_page() uses the
3549 * Change Read Column command which maybe not supported
3550 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3551 * now. We do not replace user supplied command function.
3552 */
3553 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3554 chip->cmdfunc = nand_command_lp;
3555
3556 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003557 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003558 pr_warn("Failed to detect ONFI extended param page\n");
3559 } else {
3560 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003561 }
3562
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003563 return 1;
3564}
3565
3566/*
Huang Shijie91361812014-02-21 13:39:40 +08003567 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3568 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003569static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003570{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003571 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003572 struct nand_jedec_params *p = &chip->jedec_params;
3573 struct jedec_ecc_info *ecc;
3574 int val;
3575 int i, j;
3576
3577 /* Try JEDEC for unknown chip or LP */
3578 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3579 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3580 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3581 chip->read_byte(mtd) != 'C')
3582 return 0;
3583
3584 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3585 for (i = 0; i < 3; i++) {
3586 for (j = 0; j < sizeof(*p); j++)
3587 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3588
3589 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3590 le16_to_cpu(p->crc))
3591 break;
3592 }
3593
3594 if (i == 3) {
3595 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3596 return 0;
3597 }
3598
3599 /* Check version */
3600 val = le16_to_cpu(p->revision);
3601 if (val & (1 << 2))
3602 chip->jedec_version = 10;
3603 else if (val & (1 << 1))
3604 chip->jedec_version = 1; /* vendor specific version */
3605
3606 if (!chip->jedec_version) {
3607 pr_info("unsupported JEDEC version: %d\n", val);
3608 return 0;
3609 }
3610
3611 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3612 sanitize_string(p->model, sizeof(p->model));
3613 if (!mtd->name)
3614 mtd->name = p->model;
3615
3616 mtd->writesize = le32_to_cpu(p->byte_per_page);
3617
3618 /* Please reference to the comment for nand_flash_detect_onfi. */
3619 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3620 mtd->erasesize *= mtd->writesize;
3621
3622 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3623
3624 /* Please reference to the comment for nand_flash_detect_onfi. */
3625 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3626 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3627 chip->bits_per_cell = p->bits_per_cell;
3628
3629 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003630 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003631
3632 /* ECC info */
3633 ecc = &p->ecc_info[0];
3634
3635 if (ecc->codeword_size >= 9) {
3636 chip->ecc_strength_ds = ecc->ecc_bits;
3637 chip->ecc_step_ds = 1 << ecc->codeword_size;
3638 } else {
3639 pr_warn("Invalid codeword size\n");
3640 }
3641
3642 return 1;
3643}
3644
3645/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003646 * nand_id_has_period - Check if an ID string has a given wraparound period
3647 * @id_data: the ID string
3648 * @arrlen: the length of the @id_data array
3649 * @period: the period of repitition
3650 *
3651 * Check if an ID string is repeated within a given sequence of bytes at
3652 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003653 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003654 * if the repetition has a period of @period; otherwise, returns zero.
3655 */
3656static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3657{
3658 int i, j;
3659 for (i = 0; i < period; i++)
3660 for (j = i + period; j < arrlen; j += period)
3661 if (id_data[i] != id_data[j])
3662 return 0;
3663 return 1;
3664}
3665
3666/*
3667 * nand_id_len - Get the length of an ID string returned by CMD_READID
3668 * @id_data: the ID string
3669 * @arrlen: the length of the @id_data array
3670
3671 * Returns the length of the ID string, according to known wraparound/trailing
3672 * zero patterns. If no pattern exists, returns the length of the array.
3673 */
3674static int nand_id_len(u8 *id_data, int arrlen)
3675{
3676 int last_nonzero, period;
3677
3678 /* Find last non-zero byte */
3679 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3680 if (id_data[last_nonzero])
3681 break;
3682
3683 /* All zeros */
3684 if (last_nonzero < 0)
3685 return 0;
3686
3687 /* Calculate wraparound period */
3688 for (period = 1; period < arrlen; period++)
3689 if (nand_id_has_period(id_data, arrlen, period))
3690 break;
3691
3692 /* There's a repeated pattern */
3693 if (period < arrlen)
3694 return period;
3695
3696 /* There are trailing zeros */
3697 if (last_nonzero < arrlen - 1)
3698 return last_nonzero + 1;
3699
3700 /* No pattern detected */
3701 return arrlen;
3702}
3703
Huang Shijie7db906b2013-09-25 14:58:11 +08003704/* Extract the bits of per cell from the 3rd byte of the extended ID */
3705static int nand_get_bits_per_cell(u8 cellinfo)
3706{
3707 int bits;
3708
3709 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3710 bits >>= NAND_CI_CELLTYPE_SHIFT;
3711 return bits + 1;
3712}
3713
Brian Norrise3b88bd2012-09-24 20:40:52 -07003714/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003715 * Many new NAND share similar device ID codes, which represent the size of the
3716 * chip. The rest of the parameters must be decoded according to generic or
3717 * manufacturer-specific "extended ID" decoding patterns.
3718 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003719void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003720{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003721 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003722 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003723 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003724 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003725 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003726 /* The 4th id byte is the important one */
3727 extid = id_data[3];
3728
Boris Brezillon01389b62016-06-08 10:30:18 +02003729 /* Calc pagesize */
3730 mtd->writesize = 1024 << (extid & 0x03);
3731 extid >>= 2;
3732 /* Calc oobsize */
3733 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3734 extid >>= 2;
3735 /* Calc blocksize. Blocksize is multiples of 64KiB */
3736 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3737 extid >>= 2;
3738 /* Get buswidth information */
3739 if (extid & 0x1)
3740 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003741}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003742EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003743
3744/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003745 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3746 * decodes a matching ID table entry and assigns the MTD size parameters for
3747 * the chip.
3748 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003749static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003750{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003751 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003752
3753 mtd->erasesize = type->erasesize;
3754 mtd->writesize = type->pagesize;
3755 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003756
Huang Shijie1c195e92013-09-25 14:58:12 +08003757 /* All legacy ID NAND are small-page, SLC */
3758 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003759}
3760
3761/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003762 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3763 * heuristic patterns using various detected parameters (e.g., manufacturer,
3764 * page size, cell-type information).
3765 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003766static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003767{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003768 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003769
3770 /* Set the bad block position */
3771 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3772 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3773 else
3774 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003775}
3776
Huang Shijieec6e87e2013-03-15 11:01:00 +08003777static inline bool is_full_id_nand(struct nand_flash_dev *type)
3778{
3779 return type->id_len;
3780}
3781
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003782static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003783 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003784{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003785 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003786 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003787
Huang Shijieec6e87e2013-03-15 11:01:00 +08003788 if (!strncmp(type->id, id_data, type->id_len)) {
3789 mtd->writesize = type->pagesize;
3790 mtd->erasesize = type->erasesize;
3791 mtd->oobsize = type->oobsize;
3792
Huang Shijie7db906b2013-09-25 14:58:11 +08003793 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003794 chip->chipsize = (uint64_t)type->chipsize << 20;
3795 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003796 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3797 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003798 chip->onfi_timing_mode_default =
3799 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003800
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003801 if (!mtd->name)
3802 mtd->name = type->name;
3803
Huang Shijieec6e87e2013-03-15 11:01:00 +08003804 return true;
3805 }
3806 return false;
3807}
3808
Brian Norris7e74c2d2012-09-24 20:40:49 -07003809/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003810 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3811 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3812 * table.
3813 */
3814static void nand_manufacturer_detect(struct nand_chip *chip)
3815{
3816 /*
3817 * Try manufacturer detection if available and use
3818 * nand_decode_ext_id() otherwise.
3819 */
3820 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003821 chip->manufacturer.desc->ops->detect) {
3822 /* The 3rd id byte holds MLC / multichip data */
3823 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003824 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003825 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003826 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003827 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003828}
3829
3830/*
3831 * Manufacturer initialization. This function is called for all NANDs including
3832 * ONFI and JEDEC compliant ones.
3833 * Manufacturer drivers should put all their specific initialization code in
3834 * their ->init() hook.
3835 */
3836static int nand_manufacturer_init(struct nand_chip *chip)
3837{
3838 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
3839 !chip->manufacturer.desc->ops->init)
3840 return 0;
3841
3842 return chip->manufacturer.desc->ops->init(chip);
3843}
3844
3845/*
3846 * Manufacturer cleanup. This function is called for all NANDs including
3847 * ONFI and JEDEC compliant ones.
3848 * Manufacturer drivers should put all their specific cleanup code in their
3849 * ->cleanup() hook.
3850 */
3851static void nand_manufacturer_cleanup(struct nand_chip *chip)
3852{
3853 /* Release manufacturer private data */
3854 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3855 chip->manufacturer.desc->ops->cleanup)
3856 chip->manufacturer.desc->ops->cleanup(chip);
3857}
3858
3859/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003860 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003861 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02003862static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003863{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003864 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003865 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08003866 int busw;
Boris Brezillonf84674b2017-06-02 12:18:24 +02003867 int i;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003868 u8 *id_data = chip->id.data;
3869 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Karl Beldanef89a882008-09-15 14:37:29 +02003871 /*
3872 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003873 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003874 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003875 nand_reset(chip, 0);
3876
3877 /* Select the device */
3878 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02003879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003881 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
3883 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003884 maf_id = chip->read_byte(mtd);
3885 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Brian Norris8b6e50c2011-05-25 14:59:01 -07003887 /*
3888 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003889 * interface concerns can cause random data which looks like a
3890 * possibly credible NAND flash to appear. If the two results do
3891 * not match, ignore the device completely.
3892 */
3893
3894 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3895
Brian Norris4aef9b72012-09-24 20:40:48 -07003896 /* Read entire ID string */
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003897 for (i = 0; i < ARRAY_SIZE(chip->id.data); i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003898 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003899
Boris Brezillon7f501f02016-05-24 19:20:05 +02003900 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003901 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003902 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003903 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01003904 }
3905
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003906 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02003907
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003908 /* Try to identify manufacturer */
3909 manufacturer = nand_get_manufacturer(maf_id);
3910 chip->manufacturer.desc = manufacturer;
3911
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003912 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003913 type = nand_flash_ids;
3914
Boris Brezillon29a198a2016-05-24 20:17:48 +02003915 /*
3916 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
3917 * override it.
3918 * This is required to make sure initial NAND bus width set by the
3919 * NAND controller driver is coherent with the real NAND bus width
3920 * (extracted by auto-detection code).
3921 */
3922 busw = chip->options & NAND_BUSWIDTH_16;
3923
3924 /*
3925 * The flag is only set (never cleared), reset it to its default value
3926 * before starting auto-detection.
3927 */
3928 chip->options &= ~NAND_BUSWIDTH_16;
3929
Huang Shijieec6e87e2013-03-15 11:01:00 +08003930 for (; type->name != NULL; type++) {
3931 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003932 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08003933 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003934 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003935 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003936 }
3937 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003938
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003939 chip->onfi_version = 0;
3940 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003941 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003942 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003943 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003944
3945 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003946 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08003947 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003948 }
3949
David Woodhouse5e81e882010-02-26 18:32:56 +00003950 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003951 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003952
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02003953 if (!mtd->name)
3954 mtd->name = type->name;
3955
Adrian Hunter69423d92008-12-10 13:37:21 +00003956 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003957
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003958 if (!type->pagesize)
3959 nand_manufacturer_detect(chip);
3960 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02003961 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003962
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003963 /* Get chip options */
3964 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003965
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003966ident_done:
3967
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003968 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003969 WARN_ON(busw & NAND_BUSWIDTH_16);
3970 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003971 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3972 /*
3973 * Check, if buswidth is correct. Hardware drivers should set
3974 * chip correct!
3975 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003976 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003977 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003978 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
3979 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02003980 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
3981 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003982 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003983 }
3984
Boris Brezillon7f501f02016-05-24 19:20:05 +02003985 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003986
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003987 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003988 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003989 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003990 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003991
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003992 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003993 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003994 if (chip->chipsize & 0xffffffff)
3995 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003996 else {
3997 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3998 chip->chip_shift += 32 - 1;
3999 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004000
Masahiro Yamada14157f82017-09-13 11:05:50 +09004001 if (chip->chip_shift - chip->page_shift > 16)
4002 chip->options |= NAND_ROW_ADDR_3;
4003
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004004 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004005 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004006
Brian Norris8b6e50c2011-05-25 14:59:01 -07004007 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004008 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4009 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004010
Ezequiel Garcia20171642013-11-25 08:30:31 -03004011 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004012 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004013
4014 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004015 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4016 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004017 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004018 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4019 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004020 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004021 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4022 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004023
Rafał Miłecki3755a992014-10-21 00:01:04 +02004024 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004025 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004026 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004027 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004028}
4029
Boris Brezillond48f62b2016-04-01 14:54:32 +02004030static const char * const nand_ecc_modes[] = {
4031 [NAND_ECC_NONE] = "none",
4032 [NAND_ECC_SOFT] = "soft",
4033 [NAND_ECC_HW] = "hw",
4034 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4035 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004036 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004037};
4038
4039static int of_get_nand_ecc_mode(struct device_node *np)
4040{
4041 const char *pm;
4042 int err, i;
4043
4044 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4045 if (err < 0)
4046 return err;
4047
4048 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4049 if (!strcasecmp(pm, nand_ecc_modes[i]))
4050 return i;
4051
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004052 /*
4053 * For backward compatibility we support few obsoleted values that don't
4054 * have their mappings into nand_ecc_modes_t anymore (they were merged
4055 * with other enums).
4056 */
4057 if (!strcasecmp(pm, "soft_bch"))
4058 return NAND_ECC_SOFT;
4059
Boris Brezillond48f62b2016-04-01 14:54:32 +02004060 return -ENODEV;
4061}
4062
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004063static const char * const nand_ecc_algos[] = {
4064 [NAND_ECC_HAMMING] = "hamming",
4065 [NAND_ECC_BCH] = "bch",
4066};
4067
Boris Brezillond48f62b2016-04-01 14:54:32 +02004068static int of_get_nand_ecc_algo(struct device_node *np)
4069{
4070 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004071 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004072
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004073 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4074 if (!err) {
4075 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4076 if (!strcasecmp(pm, nand_ecc_algos[i]))
4077 return i;
4078 return -ENODEV;
4079 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004080
4081 /*
4082 * For backward compatibility we also read "nand-ecc-mode" checking
4083 * for some obsoleted values that were specifying ECC algorithm.
4084 */
4085 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4086 if (err < 0)
4087 return err;
4088
4089 if (!strcasecmp(pm, "soft"))
4090 return NAND_ECC_HAMMING;
4091 else if (!strcasecmp(pm, "soft_bch"))
4092 return NAND_ECC_BCH;
4093
4094 return -ENODEV;
4095}
4096
4097static int of_get_nand_ecc_step_size(struct device_node *np)
4098{
4099 int ret;
4100 u32 val;
4101
4102 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4103 return ret ? ret : val;
4104}
4105
4106static int of_get_nand_ecc_strength(struct device_node *np)
4107{
4108 int ret;
4109 u32 val;
4110
4111 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4112 return ret ? ret : val;
4113}
4114
4115static int of_get_nand_bus_width(struct device_node *np)
4116{
4117 u32 val;
4118
4119 if (of_property_read_u32(np, "nand-bus-width", &val))
4120 return 8;
4121
4122 switch (val) {
4123 case 8:
4124 case 16:
4125 return val;
4126 default:
4127 return -EIO;
4128 }
4129}
4130
4131static bool of_get_nand_on_flash_bbt(struct device_node *np)
4132{
4133 return of_property_read_bool(np, "nand-on-flash-bbt");
4134}
4135
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004136static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004137{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004138 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004139 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004140
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004141 if (!dn)
4142 return 0;
4143
Brian Norris5844fee2015-01-23 00:22:27 -08004144 if (of_get_nand_bus_width(dn) == 16)
4145 chip->options |= NAND_BUSWIDTH_16;
4146
4147 if (of_get_nand_on_flash_bbt(dn))
4148 chip->bbt_options |= NAND_BBT_USE_FLASH;
4149
4150 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004151 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004152 ecc_strength = of_get_nand_ecc_strength(dn);
4153 ecc_step = of_get_nand_ecc_step_size(dn);
4154
Brian Norris5844fee2015-01-23 00:22:27 -08004155 if (ecc_mode >= 0)
4156 chip->ecc.mode = ecc_mode;
4157
Rafał Miłecki79082452016-03-23 11:19:02 +01004158 if (ecc_algo >= 0)
4159 chip->ecc.algo = ecc_algo;
4160
Brian Norris5844fee2015-01-23 00:22:27 -08004161 if (ecc_strength >= 0)
4162 chip->ecc.strength = ecc_strength;
4163
4164 if (ecc_step > 0)
4165 chip->ecc.size = ecc_step;
4166
Boris Brezillonba78ee02016-06-08 17:04:22 +02004167 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4168 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4169
Brian Norris5844fee2015-01-23 00:22:27 -08004170 return 0;
4171}
4172
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004173/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004174 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004175 * @mtd: MTD device structure
4176 * @maxchips: number of chips to scan for
4177 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004178 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004179 * This is the first phase of the normal nand_scan() function. It reads the
4180 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004181 *
4182 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004183int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4184 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004185{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004186 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004187 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004188 int ret;
4189
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004190 ret = nand_dt_init(chip);
4191 if (ret)
4192 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004193
Brian Norrisf7a8e382016-01-05 10:39:45 -08004194 if (!mtd->name && mtd->dev.parent)
4195 mtd->name = dev_name(mtd->dev.parent);
4196
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004197 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4198 /*
4199 * Default functions assigned for chip_select() and
4200 * cmdfunc() both expect cmd_ctrl() to be populated,
4201 * so we need to check that that's the case
4202 */
4203 pr_err("chip.cmd_ctrl() callback is not provided");
4204 return -EINVAL;
4205 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004206 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004207 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004208
4209 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004210 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004211 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004212 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004213 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004214 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004215 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 }
4217
Boris Brezillon7f501f02016-05-24 19:20:05 +02004218 nand_maf_id = chip->id.data[0];
4219 nand_dev_id = chip->id.data[1];
4220
Huang Shijie07300162012-11-09 16:23:45 +08004221 chip->select_chip(mtd, -1);
4222
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004223 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004224 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004225 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004226 nand_reset(chip, i);
4227
4228 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004230 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004232 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004233 nand_dev_id != chip->read_byte(mtd)) {
4234 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 break;
Huang Shijie07300162012-11-09 16:23:45 +08004236 }
4237 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 }
4239 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004240 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004243 chip->numchips = i;
4244 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
David Woodhouse3b85c322006-09-25 17:06:53 +01004246 return 0;
4247}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004248EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004249
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004250static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4251{
4252 struct nand_chip *chip = mtd_to_nand(mtd);
4253 struct nand_ecc_ctrl *ecc = &chip->ecc;
4254
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004255 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004256 return -EINVAL;
4257
4258 switch (ecc->algo) {
4259 case NAND_ECC_HAMMING:
4260 ecc->calculate = nand_calculate_ecc;
4261 ecc->correct = nand_correct_data;
4262 ecc->read_page = nand_read_page_swecc;
4263 ecc->read_subpage = nand_read_subpage;
4264 ecc->write_page = nand_write_page_swecc;
4265 ecc->read_page_raw = nand_read_page_raw;
4266 ecc->write_page_raw = nand_write_page_raw;
4267 ecc->read_oob = nand_read_oob_std;
4268 ecc->write_oob = nand_write_oob_std;
4269 if (!ecc->size)
4270 ecc->size = 256;
4271 ecc->bytes = 3;
4272 ecc->strength = 1;
4273 return 0;
4274 case NAND_ECC_BCH:
4275 if (!mtd_nand_has_bch()) {
4276 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4277 return -EINVAL;
4278 }
4279 ecc->calculate = nand_bch_calculate_ecc;
4280 ecc->correct = nand_bch_correct_data;
4281 ecc->read_page = nand_read_page_swecc;
4282 ecc->read_subpage = nand_read_subpage;
4283 ecc->write_page = nand_write_page_swecc;
4284 ecc->read_page_raw = nand_read_page_raw;
4285 ecc->write_page_raw = nand_write_page_raw;
4286 ecc->read_oob = nand_read_oob_std;
4287 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004288
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004289 /*
4290 * Board driver should supply ecc.size and ecc.strength
4291 * values to select how many bits are correctable.
4292 * Otherwise, default to 4 bits for large page devices.
4293 */
4294 if (!ecc->size && (mtd->oobsize >= 64)) {
4295 ecc->size = 512;
4296 ecc->strength = 4;
4297 }
4298
4299 /*
4300 * if no ecc placement scheme was provided pickup the default
4301 * large page one.
4302 */
4303 if (!mtd->ooblayout) {
4304 /* handle large page devices only */
4305 if (mtd->oobsize < 64) {
4306 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4307 return -EINVAL;
4308 }
4309
4310 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004311
4312 }
4313
4314 /*
4315 * We can only maximize ECC config when the default layout is
4316 * used, otherwise we don't know how many bytes can really be
4317 * used.
4318 */
4319 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4320 ecc->options & NAND_ECC_MAXIMIZE) {
4321 int steps, bytes;
4322
4323 /* Always prefer 1k blocks over 512bytes ones */
4324 ecc->size = 1024;
4325 steps = mtd->writesize / ecc->size;
4326
4327 /* Reserve 2 bytes for the BBM */
4328 bytes = (mtd->oobsize - 2) / steps;
4329 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004330 }
4331
4332 /* See nand_bch_init() for details. */
4333 ecc->bytes = 0;
4334 ecc->priv = nand_bch_init(mtd);
4335 if (!ecc->priv) {
4336 WARN(1, "BCH ECC initialization failed!\n");
4337 return -EINVAL;
4338 }
4339 return 0;
4340 default:
4341 WARN(1, "Unsupported ECC algorithm!\n");
4342 return -EINVAL;
4343 }
4344}
4345
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09004346/**
4347 * nand_check_ecc_caps - check the sanity of preset ECC settings
4348 * @chip: nand chip info structure
4349 * @caps: ECC caps info structure
4350 * @oobavail: OOB size that the ECC engine can use
4351 *
4352 * When ECC step size and strength are already set, check if they are supported
4353 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4354 * On success, the calculated ECC bytes is set.
4355 */
4356int nand_check_ecc_caps(struct nand_chip *chip,
4357 const struct nand_ecc_caps *caps, int oobavail)
4358{
4359 struct mtd_info *mtd = nand_to_mtd(chip);
4360 const struct nand_ecc_step_info *stepinfo;
4361 int preset_step = chip->ecc.size;
4362 int preset_strength = chip->ecc.strength;
4363 int nsteps, ecc_bytes;
4364 int i, j;
4365
4366 if (WARN_ON(oobavail < 0))
4367 return -EINVAL;
4368
4369 if (!preset_step || !preset_strength)
4370 return -ENODATA;
4371
4372 nsteps = mtd->writesize / preset_step;
4373
4374 for (i = 0; i < caps->nstepinfos; i++) {
4375 stepinfo = &caps->stepinfos[i];
4376
4377 if (stepinfo->stepsize != preset_step)
4378 continue;
4379
4380 for (j = 0; j < stepinfo->nstrengths; j++) {
4381 if (stepinfo->strengths[j] != preset_strength)
4382 continue;
4383
4384 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4385 preset_strength);
4386 if (WARN_ON_ONCE(ecc_bytes < 0))
4387 return ecc_bytes;
4388
4389 if (ecc_bytes * nsteps > oobavail) {
4390 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4391 preset_step, preset_strength);
4392 return -ENOSPC;
4393 }
4394
4395 chip->ecc.bytes = ecc_bytes;
4396
4397 return 0;
4398 }
4399 }
4400
4401 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4402 preset_step, preset_strength);
4403
4404 return -ENOTSUPP;
4405}
4406EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4407
4408/**
4409 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4410 * @chip: nand chip info structure
4411 * @caps: ECC engine caps info structure
4412 * @oobavail: OOB size that the ECC engine can use
4413 *
4414 * If a chip's ECC requirement is provided, try to meet it with the least
4415 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4416 * On success, the chosen ECC settings are set.
4417 */
4418int nand_match_ecc_req(struct nand_chip *chip,
4419 const struct nand_ecc_caps *caps, int oobavail)
4420{
4421 struct mtd_info *mtd = nand_to_mtd(chip);
4422 const struct nand_ecc_step_info *stepinfo;
4423 int req_step = chip->ecc_step_ds;
4424 int req_strength = chip->ecc_strength_ds;
4425 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4426 int best_step, best_strength, best_ecc_bytes;
4427 int best_ecc_bytes_total = INT_MAX;
4428 int i, j;
4429
4430 if (WARN_ON(oobavail < 0))
4431 return -EINVAL;
4432
4433 /* No information provided by the NAND chip */
4434 if (!req_step || !req_strength)
4435 return -ENOTSUPP;
4436
4437 /* number of correctable bits the chip requires in a page */
4438 req_corr = mtd->writesize / req_step * req_strength;
4439
4440 for (i = 0; i < caps->nstepinfos; i++) {
4441 stepinfo = &caps->stepinfos[i];
4442 step_size = stepinfo->stepsize;
4443
4444 for (j = 0; j < stepinfo->nstrengths; j++) {
4445 strength = stepinfo->strengths[j];
4446
4447 /*
4448 * If both step size and strength are smaller than the
4449 * chip's requirement, it is not easy to compare the
4450 * resulted reliability.
4451 */
4452 if (step_size < req_step && strength < req_strength)
4453 continue;
4454
4455 if (mtd->writesize % step_size)
4456 continue;
4457
4458 nsteps = mtd->writesize / step_size;
4459
4460 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4461 if (WARN_ON_ONCE(ecc_bytes < 0))
4462 continue;
4463 ecc_bytes_total = ecc_bytes * nsteps;
4464
4465 if (ecc_bytes_total > oobavail ||
4466 strength * nsteps < req_corr)
4467 continue;
4468
4469 /*
4470 * We assume the best is to meet the chip's requrement
4471 * with the least number of ECC bytes.
4472 */
4473 if (ecc_bytes_total < best_ecc_bytes_total) {
4474 best_ecc_bytes_total = ecc_bytes_total;
4475 best_step = step_size;
4476 best_strength = strength;
4477 best_ecc_bytes = ecc_bytes;
4478 }
4479 }
4480 }
4481
4482 if (best_ecc_bytes_total == INT_MAX)
4483 return -ENOTSUPP;
4484
4485 chip->ecc.size = best_step;
4486 chip->ecc.strength = best_strength;
4487 chip->ecc.bytes = best_ecc_bytes;
4488
4489 return 0;
4490}
4491EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4492
4493/**
4494 * nand_maximize_ecc - choose the max ECC strength available
4495 * @chip: nand chip info structure
4496 * @caps: ECC engine caps info structure
4497 * @oobavail: OOB size that the ECC engine can use
4498 *
4499 * Choose the max ECC strength that is supported on the controller, and can fit
4500 * within the chip's OOB. On success, the chosen ECC settings are set.
4501 */
4502int nand_maximize_ecc(struct nand_chip *chip,
4503 const struct nand_ecc_caps *caps, int oobavail)
4504{
4505 struct mtd_info *mtd = nand_to_mtd(chip);
4506 const struct nand_ecc_step_info *stepinfo;
4507 int step_size, strength, nsteps, ecc_bytes, corr;
4508 int best_corr = 0;
4509 int best_step = 0;
4510 int best_strength, best_ecc_bytes;
4511 int i, j;
4512
4513 if (WARN_ON(oobavail < 0))
4514 return -EINVAL;
4515
4516 for (i = 0; i < caps->nstepinfos; i++) {
4517 stepinfo = &caps->stepinfos[i];
4518 step_size = stepinfo->stepsize;
4519
4520 /* If chip->ecc.size is already set, respect it */
4521 if (chip->ecc.size && step_size != chip->ecc.size)
4522 continue;
4523
4524 for (j = 0; j < stepinfo->nstrengths; j++) {
4525 strength = stepinfo->strengths[j];
4526
4527 if (mtd->writesize % step_size)
4528 continue;
4529
4530 nsteps = mtd->writesize / step_size;
4531
4532 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4533 if (WARN_ON_ONCE(ecc_bytes < 0))
4534 continue;
4535
4536 if (ecc_bytes * nsteps > oobavail)
4537 continue;
4538
4539 corr = strength * nsteps;
4540
4541 /*
4542 * If the number of correctable bits is the same,
4543 * bigger step_size has more reliability.
4544 */
4545 if (corr > best_corr ||
4546 (corr == best_corr && step_size > best_step)) {
4547 best_corr = corr;
4548 best_step = step_size;
4549 best_strength = strength;
4550 best_ecc_bytes = ecc_bytes;
4551 }
4552 }
4553 }
4554
4555 if (!best_corr)
4556 return -ENOTSUPP;
4557
4558 chip->ecc.size = best_step;
4559 chip->ecc.strength = best_strength;
4560 chip->ecc.bytes = best_ecc_bytes;
4561
4562 return 0;
4563}
4564EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4565
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004566/*
4567 * Check if the chip configuration meet the datasheet requirements.
4568
4569 * If our configuration corrects A bits per B bytes and the minimum
4570 * required correction level is X bits per Y bytes, then we must ensure
4571 * both of the following are true:
4572 *
4573 * (1) A / B >= X / Y
4574 * (2) A >= X
4575 *
4576 * Requirement (1) ensures we can correct for the required bitflip density.
4577 * Requirement (2) ensures we can correct even when all bitflips are clumped
4578 * in the same sector.
4579 */
4580static bool nand_ecc_strength_good(struct mtd_info *mtd)
4581{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004582 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004583 struct nand_ecc_ctrl *ecc = &chip->ecc;
4584 int corr, ds_corr;
4585
4586 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4587 /* Not enough information */
4588 return true;
4589
4590 /*
4591 * We get the number of corrected bits per page to compare
4592 * the correction density.
4593 */
4594 corr = (mtd->writesize * ecc->strength) / ecc->size;
4595 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4596
4597 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4598}
David Woodhouse3b85c322006-09-25 17:06:53 +01004599
Marc Gonzalez3371d662016-11-15 10:56:20 +01004600static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4601{
4602 struct nand_ecc_ctrl *ecc = &chip->ecc;
4603
4604 if (nand_standard_page_accessors(ecc))
4605 return false;
4606
4607 /*
4608 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4609 * controller driver implements all the page accessors because
4610 * default helpers are not suitable when the core does not
4611 * send the READ0/PAGEPROG commands.
4612 */
4613 return (!ecc->read_page || !ecc->write_page ||
4614 !ecc->read_page_raw || !ecc->write_page_raw ||
4615 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4616 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4617 ecc->hwctl && ecc->calculate));
4618}
4619
David Woodhouse3b85c322006-09-25 17:06:53 +01004620/**
4621 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004622 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004623 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004624 * This is the second phase of the normal nand_scan() function. It fills out
4625 * all the uninitialized function pointers with the defaults and scans for a
4626 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004627 */
4628int nand_scan_tail(struct mtd_info *mtd)
4629{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004630 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004631 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004632 struct nand_buffers *nbuf = NULL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004633 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01004634
Brian Norrise2414f42012-02-06 13:44:00 -08004635 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004636 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07004637 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02004638 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07004639 }
Brian Norrise2414f42012-02-06 13:44:00 -08004640
Marc Gonzalez3371d662016-11-15 10:56:20 +01004641 if (invalid_ecc_page_accessors(chip)) {
4642 pr_err("Invalid ECC page accessors setup\n");
Boris Brezillonf84674b2017-06-02 12:18:24 +02004643 return -EINVAL;
Marc Gonzalez3371d662016-11-15 10:56:20 +01004644 }
4645
Huang Shijief02ea4e2014-01-13 14:27:12 +08004646 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004647 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Boris Brezillonf84674b2017-06-02 12:18:24 +02004648 if (!nbuf)
4649 return -ENOMEM;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004650
4651 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4652 if (!nbuf->ecccalc) {
4653 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004654 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004655 }
4656
4657 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4658 if (!nbuf->ecccode) {
4659 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004660 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004661 }
4662
4663 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4664 GFP_KERNEL);
4665 if (!nbuf->databuf) {
4666 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004667 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004668 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004669
4670 chip->buffers = nbuf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004671 } else if (!chip->buffers) {
4672 return -ENOMEM;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004673 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004674
Boris Brezillonf84674b2017-06-02 12:18:24 +02004675 /*
4676 * FIXME: some NAND manufacturer drivers expect the first die to be
4677 * selected when manufacturer->init() is called. They should be fixed
4678 * to explictly select the relevant die when interacting with the NAND
4679 * chip.
4680 */
4681 chip->select_chip(mtd, 0);
4682 ret = nand_manufacturer_init(chip);
4683 chip->select_chip(mtd, -1);
4684 if (ret)
4685 goto err_free_nbuf;
4686
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004687 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004688 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004689
4690 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004691 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004692 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004693 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004694 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004695 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004698 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 break;
4700 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004701 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02004702 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004703 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02004705 /*
4706 * Expose the whole OOB area to users if ECC_NONE
4707 * is passed. We could do that for all kind of
4708 * ->oobsize, but we must keep the old large/small
4709 * page with ECC layout when ->oobsize <= 128 for
4710 * compatibility reasons.
4711 */
4712 if (ecc->mode == NAND_ECC_NONE) {
4713 mtd_set_ooblayout(mtd,
4714 &nand_ooblayout_lp_ops);
4715 break;
4716 }
4717
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004718 WARN(1, "No oob scheme defined for oobsize %d\n",
4719 mtd->oobsize);
4720 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004721 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 }
4723 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004724
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004725 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004726 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004727 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004728 */
David Woodhouse956e9442006-09-25 17:12:39 +01004729
Huang Shijie97de79e02013-10-18 14:20:53 +08004730 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004731 case NAND_ECC_HW_OOB_FIRST:
4732 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004733 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004734 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4735 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004736 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004737 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004738 if (!ecc->read_page)
4739 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004740
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004741 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004742 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004743 if (!ecc->read_page)
4744 ecc->read_page = nand_read_page_hwecc;
4745 if (!ecc->write_page)
4746 ecc->write_page = nand_write_page_hwecc;
4747 if (!ecc->read_page_raw)
4748 ecc->read_page_raw = nand_read_page_raw;
4749 if (!ecc->write_page_raw)
4750 ecc->write_page_raw = nand_write_page_raw;
4751 if (!ecc->read_oob)
4752 ecc->read_oob = nand_read_oob_std;
4753 if (!ecc->write_oob)
4754 ecc->write_oob = nand_write_oob_std;
4755 if (!ecc->read_subpage)
4756 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004757 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004758 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004759
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004760 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004761 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4762 (!ecc->read_page ||
4763 ecc->read_page == nand_read_page_hwecc ||
4764 !ecc->write_page ||
4765 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004766 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4767 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004768 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004769 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004770 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004771 if (!ecc->read_page)
4772 ecc->read_page = nand_read_page_syndrome;
4773 if (!ecc->write_page)
4774 ecc->write_page = nand_write_page_syndrome;
4775 if (!ecc->read_page_raw)
4776 ecc->read_page_raw = nand_read_page_raw_syndrome;
4777 if (!ecc->write_page_raw)
4778 ecc->write_page_raw = nand_write_page_raw_syndrome;
4779 if (!ecc->read_oob)
4780 ecc->read_oob = nand_read_oob_syndrome;
4781 if (!ecc->write_oob)
4782 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004783
Huang Shijie97de79e02013-10-18 14:20:53 +08004784 if (mtd->writesize >= ecc->size) {
4785 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004786 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4787 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004788 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07004789 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004790 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004791 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004792 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4793 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004794 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004795 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004797 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004798 ret = nand_set_ecc_soft_ops(mtd);
4799 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004800 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004801 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01004802 }
4803 break;
4804
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004805 case NAND_ECC_ON_DIE:
4806 if (!ecc->read_page || !ecc->write_page) {
4807 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4808 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004809 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004810 }
4811 if (!ecc->read_oob)
4812 ecc->read_oob = nand_read_oob_std;
4813 if (!ecc->write_oob)
4814 ecc->write_oob = nand_write_oob_std;
4815 break;
4816
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004817 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004818 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004819 ecc->read_page = nand_read_page_raw;
4820 ecc->write_page = nand_write_page_raw;
4821 ecc->read_oob = nand_read_oob_std;
4822 ecc->read_page_raw = nand_read_page_raw;
4823 ecc->write_page_raw = nand_write_page_raw;
4824 ecc->write_oob = nand_write_oob_std;
4825 ecc->size = mtd->writesize;
4826 ecc->bytes = 0;
4827 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004831 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4832 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004833 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Brian Norris9ce244b2011-08-30 18:45:37 -07004836 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004837 if (!ecc->read_oob_raw)
4838 ecc->read_oob_raw = ecc->read_oob;
4839 if (!ecc->write_oob_raw)
4840 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004841
Boris Brezillon846031d2016-02-03 20:11:00 +01004842 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004843 mtd->ecc_strength = ecc->strength;
4844 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004845
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004846 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004847 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004848 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004849 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004850 ecc->steps = mtd->writesize / ecc->size;
4851 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004852 WARN(1, "Invalid ECC parameters\n");
4853 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004854 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004856 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004857 if (ecc->total > mtd->oobsize) {
4858 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
4859 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004860 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004861 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004862
Boris Brezillon846031d2016-02-03 20:11:00 +01004863 /*
4864 * The number of bytes available for a client to place data into
4865 * the out of band area.
4866 */
4867 ret = mtd_ooblayout_count_freebytes(mtd);
4868 if (ret < 0)
4869 ret = 0;
4870
4871 mtd->oobavail = ret;
4872
4873 /* ECC sanity check: warn if it's too weak */
4874 if (!nand_ecc_strength_good(mtd))
4875 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4876 mtd->name);
4877
Brian Norris8b6e50c2011-05-25 14:59:01 -07004878 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004879 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004880 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004881 case 2:
4882 mtd->subpage_sft = 1;
4883 break;
4884 case 4:
4885 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004886 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004887 mtd->subpage_sft = 2;
4888 break;
4889 }
4890 }
4891 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4892
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004893 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004894 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004897 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004899 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304900 switch (ecc->mode) {
4901 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304902 if (chip->page_shift > 9)
4903 chip->options |= NAND_SUBPAGE_READ;
4904 break;
4905
4906 default:
4907 break;
4908 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004909
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004911 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004912 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4913 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004914 mtd->_erase = nand_erase;
4915 mtd->_point = NULL;
4916 mtd->_unpoint = NULL;
4917 mtd->_read = nand_read;
4918 mtd->_write = nand_write;
4919 mtd->_panic_write = panic_nand_write;
4920 mtd->_read_oob = nand_read_oob;
4921 mtd->_write_oob = nand_write_oob;
4922 mtd->_sync = nand_sync;
4923 mtd->_lock = NULL;
4924 mtd->_unlock = NULL;
4925 mtd->_suspend = nand_suspend;
4926 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004927 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004928 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004929 mtd->_block_isbad = nand_block_isbad;
4930 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004931 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004932 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004934 /*
4935 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4936 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4937 * properly set.
4938 */
4939 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004940 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
Boris Brezillonf84674b2017-06-02 12:18:24 +02004942 /* Initialize the ->data_interface field. */
4943 ret = nand_init_data_interface(chip);
4944 if (ret)
4945 goto err_nand_manuf_cleanup;
4946
4947 /* Enter fastest possible mode on all dies. */
4948 for (i = 0; i < chip->numchips; i++) {
4949 chip->select_chip(mtd, i);
4950 ret = nand_setup_data_interface(chip, i);
4951 chip->select_chip(mtd, -1);
4952
4953 if (ret)
4954 goto err_nand_data_iface_cleanup;
4955 }
4956
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004957 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004958 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07004962 ret = chip->scan_bbt(mtd);
4963 if (ret)
Boris Brezillonf84674b2017-06-02 12:18:24 +02004964 goto err_nand_data_iface_cleanup;
4965
Brian Norris44d41822017-05-01 17:04:50 -07004966 return 0;
4967
Boris Brezillonf84674b2017-06-02 12:18:24 +02004968err_nand_data_iface_cleanup:
4969 nand_release_data_interface(chip);
4970
4971err_nand_manuf_cleanup:
4972 nand_manufacturer_cleanup(chip);
4973
4974err_free_nbuf:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004975 if (nbuf) {
4976 kfree(nbuf->databuf);
4977 kfree(nbuf->ecccode);
4978 kfree(nbuf->ecccalc);
4979 kfree(nbuf);
4980 }
Brian Norris78771042017-05-01 17:04:53 -07004981
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004982 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004984EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Brian Norris8b6e50c2011-05-25 14:59:01 -07004986/*
4987 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004988 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004989 * to call us from in-kernel code if the core NAND support is modular.
4990 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004991#ifdef MODULE
4992#define caller_is_module() (1)
4993#else
4994#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004995 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004996#endif
4997
4998/**
4999 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005000 * @mtd: MTD device structure
5001 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01005002 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005003 * This fills out all the uninitialized function pointers with the defaults.
5004 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005005 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005006 */
5007int nand_scan(struct mtd_info *mtd, int maxchips)
5008{
5009 int ret;
5010
David Woodhouse5e81e882010-02-26 18:32:56 +00005011 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01005012 if (!ret)
5013 ret = nand_scan_tail(mtd);
5014 return ret;
5015}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005016EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01005017
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005019 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5020 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005021 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005022void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005024 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005025 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005026 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5027
Boris Brezillond8e725d2016-09-15 10:32:50 +02005028 nand_release_data_interface(chip);
5029
Jesper Juhlfa671642005-11-07 01:01:27 -08005030 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005031 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005032 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
5033 kfree(chip->buffers->databuf);
5034 kfree(chip->buffers->ecccode);
5035 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005036 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005037 }
Brian Norris58373ff2010-07-15 12:15:44 -07005038
5039 /* Free bad block descriptor memory */
5040 if (chip->badblock_pattern && chip->badblock_pattern->options
5041 & NAND_BBT_DYNAMICSTRUCT)
5042 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005043
5044 /* Free manufacturer priv data. */
5045 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005047EXPORT_SYMBOL_GPL(nand_cleanup);
5048
5049/**
5050 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5051 * held by the NAND device
5052 * @mtd: MTD device structure
5053 */
5054void nand_release(struct mtd_info *mtd)
5055{
5056 mtd_device_unregister(mtd);
5057 nand_cleanup(mtd_to_nand(mtd));
5058}
David Woodhousee0c7d762006-05-13 18:07:53 +01005059EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005060
David Woodhousee0c7d762006-05-13 18:07:53 +01005061MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005062MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5063MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005064MODULE_DESCRIPTION("Generic NAND flash driver code");