blob: 630048f5abdc0e639c65307709e520424cdc3e18 [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 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100713 if (command != NAND_CMD_NONE)
714 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Brian Norris8b6e50c2011-05-25 14:59:01 -0700716 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200717 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
718 /* Serially input address */
719 if (column != -1) {
720 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800721 if (chip->options & NAND_BUSWIDTH_16 &&
722 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200723 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200724 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200725 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200727 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200728 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200729 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200730 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900731 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200732 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200733 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200734 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000735
736 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700737 * Program and erase have their own busy handlers status and sequential
738 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100739 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000741
Miquel Raynaldf467892017-11-08 17:00:27 +0100742 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 case NAND_CMD_PAGEPROG:
744 case NAND_CMD_ERASE1:
745 case NAND_CMD_ERASE2:
746 case NAND_CMD_SEQIN:
747 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900748 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900749 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return;
751
752 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200753 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200755 udelay(chip->chip_delay);
756 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200757 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200758 chip->cmd_ctrl(mtd,
759 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200760 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
761 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return;
763
David Woodhousee0c7d762006-05-13 18:07:53 +0100764 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200765 case NAND_CMD_READ0:
766 /*
767 * READ0 is sometimes used to exit GET STATUS mode. When this
768 * is the case no address cycles are requested, and we can use
769 * this information to detect that we should not wait for the
770 * device to be ready.
771 */
772 if (column == -1 && page_addr == -1)
773 return;
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000776 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * If we don't have access to the busy pin, we apply the given
778 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100779 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200780 if (!chip->dev_ready) {
781 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700785 /*
786 * Apply this short delay always to ensure that we do wait tWB in
787 * any case on any machine.
788 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100789 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000790
791 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200794static void nand_ccs_delay(struct nand_chip *chip)
795{
796 /*
797 * The controller already takes care of waiting for tCCS when the RNDIN
798 * or RNDOUT command is sent, return directly.
799 */
800 if (!(chip->options & NAND_WAIT_TCCS))
801 return;
802
803 /*
804 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
805 * (which should be safe for all NANDs).
806 */
807 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
808 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
809 else
810 ndelay(500);
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/**
814 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700815 * @mtd: MTD device structure
816 * @command: the command to be sent
817 * @column: the column address for this command, -1 if none
818 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200820 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700821 * devices. We don't have the separate regions as we have in the small page
822 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200824static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
825 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100827 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /* Emulate NAND_CMD_READOOB */
830 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200831 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 command = NAND_CMD_READ0;
833 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000834
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200835 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100836 if (command != NAND_CMD_NONE)
837 chip->cmd_ctrl(mtd, command,
838 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200841 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* Serially input address */
844 if (column != -1) {
845 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800846 if (chip->options & NAND_BUSWIDTH_16 &&
847 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200849 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200850 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200851
Brian Norrisf5b88de2016-10-03 09:49:35 -0700852 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200853 if (!nand_opcode_8bits(command))
854 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200857 chip->cmd_ctrl(mtd, page_addr, ctrl);
858 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200859 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900860 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200861 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200862 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200865 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000866
867 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700868 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100869 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000870 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000872
Miquel Raynaldf467892017-11-08 17:00:27 +0100873 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 case NAND_CMD_CACHEDPROG:
875 case NAND_CMD_PAGEPROG:
876 case NAND_CMD_ERASE1:
877 case NAND_CMD_ERASE2:
878 case NAND_CMD_SEQIN:
879 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900880 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900881 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000882 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200884 case NAND_CMD_RNDIN:
885 nand_ccs_delay(chip);
886 return;
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200889 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200891 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200892 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
893 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
894 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
895 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200896 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
897 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return;
899
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200900 case NAND_CMD_RNDOUT:
901 /* No ready / busy check necessary */
902 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
903 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
904 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
905 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200906
907 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200908 return;
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200911 /*
912 * READ0 is sometimes used to exit GET STATUS mode. When this
913 * is the case no address cycles are requested, and we can use
914 * this information to detect that READSTART should not be
915 * issued.
916 */
917 if (column == -1 && page_addr == -1)
918 return;
919
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200920 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
921 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
922 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
923 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000924
David Woodhousee0c7d762006-05-13 18:07:53 +0100925 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000927 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700929 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100930 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200931 if (!chip->dev_ready) {
932 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000936
Brian Norris8b6e50c2011-05-25 14:59:01 -0700937 /*
938 * Apply this short delay always to ensure that we do wait tWB in
939 * any case on any machine.
940 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100941 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000942
943 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200947 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700948 * @chip: the nand chip descriptor
949 * @mtd: MTD device structure
950 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200951 *
952 * Used when in panic, no locks are taken.
953 */
954static void panic_nand_get_device(struct nand_chip *chip,
955 struct mtd_info *mtd, int new_state)
956{
Brian Norris7854d3f2011-06-23 14:12:08 -0700957 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200958 chip->controller->active = chip;
959 chip->state = new_state;
960}
961
962/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700964 * @mtd: MTD device structure
965 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 *
967 * Get the device and lock it for exclusive access
968 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200969static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800970nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100972 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200973 spinlock_t *lock = &chip->controller->lock;
974 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100975 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200976retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100977 spin_lock(lock);
978
vimal singhb8b3ee92009-07-09 20:41:22 +0530979 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200980 if (!chip->controller->active)
981 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200982
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200983 if (chip->controller->active == chip && chip->state == FL_READY) {
984 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100985 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100986 return 0;
987 }
988 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800989 if (chip->controller->active->state == FL_PM_SUSPENDED) {
990 chip->state = FL_PM_SUSPENDED;
991 spin_unlock(lock);
992 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800993 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100994 }
995 set_current_state(TASK_UNINTERRUPTIBLE);
996 add_wait_queue(wq, &wait);
997 spin_unlock(lock);
998 schedule();
999 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 goto retry;
1001}
1002
1003/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001004 * panic_nand_wait - [GENERIC] wait until the command is done
1005 * @mtd: MTD device structure
1006 * @chip: NAND chip structure
1007 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001008 *
1009 * Wait for command done. This is a helper function for nand_wait used when
1010 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001011 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001012 */
1013static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1014 unsigned long timeo)
1015{
1016 int i;
1017 for (i = 0; i < timeo; i++) {
1018 if (chip->dev_ready) {
1019 if (chip->dev_ready(mtd))
1020 break;
1021 } else {
1022 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1023 break;
1024 }
1025 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001026 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001027}
1028
1029/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001030 * nand_wait - [DEFAULT] wait until the command is done
1031 * @mtd: MTD device structure
1032 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001034 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001035 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001036static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038
Alex Smithb70af9b2015-10-06 14:52:07 +01001039 int status;
1040 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Brian Norris8b6e50c2011-05-25 14:59:01 -07001042 /*
1043 * Apply this short delay always to ensure that we do wait tWB in any
1044 * case on any machine.
1045 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001046 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Artem Bityutskiy14c65782013-03-04 14:21:34 +02001048 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001050 if (in_interrupt() || oops_in_progress)
1051 panic_nand_wait(mtd, chip, timeo);
1052 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001053 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001054 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001055 if (chip->dev_ready) {
1056 if (chip->dev_ready(mtd))
1057 break;
1058 } else {
1059 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1060 break;
1061 }
1062 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001063 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001065
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001066 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001067 /* This can happen if in case of timeout or buggy dev_ready */
1068 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return status;
1070}
1071
1072/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001073 * nand_reset_data_interface - Reset data interface and timings
1074 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001075 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001076 *
1077 * Reset the Data interface and timings to ONFI mode 0.
1078 *
1079 * Returns 0 for success or negative error code otherwise.
1080 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001081static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001082{
1083 struct mtd_info *mtd = nand_to_mtd(chip);
1084 const struct nand_data_interface *conf;
1085 int ret;
1086
1087 if (!chip->setup_data_interface)
1088 return 0;
1089
1090 /*
1091 * The ONFI specification says:
1092 * "
1093 * To transition from NV-DDR or NV-DDR2 to the SDR data
1094 * interface, the host shall use the Reset (FFh) command
1095 * using SDR timing mode 0. A device in any timing mode is
1096 * required to recognize Reset (FFh) command issued in SDR
1097 * timing mode 0.
1098 * "
1099 *
1100 * Configure the data interface in SDR mode and set the
1101 * timings to timing mode 0.
1102 */
1103
1104 conf = nand_get_default_data_interface();
Boris Brezillon104e4422017-03-16 09:35:58 +01001105 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001106 if (ret)
1107 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1108
1109 return ret;
1110}
1111
1112/**
1113 * nand_setup_data_interface - Setup the best data interface and timings
1114 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001115 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001116 *
1117 * Find and configure the best data interface and NAND timings supported by
1118 * the chip and the driver.
1119 * First tries to retrieve supported timing modes from ONFI information,
1120 * and if the NAND chip does not support ONFI, relies on the
1121 * ->onfi_timing_mode_default specified in the nand_ids table.
1122 *
1123 * Returns 0 for success or negative error code otherwise.
1124 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001125static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001126{
1127 struct mtd_info *mtd = nand_to_mtd(chip);
1128 int ret;
1129
1130 if (!chip->setup_data_interface || !chip->data_interface)
1131 return 0;
1132
1133 /*
1134 * Ensure the timing mode has been changed on the chip side
1135 * before changing timings on the controller side.
1136 */
Boris Brezillona11bf5e2017-07-31 10:29:56 +02001137 if (chip->onfi_version &&
1138 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1139 ONFI_OPT_CMD_SET_GET_FEATURES)) {
Boris Brezillond8e725d2016-09-15 10:32:50 +02001140 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1141 chip->onfi_timing_mode_default,
1142 };
1143
1144 ret = chip->onfi_set_features(mtd, chip,
1145 ONFI_FEATURE_ADDR_TIMING_MODE,
1146 tmode_param);
1147 if (ret)
1148 goto err;
1149 }
1150
Boris Brezillon104e4422017-03-16 09:35:58 +01001151 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001152err:
1153 return ret;
1154}
1155
1156/**
1157 * nand_init_data_interface - find the best data interface and timings
1158 * @chip: The NAND chip
1159 *
1160 * Find the best data interface and NAND timings supported by the chip
1161 * and the driver.
1162 * First tries to retrieve supported timing modes from ONFI information,
1163 * and if the NAND chip does not support ONFI, relies on the
1164 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1165 * function nand_chip->data_interface is initialized with the best timing mode
1166 * available.
1167 *
1168 * Returns 0 for success or negative error code otherwise.
1169 */
1170static int nand_init_data_interface(struct nand_chip *chip)
1171{
1172 struct mtd_info *mtd = nand_to_mtd(chip);
1173 int modes, mode, ret;
1174
1175 if (!chip->setup_data_interface)
1176 return 0;
1177
1178 /*
1179 * First try to identify the best timings from ONFI parameters and
1180 * if the NAND does not support ONFI, fallback to the default ONFI
1181 * timing mode.
1182 */
1183 modes = onfi_get_async_timing_mode(chip);
1184 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1185 if (!chip->onfi_timing_mode_default)
1186 return 0;
1187
1188 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1189 }
1190
1191 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1192 GFP_KERNEL);
1193 if (!chip->data_interface)
1194 return -ENOMEM;
1195
1196 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1197 ret = onfi_init_data_interface(chip, chip->data_interface,
1198 NAND_SDR_IFACE, mode);
1199 if (ret)
1200 continue;
1201
Boris Brezillon104e4422017-03-16 09:35:58 +01001202 /* Pass -1 to only */
1203 ret = chip->setup_data_interface(mtd,
1204 NAND_DATA_IFACE_CHECK_ONLY,
1205 chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001206 if (!ret) {
1207 chip->onfi_timing_mode_default = mode;
1208 break;
1209 }
1210 }
1211
1212 return 0;
1213}
1214
1215static void nand_release_data_interface(struct nand_chip *chip)
1216{
1217 kfree(chip->data_interface);
1218}
1219
1220/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001221 * nand_reset - Reset and initialize a NAND device
1222 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001223 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001224 *
1225 * Returns 0 for success or negative error code otherwise
1226 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001227int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001228{
1229 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001230 int ret;
1231
Boris Brezillon104e4422017-03-16 09:35:58 +01001232 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001233 if (ret)
1234 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001235
Boris Brezillon73f907f2016-10-24 16:46:20 +02001236 /*
1237 * The CS line has to be released before we can apply the new NAND
1238 * interface settings, hence this weird ->select_chip() dance.
1239 */
1240 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001241 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001242 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001243
Boris Brezillon73f907f2016-10-24 16:46:20 +02001244 chip->select_chip(mtd, chipnr);
Boris Brezillon104e4422017-03-16 09:35:58 +01001245 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001246 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001247 if (ret)
1248 return ret;
1249
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001250 return 0;
1251}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02001252EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001253
1254/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001255 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1256 * @buf: buffer to test
1257 * @len: buffer length
1258 * @bitflips_threshold: maximum number of bitflips
1259 *
1260 * Check if a buffer contains only 0xff, which means the underlying region
1261 * has been erased and is ready to be programmed.
1262 * The bitflips_threshold specify the maximum number of bitflips before
1263 * considering the region is not erased.
1264 * Note: The logic of this function has been extracted from the memweight
1265 * implementation, except that nand_check_erased_buf function exit before
1266 * testing the whole buffer if the number of bitflips exceed the
1267 * bitflips_threshold value.
1268 *
1269 * Returns a positive number of bitflips less than or equal to
1270 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1271 * threshold.
1272 */
1273static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1274{
1275 const unsigned char *bitmap = buf;
1276 int bitflips = 0;
1277 int weight;
1278
1279 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1280 len--, bitmap++) {
1281 weight = hweight8(*bitmap);
1282 bitflips += BITS_PER_BYTE - weight;
1283 if (unlikely(bitflips > bitflips_threshold))
1284 return -EBADMSG;
1285 }
1286
1287 for (; len >= sizeof(long);
1288 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001289 unsigned long d = *((unsigned long *)bitmap);
1290 if (d == ~0UL)
1291 continue;
1292 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001293 bitflips += BITS_PER_LONG - weight;
1294 if (unlikely(bitflips > bitflips_threshold))
1295 return -EBADMSG;
1296 }
1297
1298 for (; len > 0; len--, bitmap++) {
1299 weight = hweight8(*bitmap);
1300 bitflips += BITS_PER_BYTE - weight;
1301 if (unlikely(bitflips > bitflips_threshold))
1302 return -EBADMSG;
1303 }
1304
1305 return bitflips;
1306}
1307
1308/**
1309 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1310 * 0xff data
1311 * @data: data buffer to test
1312 * @datalen: data length
1313 * @ecc: ECC buffer
1314 * @ecclen: ECC length
1315 * @extraoob: extra OOB buffer
1316 * @extraooblen: extra OOB length
1317 * @bitflips_threshold: maximum number of bitflips
1318 *
1319 * Check if a data buffer and its associated ECC and OOB data contains only
1320 * 0xff pattern, which means the underlying region has been erased and is
1321 * ready to be programmed.
1322 * The bitflips_threshold specify the maximum number of bitflips before
1323 * considering the region as not erased.
1324 *
1325 * Note:
1326 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1327 * different from the NAND page size. When fixing bitflips, ECC engines will
1328 * report the number of errors per chunk, and the NAND core infrastructure
1329 * expect you to return the maximum number of bitflips for the whole page.
1330 * This is why you should always use this function on a single chunk and
1331 * not on the whole page. After checking each chunk you should update your
1332 * max_bitflips value accordingly.
1333 * 2/ When checking for bitflips in erased pages you should not only check
1334 * the payload data but also their associated ECC data, because a user might
1335 * have programmed almost all bits to 1 but a few. In this case, we
1336 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1337 * this case.
1338 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1339 * data are protected by the ECC engine.
1340 * It could also be used if you support subpages and want to attach some
1341 * extra OOB data to an ECC chunk.
1342 *
1343 * Returns a positive number of bitflips less than or equal to
1344 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1345 * threshold. In case of success, the passed buffers are filled with 0xff.
1346 */
1347int nand_check_erased_ecc_chunk(void *data, int datalen,
1348 void *ecc, int ecclen,
1349 void *extraoob, int extraooblen,
1350 int bitflips_threshold)
1351{
1352 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1353
1354 data_bitflips = nand_check_erased_buf(data, datalen,
1355 bitflips_threshold);
1356 if (data_bitflips < 0)
1357 return data_bitflips;
1358
1359 bitflips_threshold -= data_bitflips;
1360
1361 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1362 if (ecc_bitflips < 0)
1363 return ecc_bitflips;
1364
1365 bitflips_threshold -= ecc_bitflips;
1366
1367 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1368 bitflips_threshold);
1369 if (extraoob_bitflips < 0)
1370 return extraoob_bitflips;
1371
1372 if (data_bitflips)
1373 memset(data, 0xff, datalen);
1374
1375 if (ecc_bitflips)
1376 memset(ecc, 0xff, ecclen);
1377
1378 if (extraoob_bitflips)
1379 memset(extraoob, 0xff, extraooblen);
1380
1381 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1382}
1383EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1384
1385/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001386 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001387 * @mtd: mtd info structure
1388 * @chip: nand chip info structure
1389 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001390 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001391 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001392 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001393 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001394 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001395int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1396 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001397{
1398 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001399 if (oob_required)
1400 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001401 return 0;
1402}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001403EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001404
1405/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001406 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001407 * @mtd: mtd info structure
1408 * @chip: nand chip info structure
1409 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001410 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001411 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001412 *
1413 * We need a special oob layout and handling even when OOB isn't used.
1414 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001415static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001416 struct nand_chip *chip, uint8_t *buf,
1417 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001418{
1419 int eccsize = chip->ecc.size;
1420 int eccbytes = chip->ecc.bytes;
1421 uint8_t *oob = chip->oob_poi;
1422 int steps, size;
1423
1424 for (steps = chip->ecc.steps; steps > 0; steps--) {
1425 chip->read_buf(mtd, buf, eccsize);
1426 buf += eccsize;
1427
1428 if (chip->ecc.prepad) {
1429 chip->read_buf(mtd, oob, chip->ecc.prepad);
1430 oob += chip->ecc.prepad;
1431 }
1432
1433 chip->read_buf(mtd, oob, eccbytes);
1434 oob += eccbytes;
1435
1436 if (chip->ecc.postpad) {
1437 chip->read_buf(mtd, oob, chip->ecc.postpad);
1438 oob += chip->ecc.postpad;
1439 }
1440 }
1441
1442 size = mtd->oobsize - (oob - chip->oob_poi);
1443 if (size)
1444 chip->read_buf(mtd, oob, size);
1445
1446 return 0;
1447}
1448
1449/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001450 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001451 * @mtd: mtd info structure
1452 * @chip: nand chip info structure
1453 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001454 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001455 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001456 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001457static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001458 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Boris Brezillon846031d2016-02-03 20:11:00 +01001460 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001461 int eccbytes = chip->ecc.bytes;
1462 int eccsteps = chip->ecc.steps;
1463 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001464 uint8_t *ecc_calc = chip->buffers->ecccalc;
1465 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001466 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001467
Brian Norris1fbb9382012-05-02 10:14:55 -07001468 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001469
1470 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1471 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1472
Boris Brezillon846031d2016-02-03 20:11:00 +01001473 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1474 chip->ecc.total);
1475 if (ret)
1476 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001477
1478 eccsteps = chip->ecc.steps;
1479 p = buf;
1480
1481 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1482 int stat;
1483
1484 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001485 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001486 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001487 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001488 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001489 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1490 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001491 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001492 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001493}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301496 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001497 * @mtd: mtd info structure
1498 * @chip: nand chip info structure
1499 * @data_offs: offset of requested data within the page
1500 * @readlen: data length
1501 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001502 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001503 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001504static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001505 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1506 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001507{
Boris Brezillon846031d2016-02-03 20:11:00 +01001508 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001509 uint8_t *p;
1510 int data_col_addr, i, gaps = 0;
1511 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1512 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001513 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001514 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001515 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001516
Brian Norris7854d3f2011-06-23 14:12:08 -07001517 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001518 start_step = data_offs / chip->ecc.size;
1519 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1520 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301521 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001522
Brian Norris8b6e50c2011-05-25 14:59:01 -07001523 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001524 datafrag_len = num_steps * chip->ecc.size;
1525 eccfrag_len = num_steps * chip->ecc.bytes;
1526
1527 data_col_addr = start_step * chip->ecc.size;
1528 /* If we read not a page aligned data */
1529 if (data_col_addr != 0)
1530 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1531
1532 p = bufpoi + data_col_addr;
1533 chip->read_buf(mtd, p, datafrag_len);
1534
Brian Norris8b6e50c2011-05-25 14:59:01 -07001535 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001536 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1537 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1538
Brian Norris8b6e50c2011-05-25 14:59:01 -07001539 /*
1540 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001541 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001542 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001543 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1544 if (ret)
1545 return ret;
1546
1547 if (oobregion.length < eccfrag_len)
1548 gaps = 1;
1549
Alexey Korolev3d459552008-05-15 17:23:18 +01001550 if (gaps) {
1551 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1552 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1553 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001554 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001555 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001556 * about buswidth alignment in read_buf.
1557 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001558 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001559 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001560 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001561 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001562 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1563 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001564 aligned_len++;
1565
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001566 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001567 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001568 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1569 }
1570
Boris Brezillon846031d2016-02-03 20:11:00 +01001571 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1572 chip->oob_poi, index, eccfrag_len);
1573 if (ret)
1574 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001575
1576 p = bufpoi + data_col_addr;
1577 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1578 int stat;
1579
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001580 stat = chip->ecc.correct(mtd, p,
1581 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001582 if (stat == -EBADMSG &&
1583 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1584 /* check for empty pages with bitflips */
1585 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1586 &chip->buffers->ecccode[i],
1587 chip->ecc.bytes,
1588 NULL, 0,
1589 chip->ecc.strength);
1590 }
1591
Mike Dunn3f91e942012-04-25 12:06:09 -07001592 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001593 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001594 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001595 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001596 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1597 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001598 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001599 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001600}
1601
1602/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001603 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001604 * @mtd: mtd info structure
1605 * @chip: nand chip info structure
1606 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001607 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001608 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001609 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001610 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001611 */
1612static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001613 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001614{
Boris Brezillon846031d2016-02-03 20:11:00 +01001615 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001616 int eccbytes = chip->ecc.bytes;
1617 int eccsteps = chip->ecc.steps;
1618 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001619 uint8_t *ecc_calc = chip->buffers->ecccalc;
1620 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001621 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001622
1623 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1624 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1625 chip->read_buf(mtd, p, eccsize);
1626 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1627 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001628 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001629
Boris Brezillon846031d2016-02-03 20:11:00 +01001630 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1631 chip->ecc.total);
1632 if (ret)
1633 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001634
1635 eccsteps = chip->ecc.steps;
1636 p = buf;
1637
1638 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1639 int stat;
1640
1641 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001642 if (stat == -EBADMSG &&
1643 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1644 /* check for empty pages with bitflips */
1645 stat = nand_check_erased_ecc_chunk(p, eccsize,
1646 &ecc_code[i], eccbytes,
1647 NULL, 0,
1648 chip->ecc.strength);
1649 }
1650
Mike Dunn3f91e942012-04-25 12:06:09 -07001651 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001652 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001653 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001654 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001655 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1656 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001657 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001658 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001659}
1660
1661/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001662 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001663 * @mtd: mtd info structure
1664 * @chip: nand chip info structure
1665 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001666 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001667 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001668 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001669 * Hardware ECC for large page chips, require OOB to be read first. For this
1670 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1671 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1672 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1673 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001674 */
1675static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001676 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001677{
Boris Brezillon846031d2016-02-03 20:11:00 +01001678 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001679 int eccbytes = chip->ecc.bytes;
1680 int eccsteps = chip->ecc.steps;
1681 uint8_t *p = buf;
1682 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001683 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001684 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001685
1686 /* Read the OOB area first */
1687 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1688 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1689 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1690
Boris Brezillon846031d2016-02-03 20:11:00 +01001691 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1692 chip->ecc.total);
1693 if (ret)
1694 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001695
1696 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1697 int stat;
1698
1699 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1700 chip->read_buf(mtd, p, eccsize);
1701 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1702
1703 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001704 if (stat == -EBADMSG &&
1705 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1706 /* check for empty pages with bitflips */
1707 stat = nand_check_erased_ecc_chunk(p, eccsize,
1708 &ecc_code[i], eccbytes,
1709 NULL, 0,
1710 chip->ecc.strength);
1711 }
1712
Mike Dunn3f91e942012-04-25 12:06:09 -07001713 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001714 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001715 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001716 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001717 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1718 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001719 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001720 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001721}
1722
1723/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001724 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001725 * @mtd: mtd info structure
1726 * @chip: nand chip info structure
1727 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001728 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001729 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001730 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001731 * The hw generator calculates the error syndrome automatically. Therefore we
1732 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001733 */
1734static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001735 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001736{
1737 int i, eccsize = chip->ecc.size;
1738 int eccbytes = chip->ecc.bytes;
1739 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001740 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001741 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001742 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001743 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001744
1745 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1746 int stat;
1747
1748 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1749 chip->read_buf(mtd, p, eccsize);
1750
1751 if (chip->ecc.prepad) {
1752 chip->read_buf(mtd, oob, chip->ecc.prepad);
1753 oob += chip->ecc.prepad;
1754 }
1755
1756 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1757 chip->read_buf(mtd, oob, eccbytes);
1758 stat = chip->ecc.correct(mtd, p, oob, NULL);
1759
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001760 oob += eccbytes;
1761
1762 if (chip->ecc.postpad) {
1763 chip->read_buf(mtd, oob, chip->ecc.postpad);
1764 oob += chip->ecc.postpad;
1765 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001766
1767 if (stat == -EBADMSG &&
1768 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1769 /* check for empty pages with bitflips */
1770 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1771 oob - eccpadbytes,
1772 eccpadbytes,
1773 NULL, 0,
1774 chip->ecc.strength);
1775 }
1776
1777 if (stat < 0) {
1778 mtd->ecc_stats.failed++;
1779 } else {
1780 mtd->ecc_stats.corrected += stat;
1781 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1782 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001783 }
1784
1785 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001786 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001787 if (i)
1788 chip->read_buf(mtd, oob, i);
1789
Mike Dunn3f91e942012-04-25 12:06:09 -07001790 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001791}
1792
1793/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001794 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001795 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001796 * @oob: oob destination address
1797 * @ops: oob ops structure
1798 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001799 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001800static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001801 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001802{
Boris Brezillon846031d2016-02-03 20:11:00 +01001803 struct nand_chip *chip = mtd_to_nand(mtd);
1804 int ret;
1805
Florian Fainellif8ac0412010-09-07 13:23:43 +02001806 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001807
Brian Norris0612b9d2011-08-30 18:45:40 -07001808 case MTD_OPS_PLACE_OOB:
1809 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001810 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1811 return oob + len;
1812
Boris Brezillon846031d2016-02-03 20:11:00 +01001813 case MTD_OPS_AUTO_OOB:
1814 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1815 ops->ooboffs, len);
1816 BUG_ON(ret);
1817 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001818
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001819 default:
1820 BUG();
1821 }
1822 return NULL;
1823}
1824
1825/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001826 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1827 * @mtd: MTD device structure
1828 * @retry_mode: the retry mode to use
1829 *
1830 * Some vendors supply a special command to shift the Vt threshold, to be used
1831 * when there are too many bitflips in a page (i.e., ECC error). After setting
1832 * a new threshold, the host should retry reading the page.
1833 */
1834static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1835{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001836 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001837
1838 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1839
1840 if (retry_mode >= chip->read_retries)
1841 return -EINVAL;
1842
1843 if (!chip->setup_read_retry)
1844 return -EOPNOTSUPP;
1845
1846 return chip->setup_read_retry(mtd, retry_mode);
1847}
1848
1849/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001850 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001851 * @mtd: MTD device structure
1852 * @from: offset to read from
1853 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001854 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001855 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001856 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001857static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1858 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001859{
Brian Norrise47f3db2012-05-02 10:14:56 -07001860 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001861 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001862 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001863 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001864 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001865 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001866
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001867 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001868 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001869 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001870 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001871 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001873 chipnr = (int)(from >> chip->chip_shift);
1874 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001876 realpage = (int)(from >> chip->page_shift);
1877 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001879 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001881 buf = ops->datbuf;
1882 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001883 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001884
Florian Fainellif8ac0412010-09-07 13:23:43 +02001885 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001886 unsigned int ecc_failures = mtd->ecc_stats.failed;
1887
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001888 bytes = min(mtd->writesize - col, readlen);
1889 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001890
Kamal Dasu66507c72014-05-01 20:51:19 -04001891 if (!aligned)
1892 use_bufpoi = 1;
1893 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09001894 use_bufpoi = !virt_addr_valid(buf) ||
1895 !IS_ALIGNED((unsigned long)buf,
1896 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04001897 else
1898 use_bufpoi = 0;
1899
Brian Norris8b6e50c2011-05-25 14:59:01 -07001900 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001901 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001902 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1903
1904 if (use_bufpoi && aligned)
1905 pr_debug("%s: using read bounce buffer for buf@%p\n",
1906 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Brian Norrisba84fb52014-01-03 15:13:33 -08001908read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01001909 if (nand_standard_page_accessors(&chip->ecc))
1910 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Mike Dunnedbc45402012-04-25 12:06:11 -07001912 /*
1913 * Now read the page into the buffer. Absent an error,
1914 * the read methods return max bitflips per ecc step.
1915 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001916 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001917 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001918 oob_required,
1919 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001920 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1921 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001922 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001923 col, bytes, bufpoi,
1924 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001925 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001926 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001927 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001928 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001929 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001930 /* Invalidate page cache */
1931 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001932 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001933 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001934
1935 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001936 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001937 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001938 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001939 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001940 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001941 chip->pagebuf_bitflips = ret;
1942 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001943 /* Invalidate page cache */
1944 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001945 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001946 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001948
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001949 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001950 int toread = min(oobreadlen, max_oobsize);
1951
1952 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01001953 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001954 oob, ops, toread);
1955 oobreadlen -= toread;
1956 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001957 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001958
1959 if (chip->options & NAND_NEED_READRDY) {
1960 /* Apply delay or wait for ready/busy pin */
1961 if (!chip->dev_ready)
1962 udelay(chip->chip_delay);
1963 else
1964 nand_wait_ready(mtd);
1965 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001966
Brian Norrisba84fb52014-01-03 15:13:33 -08001967 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001968 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001969 retry_mode++;
1970 ret = nand_setup_read_retry(mtd,
1971 retry_mode);
1972 if (ret < 0)
1973 break;
1974
1975 /* Reset failures; retry */
1976 mtd->ecc_stats.failed = ecc_failures;
1977 goto read_retry;
1978 } else {
1979 /* No more retry modes; real failure */
1980 ecc_fail = true;
1981 }
1982 }
1983
1984 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09001985 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001986 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001987 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001988 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001989 max_bitflips = max_t(unsigned int, max_bitflips,
1990 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001993 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001994
Brian Norrisba84fb52014-01-03 15:13:33 -08001995 /* Reset to retry mode 0 */
1996 if (retry_mode) {
1997 ret = nand_setup_read_retry(mtd, 0);
1998 if (ret < 0)
1999 break;
2000 retry_mode = 0;
2001 }
2002
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002003 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002004 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Brian Norris8b6e50c2011-05-25 14:59:01 -07002006 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 col = 0;
2008 /* Increment page address */
2009 realpage++;
2010
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002011 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /* Check, if we cross a chip boundary */
2013 if (!page) {
2014 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002015 chip->select_chip(mtd, -1);
2016 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002019 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002021 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002022 if (oob)
2023 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Mike Dunn3f91e942012-04-25 12:06:09 -07002025 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002026 return ret;
2027
Brian Norrisb72f3df2013-12-03 11:04:14 -08002028 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002029 return -EBADMSG;
2030
Mike Dunnedbc45402012-04-25 12:06:11 -07002031 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002032}
2033
2034/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002035 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002036 * @mtd: MTD device structure
2037 * @from: offset to read from
2038 * @len: number of bytes to read
2039 * @retlen: pointer to variable to store the number of read bytes
2040 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002041 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002042 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002043 */
2044static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2045 size_t *retlen, uint8_t *buf)
2046{
Brian Norris4a89ff82011-08-30 18:45:45 -07002047 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002048 int ret;
2049
Huang Shijie6a8214a2012-11-19 14:43:30 +08002050 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002051 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002052 ops.len = len;
2053 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002054 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002055 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002056 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002057 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002058 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
2061/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002062 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002063 * @mtd: mtd info structure
2064 * @chip: nand chip info structure
2065 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002066 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002067int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002068{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002069 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002070 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002071 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002072}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002073EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002074
2075/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002076 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002077 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002078 * @mtd: mtd info structure
2079 * @chip: nand chip info structure
2080 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002081 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002082int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2083 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002084{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002085 int length = mtd->oobsize;
2086 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2087 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002088 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002089 int i, toread, sndrnd = 0, pos;
2090
2091 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2092 for (i = 0; i < chip->ecc.steps; i++) {
2093 if (sndrnd) {
2094 pos = eccsize + i * (eccsize + chunk);
2095 if (mtd->writesize > 512)
2096 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2097 else
2098 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2099 } else
2100 sndrnd = 1;
2101 toread = min_t(int, length, chunk);
2102 chip->read_buf(mtd, bufpoi, toread);
2103 bufpoi += toread;
2104 length -= toread;
2105 }
2106 if (length > 0)
2107 chip->read_buf(mtd, bufpoi, length);
2108
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002109 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002110}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002111EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002112
2113/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002114 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002115 * @mtd: mtd info structure
2116 * @chip: nand chip info structure
2117 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002118 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002119int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002120{
2121 int status = 0;
2122 const uint8_t *buf = chip->oob_poi;
2123 int length = mtd->oobsize;
2124
2125 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2126 chip->write_buf(mtd, buf, length);
2127 /* Send command to program the OOB data */
2128 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2129
2130 status = chip->waitfunc(mtd, chip);
2131
Savin Zlobec0d420f92006-06-21 11:51:20 +02002132 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002133}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002134EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002135
2136/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002137 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002138 * with syndrome - only for large page flash
2139 * @mtd: mtd info structure
2140 * @chip: nand chip info structure
2141 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002142 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002143int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2144 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002145{
2146 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2147 int eccsize = chip->ecc.size, length = mtd->oobsize;
2148 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2149 const uint8_t *bufpoi = chip->oob_poi;
2150
2151 /*
2152 * data-ecc-data-ecc ... ecc-oob
2153 * or
2154 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2155 */
2156 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2157 pos = steps * (eccsize + chunk);
2158 steps = 0;
2159 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002160 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002161
2162 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2163 for (i = 0; i < steps; i++) {
2164 if (sndcmd) {
2165 if (mtd->writesize <= 512) {
2166 uint32_t fill = 0xFFFFFFFF;
2167
2168 len = eccsize;
2169 while (len > 0) {
2170 int num = min_t(int, len, 4);
2171 chip->write_buf(mtd, (uint8_t *)&fill,
2172 num);
2173 len -= num;
2174 }
2175 } else {
2176 pos = eccsize + i * (eccsize + chunk);
2177 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2178 }
2179 } else
2180 sndcmd = 1;
2181 len = min_t(int, length, chunk);
2182 chip->write_buf(mtd, bufpoi, len);
2183 bufpoi += len;
2184 length -= len;
2185 }
2186 if (length > 0)
2187 chip->write_buf(mtd, bufpoi, length);
2188
2189 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2190 status = chip->waitfunc(mtd, chip);
2191
2192 return status & NAND_STATUS_FAIL ? -EIO : 0;
2193}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002194EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002195
2196/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002197 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002198 * @mtd: MTD device structure
2199 * @from: offset to read from
2200 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002202 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002204static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2205 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
Brian Norrisc00a0992012-05-01 17:12:54 -07002207 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002208 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002209 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002210 int readlen = ops->ooblen;
2211 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002212 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002213 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Brian Norris289c0522011-07-19 10:06:09 -07002215 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302216 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Brian Norris041e4572011-06-23 16:45:24 -07002218 stats = mtd->ecc_stats;
2219
Boris BREZILLON29f10582016-03-07 10:46:52 +01002220 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002221
2222 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002223 pr_debug("%s: attempt to start read outside oob\n",
2224 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002225 return -EINVAL;
2226 }
2227
2228 /* Do not allow reads past end of device */
2229 if (unlikely(from >= mtd->size ||
2230 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2231 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002232 pr_debug("%s: attempt to read beyond end of device\n",
2233 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002234 return -EINVAL;
2235 }
Vitaly Wool70145682006-11-03 18:20:38 +03002236
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002237 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002238 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002240 /* Shift to get page */
2241 realpage = (int)(from >> chip->page_shift);
2242 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Florian Fainellif8ac0412010-09-07 13:23:43 +02002244 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002245 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002246 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002247 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002248 ret = chip->ecc.read_oob(mtd, chip, page);
2249
2250 if (ret < 0)
2251 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002252
2253 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002254 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002255
Brian Norris5bc7c332013-03-13 09:51:31 -07002256 if (chip->options & NAND_NEED_READRDY) {
2257 /* Apply delay or wait for ready/busy pin */
2258 if (!chip->dev_ready)
2259 udelay(chip->chip_delay);
2260 else
2261 nand_wait_ready(mtd);
2262 }
2263
Vitaly Wool70145682006-11-03 18:20:38 +03002264 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002265 if (!readlen)
2266 break;
2267
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002268 /* Increment page address */
2269 realpage++;
2270
2271 page = realpage & chip->pagemask;
2272 /* Check, if we cross a chip boundary */
2273 if (!page) {
2274 chipnr++;
2275 chip->select_chip(mtd, -1);
2276 chip->select_chip(mtd, chipnr);
2277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002279 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002281 ops->oobretlen = ops->ooblen - readlen;
2282
2283 if (ret < 0)
2284 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002285
2286 if (mtd->ecc_stats.failed - stats.failed)
2287 return -EBADMSG;
2288
2289 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
2292/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002293 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002294 * @mtd: MTD device structure
2295 * @from: offset to read from
2296 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002298 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002300static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2301 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002303 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002304
2305 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002308 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002309 pr_debug("%s: attempt to read beyond end of device\n",
2310 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return -EINVAL;
2312 }
2313
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002314 if (ops->mode != MTD_OPS_PLACE_OOB &&
2315 ops->mode != MTD_OPS_AUTO_OOB &&
2316 ops->mode != MTD_OPS_RAW)
2317 return -ENOTSUPP;
2318
Huang Shijie6a8214a2012-11-19 14:43:30 +08002319 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002321 if (!ops->datbuf)
2322 ret = nand_do_read_oob(mtd, from, ops);
2323 else
2324 ret = nand_do_read_ops(mtd, from, ops);
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002327 return ret;
2328}
2329
2330
2331/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002332 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002333 * @mtd: mtd info structure
2334 * @chip: nand chip info structure
2335 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002336 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002337 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002338 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002339 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002340 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002341int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2342 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002343{
2344 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002345 if (oob_required)
2346 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002347
2348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002350EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002352/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002353 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002354 * @mtd: mtd info structure
2355 * @chip: nand chip info structure
2356 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002357 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002358 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002359 *
2360 * We need a special oob layout and handling even when ECC isn't checked.
2361 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002362static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002363 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002364 const uint8_t *buf, int oob_required,
2365 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002366{
2367 int eccsize = chip->ecc.size;
2368 int eccbytes = chip->ecc.bytes;
2369 uint8_t *oob = chip->oob_poi;
2370 int steps, size;
2371
2372 for (steps = chip->ecc.steps; steps > 0; steps--) {
2373 chip->write_buf(mtd, buf, eccsize);
2374 buf += eccsize;
2375
2376 if (chip->ecc.prepad) {
2377 chip->write_buf(mtd, oob, chip->ecc.prepad);
2378 oob += chip->ecc.prepad;
2379 }
2380
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002381 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002382 oob += eccbytes;
2383
2384 if (chip->ecc.postpad) {
2385 chip->write_buf(mtd, oob, chip->ecc.postpad);
2386 oob += chip->ecc.postpad;
2387 }
2388 }
2389
2390 size = mtd->oobsize - (oob - chip->oob_poi);
2391 if (size)
2392 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002393
2394 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002395}
2396/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002397 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002398 * @mtd: mtd info structure
2399 * @chip: nand chip info structure
2400 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002401 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002402 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002403 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002404static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002405 const uint8_t *buf, int oob_required,
2406 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002407{
Boris Brezillon846031d2016-02-03 20:11:00 +01002408 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002409 int eccbytes = chip->ecc.bytes;
2410 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002411 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002412 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002413
Brian Norris7854d3f2011-06-23 14:12:08 -07002414 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002415 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2416 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002417
Boris Brezillon846031d2016-02-03 20:11:00 +01002418 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2419 chip->ecc.total);
2420 if (ret)
2421 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002422
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002423 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002424}
2425
2426/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002427 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002428 * @mtd: mtd info structure
2429 * @chip: nand chip info structure
2430 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002431 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002432 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002433 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002434static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002435 const uint8_t *buf, int oob_required,
2436 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002437{
Boris Brezillon846031d2016-02-03 20:11:00 +01002438 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002439 int eccbytes = chip->ecc.bytes;
2440 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002441 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002442 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002443
2444 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2445 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002446 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002447 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2448 }
2449
Boris Brezillon846031d2016-02-03 20:11:00 +01002450 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2451 chip->ecc.total);
2452 if (ret)
2453 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002454
2455 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002456
2457 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002458}
2459
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302460
2461/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002462 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302463 * @mtd: mtd info structure
2464 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002465 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302466 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002467 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302468 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002469 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302470 */
2471static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2472 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002473 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002474 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302475{
2476 uint8_t *oob_buf = chip->oob_poi;
2477 uint8_t *ecc_calc = chip->buffers->ecccalc;
2478 int ecc_size = chip->ecc.size;
2479 int ecc_bytes = chip->ecc.bytes;
2480 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302481 uint32_t start_step = offset / ecc_size;
2482 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2483 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002484 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302485
2486 for (step = 0; step < ecc_steps; step++) {
2487 /* configure controller for WRITE access */
2488 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2489
2490 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002491 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302492
2493 /* mask ECC of un-touched subpages by padding 0xFF */
2494 if ((step < start_step) || (step > end_step))
2495 memset(ecc_calc, 0xff, ecc_bytes);
2496 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002497 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302498
2499 /* mask OOB of un-touched subpages by padding 0xFF */
2500 /* if oob_required, preserve OOB metadata of written subpage */
2501 if (!oob_required || (step < start_step) || (step > end_step))
2502 memset(oob_buf, 0xff, oob_bytes);
2503
Brian Norrisd6a950802013-08-08 17:16:36 -07002504 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302505 ecc_calc += ecc_bytes;
2506 oob_buf += oob_bytes;
2507 }
2508
2509 /* copy calculated ECC for whole page to chip->buffer->oob */
2510 /* this include masked-value(0xFF) for unwritten subpages */
2511 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002512 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2513 chip->ecc.total);
2514 if (ret)
2515 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302516
2517 /* write OOB buffer to NAND device */
2518 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2519
2520 return 0;
2521}
2522
2523
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002524/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002525 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002526 * @mtd: mtd info structure
2527 * @chip: nand chip info structure
2528 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002529 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002530 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002531 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002532 * The hw generator calculates the error syndrome automatically. Therefore we
2533 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002534 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002535static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002536 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002537 const uint8_t *buf, int oob_required,
2538 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002539{
2540 int i, eccsize = chip->ecc.size;
2541 int eccbytes = chip->ecc.bytes;
2542 int eccsteps = chip->ecc.steps;
2543 const uint8_t *p = buf;
2544 uint8_t *oob = chip->oob_poi;
2545
2546 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2547
2548 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2549 chip->write_buf(mtd, p, eccsize);
2550
2551 if (chip->ecc.prepad) {
2552 chip->write_buf(mtd, oob, chip->ecc.prepad);
2553 oob += chip->ecc.prepad;
2554 }
2555
2556 chip->ecc.calculate(mtd, p, oob);
2557 chip->write_buf(mtd, oob, eccbytes);
2558 oob += eccbytes;
2559
2560 if (chip->ecc.postpad) {
2561 chip->write_buf(mtd, oob, chip->ecc.postpad);
2562 oob += chip->ecc.postpad;
2563 }
2564 }
2565
2566 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002567 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002568 if (i)
2569 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002570
2571 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002572}
2573
2574/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002575 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002576 * @mtd: MTD device structure
2577 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302578 * @offset: address offset within the page
2579 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002580 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002581 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002582 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002583 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002584 */
2585static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302586 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002587 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002588{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302589 int status, subpage;
2590
2591 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2592 chip->ecc.write_subpage)
2593 subpage = offset || (data_len < mtd->writesize);
2594 else
2595 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002596
Marc Gonzalez3371d662016-11-15 10:56:20 +01002597 if (nand_standard_page_accessors(&chip->ecc))
2598 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002599
David Woodhouse956e9442006-09-25 17:12:39 +01002600 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302601 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002602 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302603 else if (subpage)
2604 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002605 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002606 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002607 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2608 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002609
2610 if (status < 0)
2611 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002612
Boris Brezillon41145642017-05-16 18:27:49 +02002613 if (nand_standard_page_accessors(&chip->ecc)) {
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002614 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002615
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002616 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002617 if (status & NAND_STATUS_FAIL)
2618 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002619 }
2620
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002621 return 0;
2622}
2623
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002624/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002625 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002626 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002627 * @oob: oob data buffer
2628 * @len: oob data write length
2629 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002630 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002631static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2632 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002633{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002634 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002635 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002636
2637 /*
2638 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2639 * data from a previous OOB read.
2640 */
2641 memset(chip->oob_poi, 0xff, mtd->oobsize);
2642
Florian Fainellif8ac0412010-09-07 13:23:43 +02002643 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002644
Brian Norris0612b9d2011-08-30 18:45:40 -07002645 case MTD_OPS_PLACE_OOB:
2646 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002647 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2648 return oob + len;
2649
Boris Brezillon846031d2016-02-03 20:11:00 +01002650 case MTD_OPS_AUTO_OOB:
2651 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2652 ops->ooboffs, len);
2653 BUG_ON(ret);
2654 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002655
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002656 default:
2657 BUG();
2658 }
2659 return NULL;
2660}
2661
Florian Fainellif8ac0412010-09-07 13:23:43 +02002662#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002663
2664/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002665 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002666 * @mtd: MTD device structure
2667 * @to: offset to write to
2668 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002669 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002670 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002671 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002672static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2673 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002674{
Corentin Labbe73600b62017-09-02 10:49:38 +02002675 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002676 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002677 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002678
2679 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002680 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002681
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002682 uint8_t *oob = ops->oobbuf;
2683 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302684 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002685 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002686
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002687 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002688 if (!writelen)
2689 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002690
Brian Norris8b6e50c2011-05-25 14:59:01 -07002691 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002692 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002693 pr_notice("%s: attempt to write non page aligned data\n",
2694 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002695 return -EINVAL;
2696 }
2697
Thomas Gleixner29072b92006-09-28 15:38:36 +02002698 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002699
Thomas Gleixner6a930962006-06-28 00:11:45 +02002700 chipnr = (int)(to >> chip->chip_shift);
2701 chip->select_chip(mtd, chipnr);
2702
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002703 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002704 if (nand_check_wp(mtd)) {
2705 ret = -EIO;
2706 goto err_out;
2707 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002708
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002709 realpage = (int)(to >> chip->page_shift);
2710 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002711
2712 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002713 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2714 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002715 chip->pagebuf = -1;
2716
Maxim Levitsky782ce792010-02-22 20:39:36 +02002717 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002718 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2719 ret = -EINVAL;
2720 goto err_out;
2721 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002722
Florian Fainellif8ac0412010-09-07 13:23:43 +02002723 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002724 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002725 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002726 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002727 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002728
Kamal Dasu66507c72014-05-01 20:51:19 -04002729 if (part_pagewr)
2730 use_bufpoi = 1;
2731 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002732 use_bufpoi = !virt_addr_valid(buf) ||
2733 !IS_ALIGNED((unsigned long)buf,
2734 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002735 else
2736 use_bufpoi = 0;
2737
2738 /* Partial page write?, or need to use bounce buffer */
2739 if (use_bufpoi) {
2740 pr_debug("%s: using write bounce buffer for buf@%p\n",
2741 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04002742 if (part_pagewr)
2743 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002744 chip->pagebuf = -1;
2745 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2746 memcpy(&chip->buffers->databuf[column], buf, bytes);
2747 wbuf = chip->buffers->databuf;
2748 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002749
Maxim Levitsky782ce792010-02-22 20:39:36 +02002750 if (unlikely(oob)) {
2751 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002752 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002753 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002754 } else {
2755 /* We still need to erase leftover OOB data */
2756 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002757 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002758
2759 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002760 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002761 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002762 if (ret)
2763 break;
2764
2765 writelen -= bytes;
2766 if (!writelen)
2767 break;
2768
Thomas Gleixner29072b92006-09-28 15:38:36 +02002769 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002770 buf += bytes;
2771 realpage++;
2772
2773 page = realpage & chip->pagemask;
2774 /* Check, if we cross a chip boundary */
2775 if (!page) {
2776 chipnr++;
2777 chip->select_chip(mtd, -1);
2778 chip->select_chip(mtd, chipnr);
2779 }
2780 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002781
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002782 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002783 if (unlikely(oob))
2784 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002785
2786err_out:
2787 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002788 return ret;
2789}
2790
2791/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002792 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002793 * @mtd: MTD device structure
2794 * @to: offset to write to
2795 * @len: number of bytes to write
2796 * @retlen: pointer to variable to store the number of written bytes
2797 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002798 *
2799 * NAND write with ECC. Used when performing writes in interrupt context, this
2800 * may for example be called by mtdoops when writing an oops while in panic.
2801 */
2802static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2803 size_t *retlen, const uint8_t *buf)
2804{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002805 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05002806 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07002807 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002808 int ret;
2809
Brian Norris8b6e50c2011-05-25 14:59:01 -07002810 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002811 panic_nand_get_device(chip, mtd, FL_WRITING);
2812
Brent Taylor30863e382017-10-30 22:32:45 -05002813 chip->select_chip(mtd, chipnr);
2814
2815 /* Wait for the device to get ready */
2816 panic_nand_wait(mtd, chip, 400);
2817
Brian Norris0ec56dc2015-02-28 02:02:30 -08002818 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002819 ops.len = len;
2820 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002821 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002822
Brian Norris4a89ff82011-08-30 18:45:45 -07002823 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002824
Brian Norris4a89ff82011-08-30 18:45:45 -07002825 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002826 return ret;
2827}
2828
2829/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002830 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002831 * @mtd: MTD device structure
2832 * @to: offset to write to
2833 * @len: number of bytes to write
2834 * @retlen: pointer to variable to store the number of written bytes
2835 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002837 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002839static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002840 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Brian Norris4a89ff82011-08-30 18:45:45 -07002842 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002843 int ret;
2844
Huang Shijie6a8214a2012-11-19 14:43:30 +08002845 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002846 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002847 ops.len = len;
2848 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002849 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002850 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002851 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002852 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002853 return ret;
2854}
2855
2856/**
2857 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002858 * @mtd: MTD device structure
2859 * @to: offset to write to
2860 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002861 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002862 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002863 */
2864static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2865 struct mtd_oob_ops *ops)
2866{
Adrian Hunter03736152007-01-31 17:58:29 +02002867 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002868 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Brian Norris289c0522011-07-19 10:06:09 -07002870 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302871 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Boris BREZILLON29f10582016-03-07 10:46:52 +01002873 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002876 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002877 pr_debug("%s: attempt to write past end of page\n",
2878 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return -EINVAL;
2880 }
2881
Adrian Hunter03736152007-01-31 17:58:29 +02002882 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002883 pr_debug("%s: attempt to start write outside oob\n",
2884 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002885 return -EINVAL;
2886 }
2887
Jason Liu775adc3d42011-02-25 13:06:18 +08002888 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002889 if (unlikely(to >= mtd->size ||
2890 ops->ooboffs + ops->ooblen >
2891 ((mtd->size >> chip->page_shift) -
2892 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002893 pr_debug("%s: attempt to write beyond end of device\n",
2894 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002895 return -EINVAL;
2896 }
2897
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002898 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002899
2900 /*
2901 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2902 * of my DiskOnChip 2000 test units) will clear the whole data page too
2903 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2904 * it in the doc2000 driver in August 1999. dwmw2.
2905 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002906 nand_reset(chip, chipnr);
2907
2908 chip->select_chip(mtd, chipnr);
2909
2910 /* Shift to get page */
2911 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
2913 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002914 if (nand_check_wp(mtd)) {
2915 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002916 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002917 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002920 if (page == chip->pagebuf)
2921 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002923 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002924
Brian Norris0612b9d2011-08-30 18:45:40 -07002925 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002926 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2927 else
2928 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002929
Huang Shijieb0bb6902012-11-19 14:43:29 +08002930 chip->select_chip(mtd, -1);
2931
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002932 if (status)
2933 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Vitaly Wool70145682006-11-03 18:20:38 +03002935 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002937 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002938}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002940/**
2941 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002942 * @mtd: MTD device structure
2943 * @to: offset to write to
2944 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002945 */
2946static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2947 struct mtd_oob_ops *ops)
2948{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002949 int ret = -ENOTSUPP;
2950
2951 ops->retlen = 0;
2952
2953 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002954 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002955 pr_debug("%s: attempt to write beyond end of device\n",
2956 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002957 return -EINVAL;
2958 }
2959
Huang Shijie6a8214a2012-11-19 14:43:30 +08002960 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002961
Florian Fainellif8ac0412010-09-07 13:23:43 +02002962 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002963 case MTD_OPS_PLACE_OOB:
2964 case MTD_OPS_AUTO_OOB:
2965 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002966 break;
2967
2968 default:
2969 goto out;
2970 }
2971
2972 if (!ops->datbuf)
2973 ret = nand_do_write_oob(mtd, to, ops);
2974 else
2975 ret = nand_do_write_ops(mtd, to, ops);
2976
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002977out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002978 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return ret;
2980}
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982/**
Brian Norris49c50b92014-05-06 16:02:19 -07002983 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002984 * @mtd: MTD device structure
2985 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 *
Brian Norris49c50b92014-05-06 16:02:19 -07002987 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 */
Brian Norris49c50b92014-05-06 16:02:19 -07002989static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002991 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002993 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2994 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002995
2996 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
2999/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003001 * @mtd: MTD device structure
3002 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003004 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003006static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
David Woodhousee0c7d762006-05-13 18:07:53 +01003008 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003012 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003013 * @mtd: MTD device structure
3014 * @instr: erase instruction
3015 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003017 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003019int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3020 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
Adrian Hunter69423d92008-12-10 13:37:21 +00003022 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003023 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003024 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Brian Norris289c0522011-07-19 10:06:09 -07003026 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3027 __func__, (unsigned long long)instr->addr,
3028 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303030 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003034 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003037 page = (int)(instr->addr >> chip->page_shift);
3038 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003041 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
3043 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003044 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 /* Check, if it is write protected */
3047 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003048 pr_debug("%s: device is write protected!\n",
3049 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 instr->state = MTD_ERASE_FAILED;
3051 goto erase_exit;
3052 }
3053
3054 /* Loop through the pages */
3055 len = instr->len;
3056
3057 instr->state = MTD_ERASING;
3058
3059 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003060 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003061 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303062 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003063 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3064 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 instr->state = MTD_ERASE_FAILED;
3066 goto erase_exit;
3067 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003068
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003069 /*
3070 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003071 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003072 */
3073 if (page <= chip->pagebuf && chip->pagebuf <
3074 (page + pages_per_block))
3075 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Brian Norris49c50b92014-05-06 16:02:19 -07003077 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003080 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003081 pr_debug("%s: failed erase, page 0x%08x\n",
3082 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003084 instr->fail_addr =
3085 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 goto erase_exit;
3087 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003090 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 page += pages_per_block;
3092
3093 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003094 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003096 chip->select_chip(mtd, -1);
3097 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
3099 }
3100 instr->state = MTD_ERASE_DONE;
3101
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003102erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
3106 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003107 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 nand_release_device(mtd);
3109
David Woodhouse49defc02007-10-06 15:01:59 -04003110 /* Do call back function */
3111 if (!ret)
3112 mtd_erase_callback(instr);
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 /* Return more or less happy */
3115 return ret;
3116}
3117
3118/**
3119 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003120 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003122 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003124static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
Brian Norris289c0522011-07-19 10:06:09 -07003126 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003129 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003131 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132}
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003135 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003136 * @mtd: MTD device structure
3137 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003139static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303141 struct nand_chip *chip = mtd_to_nand(mtd);
3142 int chipnr = (int)(offs >> chip->chip_shift);
3143 int ret;
3144
3145 /* Select the NAND device */
3146 nand_get_device(mtd, FL_READING);
3147 chip->select_chip(mtd, chipnr);
3148
3149 ret = nand_block_checkbad(mtd, offs, 0);
3150
3151 chip->select_chip(mtd, -1);
3152 nand_release_device(mtd);
3153
3154 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
3157/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003158 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003159 * @mtd: MTD device structure
3160 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003162static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 int ret;
3165
Florian Fainellif8ac0412010-09-07 13:23:43 +02003166 ret = nand_block_isbad(mtd, ofs);
3167 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003168 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 if (ret > 0)
3170 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003171 return ret;
3172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Brian Norris5a0edb22013-07-30 17:52:58 -07003174 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
3177/**
Zach Brown56718422017-01-10 13:30:20 -06003178 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3179 * @mtd: MTD device structure
3180 * @ofs: offset relative to mtd start
3181 * @len: length of mtd
3182 */
3183static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3184{
3185 struct nand_chip *chip = mtd_to_nand(mtd);
3186 u32 part_start_block;
3187 u32 part_end_block;
3188 u32 part_start_die;
3189 u32 part_end_die;
3190
3191 /*
3192 * max_bb_per_die and blocks_per_die used to determine
3193 * the maximum bad block count.
3194 */
3195 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3196 return -ENOTSUPP;
3197
3198 /* Get the start and end of the partition in erase blocks. */
3199 part_start_block = mtd_div_by_eb(ofs, mtd);
3200 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3201
3202 /* Get the start and end LUNs of the partition. */
3203 part_start_die = part_start_block / chip->blocks_per_die;
3204 part_end_die = part_end_block / chip->blocks_per_die;
3205
3206 /*
3207 * Look up the bad blocks per unit and multiply by the number of units
3208 * that the partition spans.
3209 */
3210 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3211}
3212
3213/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003214 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3215 * @mtd: MTD device structure
3216 * @chip: nand chip info structure
3217 * @addr: feature address.
3218 * @subfeature_param: the subfeature parameters, a four bytes array.
3219 */
3220static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3221 int addr, uint8_t *subfeature_param)
3222{
3223 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003224 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003225
David Mosbergerd914c932013-05-29 15:30:13 +03003226 if (!chip->onfi_version ||
3227 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3228 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003229 return -EINVAL;
3230
3231 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003232 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3233 chip->write_byte(mtd, subfeature_param[i]);
3234
Huang Shijie7db03ec2012-09-13 14:57:52 +08003235 status = chip->waitfunc(mtd, chip);
3236 if (status & NAND_STATUS_FAIL)
3237 return -EIO;
3238 return 0;
3239}
3240
3241/**
3242 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3243 * @mtd: MTD device structure
3244 * @chip: nand chip info structure
3245 * @addr: feature address.
3246 * @subfeature_param: the subfeature parameters, a four bytes array.
3247 */
3248static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3249 int addr, uint8_t *subfeature_param)
3250{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003251 int i;
3252
David Mosbergerd914c932013-05-29 15:30:13 +03003253 if (!chip->onfi_version ||
3254 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3255 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003256 return -EINVAL;
3257
Huang Shijie7db03ec2012-09-13 14:57:52 +08003258 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003259 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3260 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003261 return 0;
3262}
3263
3264/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003265 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3266 * -ENOTSUPP
3267 * @mtd: MTD device structure
3268 * @chip: nand chip info structure
3269 * @addr: feature address.
3270 * @subfeature_param: the subfeature parameters, a four bytes array.
3271 *
3272 * Should be used by NAND controller drivers that do not support the SET/GET
3273 * FEATURES operations.
3274 */
3275int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3276 struct nand_chip *chip, int addr,
3277 u8 *subfeature_param)
3278{
3279 return -ENOTSUPP;
3280}
3281EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3282
3283/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003284 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003285 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003286 */
3287static int nand_suspend(struct mtd_info *mtd)
3288{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003289 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003290}
3291
3292/**
3293 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003294 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003295 */
3296static void nand_resume(struct mtd_info *mtd)
3297{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003298 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003299
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003300 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003301 nand_release_device(mtd);
3302 else
Brian Norrisd0370212011-07-19 10:06:08 -07003303 pr_err("%s called for a chip which is not in suspended state\n",
3304 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003305}
3306
Scott Branden72ea4032014-11-20 11:18:05 -08003307/**
3308 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3309 * prevent further operations
3310 * @mtd: MTD device structure
3311 */
3312static void nand_shutdown(struct mtd_info *mtd)
3313{
Brian Norris9ca641b2015-11-09 16:37:28 -08003314 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003315}
3316
Brian Norris8b6e50c2011-05-25 14:59:01 -07003317/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003318static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003319{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003320 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003323 if (!chip->chip_delay)
3324 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
3326 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003327 if (chip->cmdfunc == NULL)
3328 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
3330 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003331 if (chip->waitfunc == NULL)
3332 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003334 if (!chip->select_chip)
3335 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003336
Huang Shijie4204ccc2013-08-16 10:10:07 +08003337 /* set for ONFI nand */
3338 if (!chip->onfi_set_features)
3339 chip->onfi_set_features = nand_onfi_set_features;
3340 if (!chip->onfi_get_features)
3341 chip->onfi_get_features = nand_onfi_get_features;
3342
Brian Norris68e80782013-07-18 01:17:02 -07003343 /* If called twice, pointers that depend on busw may need to be reset */
3344 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003345 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3346 if (!chip->read_word)
3347 chip->read_word = nand_read_word;
3348 if (!chip->block_bad)
3349 chip->block_bad = nand_block_bad;
3350 if (!chip->block_markbad)
3351 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003352 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003353 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003354 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3355 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003356 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003357 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003358 if (!chip->scan_bbt)
3359 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003360
3361 if (!chip->controller) {
3362 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003363 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003364 }
3365
Masahiro Yamada477544c2017-03-30 17:15:05 +09003366 if (!chip->buf_align)
3367 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003368}
3369
Brian Norris8b6e50c2011-05-25 14:59:01 -07003370/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003371static void sanitize_string(uint8_t *s, size_t len)
3372{
3373 ssize_t i;
3374
Brian Norris8b6e50c2011-05-25 14:59:01 -07003375 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003376 s[len - 1] = 0;
3377
Brian Norris8b6e50c2011-05-25 14:59:01 -07003378 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003379 for (i = 0; i < len - 1; i++) {
3380 if (s[i] < ' ' || s[i] > 127)
3381 s[i] = '?';
3382 }
3383
Brian Norris8b6e50c2011-05-25 14:59:01 -07003384 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003385 strim(s);
3386}
3387
3388static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3389{
3390 int i;
3391 while (len--) {
3392 crc ^= *p++ << 8;
3393 for (i = 0; i < 8; i++)
3394 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3395 }
3396
3397 return crc;
3398}
3399
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003400/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003401static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3402 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003403{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003404 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003405 struct onfi_ext_param_page *ep;
3406 struct onfi_ext_section *s;
3407 struct onfi_ext_ecc_info *ecc;
3408 uint8_t *cursor;
3409 int ret = -EINVAL;
3410 int len;
3411 int i;
3412
3413 len = le16_to_cpu(p->ext_param_page_length) * 16;
3414 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003415 if (!ep)
3416 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003417
3418 /* Send our own NAND_CMD_PARAM. */
3419 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3420
3421 /* Use the Change Read Column command to skip the ONFI param pages. */
3422 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3423 sizeof(*p) * p->num_of_param_pages , -1);
3424
3425 /* Read out the Extended Parameter Page. */
3426 chip->read_buf(mtd, (uint8_t *)ep, len);
3427 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3428 != le16_to_cpu(ep->crc))) {
3429 pr_debug("fail in the CRC.\n");
3430 goto ext_out;
3431 }
3432
3433 /*
3434 * Check the signature.
3435 * Do not strictly follow the ONFI spec, maybe changed in future.
3436 */
3437 if (strncmp(ep->sig, "EPPS", 4)) {
3438 pr_debug("The signature is invalid.\n");
3439 goto ext_out;
3440 }
3441
3442 /* find the ECC section. */
3443 cursor = (uint8_t *)(ep + 1);
3444 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3445 s = ep->sections + i;
3446 if (s->type == ONFI_SECTION_TYPE_2)
3447 break;
3448 cursor += s->length * 16;
3449 }
3450 if (i == ONFI_EXT_SECTION_MAX) {
3451 pr_debug("We can not find the ECC section.\n");
3452 goto ext_out;
3453 }
3454
3455 /* get the info we want. */
3456 ecc = (struct onfi_ext_ecc_info *)cursor;
3457
Brian Norris4ae7d222013-09-16 18:20:21 -07003458 if (!ecc->codeword_size) {
3459 pr_debug("Invalid codeword size\n");
3460 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003461 }
3462
Brian Norris4ae7d222013-09-16 18:20:21 -07003463 chip->ecc_strength_ds = ecc->ecc_bits;
3464 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003465 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003466
3467ext_out:
3468 kfree(ep);
3469 return ret;
3470}
3471
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003472/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003473 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003474 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003475static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003476{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003477 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003478 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003479 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003480 int val;
3481
Brian Norris7854d3f2011-06-23 14:12:08 -07003482 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003483 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3484 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3485 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3486 return 0;
3487
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003488 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3489 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003490 for (j = 0; j < sizeof(*p); j++)
3491 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003492 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3493 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003494 break;
3495 }
3496 }
3497
Brian Norrisc7f23a72013-08-13 10:51:55 -07003498 if (i == 3) {
3499 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003500 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003501 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003502
Brian Norris8b6e50c2011-05-25 14:59:01 -07003503 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003504 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003505 if (val & (1 << 5))
3506 chip->onfi_version = 23;
3507 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003508 chip->onfi_version = 22;
3509 else if (val & (1 << 3))
3510 chip->onfi_version = 21;
3511 else if (val & (1 << 2))
3512 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003513 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003514 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003515
3516 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003517 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003518 return 0;
3519 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003520
3521 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3522 sanitize_string(p->model, sizeof(p->model));
3523 if (!mtd->name)
3524 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003525
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003526 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003527
3528 /*
3529 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3530 * (don't ask me who thought of this...). MTD assumes that these
3531 * dimensions will be power-of-2, so just truncate the remaining area.
3532 */
3533 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3534 mtd->erasesize *= mtd->writesize;
3535
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003536 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003537
3538 /* See erasesize comment */
3539 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003540 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003541 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003542
Zach Brown34da5f52017-01-10 13:30:21 -06003543 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3544 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3545
Huang Shijiee2985fc2013-05-17 11:17:30 +08003546 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003547 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003548
Huang Shijie10c86ba2013-05-17 11:17:26 +08003549 if (p->ecc_bits != 0xff) {
3550 chip->ecc_strength_ds = p->ecc_bits;
3551 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003552 } else if (chip->onfi_version >= 21 &&
3553 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3554
3555 /*
3556 * The nand_flash_detect_ext_param_page() uses the
3557 * Change Read Column command which maybe not supported
3558 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3559 * now. We do not replace user supplied command function.
3560 */
3561 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3562 chip->cmdfunc = nand_command_lp;
3563
3564 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003565 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003566 pr_warn("Failed to detect ONFI extended param page\n");
3567 } else {
3568 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003569 }
3570
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003571 return 1;
3572}
3573
3574/*
Huang Shijie91361812014-02-21 13:39:40 +08003575 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3576 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003577static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003578{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003579 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003580 struct nand_jedec_params *p = &chip->jedec_params;
3581 struct jedec_ecc_info *ecc;
3582 int val;
3583 int i, j;
3584
3585 /* Try JEDEC for unknown chip or LP */
3586 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3587 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3588 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3589 chip->read_byte(mtd) != 'C')
3590 return 0;
3591
3592 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3593 for (i = 0; i < 3; i++) {
3594 for (j = 0; j < sizeof(*p); j++)
3595 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3596
3597 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3598 le16_to_cpu(p->crc))
3599 break;
3600 }
3601
3602 if (i == 3) {
3603 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3604 return 0;
3605 }
3606
3607 /* Check version */
3608 val = le16_to_cpu(p->revision);
3609 if (val & (1 << 2))
3610 chip->jedec_version = 10;
3611 else if (val & (1 << 1))
3612 chip->jedec_version = 1; /* vendor specific version */
3613
3614 if (!chip->jedec_version) {
3615 pr_info("unsupported JEDEC version: %d\n", val);
3616 return 0;
3617 }
3618
3619 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3620 sanitize_string(p->model, sizeof(p->model));
3621 if (!mtd->name)
3622 mtd->name = p->model;
3623
3624 mtd->writesize = le32_to_cpu(p->byte_per_page);
3625
3626 /* Please reference to the comment for nand_flash_detect_onfi. */
3627 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3628 mtd->erasesize *= mtd->writesize;
3629
3630 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3631
3632 /* Please reference to the comment for nand_flash_detect_onfi. */
3633 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3634 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3635 chip->bits_per_cell = p->bits_per_cell;
3636
3637 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003638 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003639
3640 /* ECC info */
3641 ecc = &p->ecc_info[0];
3642
3643 if (ecc->codeword_size >= 9) {
3644 chip->ecc_strength_ds = ecc->ecc_bits;
3645 chip->ecc_step_ds = 1 << ecc->codeword_size;
3646 } else {
3647 pr_warn("Invalid codeword size\n");
3648 }
3649
3650 return 1;
3651}
3652
3653/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003654 * nand_id_has_period - Check if an ID string has a given wraparound period
3655 * @id_data: the ID string
3656 * @arrlen: the length of the @id_data array
3657 * @period: the period of repitition
3658 *
3659 * Check if an ID string is repeated within a given sequence of bytes at
3660 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003661 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003662 * if the repetition has a period of @period; otherwise, returns zero.
3663 */
3664static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3665{
3666 int i, j;
3667 for (i = 0; i < period; i++)
3668 for (j = i + period; j < arrlen; j += period)
3669 if (id_data[i] != id_data[j])
3670 return 0;
3671 return 1;
3672}
3673
3674/*
3675 * nand_id_len - Get the length of an ID string returned by CMD_READID
3676 * @id_data: the ID string
3677 * @arrlen: the length of the @id_data array
3678
3679 * Returns the length of the ID string, according to known wraparound/trailing
3680 * zero patterns. If no pattern exists, returns the length of the array.
3681 */
3682static int nand_id_len(u8 *id_data, int arrlen)
3683{
3684 int last_nonzero, period;
3685
3686 /* Find last non-zero byte */
3687 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3688 if (id_data[last_nonzero])
3689 break;
3690
3691 /* All zeros */
3692 if (last_nonzero < 0)
3693 return 0;
3694
3695 /* Calculate wraparound period */
3696 for (period = 1; period < arrlen; period++)
3697 if (nand_id_has_period(id_data, arrlen, period))
3698 break;
3699
3700 /* There's a repeated pattern */
3701 if (period < arrlen)
3702 return period;
3703
3704 /* There are trailing zeros */
3705 if (last_nonzero < arrlen - 1)
3706 return last_nonzero + 1;
3707
3708 /* No pattern detected */
3709 return arrlen;
3710}
3711
Huang Shijie7db906b2013-09-25 14:58:11 +08003712/* Extract the bits of per cell from the 3rd byte of the extended ID */
3713static int nand_get_bits_per_cell(u8 cellinfo)
3714{
3715 int bits;
3716
3717 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3718 bits >>= NAND_CI_CELLTYPE_SHIFT;
3719 return bits + 1;
3720}
3721
Brian Norrise3b88bd2012-09-24 20:40:52 -07003722/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003723 * Many new NAND share similar device ID codes, which represent the size of the
3724 * chip. The rest of the parameters must be decoded according to generic or
3725 * manufacturer-specific "extended ID" decoding patterns.
3726 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003727void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003728{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003729 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003730 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003731 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003732 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003733 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003734 /* The 4th id byte is the important one */
3735 extid = id_data[3];
3736
Boris Brezillon01389b62016-06-08 10:30:18 +02003737 /* Calc pagesize */
3738 mtd->writesize = 1024 << (extid & 0x03);
3739 extid >>= 2;
3740 /* Calc oobsize */
3741 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3742 extid >>= 2;
3743 /* Calc blocksize. Blocksize is multiples of 64KiB */
3744 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3745 extid >>= 2;
3746 /* Get buswidth information */
3747 if (extid & 0x1)
3748 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003749}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003750EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003751
3752/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003753 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3754 * decodes a matching ID table entry and assigns the MTD size parameters for
3755 * the chip.
3756 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003757static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003758{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003759 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003760
3761 mtd->erasesize = type->erasesize;
3762 mtd->writesize = type->pagesize;
3763 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003764
Huang Shijie1c195e92013-09-25 14:58:12 +08003765 /* All legacy ID NAND are small-page, SLC */
3766 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003767}
3768
3769/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003770 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3771 * heuristic patterns using various detected parameters (e.g., manufacturer,
3772 * page size, cell-type information).
3773 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003774static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003775{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003776 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003777
3778 /* Set the bad block position */
3779 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3780 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3781 else
3782 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003783}
3784
Huang Shijieec6e87e2013-03-15 11:01:00 +08003785static inline bool is_full_id_nand(struct nand_flash_dev *type)
3786{
3787 return type->id_len;
3788}
3789
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003790static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003791 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003792{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003793 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003794 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003795
Huang Shijieec6e87e2013-03-15 11:01:00 +08003796 if (!strncmp(type->id, id_data, type->id_len)) {
3797 mtd->writesize = type->pagesize;
3798 mtd->erasesize = type->erasesize;
3799 mtd->oobsize = type->oobsize;
3800
Huang Shijie7db906b2013-09-25 14:58:11 +08003801 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003802 chip->chipsize = (uint64_t)type->chipsize << 20;
3803 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003804 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3805 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003806 chip->onfi_timing_mode_default =
3807 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003808
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003809 if (!mtd->name)
3810 mtd->name = type->name;
3811
Huang Shijieec6e87e2013-03-15 11:01:00 +08003812 return true;
3813 }
3814 return false;
3815}
3816
Brian Norris7e74c2d2012-09-24 20:40:49 -07003817/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003818 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3819 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3820 * table.
3821 */
3822static void nand_manufacturer_detect(struct nand_chip *chip)
3823{
3824 /*
3825 * Try manufacturer detection if available and use
3826 * nand_decode_ext_id() otherwise.
3827 */
3828 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003829 chip->manufacturer.desc->ops->detect) {
3830 /* The 3rd id byte holds MLC / multichip data */
3831 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003832 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003833 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003834 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003835 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003836}
3837
3838/*
3839 * Manufacturer initialization. This function is called for all NANDs including
3840 * ONFI and JEDEC compliant ones.
3841 * Manufacturer drivers should put all their specific initialization code in
3842 * their ->init() hook.
3843 */
3844static int nand_manufacturer_init(struct nand_chip *chip)
3845{
3846 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
3847 !chip->manufacturer.desc->ops->init)
3848 return 0;
3849
3850 return chip->manufacturer.desc->ops->init(chip);
3851}
3852
3853/*
3854 * Manufacturer cleanup. This function is called for all NANDs including
3855 * ONFI and JEDEC compliant ones.
3856 * Manufacturer drivers should put all their specific cleanup code in their
3857 * ->cleanup() hook.
3858 */
3859static void nand_manufacturer_cleanup(struct nand_chip *chip)
3860{
3861 /* Release manufacturer private data */
3862 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3863 chip->manufacturer.desc->ops->cleanup)
3864 chip->manufacturer.desc->ops->cleanup(chip);
3865}
3866
3867/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003868 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003869 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02003870static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003871{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003872 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003873 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08003874 int busw;
Boris Brezillonf84674b2017-06-02 12:18:24 +02003875 int i;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003876 u8 *id_data = chip->id.data;
3877 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
Karl Beldanef89a882008-09-15 14:37:29 +02003879 /*
3880 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003881 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003882 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003883 nand_reset(chip, 0);
3884
3885 /* Select the device */
3886 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02003887
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003889 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
3891 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003892 maf_id = chip->read_byte(mtd);
3893 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
Brian Norris8b6e50c2011-05-25 14:59:01 -07003895 /*
3896 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003897 * interface concerns can cause random data which looks like a
3898 * possibly credible NAND flash to appear. If the two results do
3899 * not match, ignore the device completely.
3900 */
3901
3902 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3903
Brian Norris4aef9b72012-09-24 20:40:48 -07003904 /* Read entire ID string */
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003905 for (i = 0; i < ARRAY_SIZE(chip->id.data); i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003906 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003907
Boris Brezillon7f501f02016-05-24 19:20:05 +02003908 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003909 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003910 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003911 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01003912 }
3913
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003914 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02003915
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003916 /* Try to identify manufacturer */
3917 manufacturer = nand_get_manufacturer(maf_id);
3918 chip->manufacturer.desc = manufacturer;
3919
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003920 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003921 type = nand_flash_ids;
3922
Boris Brezillon29a198a2016-05-24 20:17:48 +02003923 /*
3924 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
3925 * override it.
3926 * This is required to make sure initial NAND bus width set by the
3927 * NAND controller driver is coherent with the real NAND bus width
3928 * (extracted by auto-detection code).
3929 */
3930 busw = chip->options & NAND_BUSWIDTH_16;
3931
3932 /*
3933 * The flag is only set (never cleared), reset it to its default value
3934 * before starting auto-detection.
3935 */
3936 chip->options &= ~NAND_BUSWIDTH_16;
3937
Huang Shijieec6e87e2013-03-15 11:01:00 +08003938 for (; type->name != NULL; type++) {
3939 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003940 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08003941 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003942 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003943 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003944 }
3945 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003946
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003947 chip->onfi_version = 0;
3948 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003949 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003950 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003951 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003952
3953 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003954 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08003955 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003956 }
3957
David Woodhouse5e81e882010-02-26 18:32:56 +00003958 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003959 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003960
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003961 if (!mtd->name)
3962 mtd->name = type->name;
3963
Adrian Hunter69423d92008-12-10 13:37:21 +00003964 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003965
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003966 if (!type->pagesize)
3967 nand_manufacturer_detect(chip);
3968 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02003969 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003970
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003971 /* Get chip options */
3972 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003973
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003974ident_done:
3975
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003976 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003977 WARN_ON(busw & NAND_BUSWIDTH_16);
3978 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003979 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3980 /*
3981 * Check, if buswidth is correct. Hardware drivers should set
3982 * chip correct!
3983 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003984 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003985 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003986 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
3987 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02003988 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
3989 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003990 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003991 }
3992
Boris Brezillon7f501f02016-05-24 19:20:05 +02003993 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003994
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003995 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003996 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003997 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003998 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003999
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004000 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004001 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004002 if (chip->chipsize & 0xffffffff)
4003 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004004 else {
4005 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4006 chip->chip_shift += 32 - 1;
4007 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004008
Masahiro Yamada14157f82017-09-13 11:05:50 +09004009 if (chip->chip_shift - chip->page_shift > 16)
4010 chip->options |= NAND_ROW_ADDR_3;
4011
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004012 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004013 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004014
Brian Norris8b6e50c2011-05-25 14:59:01 -07004015 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004016 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4017 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004018
Ezequiel Garcia20171642013-11-25 08:30:31 -03004019 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004020 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004021
4022 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004023 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4024 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004025 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004026 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4027 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004028 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004029 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4030 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004031
Rafał Miłecki3755a992014-10-21 00:01:04 +02004032 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004033 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004034 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004035 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004036}
4037
Boris Brezillond48f62b2016-04-01 14:54:32 +02004038static const char * const nand_ecc_modes[] = {
4039 [NAND_ECC_NONE] = "none",
4040 [NAND_ECC_SOFT] = "soft",
4041 [NAND_ECC_HW] = "hw",
4042 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4043 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004044 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004045};
4046
4047static int of_get_nand_ecc_mode(struct device_node *np)
4048{
4049 const char *pm;
4050 int err, i;
4051
4052 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4053 if (err < 0)
4054 return err;
4055
4056 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4057 if (!strcasecmp(pm, nand_ecc_modes[i]))
4058 return i;
4059
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004060 /*
4061 * For backward compatibility we support few obsoleted values that don't
4062 * have their mappings into nand_ecc_modes_t anymore (they were merged
4063 * with other enums).
4064 */
4065 if (!strcasecmp(pm, "soft_bch"))
4066 return NAND_ECC_SOFT;
4067
Boris Brezillond48f62b2016-04-01 14:54:32 +02004068 return -ENODEV;
4069}
4070
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004071static const char * const nand_ecc_algos[] = {
4072 [NAND_ECC_HAMMING] = "hamming",
4073 [NAND_ECC_BCH] = "bch",
4074};
4075
Boris Brezillond48f62b2016-04-01 14:54:32 +02004076static int of_get_nand_ecc_algo(struct device_node *np)
4077{
4078 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004079 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004080
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004081 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4082 if (!err) {
4083 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4084 if (!strcasecmp(pm, nand_ecc_algos[i]))
4085 return i;
4086 return -ENODEV;
4087 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004088
4089 /*
4090 * For backward compatibility we also read "nand-ecc-mode" checking
4091 * for some obsoleted values that were specifying ECC algorithm.
4092 */
4093 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4094 if (err < 0)
4095 return err;
4096
4097 if (!strcasecmp(pm, "soft"))
4098 return NAND_ECC_HAMMING;
4099 else if (!strcasecmp(pm, "soft_bch"))
4100 return NAND_ECC_BCH;
4101
4102 return -ENODEV;
4103}
4104
4105static int of_get_nand_ecc_step_size(struct device_node *np)
4106{
4107 int ret;
4108 u32 val;
4109
4110 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4111 return ret ? ret : val;
4112}
4113
4114static int of_get_nand_ecc_strength(struct device_node *np)
4115{
4116 int ret;
4117 u32 val;
4118
4119 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4120 return ret ? ret : val;
4121}
4122
4123static int of_get_nand_bus_width(struct device_node *np)
4124{
4125 u32 val;
4126
4127 if (of_property_read_u32(np, "nand-bus-width", &val))
4128 return 8;
4129
4130 switch (val) {
4131 case 8:
4132 case 16:
4133 return val;
4134 default:
4135 return -EIO;
4136 }
4137}
4138
4139static bool of_get_nand_on_flash_bbt(struct device_node *np)
4140{
4141 return of_property_read_bool(np, "nand-on-flash-bbt");
4142}
4143
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004144static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004145{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004146 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004147 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004148
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004149 if (!dn)
4150 return 0;
4151
Brian Norris5844fee2015-01-23 00:22:27 -08004152 if (of_get_nand_bus_width(dn) == 16)
4153 chip->options |= NAND_BUSWIDTH_16;
4154
4155 if (of_get_nand_on_flash_bbt(dn))
4156 chip->bbt_options |= NAND_BBT_USE_FLASH;
4157
4158 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004159 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004160 ecc_strength = of_get_nand_ecc_strength(dn);
4161 ecc_step = of_get_nand_ecc_step_size(dn);
4162
Brian Norris5844fee2015-01-23 00:22:27 -08004163 if (ecc_mode >= 0)
4164 chip->ecc.mode = ecc_mode;
4165
Rafał Miłecki79082452016-03-23 11:19:02 +01004166 if (ecc_algo >= 0)
4167 chip->ecc.algo = ecc_algo;
4168
Brian Norris5844fee2015-01-23 00:22:27 -08004169 if (ecc_strength >= 0)
4170 chip->ecc.strength = ecc_strength;
4171
4172 if (ecc_step > 0)
4173 chip->ecc.size = ecc_step;
4174
Boris Brezillonba78ee02016-06-08 17:04:22 +02004175 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4176 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4177
Brian Norris5844fee2015-01-23 00:22:27 -08004178 return 0;
4179}
4180
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004181/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004182 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004183 * @mtd: MTD device structure
4184 * @maxchips: number of chips to scan for
4185 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004186 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004187 * This is the first phase of the normal nand_scan() function. It reads the
4188 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004189 *
4190 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004191int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4192 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004193{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004194 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004195 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004196 int ret;
4197
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004198 ret = nand_dt_init(chip);
4199 if (ret)
4200 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004201
Brian Norrisf7a8e382016-01-05 10:39:45 -08004202 if (!mtd->name && mtd->dev.parent)
4203 mtd->name = dev_name(mtd->dev.parent);
4204
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004205 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4206 /*
4207 * Default functions assigned for chip_select() and
4208 * cmdfunc() both expect cmd_ctrl() to be populated,
4209 * so we need to check that that's the case
4210 */
4211 pr_err("chip.cmd_ctrl() callback is not provided");
4212 return -EINVAL;
4213 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004214 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004215 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004216
4217 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004218 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004219 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004220 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004221 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004222 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 }
4225
Boris Brezillon7f501f02016-05-24 19:20:05 +02004226 nand_maf_id = chip->id.data[0];
4227 nand_dev_id = chip->id.data[1];
4228
Huang Shijie07300162012-11-09 16:23:45 +08004229 chip->select_chip(mtd, -1);
4230
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004231 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004232 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004233 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004234 nand_reset(chip, i);
4235
4236 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004238 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004240 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004241 nand_dev_id != chip->read_byte(mtd)) {
4242 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 break;
Huang Shijie07300162012-11-09 16:23:45 +08004244 }
4245 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 }
4247 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004248 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004249
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004251 chip->numchips = i;
4252 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
David Woodhouse3b85c322006-09-25 17:06:53 +01004254 return 0;
4255}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004256EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004257
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004258static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4259{
4260 struct nand_chip *chip = mtd_to_nand(mtd);
4261 struct nand_ecc_ctrl *ecc = &chip->ecc;
4262
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004263 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004264 return -EINVAL;
4265
4266 switch (ecc->algo) {
4267 case NAND_ECC_HAMMING:
4268 ecc->calculate = nand_calculate_ecc;
4269 ecc->correct = nand_correct_data;
4270 ecc->read_page = nand_read_page_swecc;
4271 ecc->read_subpage = nand_read_subpage;
4272 ecc->write_page = nand_write_page_swecc;
4273 ecc->read_page_raw = nand_read_page_raw;
4274 ecc->write_page_raw = nand_write_page_raw;
4275 ecc->read_oob = nand_read_oob_std;
4276 ecc->write_oob = nand_write_oob_std;
4277 if (!ecc->size)
4278 ecc->size = 256;
4279 ecc->bytes = 3;
4280 ecc->strength = 1;
4281 return 0;
4282 case NAND_ECC_BCH:
4283 if (!mtd_nand_has_bch()) {
4284 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4285 return -EINVAL;
4286 }
4287 ecc->calculate = nand_bch_calculate_ecc;
4288 ecc->correct = nand_bch_correct_data;
4289 ecc->read_page = nand_read_page_swecc;
4290 ecc->read_subpage = nand_read_subpage;
4291 ecc->write_page = nand_write_page_swecc;
4292 ecc->read_page_raw = nand_read_page_raw;
4293 ecc->write_page_raw = nand_write_page_raw;
4294 ecc->read_oob = nand_read_oob_std;
4295 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004296
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004297 /*
4298 * Board driver should supply ecc.size and ecc.strength
4299 * values to select how many bits are correctable.
4300 * Otherwise, default to 4 bits for large page devices.
4301 */
4302 if (!ecc->size && (mtd->oobsize >= 64)) {
4303 ecc->size = 512;
4304 ecc->strength = 4;
4305 }
4306
4307 /*
4308 * if no ecc placement scheme was provided pickup the default
4309 * large page one.
4310 */
4311 if (!mtd->ooblayout) {
4312 /* handle large page devices only */
4313 if (mtd->oobsize < 64) {
4314 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4315 return -EINVAL;
4316 }
4317
4318 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004319
4320 }
4321
4322 /*
4323 * We can only maximize ECC config when the default layout is
4324 * used, otherwise we don't know how many bytes can really be
4325 * used.
4326 */
4327 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4328 ecc->options & NAND_ECC_MAXIMIZE) {
4329 int steps, bytes;
4330
4331 /* Always prefer 1k blocks over 512bytes ones */
4332 ecc->size = 1024;
4333 steps = mtd->writesize / ecc->size;
4334
4335 /* Reserve 2 bytes for the BBM */
4336 bytes = (mtd->oobsize - 2) / steps;
4337 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004338 }
4339
4340 /* See nand_bch_init() for details. */
4341 ecc->bytes = 0;
4342 ecc->priv = nand_bch_init(mtd);
4343 if (!ecc->priv) {
4344 WARN(1, "BCH ECC initialization failed!\n");
4345 return -EINVAL;
4346 }
4347 return 0;
4348 default:
4349 WARN(1, "Unsupported ECC algorithm!\n");
4350 return -EINVAL;
4351 }
4352}
4353
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09004354/**
4355 * nand_check_ecc_caps - check the sanity of preset ECC settings
4356 * @chip: nand chip info structure
4357 * @caps: ECC caps info structure
4358 * @oobavail: OOB size that the ECC engine can use
4359 *
4360 * When ECC step size and strength are already set, check if they are supported
4361 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4362 * On success, the calculated ECC bytes is set.
4363 */
4364int nand_check_ecc_caps(struct nand_chip *chip,
4365 const struct nand_ecc_caps *caps, int oobavail)
4366{
4367 struct mtd_info *mtd = nand_to_mtd(chip);
4368 const struct nand_ecc_step_info *stepinfo;
4369 int preset_step = chip->ecc.size;
4370 int preset_strength = chip->ecc.strength;
4371 int nsteps, ecc_bytes;
4372 int i, j;
4373
4374 if (WARN_ON(oobavail < 0))
4375 return -EINVAL;
4376
4377 if (!preset_step || !preset_strength)
4378 return -ENODATA;
4379
4380 nsteps = mtd->writesize / preset_step;
4381
4382 for (i = 0; i < caps->nstepinfos; i++) {
4383 stepinfo = &caps->stepinfos[i];
4384
4385 if (stepinfo->stepsize != preset_step)
4386 continue;
4387
4388 for (j = 0; j < stepinfo->nstrengths; j++) {
4389 if (stepinfo->strengths[j] != preset_strength)
4390 continue;
4391
4392 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4393 preset_strength);
4394 if (WARN_ON_ONCE(ecc_bytes < 0))
4395 return ecc_bytes;
4396
4397 if (ecc_bytes * nsteps > oobavail) {
4398 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4399 preset_step, preset_strength);
4400 return -ENOSPC;
4401 }
4402
4403 chip->ecc.bytes = ecc_bytes;
4404
4405 return 0;
4406 }
4407 }
4408
4409 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4410 preset_step, preset_strength);
4411
4412 return -ENOTSUPP;
4413}
4414EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4415
4416/**
4417 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4418 * @chip: nand chip info structure
4419 * @caps: ECC engine caps info structure
4420 * @oobavail: OOB size that the ECC engine can use
4421 *
4422 * If a chip's ECC requirement is provided, try to meet it with the least
4423 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4424 * On success, the chosen ECC settings are set.
4425 */
4426int nand_match_ecc_req(struct nand_chip *chip,
4427 const struct nand_ecc_caps *caps, int oobavail)
4428{
4429 struct mtd_info *mtd = nand_to_mtd(chip);
4430 const struct nand_ecc_step_info *stepinfo;
4431 int req_step = chip->ecc_step_ds;
4432 int req_strength = chip->ecc_strength_ds;
4433 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4434 int best_step, best_strength, best_ecc_bytes;
4435 int best_ecc_bytes_total = INT_MAX;
4436 int i, j;
4437
4438 if (WARN_ON(oobavail < 0))
4439 return -EINVAL;
4440
4441 /* No information provided by the NAND chip */
4442 if (!req_step || !req_strength)
4443 return -ENOTSUPP;
4444
4445 /* number of correctable bits the chip requires in a page */
4446 req_corr = mtd->writesize / req_step * req_strength;
4447
4448 for (i = 0; i < caps->nstepinfos; i++) {
4449 stepinfo = &caps->stepinfos[i];
4450 step_size = stepinfo->stepsize;
4451
4452 for (j = 0; j < stepinfo->nstrengths; j++) {
4453 strength = stepinfo->strengths[j];
4454
4455 /*
4456 * If both step size and strength are smaller than the
4457 * chip's requirement, it is not easy to compare the
4458 * resulted reliability.
4459 */
4460 if (step_size < req_step && strength < req_strength)
4461 continue;
4462
4463 if (mtd->writesize % step_size)
4464 continue;
4465
4466 nsteps = mtd->writesize / step_size;
4467
4468 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4469 if (WARN_ON_ONCE(ecc_bytes < 0))
4470 continue;
4471 ecc_bytes_total = ecc_bytes * nsteps;
4472
4473 if (ecc_bytes_total > oobavail ||
4474 strength * nsteps < req_corr)
4475 continue;
4476
4477 /*
4478 * We assume the best is to meet the chip's requrement
4479 * with the least number of ECC bytes.
4480 */
4481 if (ecc_bytes_total < best_ecc_bytes_total) {
4482 best_ecc_bytes_total = ecc_bytes_total;
4483 best_step = step_size;
4484 best_strength = strength;
4485 best_ecc_bytes = ecc_bytes;
4486 }
4487 }
4488 }
4489
4490 if (best_ecc_bytes_total == INT_MAX)
4491 return -ENOTSUPP;
4492
4493 chip->ecc.size = best_step;
4494 chip->ecc.strength = best_strength;
4495 chip->ecc.bytes = best_ecc_bytes;
4496
4497 return 0;
4498}
4499EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4500
4501/**
4502 * nand_maximize_ecc - choose the max ECC strength available
4503 * @chip: nand chip info structure
4504 * @caps: ECC engine caps info structure
4505 * @oobavail: OOB size that the ECC engine can use
4506 *
4507 * Choose the max ECC strength that is supported on the controller, and can fit
4508 * within the chip's OOB. On success, the chosen ECC settings are set.
4509 */
4510int nand_maximize_ecc(struct nand_chip *chip,
4511 const struct nand_ecc_caps *caps, int oobavail)
4512{
4513 struct mtd_info *mtd = nand_to_mtd(chip);
4514 const struct nand_ecc_step_info *stepinfo;
4515 int step_size, strength, nsteps, ecc_bytes, corr;
4516 int best_corr = 0;
4517 int best_step = 0;
4518 int best_strength, best_ecc_bytes;
4519 int i, j;
4520
4521 if (WARN_ON(oobavail < 0))
4522 return -EINVAL;
4523
4524 for (i = 0; i < caps->nstepinfos; i++) {
4525 stepinfo = &caps->stepinfos[i];
4526 step_size = stepinfo->stepsize;
4527
4528 /* If chip->ecc.size is already set, respect it */
4529 if (chip->ecc.size && step_size != chip->ecc.size)
4530 continue;
4531
4532 for (j = 0; j < stepinfo->nstrengths; j++) {
4533 strength = stepinfo->strengths[j];
4534
4535 if (mtd->writesize % step_size)
4536 continue;
4537
4538 nsteps = mtd->writesize / step_size;
4539
4540 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4541 if (WARN_ON_ONCE(ecc_bytes < 0))
4542 continue;
4543
4544 if (ecc_bytes * nsteps > oobavail)
4545 continue;
4546
4547 corr = strength * nsteps;
4548
4549 /*
4550 * If the number of correctable bits is the same,
4551 * bigger step_size has more reliability.
4552 */
4553 if (corr > best_corr ||
4554 (corr == best_corr && step_size > best_step)) {
4555 best_corr = corr;
4556 best_step = step_size;
4557 best_strength = strength;
4558 best_ecc_bytes = ecc_bytes;
4559 }
4560 }
4561 }
4562
4563 if (!best_corr)
4564 return -ENOTSUPP;
4565
4566 chip->ecc.size = best_step;
4567 chip->ecc.strength = best_strength;
4568 chip->ecc.bytes = best_ecc_bytes;
4569
4570 return 0;
4571}
4572EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4573
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004574/*
4575 * Check if the chip configuration meet the datasheet requirements.
4576
4577 * If our configuration corrects A bits per B bytes and the minimum
4578 * required correction level is X bits per Y bytes, then we must ensure
4579 * both of the following are true:
4580 *
4581 * (1) A / B >= X / Y
4582 * (2) A >= X
4583 *
4584 * Requirement (1) ensures we can correct for the required bitflip density.
4585 * Requirement (2) ensures we can correct even when all bitflips are clumped
4586 * in the same sector.
4587 */
4588static bool nand_ecc_strength_good(struct mtd_info *mtd)
4589{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004590 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004591 struct nand_ecc_ctrl *ecc = &chip->ecc;
4592 int corr, ds_corr;
4593
4594 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4595 /* Not enough information */
4596 return true;
4597
4598 /*
4599 * We get the number of corrected bits per page to compare
4600 * the correction density.
4601 */
4602 corr = (mtd->writesize * ecc->strength) / ecc->size;
4603 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4604
4605 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4606}
David Woodhouse3b85c322006-09-25 17:06:53 +01004607
Marc Gonzalez3371d662016-11-15 10:56:20 +01004608static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4609{
4610 struct nand_ecc_ctrl *ecc = &chip->ecc;
4611
4612 if (nand_standard_page_accessors(ecc))
4613 return false;
4614
4615 /*
4616 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4617 * controller driver implements all the page accessors because
4618 * default helpers are not suitable when the core does not
4619 * send the READ0/PAGEPROG commands.
4620 */
4621 return (!ecc->read_page || !ecc->write_page ||
4622 !ecc->read_page_raw || !ecc->write_page_raw ||
4623 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4624 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4625 ecc->hwctl && ecc->calculate));
4626}
4627
David Woodhouse3b85c322006-09-25 17:06:53 +01004628/**
4629 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004630 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004631 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004632 * This is the second phase of the normal nand_scan() function. It fills out
4633 * all the uninitialized function pointers with the defaults and scans for a
4634 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004635 */
4636int nand_scan_tail(struct mtd_info *mtd)
4637{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004638 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004639 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004640 struct nand_buffers *nbuf = NULL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004641 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01004642
Brian Norrise2414f42012-02-06 13:44:00 -08004643 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004644 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07004645 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02004646 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07004647 }
Brian Norrise2414f42012-02-06 13:44:00 -08004648
Marc Gonzalez3371d662016-11-15 10:56:20 +01004649 if (invalid_ecc_page_accessors(chip)) {
4650 pr_err("Invalid ECC page accessors setup\n");
Boris Brezillonf84674b2017-06-02 12:18:24 +02004651 return -EINVAL;
Marc Gonzalez3371d662016-11-15 10:56:20 +01004652 }
4653
Huang Shijief02ea4e2014-01-13 14:27:12 +08004654 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004655 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Boris Brezillonf84674b2017-06-02 12:18:24 +02004656 if (!nbuf)
4657 return -ENOMEM;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004658
4659 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4660 if (!nbuf->ecccalc) {
4661 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004662 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004663 }
4664
4665 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4666 if (!nbuf->ecccode) {
4667 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004668 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004669 }
4670
4671 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4672 GFP_KERNEL);
4673 if (!nbuf->databuf) {
4674 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004675 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004676 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004677
4678 chip->buffers = nbuf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004679 } else if (!chip->buffers) {
4680 return -ENOMEM;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004681 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004682
Boris Brezillonf84674b2017-06-02 12:18:24 +02004683 /*
4684 * FIXME: some NAND manufacturer drivers expect the first die to be
4685 * selected when manufacturer->init() is called. They should be fixed
4686 * to explictly select the relevant die when interacting with the NAND
4687 * chip.
4688 */
4689 chip->select_chip(mtd, 0);
4690 ret = nand_manufacturer_init(chip);
4691 chip->select_chip(mtd, -1);
4692 if (ret)
4693 goto err_free_nbuf;
4694
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004695 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004696 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004697
4698 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004699 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004700 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004701 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004702 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004703 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004706 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 break;
4708 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004709 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02004710 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004711 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02004713 /*
4714 * Expose the whole OOB area to users if ECC_NONE
4715 * is passed. We could do that for all kind of
4716 * ->oobsize, but we must keep the old large/small
4717 * page with ECC layout when ->oobsize <= 128 for
4718 * compatibility reasons.
4719 */
4720 if (ecc->mode == NAND_ECC_NONE) {
4721 mtd_set_ooblayout(mtd,
4722 &nand_ooblayout_lp_ops);
4723 break;
4724 }
4725
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004726 WARN(1, "No oob scheme defined for oobsize %d\n",
4727 mtd->oobsize);
4728 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004729 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 }
4731 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004732
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004733 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004734 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004735 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004736 */
David Woodhouse956e9442006-09-25 17:12:39 +01004737
Huang Shijie97de79e02013-10-18 14:20:53 +08004738 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004739 case NAND_ECC_HW_OOB_FIRST:
4740 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004741 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004742 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4743 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004744 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004745 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004746 if (!ecc->read_page)
4747 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004748
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004749 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004750 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004751 if (!ecc->read_page)
4752 ecc->read_page = nand_read_page_hwecc;
4753 if (!ecc->write_page)
4754 ecc->write_page = nand_write_page_hwecc;
4755 if (!ecc->read_page_raw)
4756 ecc->read_page_raw = nand_read_page_raw;
4757 if (!ecc->write_page_raw)
4758 ecc->write_page_raw = nand_write_page_raw;
4759 if (!ecc->read_oob)
4760 ecc->read_oob = nand_read_oob_std;
4761 if (!ecc->write_oob)
4762 ecc->write_oob = nand_write_oob_std;
4763 if (!ecc->read_subpage)
4764 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004765 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004766 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004767
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004768 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004769 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4770 (!ecc->read_page ||
4771 ecc->read_page == nand_read_page_hwecc ||
4772 !ecc->write_page ||
4773 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004774 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4775 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004776 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004777 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004778 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004779 if (!ecc->read_page)
4780 ecc->read_page = nand_read_page_syndrome;
4781 if (!ecc->write_page)
4782 ecc->write_page = nand_write_page_syndrome;
4783 if (!ecc->read_page_raw)
4784 ecc->read_page_raw = nand_read_page_raw_syndrome;
4785 if (!ecc->write_page_raw)
4786 ecc->write_page_raw = nand_write_page_raw_syndrome;
4787 if (!ecc->read_oob)
4788 ecc->read_oob = nand_read_oob_syndrome;
4789 if (!ecc->write_oob)
4790 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004791
Huang Shijie97de79e02013-10-18 14:20:53 +08004792 if (mtd->writesize >= ecc->size) {
4793 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004794 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4795 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004796 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07004797 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004798 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004799 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004800 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4801 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004802 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004803 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004805 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004806 ret = nand_set_ecc_soft_ops(mtd);
4807 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004808 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004809 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01004810 }
4811 break;
4812
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004813 case NAND_ECC_ON_DIE:
4814 if (!ecc->read_page || !ecc->write_page) {
4815 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4816 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004817 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004818 }
4819 if (!ecc->read_oob)
4820 ecc->read_oob = nand_read_oob_std;
4821 if (!ecc->write_oob)
4822 ecc->write_oob = nand_write_oob_std;
4823 break;
4824
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004825 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004826 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004827 ecc->read_page = nand_read_page_raw;
4828 ecc->write_page = nand_write_page_raw;
4829 ecc->read_oob = nand_read_oob_std;
4830 ecc->read_page_raw = nand_read_page_raw;
4831 ecc->write_page_raw = nand_write_page_raw;
4832 ecc->write_oob = nand_write_oob_std;
4833 ecc->size = mtd->writesize;
4834 ecc->bytes = 0;
4835 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004839 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4840 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004841 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Brian Norris9ce244b2011-08-30 18:45:37 -07004844 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004845 if (!ecc->read_oob_raw)
4846 ecc->read_oob_raw = ecc->read_oob;
4847 if (!ecc->write_oob_raw)
4848 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004849
Boris Brezillon846031d2016-02-03 20:11:00 +01004850 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004851 mtd->ecc_strength = ecc->strength;
4852 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004853
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004854 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004855 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004856 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004857 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004858 ecc->steps = mtd->writesize / ecc->size;
4859 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004860 WARN(1, "Invalid ECC parameters\n");
4861 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004862 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004864 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004865 if (ecc->total > mtd->oobsize) {
4866 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
4867 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004868 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004869 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004870
Boris Brezillon846031d2016-02-03 20:11:00 +01004871 /*
4872 * The number of bytes available for a client to place data into
4873 * the out of band area.
4874 */
4875 ret = mtd_ooblayout_count_freebytes(mtd);
4876 if (ret < 0)
4877 ret = 0;
4878
4879 mtd->oobavail = ret;
4880
4881 /* ECC sanity check: warn if it's too weak */
4882 if (!nand_ecc_strength_good(mtd))
4883 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4884 mtd->name);
4885
Brian Norris8b6e50c2011-05-25 14:59:01 -07004886 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004887 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004888 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004889 case 2:
4890 mtd->subpage_sft = 1;
4891 break;
4892 case 4:
4893 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004894 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004895 mtd->subpage_sft = 2;
4896 break;
4897 }
4898 }
4899 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4900
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004901 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004902 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004905 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004907 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304908 switch (ecc->mode) {
4909 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304910 if (chip->page_shift > 9)
4911 chip->options |= NAND_SUBPAGE_READ;
4912 break;
4913
4914 default:
4915 break;
4916 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004917
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004919 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004920 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4921 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004922 mtd->_erase = nand_erase;
4923 mtd->_point = NULL;
4924 mtd->_unpoint = NULL;
4925 mtd->_read = nand_read;
4926 mtd->_write = nand_write;
4927 mtd->_panic_write = panic_nand_write;
4928 mtd->_read_oob = nand_read_oob;
4929 mtd->_write_oob = nand_write_oob;
4930 mtd->_sync = nand_sync;
4931 mtd->_lock = NULL;
4932 mtd->_unlock = NULL;
4933 mtd->_suspend = nand_suspend;
4934 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004935 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004936 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004937 mtd->_block_isbad = nand_block_isbad;
4938 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004939 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004940 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004942 /*
4943 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4944 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4945 * properly set.
4946 */
4947 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004948 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Boris Brezillonf84674b2017-06-02 12:18:24 +02004950 /* Initialize the ->data_interface field. */
4951 ret = nand_init_data_interface(chip);
4952 if (ret)
4953 goto err_nand_manuf_cleanup;
4954
4955 /* Enter fastest possible mode on all dies. */
4956 for (i = 0; i < chip->numchips; i++) {
4957 chip->select_chip(mtd, i);
4958 ret = nand_setup_data_interface(chip, i);
4959 chip->select_chip(mtd, -1);
4960
4961 if (ret)
4962 goto err_nand_data_iface_cleanup;
4963 }
4964
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004965 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004966 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
4969 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07004970 ret = chip->scan_bbt(mtd);
4971 if (ret)
Boris Brezillonf84674b2017-06-02 12:18:24 +02004972 goto err_nand_data_iface_cleanup;
4973
Brian Norris44d41822017-05-01 17:04:50 -07004974 return 0;
4975
Boris Brezillonf84674b2017-06-02 12:18:24 +02004976err_nand_data_iface_cleanup:
4977 nand_release_data_interface(chip);
4978
4979err_nand_manuf_cleanup:
4980 nand_manufacturer_cleanup(chip);
4981
4982err_free_nbuf:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004983 if (nbuf) {
4984 kfree(nbuf->databuf);
4985 kfree(nbuf->ecccode);
4986 kfree(nbuf->ecccalc);
4987 kfree(nbuf);
4988 }
Brian Norris78771042017-05-01 17:04:53 -07004989
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004990 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004992EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Brian Norris8b6e50c2011-05-25 14:59:01 -07004994/*
4995 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004996 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004997 * to call us from in-kernel code if the core NAND support is modular.
4998 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004999#ifdef MODULE
5000#define caller_is_module() (1)
5001#else
5002#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06005003 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01005004#endif
5005
5006/**
5007 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005008 * @mtd: MTD device structure
5009 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01005010 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005011 * This fills out all the uninitialized function pointers with the defaults.
5012 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005013 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005014 */
5015int nand_scan(struct mtd_info *mtd, int maxchips)
5016{
5017 int ret;
5018
David Woodhouse5e81e882010-02-26 18:32:56 +00005019 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01005020 if (!ret)
5021 ret = nand_scan_tail(mtd);
5022 return ret;
5023}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005024EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01005025
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005027 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5028 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005029 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005030void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005032 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005033 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005034 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5035
Boris Brezillond8e725d2016-09-15 10:32:50 +02005036 nand_release_data_interface(chip);
5037
Jesper Juhlfa671642005-11-07 01:01:27 -08005038 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005039 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005040 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
5041 kfree(chip->buffers->databuf);
5042 kfree(chip->buffers->ecccode);
5043 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005044 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005045 }
Brian Norris58373ff2010-07-15 12:15:44 -07005046
5047 /* Free bad block descriptor memory */
5048 if (chip->badblock_pattern && chip->badblock_pattern->options
5049 & NAND_BBT_DYNAMICSTRUCT)
5050 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005051
5052 /* Free manufacturer priv data. */
5053 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005055EXPORT_SYMBOL_GPL(nand_cleanup);
5056
5057/**
5058 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5059 * held by the NAND device
5060 * @mtd: MTD device structure
5061 */
5062void nand_release(struct mtd_info *mtd)
5063{
5064 mtd_device_unregister(mtd);
5065 nand_cleanup(mtd_to_nand(mtd));
5066}
David Woodhousee0c7d762006-05-13 18:07:53 +01005067EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005068
David Woodhousee0c7d762006-05-13 18:07:53 +01005069MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005070MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5071MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005072MODULE_DESCRIPTION("Generic NAND flash driver code");