blob: eacc3f39cafd3f3e892d65721e36da5887650662 [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);
Miquel Raynaleb945552017-11-30 18:01:28 +01002992 int status;
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002995 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2996 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002997
Miquel Raynaleb945552017-11-30 18:01:28 +01002998 status = chip->waitfunc(mtd, chip);
2999 if (status < 0)
3000 return status;
3001
3002 return status & NAND_STATUS_FAIL ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
3005/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003007 * @mtd: MTD device structure
3008 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003010 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003012static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
David Woodhousee0c7d762006-05-13 18:07:53 +01003014 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003018 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003019 * @mtd: MTD device structure
3020 * @instr: erase instruction
3021 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003023 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003025int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3026 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
Adrian Hunter69423d92008-12-10 13:37:21 +00003028 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003029 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003030 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Brian Norris289c0522011-07-19 10:06:09 -07003032 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3033 __func__, (unsigned long long)instr->addr,
3034 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303036 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003040 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
3042 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003043 page = (int)(instr->addr >> chip->page_shift);
3044 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003047 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003050 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 /* Check, if it is write protected */
3053 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003054 pr_debug("%s: device is write protected!\n",
3055 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 instr->state = MTD_ERASE_FAILED;
3057 goto erase_exit;
3058 }
3059
3060 /* Loop through the pages */
3061 len = instr->len;
3062
3063 instr->state = MTD_ERASING;
3064
3065 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003066 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003067 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303068 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003069 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3070 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 instr->state = MTD_ERASE_FAILED;
3072 goto erase_exit;
3073 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003074
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003075 /*
3076 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003077 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003078 */
3079 if (page <= chip->pagebuf && chip->pagebuf <
3080 (page + pages_per_block))
3081 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Brian Norris49c50b92014-05-06 16:02:19 -07003083 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01003086 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07003087 pr_debug("%s: failed erase, page 0x%08x\n",
3088 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003090 instr->fail_addr =
3091 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 goto erase_exit;
3093 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003096 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 page += pages_per_block;
3098
3099 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003100 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003102 chip->select_chip(mtd, -1);
3103 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 }
3105 }
3106 instr->state = MTD_ERASE_DONE;
3107
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003108erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003113 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 nand_release_device(mtd);
3115
David Woodhouse49defc02007-10-06 15:01:59 -04003116 /* Do call back function */
3117 if (!ret)
3118 mtd_erase_callback(instr);
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 /* Return more or less happy */
3121 return ret;
3122}
3123
3124/**
3125 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003126 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003128 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003130static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Brian Norris289c0522011-07-19 10:06:09 -07003132 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003135 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003137 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003141 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003142 * @mtd: MTD device structure
3143 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003145static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303147 struct nand_chip *chip = mtd_to_nand(mtd);
3148 int chipnr = (int)(offs >> chip->chip_shift);
3149 int ret;
3150
3151 /* Select the NAND device */
3152 nand_get_device(mtd, FL_READING);
3153 chip->select_chip(mtd, chipnr);
3154
3155 ret = nand_block_checkbad(mtd, offs, 0);
3156
3157 chip->select_chip(mtd, -1);
3158 nand_release_device(mtd);
3159
3160 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
3163/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003164 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003165 * @mtd: MTD device structure
3166 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003168static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 int ret;
3171
Florian Fainellif8ac0412010-09-07 13:23:43 +02003172 ret = nand_block_isbad(mtd, ofs);
3173 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003174 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 if (ret > 0)
3176 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003177 return ret;
3178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Brian Norris5a0edb22013-07-30 17:52:58 -07003180 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
3183/**
Zach Brown56718422017-01-10 13:30:20 -06003184 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3185 * @mtd: MTD device structure
3186 * @ofs: offset relative to mtd start
3187 * @len: length of mtd
3188 */
3189static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3190{
3191 struct nand_chip *chip = mtd_to_nand(mtd);
3192 u32 part_start_block;
3193 u32 part_end_block;
3194 u32 part_start_die;
3195 u32 part_end_die;
3196
3197 /*
3198 * max_bb_per_die and blocks_per_die used to determine
3199 * the maximum bad block count.
3200 */
3201 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3202 return -ENOTSUPP;
3203
3204 /* Get the start and end of the partition in erase blocks. */
3205 part_start_block = mtd_div_by_eb(ofs, mtd);
3206 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3207
3208 /* Get the start and end LUNs of the partition. */
3209 part_start_die = part_start_block / chip->blocks_per_die;
3210 part_end_die = part_end_block / chip->blocks_per_die;
3211
3212 /*
3213 * Look up the bad blocks per unit and multiply by the number of units
3214 * that the partition spans.
3215 */
3216 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3217}
3218
3219/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003220 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3221 * @mtd: MTD device structure
3222 * @chip: nand chip info structure
3223 * @addr: feature address.
3224 * @subfeature_param: the subfeature parameters, a four bytes array.
3225 */
3226static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3227 int addr, uint8_t *subfeature_param)
3228{
3229 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003230 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003231
David Mosbergerd914c932013-05-29 15:30:13 +03003232 if (!chip->onfi_version ||
3233 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3234 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003235 return -EINVAL;
3236
3237 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003238 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3239 chip->write_byte(mtd, subfeature_param[i]);
3240
Huang Shijie7db03ec2012-09-13 14:57:52 +08003241 status = chip->waitfunc(mtd, chip);
3242 if (status & NAND_STATUS_FAIL)
3243 return -EIO;
3244 return 0;
3245}
3246
3247/**
3248 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3249 * @mtd: MTD device structure
3250 * @chip: nand chip info structure
3251 * @addr: feature address.
3252 * @subfeature_param: the subfeature parameters, a four bytes array.
3253 */
3254static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3255 int addr, uint8_t *subfeature_param)
3256{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003257 int i;
3258
David Mosbergerd914c932013-05-29 15:30:13 +03003259 if (!chip->onfi_version ||
3260 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3261 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003262 return -EINVAL;
3263
Huang Shijie7db03ec2012-09-13 14:57:52 +08003264 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003265 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3266 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003267 return 0;
3268}
3269
3270/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003271 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3272 * -ENOTSUPP
3273 * @mtd: MTD device structure
3274 * @chip: nand chip info structure
3275 * @addr: feature address.
3276 * @subfeature_param: the subfeature parameters, a four bytes array.
3277 *
3278 * Should be used by NAND controller drivers that do not support the SET/GET
3279 * FEATURES operations.
3280 */
3281int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3282 struct nand_chip *chip, int addr,
3283 u8 *subfeature_param)
3284{
3285 return -ENOTSUPP;
3286}
3287EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3288
3289/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003290 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003291 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003292 */
3293static int nand_suspend(struct mtd_info *mtd)
3294{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003295 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003296}
3297
3298/**
3299 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003300 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003301 */
3302static void nand_resume(struct mtd_info *mtd)
3303{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003304 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003305
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003306 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003307 nand_release_device(mtd);
3308 else
Brian Norrisd0370212011-07-19 10:06:08 -07003309 pr_err("%s called for a chip which is not in suspended state\n",
3310 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003311}
3312
Scott Branden72ea4032014-11-20 11:18:05 -08003313/**
3314 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3315 * prevent further operations
3316 * @mtd: MTD device structure
3317 */
3318static void nand_shutdown(struct mtd_info *mtd)
3319{
Brian Norris9ca641b2015-11-09 16:37:28 -08003320 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003321}
3322
Brian Norris8b6e50c2011-05-25 14:59:01 -07003323/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003324static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003325{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003326 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3327
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003329 if (!chip->chip_delay)
3330 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
3332 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003333 if (chip->cmdfunc == NULL)
3334 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003337 if (chip->waitfunc == NULL)
3338 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003340 if (!chip->select_chip)
3341 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003342
Huang Shijie4204ccc2013-08-16 10:10:07 +08003343 /* set for ONFI nand */
3344 if (!chip->onfi_set_features)
3345 chip->onfi_set_features = nand_onfi_set_features;
3346 if (!chip->onfi_get_features)
3347 chip->onfi_get_features = nand_onfi_get_features;
3348
Brian Norris68e80782013-07-18 01:17:02 -07003349 /* If called twice, pointers that depend on busw may need to be reset */
3350 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003351 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3352 if (!chip->read_word)
3353 chip->read_word = nand_read_word;
3354 if (!chip->block_bad)
3355 chip->block_bad = nand_block_bad;
3356 if (!chip->block_markbad)
3357 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003358 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003359 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003360 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3361 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003362 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003363 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003364 if (!chip->scan_bbt)
3365 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003366
3367 if (!chip->controller) {
3368 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003369 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003370 }
3371
Masahiro Yamada477544c2017-03-30 17:15:05 +09003372 if (!chip->buf_align)
3373 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003374}
3375
Brian Norris8b6e50c2011-05-25 14:59:01 -07003376/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003377static void sanitize_string(uint8_t *s, size_t len)
3378{
3379 ssize_t i;
3380
Brian Norris8b6e50c2011-05-25 14:59:01 -07003381 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003382 s[len - 1] = 0;
3383
Brian Norris8b6e50c2011-05-25 14:59:01 -07003384 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003385 for (i = 0; i < len - 1; i++) {
3386 if (s[i] < ' ' || s[i] > 127)
3387 s[i] = '?';
3388 }
3389
Brian Norris8b6e50c2011-05-25 14:59:01 -07003390 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003391 strim(s);
3392}
3393
3394static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3395{
3396 int i;
3397 while (len--) {
3398 crc ^= *p++ << 8;
3399 for (i = 0; i < 8; i++)
3400 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3401 }
3402
3403 return crc;
3404}
3405
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003406/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003407static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3408 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003409{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003410 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003411 struct onfi_ext_param_page *ep;
3412 struct onfi_ext_section *s;
3413 struct onfi_ext_ecc_info *ecc;
3414 uint8_t *cursor;
3415 int ret = -EINVAL;
3416 int len;
3417 int i;
3418
3419 len = le16_to_cpu(p->ext_param_page_length) * 16;
3420 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003421 if (!ep)
3422 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003423
3424 /* Send our own NAND_CMD_PARAM. */
3425 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3426
3427 /* Use the Change Read Column command to skip the ONFI param pages. */
3428 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3429 sizeof(*p) * p->num_of_param_pages , -1);
3430
3431 /* Read out the Extended Parameter Page. */
3432 chip->read_buf(mtd, (uint8_t *)ep, len);
3433 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3434 != le16_to_cpu(ep->crc))) {
3435 pr_debug("fail in the CRC.\n");
3436 goto ext_out;
3437 }
3438
3439 /*
3440 * Check the signature.
3441 * Do not strictly follow the ONFI spec, maybe changed in future.
3442 */
3443 if (strncmp(ep->sig, "EPPS", 4)) {
3444 pr_debug("The signature is invalid.\n");
3445 goto ext_out;
3446 }
3447
3448 /* find the ECC section. */
3449 cursor = (uint8_t *)(ep + 1);
3450 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3451 s = ep->sections + i;
3452 if (s->type == ONFI_SECTION_TYPE_2)
3453 break;
3454 cursor += s->length * 16;
3455 }
3456 if (i == ONFI_EXT_SECTION_MAX) {
3457 pr_debug("We can not find the ECC section.\n");
3458 goto ext_out;
3459 }
3460
3461 /* get the info we want. */
3462 ecc = (struct onfi_ext_ecc_info *)cursor;
3463
Brian Norris4ae7d222013-09-16 18:20:21 -07003464 if (!ecc->codeword_size) {
3465 pr_debug("Invalid codeword size\n");
3466 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003467 }
3468
Brian Norris4ae7d222013-09-16 18:20:21 -07003469 chip->ecc_strength_ds = ecc->ecc_bits;
3470 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003471 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003472
3473ext_out:
3474 kfree(ep);
3475 return ret;
3476}
3477
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003478/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003479 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003480 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003481static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003482{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003483 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003484 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003485 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003486 int val;
3487
Brian Norris7854d3f2011-06-23 14:12:08 -07003488 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003489 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3490 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3491 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3492 return 0;
3493
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003494 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3495 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003496 for (j = 0; j < sizeof(*p); j++)
3497 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003498 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3499 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003500 break;
3501 }
3502 }
3503
Brian Norrisc7f23a72013-08-13 10:51:55 -07003504 if (i == 3) {
3505 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003506 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003507 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003508
Brian Norris8b6e50c2011-05-25 14:59:01 -07003509 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003510 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003511 if (val & (1 << 5))
3512 chip->onfi_version = 23;
3513 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003514 chip->onfi_version = 22;
3515 else if (val & (1 << 3))
3516 chip->onfi_version = 21;
3517 else if (val & (1 << 2))
3518 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003519 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003520 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003521
3522 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003523 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003524 return 0;
3525 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003526
3527 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3528 sanitize_string(p->model, sizeof(p->model));
3529 if (!mtd->name)
3530 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003531
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003532 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003533
3534 /*
3535 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3536 * (don't ask me who thought of this...). MTD assumes that these
3537 * dimensions will be power-of-2, so just truncate the remaining area.
3538 */
3539 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3540 mtd->erasesize *= mtd->writesize;
3541
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003542 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003543
3544 /* See erasesize comment */
3545 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003546 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003547 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003548
Zach Brown34da5f52017-01-10 13:30:21 -06003549 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3550 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3551
Huang Shijiee2985fc2013-05-17 11:17:30 +08003552 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003553 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003554
Huang Shijie10c86ba2013-05-17 11:17:26 +08003555 if (p->ecc_bits != 0xff) {
3556 chip->ecc_strength_ds = p->ecc_bits;
3557 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003558 } else if (chip->onfi_version >= 21 &&
3559 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3560
3561 /*
3562 * The nand_flash_detect_ext_param_page() uses the
3563 * Change Read Column command which maybe not supported
3564 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3565 * now. We do not replace user supplied command function.
3566 */
3567 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3568 chip->cmdfunc = nand_command_lp;
3569
3570 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003571 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003572 pr_warn("Failed to detect ONFI extended param page\n");
3573 } else {
3574 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003575 }
3576
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003577 return 1;
3578}
3579
3580/*
Huang Shijie91361812014-02-21 13:39:40 +08003581 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3582 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003583static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003584{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003585 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003586 struct nand_jedec_params *p = &chip->jedec_params;
3587 struct jedec_ecc_info *ecc;
3588 int val;
3589 int i, j;
3590
3591 /* Try JEDEC for unknown chip or LP */
3592 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3593 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3594 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3595 chip->read_byte(mtd) != 'C')
3596 return 0;
3597
3598 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3599 for (i = 0; i < 3; i++) {
3600 for (j = 0; j < sizeof(*p); j++)
3601 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3602
3603 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3604 le16_to_cpu(p->crc))
3605 break;
3606 }
3607
3608 if (i == 3) {
3609 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3610 return 0;
3611 }
3612
3613 /* Check version */
3614 val = le16_to_cpu(p->revision);
3615 if (val & (1 << 2))
3616 chip->jedec_version = 10;
3617 else if (val & (1 << 1))
3618 chip->jedec_version = 1; /* vendor specific version */
3619
3620 if (!chip->jedec_version) {
3621 pr_info("unsupported JEDEC version: %d\n", val);
3622 return 0;
3623 }
3624
3625 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3626 sanitize_string(p->model, sizeof(p->model));
3627 if (!mtd->name)
3628 mtd->name = p->model;
3629
3630 mtd->writesize = le32_to_cpu(p->byte_per_page);
3631
3632 /* Please reference to the comment for nand_flash_detect_onfi. */
3633 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3634 mtd->erasesize *= mtd->writesize;
3635
3636 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3637
3638 /* Please reference to the comment for nand_flash_detect_onfi. */
3639 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3640 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3641 chip->bits_per_cell = p->bits_per_cell;
3642
3643 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003644 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003645
3646 /* ECC info */
3647 ecc = &p->ecc_info[0];
3648
3649 if (ecc->codeword_size >= 9) {
3650 chip->ecc_strength_ds = ecc->ecc_bits;
3651 chip->ecc_step_ds = 1 << ecc->codeword_size;
3652 } else {
3653 pr_warn("Invalid codeword size\n");
3654 }
3655
3656 return 1;
3657}
3658
3659/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003660 * nand_id_has_period - Check if an ID string has a given wraparound period
3661 * @id_data: the ID string
3662 * @arrlen: the length of the @id_data array
3663 * @period: the period of repitition
3664 *
3665 * Check if an ID string is repeated within a given sequence of bytes at
3666 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003667 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003668 * if the repetition has a period of @period; otherwise, returns zero.
3669 */
3670static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3671{
3672 int i, j;
3673 for (i = 0; i < period; i++)
3674 for (j = i + period; j < arrlen; j += period)
3675 if (id_data[i] != id_data[j])
3676 return 0;
3677 return 1;
3678}
3679
3680/*
3681 * nand_id_len - Get the length of an ID string returned by CMD_READID
3682 * @id_data: the ID string
3683 * @arrlen: the length of the @id_data array
3684
3685 * Returns the length of the ID string, according to known wraparound/trailing
3686 * zero patterns. If no pattern exists, returns the length of the array.
3687 */
3688static int nand_id_len(u8 *id_data, int arrlen)
3689{
3690 int last_nonzero, period;
3691
3692 /* Find last non-zero byte */
3693 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3694 if (id_data[last_nonzero])
3695 break;
3696
3697 /* All zeros */
3698 if (last_nonzero < 0)
3699 return 0;
3700
3701 /* Calculate wraparound period */
3702 for (period = 1; period < arrlen; period++)
3703 if (nand_id_has_period(id_data, arrlen, period))
3704 break;
3705
3706 /* There's a repeated pattern */
3707 if (period < arrlen)
3708 return period;
3709
3710 /* There are trailing zeros */
3711 if (last_nonzero < arrlen - 1)
3712 return last_nonzero + 1;
3713
3714 /* No pattern detected */
3715 return arrlen;
3716}
3717
Huang Shijie7db906b2013-09-25 14:58:11 +08003718/* Extract the bits of per cell from the 3rd byte of the extended ID */
3719static int nand_get_bits_per_cell(u8 cellinfo)
3720{
3721 int bits;
3722
3723 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3724 bits >>= NAND_CI_CELLTYPE_SHIFT;
3725 return bits + 1;
3726}
3727
Brian Norrise3b88bd2012-09-24 20:40:52 -07003728/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003729 * Many new NAND share similar device ID codes, which represent the size of the
3730 * chip. The rest of the parameters must be decoded according to generic or
3731 * manufacturer-specific "extended ID" decoding patterns.
3732 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003733void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003734{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003735 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003736 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003737 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003738 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003739 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003740 /* The 4th id byte is the important one */
3741 extid = id_data[3];
3742
Boris Brezillon01389b62016-06-08 10:30:18 +02003743 /* Calc pagesize */
3744 mtd->writesize = 1024 << (extid & 0x03);
3745 extid >>= 2;
3746 /* Calc oobsize */
3747 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3748 extid >>= 2;
3749 /* Calc blocksize. Blocksize is multiples of 64KiB */
3750 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3751 extid >>= 2;
3752 /* Get buswidth information */
3753 if (extid & 0x1)
3754 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003755}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003756EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003757
3758/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003759 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3760 * decodes a matching ID table entry and assigns the MTD size parameters for
3761 * the chip.
3762 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003763static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003764{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003765 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003766
3767 mtd->erasesize = type->erasesize;
3768 mtd->writesize = type->pagesize;
3769 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003770
Huang Shijie1c195e92013-09-25 14:58:12 +08003771 /* All legacy ID NAND are small-page, SLC */
3772 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003773}
3774
3775/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003776 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3777 * heuristic patterns using various detected parameters (e.g., manufacturer,
3778 * page size, cell-type information).
3779 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003780static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003781{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003782 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003783
3784 /* Set the bad block position */
3785 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3786 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3787 else
3788 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003789}
3790
Huang Shijieec6e87e2013-03-15 11:01:00 +08003791static inline bool is_full_id_nand(struct nand_flash_dev *type)
3792{
3793 return type->id_len;
3794}
3795
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003796static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003797 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003798{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003799 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003800 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003801
Huang Shijieec6e87e2013-03-15 11:01:00 +08003802 if (!strncmp(type->id, id_data, type->id_len)) {
3803 mtd->writesize = type->pagesize;
3804 mtd->erasesize = type->erasesize;
3805 mtd->oobsize = type->oobsize;
3806
Huang Shijie7db906b2013-09-25 14:58:11 +08003807 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003808 chip->chipsize = (uint64_t)type->chipsize << 20;
3809 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003810 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3811 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003812 chip->onfi_timing_mode_default =
3813 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003814
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003815 if (!mtd->name)
3816 mtd->name = type->name;
3817
Huang Shijieec6e87e2013-03-15 11:01:00 +08003818 return true;
3819 }
3820 return false;
3821}
3822
Brian Norris7e74c2d2012-09-24 20:40:49 -07003823/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003824 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3825 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3826 * table.
3827 */
3828static void nand_manufacturer_detect(struct nand_chip *chip)
3829{
3830 /*
3831 * Try manufacturer detection if available and use
3832 * nand_decode_ext_id() otherwise.
3833 */
3834 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003835 chip->manufacturer.desc->ops->detect) {
3836 /* The 3rd id byte holds MLC / multichip data */
3837 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003838 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003839 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003840 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003841 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003842}
3843
3844/*
3845 * Manufacturer initialization. This function is called for all NANDs including
3846 * ONFI and JEDEC compliant ones.
3847 * Manufacturer drivers should put all their specific initialization code in
3848 * their ->init() hook.
3849 */
3850static int nand_manufacturer_init(struct nand_chip *chip)
3851{
3852 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
3853 !chip->manufacturer.desc->ops->init)
3854 return 0;
3855
3856 return chip->manufacturer.desc->ops->init(chip);
3857}
3858
3859/*
3860 * Manufacturer cleanup. This function is called for all NANDs including
3861 * ONFI and JEDEC compliant ones.
3862 * Manufacturer drivers should put all their specific cleanup code in their
3863 * ->cleanup() hook.
3864 */
3865static void nand_manufacturer_cleanup(struct nand_chip *chip)
3866{
3867 /* Release manufacturer private data */
3868 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3869 chip->manufacturer.desc->ops->cleanup)
3870 chip->manufacturer.desc->ops->cleanup(chip);
3871}
3872
3873/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003874 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003875 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02003876static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003877{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003878 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003879 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08003880 int busw;
Boris Brezillonf84674b2017-06-02 12:18:24 +02003881 int i;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003882 u8 *id_data = chip->id.data;
3883 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Karl Beldanef89a882008-09-15 14:37:29 +02003885 /*
3886 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003887 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003888 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003889 nand_reset(chip, 0);
3890
3891 /* Select the device */
3892 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02003893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003895 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
3897 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003898 maf_id = chip->read_byte(mtd);
3899 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Brian Norris8b6e50c2011-05-25 14:59:01 -07003901 /*
3902 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003903 * interface concerns can cause random data which looks like a
3904 * possibly credible NAND flash to appear. If the two results do
3905 * not match, ignore the device completely.
3906 */
3907
3908 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3909
Brian Norris4aef9b72012-09-24 20:40:48 -07003910 /* Read entire ID string */
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003911 for (i = 0; i < ARRAY_SIZE(chip->id.data); i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003912 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003913
Boris Brezillon7f501f02016-05-24 19:20:05 +02003914 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003915 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003916 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003917 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01003918 }
3919
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003920 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02003921
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003922 /* Try to identify manufacturer */
3923 manufacturer = nand_get_manufacturer(maf_id);
3924 chip->manufacturer.desc = manufacturer;
3925
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003926 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003927 type = nand_flash_ids;
3928
Boris Brezillon29a198a2016-05-24 20:17:48 +02003929 /*
3930 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
3931 * override it.
3932 * This is required to make sure initial NAND bus width set by the
3933 * NAND controller driver is coherent with the real NAND bus width
3934 * (extracted by auto-detection code).
3935 */
3936 busw = chip->options & NAND_BUSWIDTH_16;
3937
3938 /*
3939 * The flag is only set (never cleared), reset it to its default value
3940 * before starting auto-detection.
3941 */
3942 chip->options &= ~NAND_BUSWIDTH_16;
3943
Huang Shijieec6e87e2013-03-15 11:01:00 +08003944 for (; type->name != NULL; type++) {
3945 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003946 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08003947 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003948 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003949 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003950 }
3951 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003952
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003953 chip->onfi_version = 0;
3954 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003955 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003956 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003957 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003958
3959 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003960 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08003961 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003962 }
3963
David Woodhouse5e81e882010-02-26 18:32:56 +00003964 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003965 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003966
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003967 if (!mtd->name)
3968 mtd->name = type->name;
3969
Adrian Hunter69423d92008-12-10 13:37:21 +00003970 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003971
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003972 if (!type->pagesize)
3973 nand_manufacturer_detect(chip);
3974 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02003975 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003976
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003977 /* Get chip options */
3978 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003979
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003980ident_done:
3981
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003982 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003983 WARN_ON(busw & NAND_BUSWIDTH_16);
3984 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003985 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3986 /*
3987 * Check, if buswidth is correct. Hardware drivers should set
3988 * chip correct!
3989 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003990 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003991 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003992 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
3993 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02003994 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
3995 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003996 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003997 }
3998
Boris Brezillon7f501f02016-05-24 19:20:05 +02003999 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004000
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004001 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004002 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004003 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004004 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004005
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004006 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004007 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004008 if (chip->chipsize & 0xffffffff)
4009 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004010 else {
4011 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4012 chip->chip_shift += 32 - 1;
4013 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004014
Masahiro Yamada14157f82017-09-13 11:05:50 +09004015 if (chip->chip_shift - chip->page_shift > 16)
4016 chip->options |= NAND_ROW_ADDR_3;
4017
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004018 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004019 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004020
Brian Norris8b6e50c2011-05-25 14:59:01 -07004021 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004022 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4023 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004024
Ezequiel Garcia20171642013-11-25 08:30:31 -03004025 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004026 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004027
4028 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004029 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4030 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004031 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004032 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4033 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004034 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004035 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4036 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004037
Rafał Miłecki3755a992014-10-21 00:01:04 +02004038 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004039 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004040 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004041 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004042}
4043
Boris Brezillond48f62b2016-04-01 14:54:32 +02004044static const char * const nand_ecc_modes[] = {
4045 [NAND_ECC_NONE] = "none",
4046 [NAND_ECC_SOFT] = "soft",
4047 [NAND_ECC_HW] = "hw",
4048 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4049 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004050 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004051};
4052
4053static int of_get_nand_ecc_mode(struct device_node *np)
4054{
4055 const char *pm;
4056 int err, i;
4057
4058 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4059 if (err < 0)
4060 return err;
4061
4062 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4063 if (!strcasecmp(pm, nand_ecc_modes[i]))
4064 return i;
4065
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004066 /*
4067 * For backward compatibility we support few obsoleted values that don't
4068 * have their mappings into nand_ecc_modes_t anymore (they were merged
4069 * with other enums).
4070 */
4071 if (!strcasecmp(pm, "soft_bch"))
4072 return NAND_ECC_SOFT;
4073
Boris Brezillond48f62b2016-04-01 14:54:32 +02004074 return -ENODEV;
4075}
4076
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004077static const char * const nand_ecc_algos[] = {
4078 [NAND_ECC_HAMMING] = "hamming",
4079 [NAND_ECC_BCH] = "bch",
4080};
4081
Boris Brezillond48f62b2016-04-01 14:54:32 +02004082static int of_get_nand_ecc_algo(struct device_node *np)
4083{
4084 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004085 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004086
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004087 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4088 if (!err) {
4089 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4090 if (!strcasecmp(pm, nand_ecc_algos[i]))
4091 return i;
4092 return -ENODEV;
4093 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004094
4095 /*
4096 * For backward compatibility we also read "nand-ecc-mode" checking
4097 * for some obsoleted values that were specifying ECC algorithm.
4098 */
4099 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4100 if (err < 0)
4101 return err;
4102
4103 if (!strcasecmp(pm, "soft"))
4104 return NAND_ECC_HAMMING;
4105 else if (!strcasecmp(pm, "soft_bch"))
4106 return NAND_ECC_BCH;
4107
4108 return -ENODEV;
4109}
4110
4111static int of_get_nand_ecc_step_size(struct device_node *np)
4112{
4113 int ret;
4114 u32 val;
4115
4116 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4117 return ret ? ret : val;
4118}
4119
4120static int of_get_nand_ecc_strength(struct device_node *np)
4121{
4122 int ret;
4123 u32 val;
4124
4125 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4126 return ret ? ret : val;
4127}
4128
4129static int of_get_nand_bus_width(struct device_node *np)
4130{
4131 u32 val;
4132
4133 if (of_property_read_u32(np, "nand-bus-width", &val))
4134 return 8;
4135
4136 switch (val) {
4137 case 8:
4138 case 16:
4139 return val;
4140 default:
4141 return -EIO;
4142 }
4143}
4144
4145static bool of_get_nand_on_flash_bbt(struct device_node *np)
4146{
4147 return of_property_read_bool(np, "nand-on-flash-bbt");
4148}
4149
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004150static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004151{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004152 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004153 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004154
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004155 if (!dn)
4156 return 0;
4157
Brian Norris5844fee2015-01-23 00:22:27 -08004158 if (of_get_nand_bus_width(dn) == 16)
4159 chip->options |= NAND_BUSWIDTH_16;
4160
4161 if (of_get_nand_on_flash_bbt(dn))
4162 chip->bbt_options |= NAND_BBT_USE_FLASH;
4163
4164 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004165 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004166 ecc_strength = of_get_nand_ecc_strength(dn);
4167 ecc_step = of_get_nand_ecc_step_size(dn);
4168
Brian Norris5844fee2015-01-23 00:22:27 -08004169 if (ecc_mode >= 0)
4170 chip->ecc.mode = ecc_mode;
4171
Rafał Miłecki79082452016-03-23 11:19:02 +01004172 if (ecc_algo >= 0)
4173 chip->ecc.algo = ecc_algo;
4174
Brian Norris5844fee2015-01-23 00:22:27 -08004175 if (ecc_strength >= 0)
4176 chip->ecc.strength = ecc_strength;
4177
4178 if (ecc_step > 0)
4179 chip->ecc.size = ecc_step;
4180
Boris Brezillonba78ee02016-06-08 17:04:22 +02004181 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4182 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4183
Brian Norris5844fee2015-01-23 00:22:27 -08004184 return 0;
4185}
4186
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004187/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004188 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004189 * @mtd: MTD device structure
4190 * @maxchips: number of chips to scan for
4191 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004192 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004193 * This is the first phase of the normal nand_scan() function. It reads the
4194 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004195 *
4196 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004197int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4198 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004199{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004200 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004201 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004202 int ret;
4203
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004204 ret = nand_dt_init(chip);
4205 if (ret)
4206 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004207
Brian Norrisf7a8e382016-01-05 10:39:45 -08004208 if (!mtd->name && mtd->dev.parent)
4209 mtd->name = dev_name(mtd->dev.parent);
4210
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004211 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4212 /*
4213 * Default functions assigned for chip_select() and
4214 * cmdfunc() both expect cmd_ctrl() to be populated,
4215 * so we need to check that that's the case
4216 */
4217 pr_err("chip.cmd_ctrl() callback is not provided");
4218 return -EINVAL;
4219 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004220 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004221 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004222
4223 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004224 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004225 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004226 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004227 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004228 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 }
4231
Boris Brezillon7f501f02016-05-24 19:20:05 +02004232 nand_maf_id = chip->id.data[0];
4233 nand_dev_id = chip->id.data[1];
4234
Huang Shijie07300162012-11-09 16:23:45 +08004235 chip->select_chip(mtd, -1);
4236
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004237 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004238 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004239 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004240 nand_reset(chip, i);
4241
4242 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004244 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004246 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004247 nand_dev_id != chip->read_byte(mtd)) {
4248 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 break;
Huang Shijie07300162012-11-09 16:23:45 +08004250 }
4251 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 }
4253 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004254 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004255
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004257 chip->numchips = i;
4258 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
David Woodhouse3b85c322006-09-25 17:06:53 +01004260 return 0;
4261}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004262EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004263
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004264static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4265{
4266 struct nand_chip *chip = mtd_to_nand(mtd);
4267 struct nand_ecc_ctrl *ecc = &chip->ecc;
4268
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004269 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004270 return -EINVAL;
4271
4272 switch (ecc->algo) {
4273 case NAND_ECC_HAMMING:
4274 ecc->calculate = nand_calculate_ecc;
4275 ecc->correct = nand_correct_data;
4276 ecc->read_page = nand_read_page_swecc;
4277 ecc->read_subpage = nand_read_subpage;
4278 ecc->write_page = nand_write_page_swecc;
4279 ecc->read_page_raw = nand_read_page_raw;
4280 ecc->write_page_raw = nand_write_page_raw;
4281 ecc->read_oob = nand_read_oob_std;
4282 ecc->write_oob = nand_write_oob_std;
4283 if (!ecc->size)
4284 ecc->size = 256;
4285 ecc->bytes = 3;
4286 ecc->strength = 1;
4287 return 0;
4288 case NAND_ECC_BCH:
4289 if (!mtd_nand_has_bch()) {
4290 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4291 return -EINVAL;
4292 }
4293 ecc->calculate = nand_bch_calculate_ecc;
4294 ecc->correct = nand_bch_correct_data;
4295 ecc->read_page = nand_read_page_swecc;
4296 ecc->read_subpage = nand_read_subpage;
4297 ecc->write_page = nand_write_page_swecc;
4298 ecc->read_page_raw = nand_read_page_raw;
4299 ecc->write_page_raw = nand_write_page_raw;
4300 ecc->read_oob = nand_read_oob_std;
4301 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004302
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004303 /*
4304 * Board driver should supply ecc.size and ecc.strength
4305 * values to select how many bits are correctable.
4306 * Otherwise, default to 4 bits for large page devices.
4307 */
4308 if (!ecc->size && (mtd->oobsize >= 64)) {
4309 ecc->size = 512;
4310 ecc->strength = 4;
4311 }
4312
4313 /*
4314 * if no ecc placement scheme was provided pickup the default
4315 * large page one.
4316 */
4317 if (!mtd->ooblayout) {
4318 /* handle large page devices only */
4319 if (mtd->oobsize < 64) {
4320 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4321 return -EINVAL;
4322 }
4323
4324 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004325
4326 }
4327
4328 /*
4329 * We can only maximize ECC config when the default layout is
4330 * used, otherwise we don't know how many bytes can really be
4331 * used.
4332 */
4333 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4334 ecc->options & NAND_ECC_MAXIMIZE) {
4335 int steps, bytes;
4336
4337 /* Always prefer 1k blocks over 512bytes ones */
4338 ecc->size = 1024;
4339 steps = mtd->writesize / ecc->size;
4340
4341 /* Reserve 2 bytes for the BBM */
4342 bytes = (mtd->oobsize - 2) / steps;
4343 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004344 }
4345
4346 /* See nand_bch_init() for details. */
4347 ecc->bytes = 0;
4348 ecc->priv = nand_bch_init(mtd);
4349 if (!ecc->priv) {
4350 WARN(1, "BCH ECC initialization failed!\n");
4351 return -EINVAL;
4352 }
4353 return 0;
4354 default:
4355 WARN(1, "Unsupported ECC algorithm!\n");
4356 return -EINVAL;
4357 }
4358}
4359
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09004360/**
4361 * nand_check_ecc_caps - check the sanity of preset ECC settings
4362 * @chip: nand chip info structure
4363 * @caps: ECC caps info structure
4364 * @oobavail: OOB size that the ECC engine can use
4365 *
4366 * When ECC step size and strength are already set, check if they are supported
4367 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4368 * On success, the calculated ECC bytes is set.
4369 */
4370int nand_check_ecc_caps(struct nand_chip *chip,
4371 const struct nand_ecc_caps *caps, int oobavail)
4372{
4373 struct mtd_info *mtd = nand_to_mtd(chip);
4374 const struct nand_ecc_step_info *stepinfo;
4375 int preset_step = chip->ecc.size;
4376 int preset_strength = chip->ecc.strength;
4377 int nsteps, ecc_bytes;
4378 int i, j;
4379
4380 if (WARN_ON(oobavail < 0))
4381 return -EINVAL;
4382
4383 if (!preset_step || !preset_strength)
4384 return -ENODATA;
4385
4386 nsteps = mtd->writesize / preset_step;
4387
4388 for (i = 0; i < caps->nstepinfos; i++) {
4389 stepinfo = &caps->stepinfos[i];
4390
4391 if (stepinfo->stepsize != preset_step)
4392 continue;
4393
4394 for (j = 0; j < stepinfo->nstrengths; j++) {
4395 if (stepinfo->strengths[j] != preset_strength)
4396 continue;
4397
4398 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4399 preset_strength);
4400 if (WARN_ON_ONCE(ecc_bytes < 0))
4401 return ecc_bytes;
4402
4403 if (ecc_bytes * nsteps > oobavail) {
4404 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4405 preset_step, preset_strength);
4406 return -ENOSPC;
4407 }
4408
4409 chip->ecc.bytes = ecc_bytes;
4410
4411 return 0;
4412 }
4413 }
4414
4415 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4416 preset_step, preset_strength);
4417
4418 return -ENOTSUPP;
4419}
4420EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4421
4422/**
4423 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4424 * @chip: nand chip info structure
4425 * @caps: ECC engine caps info structure
4426 * @oobavail: OOB size that the ECC engine can use
4427 *
4428 * If a chip's ECC requirement is provided, try to meet it with the least
4429 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4430 * On success, the chosen ECC settings are set.
4431 */
4432int nand_match_ecc_req(struct nand_chip *chip,
4433 const struct nand_ecc_caps *caps, int oobavail)
4434{
4435 struct mtd_info *mtd = nand_to_mtd(chip);
4436 const struct nand_ecc_step_info *stepinfo;
4437 int req_step = chip->ecc_step_ds;
4438 int req_strength = chip->ecc_strength_ds;
4439 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4440 int best_step, best_strength, best_ecc_bytes;
4441 int best_ecc_bytes_total = INT_MAX;
4442 int i, j;
4443
4444 if (WARN_ON(oobavail < 0))
4445 return -EINVAL;
4446
4447 /* No information provided by the NAND chip */
4448 if (!req_step || !req_strength)
4449 return -ENOTSUPP;
4450
4451 /* number of correctable bits the chip requires in a page */
4452 req_corr = mtd->writesize / req_step * req_strength;
4453
4454 for (i = 0; i < caps->nstepinfos; i++) {
4455 stepinfo = &caps->stepinfos[i];
4456 step_size = stepinfo->stepsize;
4457
4458 for (j = 0; j < stepinfo->nstrengths; j++) {
4459 strength = stepinfo->strengths[j];
4460
4461 /*
4462 * If both step size and strength are smaller than the
4463 * chip's requirement, it is not easy to compare the
4464 * resulted reliability.
4465 */
4466 if (step_size < req_step && strength < req_strength)
4467 continue;
4468
4469 if (mtd->writesize % step_size)
4470 continue;
4471
4472 nsteps = mtd->writesize / step_size;
4473
4474 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4475 if (WARN_ON_ONCE(ecc_bytes < 0))
4476 continue;
4477 ecc_bytes_total = ecc_bytes * nsteps;
4478
4479 if (ecc_bytes_total > oobavail ||
4480 strength * nsteps < req_corr)
4481 continue;
4482
4483 /*
4484 * We assume the best is to meet the chip's requrement
4485 * with the least number of ECC bytes.
4486 */
4487 if (ecc_bytes_total < best_ecc_bytes_total) {
4488 best_ecc_bytes_total = ecc_bytes_total;
4489 best_step = step_size;
4490 best_strength = strength;
4491 best_ecc_bytes = ecc_bytes;
4492 }
4493 }
4494 }
4495
4496 if (best_ecc_bytes_total == INT_MAX)
4497 return -ENOTSUPP;
4498
4499 chip->ecc.size = best_step;
4500 chip->ecc.strength = best_strength;
4501 chip->ecc.bytes = best_ecc_bytes;
4502
4503 return 0;
4504}
4505EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4506
4507/**
4508 * nand_maximize_ecc - choose the max ECC strength available
4509 * @chip: nand chip info structure
4510 * @caps: ECC engine caps info structure
4511 * @oobavail: OOB size that the ECC engine can use
4512 *
4513 * Choose the max ECC strength that is supported on the controller, and can fit
4514 * within the chip's OOB. On success, the chosen ECC settings are set.
4515 */
4516int nand_maximize_ecc(struct nand_chip *chip,
4517 const struct nand_ecc_caps *caps, int oobavail)
4518{
4519 struct mtd_info *mtd = nand_to_mtd(chip);
4520 const struct nand_ecc_step_info *stepinfo;
4521 int step_size, strength, nsteps, ecc_bytes, corr;
4522 int best_corr = 0;
4523 int best_step = 0;
4524 int best_strength, best_ecc_bytes;
4525 int i, j;
4526
4527 if (WARN_ON(oobavail < 0))
4528 return -EINVAL;
4529
4530 for (i = 0; i < caps->nstepinfos; i++) {
4531 stepinfo = &caps->stepinfos[i];
4532 step_size = stepinfo->stepsize;
4533
4534 /* If chip->ecc.size is already set, respect it */
4535 if (chip->ecc.size && step_size != chip->ecc.size)
4536 continue;
4537
4538 for (j = 0; j < stepinfo->nstrengths; j++) {
4539 strength = stepinfo->strengths[j];
4540
4541 if (mtd->writesize % step_size)
4542 continue;
4543
4544 nsteps = mtd->writesize / step_size;
4545
4546 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4547 if (WARN_ON_ONCE(ecc_bytes < 0))
4548 continue;
4549
4550 if (ecc_bytes * nsteps > oobavail)
4551 continue;
4552
4553 corr = strength * nsteps;
4554
4555 /*
4556 * If the number of correctable bits is the same,
4557 * bigger step_size has more reliability.
4558 */
4559 if (corr > best_corr ||
4560 (corr == best_corr && step_size > best_step)) {
4561 best_corr = corr;
4562 best_step = step_size;
4563 best_strength = strength;
4564 best_ecc_bytes = ecc_bytes;
4565 }
4566 }
4567 }
4568
4569 if (!best_corr)
4570 return -ENOTSUPP;
4571
4572 chip->ecc.size = best_step;
4573 chip->ecc.strength = best_strength;
4574 chip->ecc.bytes = best_ecc_bytes;
4575
4576 return 0;
4577}
4578EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4579
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004580/*
4581 * Check if the chip configuration meet the datasheet requirements.
4582
4583 * If our configuration corrects A bits per B bytes and the minimum
4584 * required correction level is X bits per Y bytes, then we must ensure
4585 * both of the following are true:
4586 *
4587 * (1) A / B >= X / Y
4588 * (2) A >= X
4589 *
4590 * Requirement (1) ensures we can correct for the required bitflip density.
4591 * Requirement (2) ensures we can correct even when all bitflips are clumped
4592 * in the same sector.
4593 */
4594static bool nand_ecc_strength_good(struct mtd_info *mtd)
4595{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004596 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004597 struct nand_ecc_ctrl *ecc = &chip->ecc;
4598 int corr, ds_corr;
4599
4600 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4601 /* Not enough information */
4602 return true;
4603
4604 /*
4605 * We get the number of corrected bits per page to compare
4606 * the correction density.
4607 */
4608 corr = (mtd->writesize * ecc->strength) / ecc->size;
4609 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4610
4611 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4612}
David Woodhouse3b85c322006-09-25 17:06:53 +01004613
Marc Gonzalez3371d662016-11-15 10:56:20 +01004614static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4615{
4616 struct nand_ecc_ctrl *ecc = &chip->ecc;
4617
4618 if (nand_standard_page_accessors(ecc))
4619 return false;
4620
4621 /*
4622 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4623 * controller driver implements all the page accessors because
4624 * default helpers are not suitable when the core does not
4625 * send the READ0/PAGEPROG commands.
4626 */
4627 return (!ecc->read_page || !ecc->write_page ||
4628 !ecc->read_page_raw || !ecc->write_page_raw ||
4629 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4630 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4631 ecc->hwctl && ecc->calculate));
4632}
4633
David Woodhouse3b85c322006-09-25 17:06:53 +01004634/**
4635 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004636 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004637 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004638 * This is the second phase of the normal nand_scan() function. It fills out
4639 * all the uninitialized function pointers with the defaults and scans for a
4640 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004641 */
4642int nand_scan_tail(struct mtd_info *mtd)
4643{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004644 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004645 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004646 struct nand_buffers *nbuf = NULL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004647 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01004648
Brian Norrise2414f42012-02-06 13:44:00 -08004649 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004650 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07004651 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02004652 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07004653 }
Brian Norrise2414f42012-02-06 13:44:00 -08004654
Marc Gonzalez3371d662016-11-15 10:56:20 +01004655 if (invalid_ecc_page_accessors(chip)) {
4656 pr_err("Invalid ECC page accessors setup\n");
Boris Brezillonf84674b2017-06-02 12:18:24 +02004657 return -EINVAL;
Marc Gonzalez3371d662016-11-15 10:56:20 +01004658 }
4659
Huang Shijief02ea4e2014-01-13 14:27:12 +08004660 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004661 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Boris Brezillonf84674b2017-06-02 12:18:24 +02004662 if (!nbuf)
4663 return -ENOMEM;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004664
4665 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4666 if (!nbuf->ecccalc) {
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->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4672 if (!nbuf->ecccode) {
4673 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004674 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004675 }
4676
4677 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4678 GFP_KERNEL);
4679 if (!nbuf->databuf) {
4680 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004681 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004682 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004683
4684 chip->buffers = nbuf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004685 } else if (!chip->buffers) {
4686 return -ENOMEM;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004687 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004688
Boris Brezillonf84674b2017-06-02 12:18:24 +02004689 /*
4690 * FIXME: some NAND manufacturer drivers expect the first die to be
4691 * selected when manufacturer->init() is called. They should be fixed
4692 * to explictly select the relevant die when interacting with the NAND
4693 * chip.
4694 */
4695 chip->select_chip(mtd, 0);
4696 ret = nand_manufacturer_init(chip);
4697 chip->select_chip(mtd, -1);
4698 if (ret)
4699 goto err_free_nbuf;
4700
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004701 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004702 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004703
4704 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004705 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004706 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004707 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004708 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004709 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004712 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 break;
4714 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004715 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02004716 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004717 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02004719 /*
4720 * Expose the whole OOB area to users if ECC_NONE
4721 * is passed. We could do that for all kind of
4722 * ->oobsize, but we must keep the old large/small
4723 * page with ECC layout when ->oobsize <= 128 for
4724 * compatibility reasons.
4725 */
4726 if (ecc->mode == NAND_ECC_NONE) {
4727 mtd_set_ooblayout(mtd,
4728 &nand_ooblayout_lp_ops);
4729 break;
4730 }
4731
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004732 WARN(1, "No oob scheme defined for oobsize %d\n",
4733 mtd->oobsize);
4734 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004735 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 }
4737 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004738
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004739 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004740 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004741 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004742 */
David Woodhouse956e9442006-09-25 17:12:39 +01004743
Huang Shijie97de79e02013-10-18 14:20:53 +08004744 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004745 case NAND_ECC_HW_OOB_FIRST:
4746 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004747 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004748 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4749 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004750 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004751 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004752 if (!ecc->read_page)
4753 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004754
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004755 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004756 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004757 if (!ecc->read_page)
4758 ecc->read_page = nand_read_page_hwecc;
4759 if (!ecc->write_page)
4760 ecc->write_page = nand_write_page_hwecc;
4761 if (!ecc->read_page_raw)
4762 ecc->read_page_raw = nand_read_page_raw;
4763 if (!ecc->write_page_raw)
4764 ecc->write_page_raw = nand_write_page_raw;
4765 if (!ecc->read_oob)
4766 ecc->read_oob = nand_read_oob_std;
4767 if (!ecc->write_oob)
4768 ecc->write_oob = nand_write_oob_std;
4769 if (!ecc->read_subpage)
4770 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004771 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004772 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004773
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004774 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004775 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4776 (!ecc->read_page ||
4777 ecc->read_page == nand_read_page_hwecc ||
4778 !ecc->write_page ||
4779 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004780 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4781 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004782 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004783 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004784 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004785 if (!ecc->read_page)
4786 ecc->read_page = nand_read_page_syndrome;
4787 if (!ecc->write_page)
4788 ecc->write_page = nand_write_page_syndrome;
4789 if (!ecc->read_page_raw)
4790 ecc->read_page_raw = nand_read_page_raw_syndrome;
4791 if (!ecc->write_page_raw)
4792 ecc->write_page_raw = nand_write_page_raw_syndrome;
4793 if (!ecc->read_oob)
4794 ecc->read_oob = nand_read_oob_syndrome;
4795 if (!ecc->write_oob)
4796 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004797
Huang Shijie97de79e02013-10-18 14:20:53 +08004798 if (mtd->writesize >= ecc->size) {
4799 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004800 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4801 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004802 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07004803 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004804 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004805 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004806 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4807 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004808 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004809 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004811 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004812 ret = nand_set_ecc_soft_ops(mtd);
4813 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004814 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004815 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01004816 }
4817 break;
4818
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004819 case NAND_ECC_ON_DIE:
4820 if (!ecc->read_page || !ecc->write_page) {
4821 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4822 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004823 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004824 }
4825 if (!ecc->read_oob)
4826 ecc->read_oob = nand_read_oob_std;
4827 if (!ecc->write_oob)
4828 ecc->write_oob = nand_write_oob_std;
4829 break;
4830
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004831 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004832 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004833 ecc->read_page = nand_read_page_raw;
4834 ecc->write_page = nand_write_page_raw;
4835 ecc->read_oob = nand_read_oob_std;
4836 ecc->read_page_raw = nand_read_page_raw;
4837 ecc->write_page_raw = nand_write_page_raw;
4838 ecc->write_oob = nand_write_oob_std;
4839 ecc->size = mtd->writesize;
4840 ecc->bytes = 0;
4841 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004845 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4846 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004847 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
Brian Norris9ce244b2011-08-30 18:45:37 -07004850 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004851 if (!ecc->read_oob_raw)
4852 ecc->read_oob_raw = ecc->read_oob;
4853 if (!ecc->write_oob_raw)
4854 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004855
Boris Brezillon846031d2016-02-03 20:11:00 +01004856 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004857 mtd->ecc_strength = ecc->strength;
4858 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004859
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004860 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004861 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004862 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004863 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004864 ecc->steps = mtd->writesize / ecc->size;
4865 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004866 WARN(1, "Invalid ECC parameters\n");
4867 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004868 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004870 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004871 if (ecc->total > mtd->oobsize) {
4872 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
4873 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004874 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004875 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004876
Boris Brezillon846031d2016-02-03 20:11:00 +01004877 /*
4878 * The number of bytes available for a client to place data into
4879 * the out of band area.
4880 */
4881 ret = mtd_ooblayout_count_freebytes(mtd);
4882 if (ret < 0)
4883 ret = 0;
4884
4885 mtd->oobavail = ret;
4886
4887 /* ECC sanity check: warn if it's too weak */
4888 if (!nand_ecc_strength_good(mtd))
4889 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4890 mtd->name);
4891
Brian Norris8b6e50c2011-05-25 14:59:01 -07004892 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004893 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004894 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004895 case 2:
4896 mtd->subpage_sft = 1;
4897 break;
4898 case 4:
4899 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004900 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004901 mtd->subpage_sft = 2;
4902 break;
4903 }
4904 }
4905 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4906
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004907 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004908 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004911 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004913 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304914 switch (ecc->mode) {
4915 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304916 if (chip->page_shift > 9)
4917 chip->options |= NAND_SUBPAGE_READ;
4918 break;
4919
4920 default:
4921 break;
4922 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004923
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004925 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004926 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4927 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004928 mtd->_erase = nand_erase;
4929 mtd->_point = NULL;
4930 mtd->_unpoint = NULL;
4931 mtd->_read = nand_read;
4932 mtd->_write = nand_write;
4933 mtd->_panic_write = panic_nand_write;
4934 mtd->_read_oob = nand_read_oob;
4935 mtd->_write_oob = nand_write_oob;
4936 mtd->_sync = nand_sync;
4937 mtd->_lock = NULL;
4938 mtd->_unlock = NULL;
4939 mtd->_suspend = nand_suspend;
4940 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004941 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004942 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004943 mtd->_block_isbad = nand_block_isbad;
4944 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004945 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004946 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004948 /*
4949 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4950 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4951 * properly set.
4952 */
4953 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004954 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Boris Brezillonf84674b2017-06-02 12:18:24 +02004956 /* Initialize the ->data_interface field. */
4957 ret = nand_init_data_interface(chip);
4958 if (ret)
4959 goto err_nand_manuf_cleanup;
4960
4961 /* Enter fastest possible mode on all dies. */
4962 for (i = 0; i < chip->numchips; i++) {
4963 chip->select_chip(mtd, i);
4964 ret = nand_setup_data_interface(chip, i);
4965 chip->select_chip(mtd, -1);
4966
4967 if (ret)
4968 goto err_nand_data_iface_cleanup;
4969 }
4970
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004971 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004972 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004973 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
4975 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07004976 ret = chip->scan_bbt(mtd);
4977 if (ret)
Boris Brezillonf84674b2017-06-02 12:18:24 +02004978 goto err_nand_data_iface_cleanup;
4979
Brian Norris44d41822017-05-01 17:04:50 -07004980 return 0;
4981
Boris Brezillonf84674b2017-06-02 12:18:24 +02004982err_nand_data_iface_cleanup:
4983 nand_release_data_interface(chip);
4984
4985err_nand_manuf_cleanup:
4986 nand_manufacturer_cleanup(chip);
4987
4988err_free_nbuf:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004989 if (nbuf) {
4990 kfree(nbuf->databuf);
4991 kfree(nbuf->ecccode);
4992 kfree(nbuf->ecccalc);
4993 kfree(nbuf);
4994 }
Brian Norris78771042017-05-01 17:04:53 -07004995
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004996 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004998EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
Brian Norris8b6e50c2011-05-25 14:59:01 -07005000/*
5001 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005002 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07005003 * to call us from in-kernel code if the core NAND support is modular.
5004 */
David Woodhouse3b85c322006-09-25 17:06:53 +01005005#ifdef MODULE
5006#define caller_is_module() (1)
5007#else
5008#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06005009 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01005010#endif
5011
5012/**
5013 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005014 * @mtd: MTD device structure
5015 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01005016 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005017 * This fills out all the uninitialized function pointers with the defaults.
5018 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005019 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005020 */
5021int nand_scan(struct mtd_info *mtd, int maxchips)
5022{
5023 int ret;
5024
David Woodhouse5e81e882010-02-26 18:32:56 +00005025 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01005026 if (!ret)
5027 ret = nand_scan_tail(mtd);
5028 return ret;
5029}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005030EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01005031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005033 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5034 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005035 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005036void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005038 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005039 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005040 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5041
Boris Brezillond8e725d2016-09-15 10:32:50 +02005042 nand_release_data_interface(chip);
5043
Jesper Juhlfa671642005-11-07 01:01:27 -08005044 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005045 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005046 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
5047 kfree(chip->buffers->databuf);
5048 kfree(chip->buffers->ecccode);
5049 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005050 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005051 }
Brian Norris58373ff2010-07-15 12:15:44 -07005052
5053 /* Free bad block descriptor memory */
5054 if (chip->badblock_pattern && chip->badblock_pattern->options
5055 & NAND_BBT_DYNAMICSTRUCT)
5056 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005057
5058 /* Free manufacturer priv data. */
5059 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005061EXPORT_SYMBOL_GPL(nand_cleanup);
5062
5063/**
5064 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5065 * held by the NAND device
5066 * @mtd: MTD device structure
5067 */
5068void nand_release(struct mtd_info *mtd)
5069{
5070 mtd_device_unregister(mtd);
5071 nand_cleanup(mtd_to_nand(mtd));
5072}
David Woodhousee0c7d762006-05-13 18:07:53 +01005073EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005074
David Woodhousee0c7d762006-05-13 18:07:53 +01005075MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005076MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5077MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005078MODULE_DESCRIPTION("Generic NAND flash driver code");