blob: e3bf33bc1fb610843dd4971b1d9b36369a2a9685 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010039#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/types.h>
41#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020042#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010044#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020047#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020049#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010050
Huang Shijie6a8214a2012-11-19 14:43:30 +080051static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020053static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
Boris Brezillon41b207a2016-02-03 19:06:15 +010056/* Define default oob placement schemes for large and small page devices */
57static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59{
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020068 if (mtd->oobsize == 16)
69 oobregion->length = 4;
70 else
71 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010072 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020073 if (mtd->oobsize == 8)
74 return -ERANGE;
75
Boris Brezillon41b207a2016-02-03 19:06:15 +010076 oobregion->offset = 6;
77 oobregion->length = ecc->total - 4;
78 }
79
80 return 0;
81}
82
83static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85{
86 if (section > 1)
87 return -ERANGE;
88
89 if (mtd->oobsize == 16) {
90 if (section)
91 return -ERANGE;
92
93 oobregion->length = 8;
94 oobregion->offset = 8;
95 } else {
96 oobregion->length = 2;
97 if (!section)
98 oobregion->offset = 3;
99 else
100 oobregion->offset = 6;
101 }
102
103 return 0;
104}
105
106const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107 .ecc = nand_ooblayout_ecc_sp,
108 .free = nand_ooblayout_free_sp,
109};
110EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
111
112static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113 struct mtd_oob_region *oobregion)
114{
115 struct nand_chip *chip = mtd_to_nand(mtd);
116 struct nand_ecc_ctrl *ecc = &chip->ecc;
117
Miquel Raynal882fd152017-08-26 17:19:15 +0200118 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100119 return -ERANGE;
120
121 oobregion->length = ecc->total;
122 oobregion->offset = mtd->oobsize - oobregion->length;
123
124 return 0;
125}
126
127static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128 struct mtd_oob_region *oobregion)
129{
130 struct nand_chip *chip = mtd_to_nand(mtd);
131 struct nand_ecc_ctrl *ecc = &chip->ecc;
132
133 if (section)
134 return -ERANGE;
135
136 oobregion->length = mtd->oobsize - ecc->total - 2;
137 oobregion->offset = 2;
138
139 return 0;
140}
141
142const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143 .ecc = nand_ooblayout_ecc_lp,
144 .free = nand_ooblayout_free_lp,
145};
146EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200147
Alexander Couzens6a623e02017-05-02 12:19:00 +0200148/*
149 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
150 * are placed at a fixed offset.
151 */
152static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153 struct mtd_oob_region *oobregion)
154{
155 struct nand_chip *chip = mtd_to_nand(mtd);
156 struct nand_ecc_ctrl *ecc = &chip->ecc;
157
158 if (section)
159 return -ERANGE;
160
161 switch (mtd->oobsize) {
162 case 64:
163 oobregion->offset = 40;
164 break;
165 case 128:
166 oobregion->offset = 80;
167 break;
168 default:
169 return -EINVAL;
170 }
171
172 oobregion->length = ecc->total;
173 if (oobregion->offset + oobregion->length > mtd->oobsize)
174 return -ERANGE;
175
176 return 0;
177}
178
179static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180 struct mtd_oob_region *oobregion)
181{
182 struct nand_chip *chip = mtd_to_nand(mtd);
183 struct nand_ecc_ctrl *ecc = &chip->ecc;
184 int ecc_offset = 0;
185
186 if (section < 0 || section > 1)
187 return -ERANGE;
188
189 switch (mtd->oobsize) {
190 case 64:
191 ecc_offset = 40;
192 break;
193 case 128:
194 ecc_offset = 80;
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 if (section == 0) {
201 oobregion->offset = 2;
202 oobregion->length = ecc_offset - 2;
203 } else {
204 oobregion->offset = ecc_offset + ecc->total;
205 oobregion->length = mtd->oobsize - oobregion->offset;
206 }
207
208 return 0;
209}
210
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100211static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200212 .ecc = nand_ooblayout_ecc_lp_hamming,
213 .free = nand_ooblayout_free_lp_hamming,
214};
215
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530216static int check_offs_len(struct mtd_info *mtd,
217 loff_t ofs, uint64_t len)
218{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100219 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530220 int ret = 0;
221
222 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
228 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300229 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700230 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530231 ret = -EINVAL;
232 }
233
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530234 return ret;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700239 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800241 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100243static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100245 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200247 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 spin_lock(&chip->controller->lock);
249 chip->controller->active = NULL;
250 chip->state = FL_READY;
251 wake_up(&chip->controller->wq);
252 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700257 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700259 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200261static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100263 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200264 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/**
Masanari Iida064a7692012-11-09 23:20:58 +0900268 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700269 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700271 * Default read function for 16bit buswidth with endianness conversion.
272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200274static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100276 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700282 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700284 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286static u16 nand_read_word(struct mtd_info *mtd)
287{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100288 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200289 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700294 * @mtd: MTD device structure
295 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * Default select function for 1 chip devices.
298 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200299static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100301 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200302
303 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200305 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309
310 default:
311 BUG();
312 }
313}
314
315/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100316 * nand_write_byte - [DEFAULT] write single byte to chip
317 * @mtd: MTD device structure
318 * @byte: value to write
319 *
320 * Default function to write a byte to I/O[7:0]
321 */
322static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
323{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100324 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100325
326 chip->write_buf(mtd, &byte, 1);
327}
328
329/**
330 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
331 * @mtd: MTD device structure
332 * @byte: value to write
333 *
334 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
335 */
336static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
337{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100338 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100339 uint16_t word = byte;
340
341 /*
342 * It's not entirely clear what should happen to I/O[15:8] when writing
343 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
344 *
345 * When the host supports a 16-bit bus width, only data is
346 * transferred at the 16-bit width. All address and command line
347 * transfers shall use only the lower 8-bits of the data bus. During
348 * command transfers, the host may place any value on the upper
349 * 8-bits of the data bus. During address transfers, the host shall
350 * set the upper 8-bits of the data bus to 00h.
351 *
352 * One user of the write_byte callback is nand_onfi_set_features. The
353 * four parameters are specified to be written to I/O[7:0], but this is
354 * neither an address nor a command transfer. Let's assume a 0 on the
355 * upper I/O lines is OK.
356 */
357 chip->write_buf(mtd, (uint8_t *)&word, 2);
358}
359
360/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700362 * @mtd: MTD device structure
363 * @buf: data buffer
364 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700366 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200368static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100370 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Alexander Shiyan76413832013-04-13 09:32:13 +0400372 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000376 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700377 * @mtd: MTD device structure
378 * @buf: buffer to store date
379 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700381 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200383static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100385 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alexander Shiyan76413832013-04-13 09:32:13 +0400387 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700392 * @mtd: MTD device structure
393 * @buf: data buffer
394 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700396 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200398static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100400 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000402
Alexander Shiyan76413832013-04-13 09:32:13 +0400403 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000407 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700408 * @mtd: MTD device structure
409 * @buf: buffer to store date
410 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700412 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200414static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100416 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Alexander Shiyan76413832013-04-13 09:32:13 +0400419 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700424 * @mtd: MTD device structure
425 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000427 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530429static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Masahiro Yamadac120e752017-03-23 05:07:01 +0900431 int page, page_end, res;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100432 struct nand_chip *chip = mtd_to_nand(mtd);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900433 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Brian Norris5fb15492011-05-31 16:31:21 -0700435 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700436 ofs += mtd->erasesize - mtd->writesize;
437
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100438 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900439 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100440
Masahiro Yamadac120e752017-03-23 05:07:01 +0900441 for (; page < page_end; page++) {
442 res = chip->ecc.read_oob(mtd, chip, page);
443 if (res)
444 return res;
445
446 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000447
Brian Norriscdbec052012-01-13 18:11:48 -0800448 if (likely(chip->badblockbits == 8))
449 res = bad != 0xFF;
450 else
451 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900452 if (res)
453 return res;
454 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200455
Masahiro Yamadac120e752017-03-23 05:07:01 +0900456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700460 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700461 * @mtd: MTD device structure
462 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700464 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700465 * specific driver. It provides the details for writing a bad block marker to a
466 * block.
467 */
468static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
469{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100470 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700471 struct mtd_oob_ops ops;
472 uint8_t buf[2] = { 0, 0 };
473 int ret = 0, res, i = 0;
474
Brian Norris0ec56dc2015-02-28 02:02:30 -0800475 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700476 ops.oobbuf = buf;
477 ops.ooboffs = chip->badblockpos;
478 if (chip->options & NAND_BUSWIDTH_16) {
479 ops.ooboffs &= ~0x01;
480 ops.len = ops.ooblen = 2;
481 } else {
482 ops.len = ops.ooblen = 1;
483 }
484 ops.mode = MTD_OPS_PLACE_OOB;
485
486 /* Write to first/last page(s) if necessary */
487 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
488 ofs += mtd->erasesize - mtd->writesize;
489 do {
490 res = nand_do_write_oob(mtd, ofs, &ops);
491 if (!ret)
492 ret = res;
493
494 i++;
495 ofs += mtd->writesize;
496 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
497
498 return ret;
499}
500
501/**
502 * nand_block_markbad_lowlevel - mark a block bad
503 * @mtd: MTD device structure
504 * @ofs: offset from device start
505 *
506 * This function performs the generic NAND bad block marking steps (i.e., bad
507 * block table(s) and/or marker(s)). We only allow the hardware driver to
508 * specify how to write bad block markers to OOB (chip->block_markbad).
509 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700510 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300511 *
Brian Norrise2414f42012-02-06 13:44:00 -0800512 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700513 * (2) write bad block marker to OOB area of affected block (unless flag
514 * NAND_BBT_NO_OOB_BBM is present)
515 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300516 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700517 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800518 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700520static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100522 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700523 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000524
Brian Norrisb32843b2013-07-30 17:52:59 -0700525 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800526 struct erase_info einfo;
527
528 /* Attempt erase before marking OOB */
529 memset(&einfo, 0, sizeof(einfo));
530 einfo.mtd = mtd;
531 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300532 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800533 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800534
Brian Norrisb32843b2013-07-30 17:52:59 -0700535 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800536 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700537 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300538 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200539 }
Brian Norrise2414f42012-02-06 13:44:00 -0800540
Brian Norrisb32843b2013-07-30 17:52:59 -0700541 /* Mark block bad in BBT */
542 if (chip->bbt) {
543 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800544 if (!ret)
545 ret = res;
546 }
547
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200548 if (!ret)
549 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300550
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000554/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700556 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700558 * Check, if the device is write protected. The function expects, that the
559 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100561static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100563 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100564 u8 status;
565 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200566
Brian Norris8b6e50c2011-05-25 14:59:01 -0700567 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200568 if (chip->options & NAND_BROKEN_XD)
569 return 0;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100572 ret = nand_status_op(chip, &status);
573 if (ret)
574 return ret;
575
576 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800580 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700581 * @mtd: MTD device structure
582 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300583 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800584 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300585 */
586static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
587{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100588 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300589
590 if (!chip->bbt)
591 return 0;
592 /* Return info from the table */
593 return nand_isreserved_bbt(mtd, ofs);
594}
595
596/**
597 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
598 * @mtd: MTD device structure
599 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700600 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
602 * Check, if the block is bad. Either by reading the bad block table or
603 * calling of the scan function.
604 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530605static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100607 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000608
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200609 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530610 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100613 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200616/**
617 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700618 * @mtd: MTD device structure
619 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200620 *
621 * Helper function for nand_wait_ready used when needing to wait in interrupt
622 * context.
623 */
624static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
625{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100626 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200627 int i;
628
629 /* Wait for the device to get ready */
630 for (i = 0; i < timeo; i++) {
631 if (chip->dev_ready(mtd))
632 break;
633 touch_softlockup_watchdog();
634 mdelay(1);
635 }
636}
637
Alex Smithb70af9b2015-10-06 14:52:07 +0100638/**
639 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
640 * @mtd: MTD device structure
641 *
642 * Wait for the ready pin after a command, and warn if a timeout occurs.
643 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100644void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000645{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100646 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100647 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000648
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200649 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100650 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200651
Brian Norris7854d3f2011-06-23 14:12:08 -0700652 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100653 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000654 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300656 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100657 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000658 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100659
Brian Norris9ebfdf52016-03-04 17:19:23 -0800660 if (!chip->dev_ready(mtd))
661 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000662}
David Woodhouse4b648b02006-09-25 17:05:24 +0100663EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200666 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
667 * @mtd: MTD device structure
668 * @timeo: Timeout in ms
669 *
670 * Wait for status ready (i.e. command done) or timeout.
671 */
672static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
673{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100674 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100675 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200676
677 timeo = jiffies + msecs_to_jiffies(timeo);
678 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100679 u8 status;
680
681 ret = nand_read_data_op(chip, &status, sizeof(status), true);
682 if (ret)
683 return;
684
685 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200686 break;
687 touch_softlockup_watchdog();
688 } while (time_before(jiffies, timeo));
689};
690
691/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700693 * @mtd: MTD device structure
694 * @command: the command to be sent
695 * @column: the column address for this command, -1 if none
696 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700698 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200699 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200701static void nand_command(struct mtd_info *mtd, unsigned int command,
702 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100704 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200705 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Brian Norris8b6e50c2011-05-25 14:59:01 -0700707 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (command == NAND_CMD_SEQIN) {
709 int readcmd;
710
Joern Engel28318772006-05-22 23:18:05 +0200711 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200713 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 readcmd = NAND_CMD_READOOB;
715 } else if (column < 256) {
716 /* First 256 bytes --> READ0 */
717 readcmd = NAND_CMD_READ0;
718 } else {
719 column -= 256;
720 readcmd = NAND_CMD_READ1;
721 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200722 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200723 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100725 if (command != NAND_CMD_NONE)
726 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Brian Norris8b6e50c2011-05-25 14:59:01 -0700728 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200729 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
730 /* Serially input address */
731 if (column != -1) {
732 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800733 if (chip->options & NAND_BUSWIDTH_16 &&
734 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200735 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200736 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200737 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200739 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200740 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200741 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200742 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900743 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200744 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200745 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200746 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000747
748 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700749 * Program and erase have their own busy handlers status and sequential
750 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000753
Miquel Raynaldf467892017-11-08 17:00:27 +0100754 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 case NAND_CMD_PAGEPROG:
756 case NAND_CMD_ERASE1:
757 case NAND_CMD_ERASE2:
758 case NAND_CMD_SEQIN:
759 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900760 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900761 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return;
763
764 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200765 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200767 udelay(chip->chip_delay);
768 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200769 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200770 chip->cmd_ctrl(mtd,
771 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200772 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
773 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return;
775
David Woodhousee0c7d762006-05-13 18:07:53 +0100776 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200777 case NAND_CMD_READ0:
778 /*
779 * READ0 is sometimes used to exit GET STATUS mode. When this
780 * is the case no address cycles are requested, and we can use
781 * this information to detect that we should not wait for the
782 * device to be ready.
783 */
784 if (column == -1 && page_addr == -1)
785 return;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000788 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * If we don't have access to the busy pin, we apply the given
790 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100791 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200792 if (!chip->dev_ready) {
793 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700797 /*
798 * Apply this short delay always to ensure that we do wait tWB in
799 * any case on any machine.
800 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100801 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000802
803 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200806static void nand_ccs_delay(struct nand_chip *chip)
807{
808 /*
809 * The controller already takes care of waiting for tCCS when the RNDIN
810 * or RNDOUT command is sent, return directly.
811 */
812 if (!(chip->options & NAND_WAIT_TCCS))
813 return;
814
815 /*
816 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
817 * (which should be safe for all NANDs).
818 */
819 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
820 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
821 else
822 ndelay(500);
823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/**
826 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700827 * @mtd: MTD device structure
828 * @command: the command to be sent
829 * @column: the column address for this command, -1 if none
830 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200832 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700833 * devices. We don't have the separate regions as we have in the small page
834 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200836static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
837 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100839 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* Emulate NAND_CMD_READOOB */
842 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200843 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 command = NAND_CMD_READ0;
845 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000846
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200847 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100848 if (command != NAND_CMD_NONE)
849 chip->cmd_ctrl(mtd, command,
850 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200853 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* Serially input address */
856 if (column != -1) {
857 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800858 if (chip->options & NAND_BUSWIDTH_16 &&
859 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200861 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200862 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200863
Brian Norrisf5b88de2016-10-03 09:49:35 -0700864 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200865 if (!nand_opcode_8bits(command))
866 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200869 chip->cmd_ctrl(mtd, page_addr, ctrl);
870 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200871 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900872 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200873 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200874 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200877 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000878
879 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700880 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100881 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000884
Miquel Raynaldf467892017-11-08 17:00:27 +0100885 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 case NAND_CMD_CACHEDPROG:
887 case NAND_CMD_PAGEPROG:
888 case NAND_CMD_ERASE1:
889 case NAND_CMD_ERASE2:
890 case NAND_CMD_SEQIN:
891 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900892 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900893 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000894 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200896 case NAND_CMD_RNDIN:
897 nand_ccs_delay(chip);
898 return;
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200901 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200903 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200904 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
905 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
906 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
907 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200908 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
909 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return;
911
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200912 case NAND_CMD_RNDOUT:
913 /* No ready / busy check necessary */
914 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
915 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
916 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
917 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200918
919 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200920 return;
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200923 /*
924 * READ0 is sometimes used to exit GET STATUS mode. When this
925 * is the case no address cycles are requested, and we can use
926 * this information to detect that READSTART should not be
927 * issued.
928 */
929 if (column == -1 && page_addr == -1)
930 return;
931
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200932 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
933 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
934 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
935 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000936
David Woodhousee0c7d762006-05-13 18:07:53 +0100937 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000939 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700941 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100942 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200943 if (!chip->dev_ready) {
944 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000948
Brian Norris8b6e50c2011-05-25 14:59:01 -0700949 /*
950 * Apply this short delay always to ensure that we do wait tWB in
951 * any case on any machine.
952 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100953 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000954
955 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200959 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700960 * @chip: the nand chip descriptor
961 * @mtd: MTD device structure
962 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200963 *
964 * Used when in panic, no locks are taken.
965 */
966static void panic_nand_get_device(struct nand_chip *chip,
967 struct mtd_info *mtd, int new_state)
968{
Brian Norris7854d3f2011-06-23 14:12:08 -0700969 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200970 chip->controller->active = chip;
971 chip->state = new_state;
972}
973
974/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700976 * @mtd: MTD device structure
977 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 *
979 * Get the device and lock it for exclusive access
980 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200981static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800982nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100984 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200985 spinlock_t *lock = &chip->controller->lock;
986 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100987 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200988retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100989 spin_lock(lock);
990
vimal singhb8b3ee92009-07-09 20:41:22 +0530991 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200992 if (!chip->controller->active)
993 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200994
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200995 if (chip->controller->active == chip && chip->state == FL_READY) {
996 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100997 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100998 return 0;
999 }
1000 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001001 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1002 chip->state = FL_PM_SUSPENDED;
1003 spin_unlock(lock);
1004 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001005 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001006 }
1007 set_current_state(TASK_UNINTERRUPTIBLE);
1008 add_wait_queue(wq, &wait);
1009 spin_unlock(lock);
1010 schedule();
1011 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto retry;
1013}
1014
1015/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001016 * panic_nand_wait - [GENERIC] wait until the command is done
1017 * @mtd: MTD device structure
1018 * @chip: NAND chip structure
1019 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001020 *
1021 * Wait for command done. This is a helper function for nand_wait used when
1022 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001023 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001024 */
1025static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1026 unsigned long timeo)
1027{
1028 int i;
1029 for (i = 0; i < timeo; i++) {
1030 if (chip->dev_ready) {
1031 if (chip->dev_ready(mtd))
1032 break;
1033 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001034 int ret;
1035 u8 status;
1036
1037 ret = nand_read_data_op(chip, &status, sizeof(status),
1038 true);
1039 if (ret)
1040 return;
1041
1042 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001043 break;
1044 }
1045 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001046 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001047}
1048
1049/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001050 * nand_wait - [DEFAULT] wait until the command is done
1051 * @mtd: MTD device structure
1052 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001054 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001055 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001056static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058
Alex Smithb70af9b2015-10-06 14:52:07 +01001059 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001060 u8 status;
1061 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Brian Norris8b6e50c2011-05-25 14:59:01 -07001063 /*
1064 * Apply this short delay always to ensure that we do wait tWB in any
1065 * case on any machine.
1066 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001067 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Boris Brezillon97d90da2017-11-30 18:01:29 +01001069 ret = nand_status_op(chip, NULL);
1070 if (ret)
1071 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001073 if (in_interrupt() || oops_in_progress)
1074 panic_nand_wait(mtd, chip, timeo);
1075 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001076 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001077 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001078 if (chip->dev_ready) {
1079 if (chip->dev_ready(mtd))
1080 break;
1081 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001082 ret = nand_read_data_op(chip, &status,
1083 sizeof(status), true);
1084 if (ret)
1085 return ret;
1086
1087 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001088 break;
1089 }
1090 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001091 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001093
Boris Brezillon97d90da2017-11-30 18:01:29 +01001094 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1095 if (ret)
1096 return ret;
1097
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001098 /* This can happen if in case of timeout or buggy dev_ready */
1099 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return status;
1101}
1102
1103/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001104 * nand_reset_data_interface - Reset data interface and timings
1105 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001106 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001107 *
1108 * Reset the Data interface and timings to ONFI mode 0.
1109 *
1110 * Returns 0 for success or negative error code otherwise.
1111 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001112static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001113{
1114 struct mtd_info *mtd = nand_to_mtd(chip);
1115 const struct nand_data_interface *conf;
1116 int ret;
1117
1118 if (!chip->setup_data_interface)
1119 return 0;
1120
1121 /*
1122 * The ONFI specification says:
1123 * "
1124 * To transition from NV-DDR or NV-DDR2 to the SDR data
1125 * interface, the host shall use the Reset (FFh) command
1126 * using SDR timing mode 0. A device in any timing mode is
1127 * required to recognize Reset (FFh) command issued in SDR
1128 * timing mode 0.
1129 * "
1130 *
1131 * Configure the data interface in SDR mode and set the
1132 * timings to timing mode 0.
1133 */
1134
1135 conf = nand_get_default_data_interface();
Boris Brezillon104e4422017-03-16 09:35:58 +01001136 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001137 if (ret)
1138 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1139
1140 return ret;
1141}
1142
1143/**
1144 * nand_setup_data_interface - Setup the best data interface and timings
1145 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001146 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001147 *
1148 * Find and configure the best data interface and NAND timings supported by
1149 * the chip and the driver.
1150 * First tries to retrieve supported timing modes from ONFI information,
1151 * and if the NAND chip does not support ONFI, relies on the
1152 * ->onfi_timing_mode_default specified in the nand_ids table.
1153 *
1154 * Returns 0 for success or negative error code otherwise.
1155 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001156static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001157{
1158 struct mtd_info *mtd = nand_to_mtd(chip);
1159 int ret;
1160
1161 if (!chip->setup_data_interface || !chip->data_interface)
1162 return 0;
1163
1164 /*
1165 * Ensure the timing mode has been changed on the chip side
1166 * before changing timings on the controller side.
1167 */
Boris Brezillona11bf5e2017-07-31 10:29:56 +02001168 if (chip->onfi_version &&
1169 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1170 ONFI_OPT_CMD_SET_GET_FEATURES)) {
Boris Brezillond8e725d2016-09-15 10:32:50 +02001171 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1172 chip->onfi_timing_mode_default,
1173 };
1174
1175 ret = chip->onfi_set_features(mtd, chip,
1176 ONFI_FEATURE_ADDR_TIMING_MODE,
1177 tmode_param);
1178 if (ret)
1179 goto err;
1180 }
1181
Boris Brezillon104e4422017-03-16 09:35:58 +01001182 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001183err:
1184 return ret;
1185}
1186
1187/**
1188 * nand_init_data_interface - find the best data interface and timings
1189 * @chip: The NAND chip
1190 *
1191 * Find the best data interface and NAND timings supported by the chip
1192 * and the driver.
1193 * First tries to retrieve supported timing modes from ONFI information,
1194 * and if the NAND chip does not support ONFI, relies on the
1195 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1196 * function nand_chip->data_interface is initialized with the best timing mode
1197 * available.
1198 *
1199 * Returns 0 for success or negative error code otherwise.
1200 */
1201static int nand_init_data_interface(struct nand_chip *chip)
1202{
1203 struct mtd_info *mtd = nand_to_mtd(chip);
1204 int modes, mode, ret;
1205
1206 if (!chip->setup_data_interface)
1207 return 0;
1208
1209 /*
1210 * First try to identify the best timings from ONFI parameters and
1211 * if the NAND does not support ONFI, fallback to the default ONFI
1212 * timing mode.
1213 */
1214 modes = onfi_get_async_timing_mode(chip);
1215 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1216 if (!chip->onfi_timing_mode_default)
1217 return 0;
1218
1219 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1220 }
1221
1222 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1223 GFP_KERNEL);
1224 if (!chip->data_interface)
1225 return -ENOMEM;
1226
1227 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1228 ret = onfi_init_data_interface(chip, chip->data_interface,
1229 NAND_SDR_IFACE, mode);
1230 if (ret)
1231 continue;
1232
Boris Brezillon104e4422017-03-16 09:35:58 +01001233 /* Pass -1 to only */
1234 ret = chip->setup_data_interface(mtd,
1235 NAND_DATA_IFACE_CHECK_ONLY,
1236 chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001237 if (!ret) {
1238 chip->onfi_timing_mode_default = mode;
1239 break;
1240 }
1241 }
1242
1243 return 0;
1244}
1245
1246static void nand_release_data_interface(struct nand_chip *chip)
1247{
1248 kfree(chip->data_interface);
1249}
1250
1251/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001252 * nand_read_page_op - Do a READ PAGE operation
1253 * @chip: The NAND chip
1254 * @page: page to read
1255 * @offset_in_page: offset within the page
1256 * @buf: buffer used to store the data
1257 * @len: length of the buffer
1258 *
1259 * This function issues a READ PAGE operation.
1260 * This function does not select/unselect the CS line.
1261 *
1262 * Returns 0 on success, a negative error code otherwise.
1263 */
1264int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1265 unsigned int offset_in_page, void *buf, unsigned int len)
1266{
1267 struct mtd_info *mtd = nand_to_mtd(chip);
1268
1269 if (len && !buf)
1270 return -EINVAL;
1271
1272 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1273 return -EINVAL;
1274
1275 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1276 if (len)
1277 chip->read_buf(mtd, buf, len);
1278
1279 return 0;
1280}
1281EXPORT_SYMBOL_GPL(nand_read_page_op);
1282
1283/**
1284 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1285 * @chip: The NAND chip
1286 * @page: parameter page to read
1287 * @buf: buffer used to store the data
1288 * @len: length of the buffer
1289 *
1290 * This function issues a READ PARAMETER PAGE operation.
1291 * This function does not select/unselect the CS line.
1292 *
1293 * Returns 0 on success, a negative error code otherwise.
1294 */
1295static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1296 unsigned int len)
1297{
1298 struct mtd_info *mtd = nand_to_mtd(chip);
1299 unsigned int i;
1300 u8 *p = buf;
1301
1302 if (len && !buf)
1303 return -EINVAL;
1304
1305 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1306 for (i = 0; i < len; i++)
1307 p[i] = chip->read_byte(mtd);
1308
1309 return 0;
1310}
1311
1312/**
1313 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1314 * @chip: The NAND chip
1315 * @offset_in_page: offset within the page
1316 * @buf: buffer used to store the data
1317 * @len: length of the buffer
1318 * @force_8bit: force 8-bit bus access
1319 *
1320 * This function issues a CHANGE READ COLUMN operation.
1321 * This function does not select/unselect the CS line.
1322 *
1323 * Returns 0 on success, a negative error code otherwise.
1324 */
1325int nand_change_read_column_op(struct nand_chip *chip,
1326 unsigned int offset_in_page, void *buf,
1327 unsigned int len, bool force_8bit)
1328{
1329 struct mtd_info *mtd = nand_to_mtd(chip);
1330
1331 if (len && !buf)
1332 return -EINVAL;
1333
1334 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1335 return -EINVAL;
1336
1337 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1338 if (len)
1339 chip->read_buf(mtd, buf, len);
1340
1341 return 0;
1342}
1343EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1344
1345/**
1346 * nand_read_oob_op - Do a READ OOB operation
1347 * @chip: The NAND chip
1348 * @page: page to read
1349 * @offset_in_oob: offset within the OOB area
1350 * @buf: buffer used to store the data
1351 * @len: length of the buffer
1352 *
1353 * This function issues a READ OOB operation.
1354 * This function does not select/unselect the CS line.
1355 *
1356 * Returns 0 on success, a negative error code otherwise.
1357 */
1358int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1359 unsigned int offset_in_oob, void *buf, unsigned int len)
1360{
1361 struct mtd_info *mtd = nand_to_mtd(chip);
1362
1363 if (len && !buf)
1364 return -EINVAL;
1365
1366 if (offset_in_oob + len > mtd->oobsize)
1367 return -EINVAL;
1368
1369 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1370 if (len)
1371 chip->read_buf(mtd, buf, len);
1372
1373 return 0;
1374}
1375EXPORT_SYMBOL_GPL(nand_read_oob_op);
1376
1377/**
1378 * nand_prog_page_begin_op - starts a PROG PAGE operation
1379 * @chip: The NAND chip
1380 * @page: page to write
1381 * @offset_in_page: offset within the page
1382 * @buf: buffer containing the data to write to the page
1383 * @len: length of the buffer
1384 *
1385 * This function issues the first half of a PROG PAGE operation.
1386 * This function does not select/unselect the CS line.
1387 *
1388 * Returns 0 on success, a negative error code otherwise.
1389 */
1390int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1391 unsigned int offset_in_page, const void *buf,
1392 unsigned int len)
1393{
1394 struct mtd_info *mtd = nand_to_mtd(chip);
1395
1396 if (len && !buf)
1397 return -EINVAL;
1398
1399 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1400 return -EINVAL;
1401
1402 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1403
1404 if (buf)
1405 chip->write_buf(mtd, buf, len);
1406
1407 return 0;
1408}
1409EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1410
1411/**
1412 * nand_prog_page_end_op - ends a PROG PAGE operation
1413 * @chip: The NAND chip
1414 *
1415 * This function issues the second half of a PROG PAGE operation.
1416 * This function does not select/unselect the CS line.
1417 *
1418 * Returns 0 on success, a negative error code otherwise.
1419 */
1420int nand_prog_page_end_op(struct nand_chip *chip)
1421{
1422 struct mtd_info *mtd = nand_to_mtd(chip);
1423 int status;
1424
1425 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1426
1427 status = chip->waitfunc(mtd, chip);
1428 if (status & NAND_STATUS_FAIL)
1429 return -EIO;
1430
1431 return 0;
1432}
1433EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1434
1435/**
1436 * nand_prog_page_op - Do a full PROG PAGE operation
1437 * @chip: The NAND chip
1438 * @page: page to write
1439 * @offset_in_page: offset within the page
1440 * @buf: buffer containing the data to write to the page
1441 * @len: length of the buffer
1442 *
1443 * This function issues a full PROG PAGE operation.
1444 * This function does not select/unselect the CS line.
1445 *
1446 * Returns 0 on success, a negative error code otherwise.
1447 */
1448int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1449 unsigned int offset_in_page, const void *buf,
1450 unsigned int len)
1451{
1452 struct mtd_info *mtd = nand_to_mtd(chip);
1453 int status;
1454
1455 if (!len || !buf)
1456 return -EINVAL;
1457
1458 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1459 return -EINVAL;
1460
1461 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1462 chip->write_buf(mtd, buf, len);
1463 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1464
1465 status = chip->waitfunc(mtd, chip);
1466 if (status & NAND_STATUS_FAIL)
1467 return -EIO;
1468
1469 return 0;
1470}
1471EXPORT_SYMBOL_GPL(nand_prog_page_op);
1472
1473/**
1474 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1475 * @chip: The NAND chip
1476 * @offset_in_page: offset within the page
1477 * @buf: buffer containing the data to send to the NAND
1478 * @len: length of the buffer
1479 * @force_8bit: force 8-bit bus access
1480 *
1481 * This function issues a CHANGE WRITE COLUMN operation.
1482 * This function does not select/unselect the CS line.
1483 *
1484 * Returns 0 on success, a negative error code otherwise.
1485 */
1486int nand_change_write_column_op(struct nand_chip *chip,
1487 unsigned int offset_in_page,
1488 const void *buf, unsigned int len,
1489 bool force_8bit)
1490{
1491 struct mtd_info *mtd = nand_to_mtd(chip);
1492
1493 if (len && !buf)
1494 return -EINVAL;
1495
1496 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1497 return -EINVAL;
1498
1499 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1500 if (len)
1501 chip->write_buf(mtd, buf, len);
1502
1503 return 0;
1504}
1505EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1506
1507/**
1508 * nand_readid_op - Do a READID operation
1509 * @chip: The NAND chip
1510 * @addr: address cycle to pass after the READID command
1511 * @buf: buffer used to store the ID
1512 * @len: length of the buffer
1513 *
1514 * This function sends a READID command and reads back the ID returned by the
1515 * NAND.
1516 * This function does not select/unselect the CS line.
1517 *
1518 * Returns 0 on success, a negative error code otherwise.
1519 */
1520int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1521 unsigned int len)
1522{
1523 struct mtd_info *mtd = nand_to_mtd(chip);
1524 unsigned int i;
1525 u8 *id = buf;
1526
1527 if (len && !buf)
1528 return -EINVAL;
1529
1530 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1531
1532 for (i = 0; i < len; i++)
1533 id[i] = chip->read_byte(mtd);
1534
1535 return 0;
1536}
1537EXPORT_SYMBOL_GPL(nand_readid_op);
1538
1539/**
1540 * nand_status_op - Do a STATUS operation
1541 * @chip: The NAND chip
1542 * @status: out variable to store the NAND status
1543 *
1544 * This function sends a STATUS command and reads back the status returned by
1545 * the NAND.
1546 * This function does not select/unselect the CS line.
1547 *
1548 * Returns 0 on success, a negative error code otherwise.
1549 */
1550int nand_status_op(struct nand_chip *chip, u8 *status)
1551{
1552 struct mtd_info *mtd = nand_to_mtd(chip);
1553
1554 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1555 if (status)
1556 *status = chip->read_byte(mtd);
1557
1558 return 0;
1559}
1560EXPORT_SYMBOL_GPL(nand_status_op);
1561
1562/**
1563 * nand_exit_status_op - Exit a STATUS operation
1564 * @chip: The NAND chip
1565 *
1566 * This function sends a READ0 command to cancel the effect of the STATUS
1567 * command to avoid reading only the status until a new read command is sent.
1568 *
1569 * This function does not select/unselect the CS line.
1570 *
1571 * Returns 0 on success, a negative error code otherwise.
1572 */
1573int nand_exit_status_op(struct nand_chip *chip)
1574{
1575 struct mtd_info *mtd = nand_to_mtd(chip);
1576
1577 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1578
1579 return 0;
1580}
1581EXPORT_SYMBOL_GPL(nand_exit_status_op);
1582
1583/**
1584 * nand_erase_op - Do an erase operation
1585 * @chip: The NAND chip
1586 * @eraseblock: block to erase
1587 *
1588 * This function sends an ERASE command and waits for the NAND to be ready
1589 * before returning.
1590 * This function does not select/unselect the CS line.
1591 *
1592 * Returns 0 on success, a negative error code otherwise.
1593 */
1594int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1595{
1596 struct mtd_info *mtd = nand_to_mtd(chip);
1597 unsigned int page = eraseblock <<
1598 (chip->phys_erase_shift - chip->page_shift);
1599 int status;
1600
1601 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1602 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1603
1604 status = chip->waitfunc(mtd, chip);
1605 if (status < 0)
1606 return status;
1607
1608 if (status & NAND_STATUS_FAIL)
1609 return -EIO;
1610
1611 return 0;
1612}
1613EXPORT_SYMBOL_GPL(nand_erase_op);
1614
1615/**
1616 * nand_set_features_op - Do a SET FEATURES operation
1617 * @chip: The NAND chip
1618 * @feature: feature id
1619 * @data: 4 bytes of data
1620 *
1621 * This function sends a SET FEATURES command and waits for the NAND to be
1622 * ready before returning.
1623 * This function does not select/unselect the CS line.
1624 *
1625 * Returns 0 on success, a negative error code otherwise.
1626 */
1627static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1628 const void *data)
1629{
1630 struct mtd_info *mtd = nand_to_mtd(chip);
1631 const u8 *params = data;
1632 int i, status;
1633
1634 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1635 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1636 chip->write_byte(mtd, params[i]);
1637
1638 status = chip->waitfunc(mtd, chip);
1639 if (status & NAND_STATUS_FAIL)
1640 return -EIO;
1641
1642 return 0;
1643}
1644
1645/**
1646 * nand_get_features_op - Do a GET FEATURES operation
1647 * @chip: The NAND chip
1648 * @feature: feature id
1649 * @data: 4 bytes of data
1650 *
1651 * This function sends a GET FEATURES command and waits for the NAND to be
1652 * ready before returning.
1653 * This function does not select/unselect the CS line.
1654 *
1655 * Returns 0 on success, a negative error code otherwise.
1656 */
1657static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1658 void *data)
1659{
1660 struct mtd_info *mtd = nand_to_mtd(chip);
1661 u8 *params = data;
1662 int i;
1663
1664 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1665 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1666 params[i] = chip->read_byte(mtd);
1667
1668 return 0;
1669}
1670
1671/**
1672 * nand_reset_op - Do a reset operation
1673 * @chip: The NAND chip
1674 *
1675 * This function sends a RESET command and waits for the NAND to be ready
1676 * before returning.
1677 * This function does not select/unselect the CS line.
1678 *
1679 * Returns 0 on success, a negative error code otherwise.
1680 */
1681int nand_reset_op(struct nand_chip *chip)
1682{
1683 struct mtd_info *mtd = nand_to_mtd(chip);
1684
1685 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1686
1687 return 0;
1688}
1689EXPORT_SYMBOL_GPL(nand_reset_op);
1690
1691/**
1692 * nand_read_data_op - Read data from the NAND
1693 * @chip: The NAND chip
1694 * @buf: buffer used to store the data
1695 * @len: length of the buffer
1696 * @force_8bit: force 8-bit bus access
1697 *
1698 * This function does a raw data read on the bus. Usually used after launching
1699 * another NAND operation like nand_read_page_op().
1700 * This function does not select/unselect the CS line.
1701 *
1702 * Returns 0 on success, a negative error code otherwise.
1703 */
1704int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1705 bool force_8bit)
1706{
1707 struct mtd_info *mtd = nand_to_mtd(chip);
1708
1709 if (!len || !buf)
1710 return -EINVAL;
1711
1712 if (force_8bit) {
1713 u8 *p = buf;
1714 unsigned int i;
1715
1716 for (i = 0; i < len; i++)
1717 p[i] = chip->read_byte(mtd);
1718 } else {
1719 chip->read_buf(mtd, buf, len);
1720 }
1721
1722 return 0;
1723}
1724EXPORT_SYMBOL_GPL(nand_read_data_op);
1725
1726/**
1727 * nand_write_data_op - Write data from the NAND
1728 * @chip: The NAND chip
1729 * @buf: buffer containing the data to send on the bus
1730 * @len: length of the buffer
1731 * @force_8bit: force 8-bit bus access
1732 *
1733 * This function does a raw data write on the bus. Usually used after launching
1734 * another NAND operation like nand_write_page_begin_op().
1735 * This function does not select/unselect the CS line.
1736 *
1737 * Returns 0 on success, a negative error code otherwise.
1738 */
1739int nand_write_data_op(struct nand_chip *chip, const void *buf,
1740 unsigned int len, bool force_8bit)
1741{
1742 struct mtd_info *mtd = nand_to_mtd(chip);
1743
1744 if (!len || !buf)
1745 return -EINVAL;
1746
1747 if (force_8bit) {
1748 const u8 *p = buf;
1749 unsigned int i;
1750
1751 for (i = 0; i < len; i++)
1752 chip->write_byte(mtd, p[i]);
1753 } else {
1754 chip->write_buf(mtd, buf, len);
1755 }
1756
1757 return 0;
1758}
1759EXPORT_SYMBOL_GPL(nand_write_data_op);
1760
1761/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001762 * nand_reset - Reset and initialize a NAND device
1763 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001764 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001765 *
1766 * Returns 0 for success or negative error code otherwise
1767 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001768int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001769{
1770 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001771 int ret;
1772
Boris Brezillon104e4422017-03-16 09:35:58 +01001773 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001774 if (ret)
1775 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001776
Boris Brezillon73f907f2016-10-24 16:46:20 +02001777 /*
1778 * The CS line has to be released before we can apply the new NAND
1779 * interface settings, hence this weird ->select_chip() dance.
1780 */
1781 chip->select_chip(mtd, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001782 ret = nand_reset_op(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001783 chip->select_chip(mtd, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001784 if (ret)
1785 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001786
Boris Brezillon73f907f2016-10-24 16:46:20 +02001787 chip->select_chip(mtd, chipnr);
Boris Brezillon104e4422017-03-16 09:35:58 +01001788 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001789 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001790 if (ret)
1791 return ret;
1792
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001793 return 0;
1794}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02001795EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001796
1797/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001798 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1799 * @buf: buffer to test
1800 * @len: buffer length
1801 * @bitflips_threshold: maximum number of bitflips
1802 *
1803 * Check if a buffer contains only 0xff, which means the underlying region
1804 * has been erased and is ready to be programmed.
1805 * The bitflips_threshold specify the maximum number of bitflips before
1806 * considering the region is not erased.
1807 * Note: The logic of this function has been extracted from the memweight
1808 * implementation, except that nand_check_erased_buf function exit before
1809 * testing the whole buffer if the number of bitflips exceed the
1810 * bitflips_threshold value.
1811 *
1812 * Returns a positive number of bitflips less than or equal to
1813 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1814 * threshold.
1815 */
1816static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1817{
1818 const unsigned char *bitmap = buf;
1819 int bitflips = 0;
1820 int weight;
1821
1822 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1823 len--, bitmap++) {
1824 weight = hweight8(*bitmap);
1825 bitflips += BITS_PER_BYTE - weight;
1826 if (unlikely(bitflips > bitflips_threshold))
1827 return -EBADMSG;
1828 }
1829
1830 for (; len >= sizeof(long);
1831 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001832 unsigned long d = *((unsigned long *)bitmap);
1833 if (d == ~0UL)
1834 continue;
1835 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001836 bitflips += BITS_PER_LONG - weight;
1837 if (unlikely(bitflips > bitflips_threshold))
1838 return -EBADMSG;
1839 }
1840
1841 for (; len > 0; len--, bitmap++) {
1842 weight = hweight8(*bitmap);
1843 bitflips += BITS_PER_BYTE - weight;
1844 if (unlikely(bitflips > bitflips_threshold))
1845 return -EBADMSG;
1846 }
1847
1848 return bitflips;
1849}
1850
1851/**
1852 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1853 * 0xff data
1854 * @data: data buffer to test
1855 * @datalen: data length
1856 * @ecc: ECC buffer
1857 * @ecclen: ECC length
1858 * @extraoob: extra OOB buffer
1859 * @extraooblen: extra OOB length
1860 * @bitflips_threshold: maximum number of bitflips
1861 *
1862 * Check if a data buffer and its associated ECC and OOB data contains only
1863 * 0xff pattern, which means the underlying region has been erased and is
1864 * ready to be programmed.
1865 * The bitflips_threshold specify the maximum number of bitflips before
1866 * considering the region as not erased.
1867 *
1868 * Note:
1869 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1870 * different from the NAND page size. When fixing bitflips, ECC engines will
1871 * report the number of errors per chunk, and the NAND core infrastructure
1872 * expect you to return the maximum number of bitflips for the whole page.
1873 * This is why you should always use this function on a single chunk and
1874 * not on the whole page. After checking each chunk you should update your
1875 * max_bitflips value accordingly.
1876 * 2/ When checking for bitflips in erased pages you should not only check
1877 * the payload data but also their associated ECC data, because a user might
1878 * have programmed almost all bits to 1 but a few. In this case, we
1879 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1880 * this case.
1881 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1882 * data are protected by the ECC engine.
1883 * It could also be used if you support subpages and want to attach some
1884 * extra OOB data to an ECC chunk.
1885 *
1886 * Returns a positive number of bitflips less than or equal to
1887 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1888 * threshold. In case of success, the passed buffers are filled with 0xff.
1889 */
1890int nand_check_erased_ecc_chunk(void *data, int datalen,
1891 void *ecc, int ecclen,
1892 void *extraoob, int extraooblen,
1893 int bitflips_threshold)
1894{
1895 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1896
1897 data_bitflips = nand_check_erased_buf(data, datalen,
1898 bitflips_threshold);
1899 if (data_bitflips < 0)
1900 return data_bitflips;
1901
1902 bitflips_threshold -= data_bitflips;
1903
1904 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1905 if (ecc_bitflips < 0)
1906 return ecc_bitflips;
1907
1908 bitflips_threshold -= ecc_bitflips;
1909
1910 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1911 bitflips_threshold);
1912 if (extraoob_bitflips < 0)
1913 return extraoob_bitflips;
1914
1915 if (data_bitflips)
1916 memset(data, 0xff, datalen);
1917
1918 if (ecc_bitflips)
1919 memset(ecc, 0xff, ecclen);
1920
1921 if (extraoob_bitflips)
1922 memset(extraoob, 0xff, extraooblen);
1923
1924 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1925}
1926EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1927
1928/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001929 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001930 * @mtd: mtd info structure
1931 * @chip: nand chip info structure
1932 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001933 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001934 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001935 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001936 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001937 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001938int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1939 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001940{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001941 int ret;
1942
Boris Brezillon25f815f2017-11-30 18:01:30 +01001943 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001944 if (ret)
1945 return ret;
1946
1947 if (oob_required) {
1948 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
1949 false);
1950 if (ret)
1951 return ret;
1952 }
1953
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001954 return 0;
1955}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001956EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001957
1958/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001959 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001960 * @mtd: mtd info structure
1961 * @chip: nand chip info structure
1962 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001963 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001964 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001965 *
1966 * We need a special oob layout and handling even when OOB isn't used.
1967 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001968static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001969 struct nand_chip *chip, uint8_t *buf,
1970 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001971{
1972 int eccsize = chip->ecc.size;
1973 int eccbytes = chip->ecc.bytes;
1974 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001975 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08001976
Boris Brezillon25f815f2017-11-30 18:01:30 +01001977 ret = nand_read_page_op(chip, page, 0, NULL, 0);
1978 if (ret)
1979 return ret;
1980
David Brownell52ff49d2009-03-04 12:01:36 -08001981 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001982 ret = nand_read_data_op(chip, buf, eccsize, false);
1983 if (ret)
1984 return ret;
1985
David Brownell52ff49d2009-03-04 12:01:36 -08001986 buf += eccsize;
1987
1988 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001989 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
1990 false);
1991 if (ret)
1992 return ret;
1993
David Brownell52ff49d2009-03-04 12:01:36 -08001994 oob += chip->ecc.prepad;
1995 }
1996
Boris Brezillon97d90da2017-11-30 18:01:29 +01001997 ret = nand_read_data_op(chip, oob, eccbytes, false);
1998 if (ret)
1999 return ret;
2000
David Brownell52ff49d2009-03-04 12:01:36 -08002001 oob += eccbytes;
2002
2003 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002004 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2005 false);
2006 if (ret)
2007 return ret;
2008
David Brownell52ff49d2009-03-04 12:01:36 -08002009 oob += chip->ecc.postpad;
2010 }
2011 }
2012
2013 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002014 if (size) {
2015 ret = nand_read_data_op(chip, oob, size, false);
2016 if (ret)
2017 return ret;
2018 }
David Brownell52ff49d2009-03-04 12:01:36 -08002019
2020 return 0;
2021}
2022
2023/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002024 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002025 * @mtd: mtd info structure
2026 * @chip: nand chip info structure
2027 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002028 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002029 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00002030 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002031static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002032 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Boris Brezillon846031d2016-02-03 20:11:00 +01002034 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002035 int eccbytes = chip->ecc.bytes;
2036 int eccsteps = chip->ecc.steps;
2037 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002038 uint8_t *ecc_calc = chip->buffers->ecccalc;
2039 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07002040 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002041
Brian Norris1fbb9382012-05-02 10:14:55 -07002042 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002043
2044 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2045 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2046
Boris Brezillon846031d2016-02-03 20:11:00 +01002047 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2048 chip->ecc.total);
2049 if (ret)
2050 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002051
2052 eccsteps = chip->ecc.steps;
2053 p = buf;
2054
2055 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2056 int stat;
2057
2058 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07002059 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002060 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002061 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002062 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002063 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2064 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002065 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002066 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01002067}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302070 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002071 * @mtd: mtd info structure
2072 * @chip: nand chip info structure
2073 * @data_offs: offset of requested data within the page
2074 * @readlen: data length
2075 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08002076 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01002077 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002078static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08002079 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
2080 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01002081{
Boris Brezillon846031d2016-02-03 20:11:00 +01002082 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002083 uint8_t *p;
2084 int data_col_addr, i, gaps = 0;
2085 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
2086 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01002087 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07002088 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01002089 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01002090
Brian Norris7854d3f2011-06-23 14:12:08 -07002091 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01002092 start_step = data_offs / chip->ecc.size;
2093 end_step = (data_offs + readlen - 1) / chip->ecc.size;
2094 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10302095 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01002096
Brian Norris8b6e50c2011-05-25 14:59:01 -07002097 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01002098 datafrag_len = num_steps * chip->ecc.size;
2099 eccfrag_len = num_steps * chip->ecc.bytes;
2100
2101 data_col_addr = start_step * chip->ecc.size;
2102 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01002103 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01002104 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002105 if (ret)
2106 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002107
Brian Norris8b6e50c2011-05-25 14:59:01 -07002108 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01002109 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
2110 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
2111
Brian Norris8b6e50c2011-05-25 14:59:01 -07002112 /*
2113 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07002114 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07002115 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002116 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
2117 if (ret)
2118 return ret;
2119
2120 if (oobregion.length < eccfrag_len)
2121 gaps = 1;
2122
Alexey Korolev3d459552008-05-15 17:23:18 +01002123 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002124 ret = nand_change_read_column_op(chip, mtd->writesize,
2125 chip->oob_poi, mtd->oobsize,
2126 false);
2127 if (ret)
2128 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002129 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002130 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002131 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07002132 * about buswidth alignment in read_buf.
2133 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002134 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01002135 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01002136 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002137 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01002138 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
2139 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01002140 aligned_len++;
2141
Boris Brezillon97d90da2017-11-30 18:01:29 +01002142 ret = nand_change_read_column_op(chip,
2143 mtd->writesize + aligned_pos,
2144 &chip->oob_poi[aligned_pos],
2145 aligned_len, false);
2146 if (ret)
2147 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002148 }
2149
Boris Brezillon846031d2016-02-03 20:11:00 +01002150 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
2151 chip->oob_poi, index, eccfrag_len);
2152 if (ret)
2153 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01002154
2155 p = bufpoi + data_col_addr;
2156 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
2157 int stat;
2158
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002159 stat = chip->ecc.correct(mtd, p,
2160 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002161 if (stat == -EBADMSG &&
2162 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2163 /* check for empty pages with bitflips */
2164 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2165 &chip->buffers->ecccode[i],
2166 chip->ecc.bytes,
2167 NULL, 0,
2168 chip->ecc.strength);
2169 }
2170
Mike Dunn3f91e942012-04-25 12:06:09 -07002171 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002172 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002173 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01002174 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002175 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2176 }
Alexey Korolev3d459552008-05-15 17:23:18 +01002177 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002178 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01002179}
2180
2181/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002182 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002183 * @mtd: mtd info structure
2184 * @chip: nand chip info structure
2185 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002186 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002187 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002188 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002189 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002190 */
2191static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002192 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002193{
Boris Brezillon846031d2016-02-03 20:11:00 +01002194 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002195 int eccbytes = chip->ecc.bytes;
2196 int eccsteps = chip->ecc.steps;
2197 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002198 uint8_t *ecc_calc = chip->buffers->ecccalc;
2199 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07002200 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002201
Boris Brezillon25f815f2017-11-30 18:01:30 +01002202 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2203 if (ret)
2204 return ret;
2205
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002206 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2207 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002208
2209 ret = nand_read_data_op(chip, p, eccsize, false);
2210 if (ret)
2211 return ret;
2212
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002213 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2214 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002215
2216 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2217 if (ret)
2218 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002219
Boris Brezillon846031d2016-02-03 20:11:00 +01002220 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2221 chip->ecc.total);
2222 if (ret)
2223 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002224
2225 eccsteps = chip->ecc.steps;
2226 p = buf;
2227
2228 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2229 int stat;
2230
2231 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002232 if (stat == -EBADMSG &&
2233 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2234 /* check for empty pages with bitflips */
2235 stat = nand_check_erased_ecc_chunk(p, eccsize,
2236 &ecc_code[i], eccbytes,
2237 NULL, 0,
2238 chip->ecc.strength);
2239 }
2240
Mike Dunn3f91e942012-04-25 12:06:09 -07002241 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002242 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002243 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002244 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002245 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2246 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002247 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002248 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002249}
2250
2251/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002252 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07002253 * @mtd: mtd info structure
2254 * @chip: nand chip info structure
2255 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002256 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002257 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002258 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002259 * Hardware ECC for large page chips, require OOB to be read first. For this
2260 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2261 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2262 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2263 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002264 */
2265static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002266 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002267{
Boris Brezillon846031d2016-02-03 20:11:00 +01002268 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002269 int eccbytes = chip->ecc.bytes;
2270 int eccsteps = chip->ecc.steps;
2271 uint8_t *p = buf;
2272 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002273 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07002274 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002275
2276 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01002277 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2278 if (ret)
2279 return ret;
2280
2281 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2282 if (ret)
2283 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002284
Boris Brezillon846031d2016-02-03 20:11:00 +01002285 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
2286 chip->ecc.total);
2287 if (ret)
2288 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002289
2290 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2291 int stat;
2292
2293 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002294
2295 ret = nand_read_data_op(chip, p, eccsize, false);
2296 if (ret)
2297 return ret;
2298
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002299 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2300
2301 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002302 if (stat == -EBADMSG &&
2303 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2304 /* check for empty pages with bitflips */
2305 stat = nand_check_erased_ecc_chunk(p, eccsize,
2306 &ecc_code[i], eccbytes,
2307 NULL, 0,
2308 chip->ecc.strength);
2309 }
2310
Mike Dunn3f91e942012-04-25 12:06:09 -07002311 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002312 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07002313 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002314 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07002315 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2316 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002317 }
Mike Dunn3f91e942012-04-25 12:06:09 -07002318 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07002319}
2320
2321/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002322 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07002323 * @mtd: mtd info structure
2324 * @chip: nand chip info structure
2325 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002326 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002327 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002328 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002329 * The hw generator calculates the error syndrome automatically. Therefore we
2330 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002331 */
2332static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002333 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002334{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002335 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002336 int eccbytes = chip->ecc.bytes;
2337 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002338 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002339 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002340 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07002341 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002342
Boris Brezillon25f815f2017-11-30 18:01:30 +01002343 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2344 if (ret)
2345 return ret;
2346
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002347 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2348 int stat;
2349
2350 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002351
2352 ret = nand_read_data_op(chip, p, eccsize, false);
2353 if (ret)
2354 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002355
2356 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002357 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2358 false);
2359 if (ret)
2360 return ret;
2361
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002362 oob += chip->ecc.prepad;
2363 }
2364
2365 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002366
2367 ret = nand_read_data_op(chip, oob, eccbytes, false);
2368 if (ret)
2369 return ret;
2370
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002371 stat = chip->ecc.correct(mtd, p, oob, NULL);
2372
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002373 oob += eccbytes;
2374
2375 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002376 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2377 false);
2378 if (ret)
2379 return ret;
2380
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002381 oob += chip->ecc.postpad;
2382 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01002383
2384 if (stat == -EBADMSG &&
2385 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2386 /* check for empty pages with bitflips */
2387 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2388 oob - eccpadbytes,
2389 eccpadbytes,
2390 NULL, 0,
2391 chip->ecc.strength);
2392 }
2393
2394 if (stat < 0) {
2395 mtd->ecc_stats.failed++;
2396 } else {
2397 mtd->ecc_stats.corrected += stat;
2398 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2399 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002400 }
2401
2402 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002403 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002404 if (i) {
2405 ret = nand_read_data_op(chip, oob, i, false);
2406 if (ret)
2407 return ret;
2408 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002409
Mike Dunn3f91e942012-04-25 12:06:09 -07002410 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002411}
2412
2413/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002414 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01002415 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002416 * @oob: oob destination address
2417 * @ops: oob ops structure
2418 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002419 */
Boris Brezillon846031d2016-02-03 20:11:00 +01002420static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03002421 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002422{
Boris Brezillon846031d2016-02-03 20:11:00 +01002423 struct nand_chip *chip = mtd_to_nand(mtd);
2424 int ret;
2425
Florian Fainellif8ac0412010-09-07 13:23:43 +02002426 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002427
Brian Norris0612b9d2011-08-30 18:45:40 -07002428 case MTD_OPS_PLACE_OOB:
2429 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002430 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2431 return oob + len;
2432
Boris Brezillon846031d2016-02-03 20:11:00 +01002433 case MTD_OPS_AUTO_OOB:
2434 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
2435 ops->ooboffs, len);
2436 BUG_ON(ret);
2437 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002438
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002439 default:
2440 BUG();
2441 }
2442 return NULL;
2443}
2444
2445/**
Brian Norrisba84fb52014-01-03 15:13:33 -08002446 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
2447 * @mtd: MTD device structure
2448 * @retry_mode: the retry mode to use
2449 *
2450 * Some vendors supply a special command to shift the Vt threshold, to be used
2451 * when there are too many bitflips in a page (i.e., ECC error). After setting
2452 * a new threshold, the host should retry reading the page.
2453 */
2454static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2455{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002456 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08002457
2458 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2459
2460 if (retry_mode >= chip->read_retries)
2461 return -EINVAL;
2462
2463 if (!chip->setup_read_retry)
2464 return -EOPNOTSUPP;
2465
2466 return chip->setup_read_retry(mtd, retry_mode);
2467}
2468
2469/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002470 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002471 * @mtd: MTD device structure
2472 * @from: offset to read from
2473 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00002474 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002475 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00002476 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002477static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2478 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00002479{
Brian Norrise47f3db2012-05-02 10:14:56 -07002480 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002481 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002482 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002483 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03002484 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002485 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02002486
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002487 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002488 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07002489 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08002490 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08002491 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002493 chipnr = (int)(from >> chip->chip_shift);
2494 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002496 realpage = (int)(from >> chip->page_shift);
2497 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002499 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002501 buf = ops->datbuf;
2502 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07002503 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002504
Florian Fainellif8ac0412010-09-07 13:23:43 +02002505 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08002506 unsigned int ecc_failures = mtd->ecc_stats.failed;
2507
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002508 bytes = min(mtd->writesize - col, readlen);
2509 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002510
Kamal Dasu66507c72014-05-01 20:51:19 -04002511 if (!aligned)
2512 use_bufpoi = 1;
2513 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002514 use_bufpoi = !virt_addr_valid(buf) ||
2515 !IS_ALIGNED((unsigned long)buf,
2516 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002517 else
2518 use_bufpoi = 0;
2519
Brian Norris8b6e50c2011-05-25 14:59:01 -07002520 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002521 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002522 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2523
2524 if (use_bufpoi && aligned)
2525 pr_debug("%s: using read bounce buffer for buf@%p\n",
2526 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Brian Norrisba84fb52014-01-03 15:13:33 -08002528read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07002529 /*
2530 * Now read the page into the buffer. Absent an error,
2531 * the read methods return max bitflips per ecc step.
2532 */
Brian Norris0612b9d2011-08-30 18:45:40 -07002533 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07002534 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002535 oob_required,
2536 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002537 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
2538 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002539 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08002540 col, bytes, bufpoi,
2541 page);
David Woodhouse956e9442006-09-25 17:12:39 +01002542 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07002543 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002544 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07002545 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002546 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07002547 /* Invalidate page cache */
2548 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01002549 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07002550 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002551
2552 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04002553 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002554 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08002555 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07002556 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002557 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07002558 chip->pagebuf_bitflips = ret;
2559 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07002560 /* Invalidate page cache */
2561 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07002562 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002563 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002565
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002566 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002567 int toread = min(oobreadlen, max_oobsize);
2568
2569 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01002570 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002571 oob, ops, toread);
2572 oobreadlen -= toread;
2573 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002574 }
Brian Norris5bc7c332013-03-13 09:51:31 -07002575
2576 if (chip->options & NAND_NEED_READRDY) {
2577 /* Apply delay or wait for ready/busy pin */
2578 if (!chip->dev_ready)
2579 udelay(chip->chip_delay);
2580 else
2581 nand_wait_ready(mtd);
2582 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002583
Brian Norrisba84fb52014-01-03 15:13:33 -08002584 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002585 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002586 retry_mode++;
2587 ret = nand_setup_read_retry(mtd,
2588 retry_mode);
2589 if (ret < 0)
2590 break;
2591
2592 /* Reset failures; retry */
2593 mtd->ecc_stats.failed = ecc_failures;
2594 goto read_retry;
2595 } else {
2596 /* No more retry modes; real failure */
2597 ecc_fail = true;
2598 }
2599 }
2600
2601 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09002602 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002603 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002604 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002605 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002606 max_bitflips = max_t(unsigned int, max_bitflips,
2607 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002610 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002611
Brian Norrisba84fb52014-01-03 15:13:33 -08002612 /* Reset to retry mode 0 */
2613 if (retry_mode) {
2614 ret = nand_setup_read_retry(mtd, 0);
2615 if (ret < 0)
2616 break;
2617 retry_mode = 0;
2618 }
2619
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002620 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Brian Norris8b6e50c2011-05-25 14:59:01 -07002623 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 col = 0;
2625 /* Increment page address */
2626 realpage++;
2627
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002628 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 /* Check, if we cross a chip boundary */
2630 if (!page) {
2631 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002632 chip->select_chip(mtd, -1);
2633 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002636 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002638 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002639 if (oob)
2640 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Mike Dunn3f91e942012-04-25 12:06:09 -07002642 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002643 return ret;
2644
Brian Norrisb72f3df2013-12-03 11:04:14 -08002645 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002646 return -EBADMSG;
2647
Mike Dunnedbc45402012-04-25 12:06:11 -07002648 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002649}
2650
2651/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002652 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002653 * @mtd: MTD device structure
2654 * @from: offset to read from
2655 * @len: number of bytes to read
2656 * @retlen: pointer to variable to store the number of read bytes
2657 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002658 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002659 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002660 */
2661static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2662 size_t *retlen, uint8_t *buf)
2663{
Brian Norris4a89ff82011-08-30 18:45:45 -07002664 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002665 int ret;
2666
Huang Shijie6a8214a2012-11-19 14:43:30 +08002667 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002668 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002669 ops.len = len;
2670 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002671 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002672 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002673 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002674 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002675 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
2678/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002679 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002680 * @mtd: mtd info structure
2681 * @chip: nand chip info structure
2682 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002683 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002684int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002685{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002686 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002687}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002688EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002689
2690/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002691 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002692 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002693 * @mtd: mtd info structure
2694 * @chip: nand chip info structure
2695 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002696 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002697int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2698 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002699{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002700 int length = mtd->oobsize;
2701 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2702 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002703 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002704 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002705
Boris Brezillon97d90da2017-11-30 18:01:29 +01002706 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
2707 if (ret)
2708 return ret;
2709
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002710 for (i = 0; i < chip->ecc.steps; i++) {
2711 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002712 int ret;
2713
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002714 pos = eccsize + i * (eccsize + chunk);
2715 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002716 ret = nand_change_read_column_op(chip, pos,
2717 NULL, 0,
2718 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002719 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01002720 ret = nand_read_page_op(chip, page, pos, NULL,
2721 0);
2722
2723 if (ret)
2724 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002725 } else
2726 sndrnd = 1;
2727 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002728
2729 ret = nand_read_data_op(chip, bufpoi, toread, false);
2730 if (ret)
2731 return ret;
2732
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002733 bufpoi += toread;
2734 length -= toread;
2735 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002736 if (length > 0) {
2737 ret = nand_read_data_op(chip, bufpoi, length, false);
2738 if (ret)
2739 return ret;
2740 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002741
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002742 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002743}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002744EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002745
2746/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002747 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002748 * @mtd: mtd info structure
2749 * @chip: nand chip info structure
2750 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002751 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002752int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002753{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002754 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
2755 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002756}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002757EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002758
2759/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002760 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002761 * with syndrome - only for large page flash
2762 * @mtd: mtd info structure
2763 * @chip: nand chip info structure
2764 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002765 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002766int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2767 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002768{
2769 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2770 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002771 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002772 const uint8_t *bufpoi = chip->oob_poi;
2773
2774 /*
2775 * data-ecc-data-ecc ... ecc-oob
2776 * or
2777 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2778 */
2779 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2780 pos = steps * (eccsize + chunk);
2781 steps = 0;
2782 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002783 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002784
Boris Brezillon97d90da2017-11-30 18:01:29 +01002785 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
2786 if (ret)
2787 return ret;
2788
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002789 for (i = 0; i < steps; i++) {
2790 if (sndcmd) {
2791 if (mtd->writesize <= 512) {
2792 uint32_t fill = 0xFFFFFFFF;
2793
2794 len = eccsize;
2795 while (len > 0) {
2796 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002797
2798 ret = nand_write_data_op(chip, &fill,
2799 num, false);
2800 if (ret)
2801 return ret;
2802
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002803 len -= num;
2804 }
2805 } else {
2806 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002807 ret = nand_change_write_column_op(chip, pos,
2808 NULL, 0,
2809 false);
2810 if (ret)
2811 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002812 }
2813 } else
2814 sndcmd = 1;
2815 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002816
2817 ret = nand_write_data_op(chip, bufpoi, len, false);
2818 if (ret)
2819 return ret;
2820
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002821 bufpoi += len;
2822 length -= len;
2823 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002824 if (length > 0) {
2825 ret = nand_write_data_op(chip, bufpoi, length, false);
2826 if (ret)
2827 return ret;
2828 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002829
Boris Brezillon97d90da2017-11-30 18:01:29 +01002830 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002831}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002832EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002833
2834/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002835 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002836 * @mtd: MTD device structure
2837 * @from: offset to read from
2838 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002840 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002842static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2843 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
Brian Norrisc00a0992012-05-01 17:12:54 -07002845 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002846 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002847 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002848 int readlen = ops->ooblen;
2849 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002850 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002851 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Brian Norris289c0522011-07-19 10:06:09 -07002853 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302854 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Brian Norris041e4572011-06-23 16:45:24 -07002856 stats = mtd->ecc_stats;
2857
Boris BREZILLON29f10582016-03-07 10:46:52 +01002858 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002859
2860 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002861 pr_debug("%s: attempt to start read outside oob\n",
2862 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002863 return -EINVAL;
2864 }
2865
2866 /* Do not allow reads past end of device */
2867 if (unlikely(from >= mtd->size ||
2868 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2869 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002870 pr_debug("%s: attempt to read beyond end of device\n",
2871 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002872 return -EINVAL;
2873 }
Vitaly Wool70145682006-11-03 18:20:38 +03002874
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002875 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002876 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002878 /* Shift to get page */
2879 realpage = (int)(from >> chip->page_shift);
2880 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Florian Fainellif8ac0412010-09-07 13:23:43 +02002882 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002883 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002884 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002885 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002886 ret = chip->ecc.read_oob(mtd, chip, page);
2887
2888 if (ret < 0)
2889 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002890
2891 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002892 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002893
Brian Norris5bc7c332013-03-13 09:51:31 -07002894 if (chip->options & NAND_NEED_READRDY) {
2895 /* Apply delay or wait for ready/busy pin */
2896 if (!chip->dev_ready)
2897 udelay(chip->chip_delay);
2898 else
2899 nand_wait_ready(mtd);
2900 }
2901
Vitaly Wool70145682006-11-03 18:20:38 +03002902 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002903 if (!readlen)
2904 break;
2905
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002906 /* Increment page address */
2907 realpage++;
2908
2909 page = realpage & chip->pagemask;
2910 /* Check, if we cross a chip boundary */
2911 if (!page) {
2912 chipnr++;
2913 chip->select_chip(mtd, -1);
2914 chip->select_chip(mtd, chipnr);
2915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002917 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002919 ops->oobretlen = ops->ooblen - readlen;
2920
2921 if (ret < 0)
2922 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002923
2924 if (mtd->ecc_stats.failed - stats.failed)
2925 return -EBADMSG;
2926
2927 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
2930/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002931 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002932 * @mtd: MTD device structure
2933 * @from: offset to read from
2934 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002936 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002938static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2939 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002941 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002942
2943 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002946 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002947 pr_debug("%s: attempt to read beyond end of device\n",
2948 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return -EINVAL;
2950 }
2951
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002952 if (ops->mode != MTD_OPS_PLACE_OOB &&
2953 ops->mode != MTD_OPS_AUTO_OOB &&
2954 ops->mode != MTD_OPS_RAW)
2955 return -ENOTSUPP;
2956
Huang Shijie6a8214a2012-11-19 14:43:30 +08002957 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002959 if (!ops->datbuf)
2960 ret = nand_do_read_oob(mtd, from, ops);
2961 else
2962 ret = nand_do_read_ops(mtd, from, ops);
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002965 return ret;
2966}
2967
2968
2969/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002970 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002971 * @mtd: mtd info structure
2972 * @chip: nand chip info structure
2973 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002974 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002975 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002976 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002977 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002978 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002979int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2980 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002981{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002982 int ret;
2983
Boris Brezillon25f815f2017-11-30 18:01:30 +01002984 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002985 if (ret)
2986 return ret;
2987
2988 if (oob_required) {
2989 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
2990 false);
2991 if (ret)
2992 return ret;
2993 }
Josh Wufdbad98d2012-06-25 18:07:45 +08002994
Boris Brezillon25f815f2017-11-30 18:01:30 +01002995 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002997EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002999/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003000 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003001 * @mtd: mtd info structure
3002 * @chip: nand chip info structure
3003 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003004 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003005 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003006 *
3007 * We need a special oob layout and handling even when ECC isn't checked.
3008 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003009static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003010 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003011 const uint8_t *buf, int oob_required,
3012 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003013{
3014 int eccsize = chip->ecc.size;
3015 int eccbytes = chip->ecc.bytes;
3016 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003017 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003018
Boris Brezillon25f815f2017-11-30 18:01:30 +01003019 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3020 if (ret)
3021 return ret;
3022
David Brownell52ff49d2009-03-04 12:01:36 -08003023 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003024 ret = nand_write_data_op(chip, buf, eccsize, false);
3025 if (ret)
3026 return ret;
3027
David Brownell52ff49d2009-03-04 12:01:36 -08003028 buf += eccsize;
3029
3030 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003031 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3032 false);
3033 if (ret)
3034 return ret;
3035
David Brownell52ff49d2009-03-04 12:01:36 -08003036 oob += chip->ecc.prepad;
3037 }
3038
Boris Brezillon97d90da2017-11-30 18:01:29 +01003039 ret = nand_write_data_op(chip, oob, eccbytes, false);
3040 if (ret)
3041 return ret;
3042
David Brownell52ff49d2009-03-04 12:01:36 -08003043 oob += eccbytes;
3044
3045 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003046 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3047 false);
3048 if (ret)
3049 return ret;
3050
David Brownell52ff49d2009-03-04 12:01:36 -08003051 oob += chip->ecc.postpad;
3052 }
3053 }
3054
3055 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003056 if (size) {
3057 ret = nand_write_data_op(chip, oob, size, false);
3058 if (ret)
3059 return ret;
3060 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003061
Boris Brezillon25f815f2017-11-30 18:01:30 +01003062 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003063}
3064/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003065 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003066 * @mtd: mtd info structure
3067 * @chip: nand chip info structure
3068 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003069 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003070 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003071 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003072static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003073 const uint8_t *buf, int oob_required,
3074 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003075{
Boris Brezillon846031d2016-02-03 20:11:00 +01003076 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003077 int eccbytes = chip->ecc.bytes;
3078 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003079 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003080 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003081
Brian Norris7854d3f2011-06-23 14:12:08 -07003082 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003083 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3084 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003085
Boris Brezillon846031d2016-02-03 20:11:00 +01003086 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3087 chip->ecc.total);
3088 if (ret)
3089 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003090
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003091 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003092}
3093
3094/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003095 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003096 * @mtd: mtd info structure
3097 * @chip: nand chip info structure
3098 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003099 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003100 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003101 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003102static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003103 const uint8_t *buf, int oob_required,
3104 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003105{
Boris Brezillon846031d2016-02-03 20:11:00 +01003106 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003107 int eccbytes = chip->ecc.bytes;
3108 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003109 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003110 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003111
Boris Brezillon25f815f2017-11-30 18:01:30 +01003112 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3113 if (ret)
3114 return ret;
3115
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003116 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3117 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003118
3119 ret = nand_write_data_op(chip, p, eccsize, false);
3120 if (ret)
3121 return ret;
3122
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003123 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3124 }
3125
Boris Brezillon846031d2016-02-03 20:11:00 +01003126 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3127 chip->ecc.total);
3128 if (ret)
3129 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003130
Boris Brezillon97d90da2017-11-30 18:01:29 +01003131 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3132 if (ret)
3133 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003134
Boris Brezillon25f815f2017-11-30 18:01:30 +01003135 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003136}
3137
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303138
3139/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08003140 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303141 * @mtd: mtd info structure
3142 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07003143 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303144 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07003145 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303146 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003147 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303148 */
3149static int nand_write_subpage_hwecc(struct mtd_info *mtd,
3150 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07003151 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003152 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303153{
3154 uint8_t *oob_buf = chip->oob_poi;
3155 uint8_t *ecc_calc = chip->buffers->ecccalc;
3156 int ecc_size = chip->ecc.size;
3157 int ecc_bytes = chip->ecc.bytes;
3158 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303159 uint32_t start_step = offset / ecc_size;
3160 uint32_t end_step = (offset + data_len - 1) / ecc_size;
3161 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01003162 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303163
Boris Brezillon25f815f2017-11-30 18:01:30 +01003164 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3165 if (ret)
3166 return ret;
3167
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303168 for (step = 0; step < ecc_steps; step++) {
3169 /* configure controller for WRITE access */
3170 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
3171
3172 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003173 ret = nand_write_data_op(chip, buf, ecc_size, false);
3174 if (ret)
3175 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303176
3177 /* mask ECC of un-touched subpages by padding 0xFF */
3178 if ((step < start_step) || (step > end_step))
3179 memset(ecc_calc, 0xff, ecc_bytes);
3180 else
Brian Norrisd6a950802013-08-08 17:16:36 -07003181 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303182
3183 /* mask OOB of un-touched subpages by padding 0xFF */
3184 /* if oob_required, preserve OOB metadata of written subpage */
3185 if (!oob_required || (step < start_step) || (step > end_step))
3186 memset(oob_buf, 0xff, oob_bytes);
3187
Brian Norrisd6a950802013-08-08 17:16:36 -07003188 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303189 ecc_calc += ecc_bytes;
3190 oob_buf += oob_bytes;
3191 }
3192
3193 /* copy calculated ECC for whole page to chip->buffer->oob */
3194 /* this include masked-value(0xFF) for unwritten subpages */
3195 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01003196 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
3197 chip->ecc.total);
3198 if (ret)
3199 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303200
3201 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003202 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3203 if (ret)
3204 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303205
Boris Brezillon25f815f2017-11-30 18:01:30 +01003206 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303207}
3208
3209
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003210/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003211 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003212 * @mtd: mtd info structure
3213 * @chip: nand chip info structure
3214 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003215 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003216 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003217 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003218 * The hw generator calculates the error syndrome automatically. Therefore we
3219 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003220 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003221static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003222 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003223 const uint8_t *buf, int oob_required,
3224 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003225{
3226 int i, eccsize = chip->ecc.size;
3227 int eccbytes = chip->ecc.bytes;
3228 int eccsteps = chip->ecc.steps;
3229 const uint8_t *p = buf;
3230 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003231 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003232
Boris Brezillon25f815f2017-11-30 18:01:30 +01003233 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3234 if (ret)
3235 return ret;
3236
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003237 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003238 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003239
3240 ret = nand_write_data_op(chip, p, eccsize, false);
3241 if (ret)
3242 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003243
3244 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003245 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3246 false);
3247 if (ret)
3248 return ret;
3249
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003250 oob += chip->ecc.prepad;
3251 }
3252
3253 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003254
3255 ret = nand_write_data_op(chip, oob, eccbytes, false);
3256 if (ret)
3257 return ret;
3258
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003259 oob += eccbytes;
3260
3261 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003262 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3263 false);
3264 if (ret)
3265 return ret;
3266
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003267 oob += chip->ecc.postpad;
3268 }
3269 }
3270
3271 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003272 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003273 if (i) {
3274 ret = nand_write_data_op(chip, oob, i, false);
3275 if (ret)
3276 return ret;
3277 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003278
Boris Brezillon25f815f2017-11-30 18:01:30 +01003279 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003280}
3281
3282/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003283 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07003284 * @mtd: MTD device structure
3285 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303286 * @offset: address offset within the page
3287 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07003288 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07003289 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07003290 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07003291 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003292 */
3293static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303294 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02003295 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003296{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303297 int status, subpage;
3298
3299 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3300 chip->ecc.write_subpage)
3301 subpage = offset || (data_len < mtd->writesize);
3302 else
3303 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003304
David Woodhouse956e9442006-09-25 17:12:39 +01003305 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303306 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003307 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303308 else if (subpage)
3309 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003310 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003311 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003312 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
3313 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08003314
3315 if (status < 0)
3316 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003317
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003318 return 0;
3319}
3320
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003321/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003322 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003323 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003324 * @oob: oob data buffer
3325 * @len: oob data write length
3326 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003327 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003328static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3329 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003330{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003331 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01003332 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003333
3334 /*
3335 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3336 * data from a previous OOB read.
3337 */
3338 memset(chip->oob_poi, 0xff, mtd->oobsize);
3339
Florian Fainellif8ac0412010-09-07 13:23:43 +02003340 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003341
Brian Norris0612b9d2011-08-30 18:45:40 -07003342 case MTD_OPS_PLACE_OOB:
3343 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003344 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3345 return oob + len;
3346
Boris Brezillon846031d2016-02-03 20:11:00 +01003347 case MTD_OPS_AUTO_OOB:
3348 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
3349 ops->ooboffs, len);
3350 BUG_ON(ret);
3351 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003352
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003353 default:
3354 BUG();
3355 }
3356 return NULL;
3357}
3358
Florian Fainellif8ac0412010-09-07 13:23:43 +02003359#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003360
3361/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003362 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003363 * @mtd: MTD device structure
3364 * @to: offset to write to
3365 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003366 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003367 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003368 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003369static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3370 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003371{
Corentin Labbe73600b62017-09-02 10:49:38 +02003372 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003373 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003374 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02003375
3376 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003377 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003378
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003379 uint8_t *oob = ops->oobbuf;
3380 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303381 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07003382 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003383
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003384 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003385 if (!writelen)
3386 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003387
Brian Norris8b6e50c2011-05-25 14:59:01 -07003388 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003389 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003390 pr_notice("%s: attempt to write non page aligned data\n",
3391 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003392 return -EINVAL;
3393 }
3394
Thomas Gleixner29072b92006-09-28 15:38:36 +02003395 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003396
Thomas Gleixner6a930962006-06-28 00:11:45 +02003397 chipnr = (int)(to >> chip->chip_shift);
3398 chip->select_chip(mtd, chipnr);
3399
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003400 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003401 if (nand_check_wp(mtd)) {
3402 ret = -EIO;
3403 goto err_out;
3404 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003405
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003406 realpage = (int)(to >> chip->page_shift);
3407 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003408
3409 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07003410 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3411 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003412 chip->pagebuf = -1;
3413
Maxim Levitsky782ce792010-02-22 20:39:36 +02003414 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003415 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3416 ret = -EINVAL;
3417 goto err_out;
3418 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02003419
Florian Fainellif8ac0412010-09-07 13:23:43 +02003420 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003421 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003422 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003423 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02003424 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02003425
Kamal Dasu66507c72014-05-01 20:51:19 -04003426 if (part_pagewr)
3427 use_bufpoi = 1;
3428 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003429 use_bufpoi = !virt_addr_valid(buf) ||
3430 !IS_ALIGNED((unsigned long)buf,
3431 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003432 else
3433 use_bufpoi = 0;
3434
3435 /* Partial page write?, or need to use bounce buffer */
3436 if (use_bufpoi) {
3437 pr_debug("%s: using write bounce buffer for buf@%p\n",
3438 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04003439 if (part_pagewr)
3440 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02003441 chip->pagebuf = -1;
3442 memset(chip->buffers->databuf, 0xff, mtd->writesize);
3443 memcpy(&chip->buffers->databuf[column], buf, bytes);
3444 wbuf = chip->buffers->databuf;
3445 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003446
Maxim Levitsky782ce792010-02-22 20:39:36 +02003447 if (unlikely(oob)) {
3448 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003449 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003450 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003451 } else {
3452 /* We still need to erase leftover OOB data */
3453 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02003454 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003455
3456 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02003457 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01003458 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003459 if (ret)
3460 break;
3461
3462 writelen -= bytes;
3463 if (!writelen)
3464 break;
3465
Thomas Gleixner29072b92006-09-28 15:38:36 +02003466 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003467 buf += bytes;
3468 realpage++;
3469
3470 page = realpage & chip->pagemask;
3471 /* Check, if we cross a chip boundary */
3472 if (!page) {
3473 chipnr++;
3474 chip->select_chip(mtd, -1);
3475 chip->select_chip(mtd, chipnr);
3476 }
3477 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003478
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003479 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03003480 if (unlikely(oob))
3481 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003482
3483err_out:
3484 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003485 return ret;
3486}
3487
3488/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003489 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003490 * @mtd: MTD device structure
3491 * @to: offset to write to
3492 * @len: number of bytes to write
3493 * @retlen: pointer to variable to store the number of written bytes
3494 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003495 *
3496 * NAND write with ECC. Used when performing writes in interrupt context, this
3497 * may for example be called by mtdoops when writing an oops while in panic.
3498 */
3499static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3500 size_t *retlen, const uint8_t *buf)
3501{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003502 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05003503 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07003504 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003505 int ret;
3506
Brian Norris8b6e50c2011-05-25 14:59:01 -07003507 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003508 panic_nand_get_device(chip, mtd, FL_WRITING);
3509
Brent Taylor30863e382017-10-30 22:32:45 -05003510 chip->select_chip(mtd, chipnr);
3511
3512 /* Wait for the device to get ready */
3513 panic_nand_wait(mtd, chip, 400);
3514
Brian Norris0ec56dc2015-02-28 02:02:30 -08003515 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07003516 ops.len = len;
3517 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08003518 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003519
Brian Norris4a89ff82011-08-30 18:45:45 -07003520 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003521
Brian Norris4a89ff82011-08-30 18:45:45 -07003522 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003523 return ret;
3524}
3525
3526/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003527 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003528 * @mtd: MTD device structure
3529 * @to: offset to write to
3530 * @len: number of bytes to write
3531 * @retlen: pointer to variable to store the number of written bytes
3532 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003534 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003536static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003537 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
Brian Norris4a89ff82011-08-30 18:45:45 -07003539 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003540 int ret;
3541
Huang Shijie6a8214a2012-11-19 14:43:30 +08003542 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08003543 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07003544 ops.len = len;
3545 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08003546 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07003547 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07003548 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003549 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003550 return ret;
3551}
3552
3553/**
3554 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003555 * @mtd: MTD device structure
3556 * @to: offset to write to
3557 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003558 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003559 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003560 */
3561static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3562 struct mtd_oob_ops *ops)
3563{
Adrian Hunter03736152007-01-31 17:58:29 +02003564 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003565 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Brian Norris289c0522011-07-19 10:06:09 -07003567 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303568 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Boris BREZILLON29f10582016-03-07 10:46:52 +01003570 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02003573 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07003574 pr_debug("%s: attempt to write past end of page\n",
3575 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 return -EINVAL;
3577 }
3578
Adrian Hunter03736152007-01-31 17:58:29 +02003579 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003580 pr_debug("%s: attempt to start write outside oob\n",
3581 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003582 return -EINVAL;
3583 }
3584
Jason Liu775adc3d42011-02-25 13:06:18 +08003585 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02003586 if (unlikely(to >= mtd->size ||
3587 ops->ooboffs + ops->ooblen >
3588 ((mtd->size >> chip->page_shift) -
3589 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003590 pr_debug("%s: attempt to write beyond end of device\n",
3591 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003592 return -EINVAL;
3593 }
3594
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003595 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003596
3597 /*
3598 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3599 * of my DiskOnChip 2000 test units) will clear the whole data page too
3600 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3601 * it in the doc2000 driver in August 1999. dwmw2.
3602 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003603 nand_reset(chip, chipnr);
3604
3605 chip->select_chip(mtd, chipnr);
3606
3607 /* Shift to get page */
3608 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003611 if (nand_check_wp(mtd)) {
3612 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003613 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003614 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003617 if (page == chip->pagebuf)
3618 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003620 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07003621
Brian Norris0612b9d2011-08-30 18:45:40 -07003622 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07003623 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3624 else
3625 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003626
Huang Shijieb0bb6902012-11-19 14:43:29 +08003627 chip->select_chip(mtd, -1);
3628
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003629 if (status)
3630 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Vitaly Wool70145682006-11-03 18:20:38 +03003632 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003634 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003635}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003637/**
3638 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003639 * @mtd: MTD device structure
3640 * @to: offset to write to
3641 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003642 */
3643static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3644 struct mtd_oob_ops *ops)
3645{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003646 int ret = -ENOTSUPP;
3647
3648 ops->retlen = 0;
3649
3650 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003651 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003652 pr_debug("%s: attempt to write beyond end of device\n",
3653 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003654 return -EINVAL;
3655 }
3656
Huang Shijie6a8214a2012-11-19 14:43:30 +08003657 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003658
Florian Fainellif8ac0412010-09-07 13:23:43 +02003659 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003660 case MTD_OPS_PLACE_OOB:
3661 case MTD_OPS_AUTO_OOB:
3662 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003663 break;
3664
3665 default:
3666 goto out;
3667 }
3668
3669 if (!ops->datbuf)
3670 ret = nand_do_write_oob(mtd, to, ops);
3671 else
3672 ret = nand_do_write_ops(mtd, to, ops);
3673
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003674out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003675 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return ret;
3677}
3678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679/**
Brian Norris49c50b92014-05-06 16:02:19 -07003680 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003681 * @mtd: MTD device structure
3682 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 *
Brian Norris49c50b92014-05-06 16:02:19 -07003684 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 */
Brian Norris49c50b92014-05-06 16:02:19 -07003686static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003688 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003689 unsigned int eraseblock;
Miquel Raynaleb945552017-11-30 18:01:28 +01003690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003692 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07003693
Boris Brezillon97d90da2017-11-30 18:01:29 +01003694 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695}
3696
3697/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003699 * @mtd: MTD device structure
3700 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003702 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003704static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
David Woodhousee0c7d762006-05-13 18:07:53 +01003706 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003710 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003711 * @mtd: MTD device structure
3712 * @instr: erase instruction
3713 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003715 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003717int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3718 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719{
Adrian Hunter69423d92008-12-10 13:37:21 +00003720 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003721 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003722 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Brian Norris289c0522011-07-19 10:06:09 -07003724 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3725 __func__, (unsigned long long)instr->addr,
3726 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303728 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003732 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
3734 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003735 page = (int)(instr->addr >> chip->page_shift);
3736 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
3738 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003739 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
3741 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003742 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 /* Check, if it is write protected */
3745 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003746 pr_debug("%s: device is write protected!\n",
3747 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 instr->state = MTD_ERASE_FAILED;
3749 goto erase_exit;
3750 }
3751
3752 /* Loop through the pages */
3753 len = instr->len;
3754
3755 instr->state = MTD_ERASING;
3756
3757 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003758 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003759 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303760 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003761 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3762 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 instr->state = MTD_ERASE_FAILED;
3764 goto erase_exit;
3765 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003766
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003767 /*
3768 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003769 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003770 */
3771 if (page <= chip->pagebuf && chip->pagebuf <
3772 (page + pages_per_block))
3773 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Brian Norris49c50b92014-05-06 16:02:19 -07003775 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01003778 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07003779 pr_debug("%s: failed erase, page 0x%08x\n",
3780 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003782 instr->fail_addr =
3783 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 goto erase_exit;
3785 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003788 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 page += pages_per_block;
3790
3791 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003792 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003794 chip->select_chip(mtd, -1);
3795 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 }
3797 }
3798 instr->state = MTD_ERASE_DONE;
3799
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003800erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
3804 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003805 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 nand_release_device(mtd);
3807
David Woodhouse49defc02007-10-06 15:01:59 -04003808 /* Do call back function */
3809 if (!ret)
3810 mtd_erase_callback(instr);
3811
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 /* Return more or less happy */
3813 return ret;
3814}
3815
3816/**
3817 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003818 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003820 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003822static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823{
Brian Norris289c0522011-07-19 10:06:09 -07003824 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
3826 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003827 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003829 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830}
3831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003833 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003834 * @mtd: MTD device structure
3835 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003837static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303839 struct nand_chip *chip = mtd_to_nand(mtd);
3840 int chipnr = (int)(offs >> chip->chip_shift);
3841 int ret;
3842
3843 /* Select the NAND device */
3844 nand_get_device(mtd, FL_READING);
3845 chip->select_chip(mtd, chipnr);
3846
3847 ret = nand_block_checkbad(mtd, offs, 0);
3848
3849 chip->select_chip(mtd, -1);
3850 nand_release_device(mtd);
3851
3852 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853}
3854
3855/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003856 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003857 * @mtd: MTD device structure
3858 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003860static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 int ret;
3863
Florian Fainellif8ac0412010-09-07 13:23:43 +02003864 ret = nand_block_isbad(mtd, ofs);
3865 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003866 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 if (ret > 0)
3868 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003869 return ret;
3870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
Brian Norris5a0edb22013-07-30 17:52:58 -07003872 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873}
3874
3875/**
Zach Brown56718422017-01-10 13:30:20 -06003876 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3877 * @mtd: MTD device structure
3878 * @ofs: offset relative to mtd start
3879 * @len: length of mtd
3880 */
3881static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3882{
3883 struct nand_chip *chip = mtd_to_nand(mtd);
3884 u32 part_start_block;
3885 u32 part_end_block;
3886 u32 part_start_die;
3887 u32 part_end_die;
3888
3889 /*
3890 * max_bb_per_die and blocks_per_die used to determine
3891 * the maximum bad block count.
3892 */
3893 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3894 return -ENOTSUPP;
3895
3896 /* Get the start and end of the partition in erase blocks. */
3897 part_start_block = mtd_div_by_eb(ofs, mtd);
3898 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3899
3900 /* Get the start and end LUNs of the partition. */
3901 part_start_die = part_start_block / chip->blocks_per_die;
3902 part_end_die = part_end_block / chip->blocks_per_die;
3903
3904 /*
3905 * Look up the bad blocks per unit and multiply by the number of units
3906 * that the partition spans.
3907 */
3908 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3909}
3910
3911/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003912 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3913 * @mtd: MTD device structure
3914 * @chip: nand chip info structure
3915 * @addr: feature address.
3916 * @subfeature_param: the subfeature parameters, a four bytes array.
3917 */
3918static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3919 int addr, uint8_t *subfeature_param)
3920{
David Mosbergerd914c932013-05-29 15:30:13 +03003921 if (!chip->onfi_version ||
3922 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3923 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003924 return -EINVAL;
3925
Boris Brezillon97d90da2017-11-30 18:01:29 +01003926 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003927}
3928
3929/**
3930 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3931 * @mtd: MTD device structure
3932 * @chip: nand chip info structure
3933 * @addr: feature address.
3934 * @subfeature_param: the subfeature parameters, a four bytes array.
3935 */
3936static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3937 int addr, uint8_t *subfeature_param)
3938{
David Mosbergerd914c932013-05-29 15:30:13 +03003939 if (!chip->onfi_version ||
3940 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3941 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003942 return -EINVAL;
3943
Boris Brezillon97d90da2017-11-30 18:01:29 +01003944 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003945}
3946
3947/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003948 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3949 * -ENOTSUPP
3950 * @mtd: MTD device structure
3951 * @chip: nand chip info structure
3952 * @addr: feature address.
3953 * @subfeature_param: the subfeature parameters, a four bytes array.
3954 *
3955 * Should be used by NAND controller drivers that do not support the SET/GET
3956 * FEATURES operations.
3957 */
3958int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3959 struct nand_chip *chip, int addr,
3960 u8 *subfeature_param)
3961{
3962 return -ENOTSUPP;
3963}
3964EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3965
3966/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003967 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003968 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003969 */
3970static int nand_suspend(struct mtd_info *mtd)
3971{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003972 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003973}
3974
3975/**
3976 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003977 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003978 */
3979static void nand_resume(struct mtd_info *mtd)
3980{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003981 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003982
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003983 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003984 nand_release_device(mtd);
3985 else
Brian Norrisd0370212011-07-19 10:06:08 -07003986 pr_err("%s called for a chip which is not in suspended state\n",
3987 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003988}
3989
Scott Branden72ea4032014-11-20 11:18:05 -08003990/**
3991 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3992 * prevent further operations
3993 * @mtd: MTD device structure
3994 */
3995static void nand_shutdown(struct mtd_info *mtd)
3996{
Brian Norris9ca641b2015-11-09 16:37:28 -08003997 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003998}
3999
Brian Norris8b6e50c2011-05-25 14:59:01 -07004000/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004001static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004002{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004003 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4004
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004006 if (!chip->chip_delay)
4007 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
4009 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004010 if (chip->cmdfunc == NULL)
4011 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
4013 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004014 if (chip->waitfunc == NULL)
4015 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004017 if (!chip->select_chip)
4018 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004019
Huang Shijie4204ccc2013-08-16 10:10:07 +08004020 /* set for ONFI nand */
4021 if (!chip->onfi_set_features)
4022 chip->onfi_set_features = nand_onfi_set_features;
4023 if (!chip->onfi_get_features)
4024 chip->onfi_get_features = nand_onfi_get_features;
4025
Brian Norris68e80782013-07-18 01:17:02 -07004026 /* If called twice, pointers that depend on busw may need to be reset */
4027 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004028 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4029 if (!chip->read_word)
4030 chip->read_word = nand_read_word;
4031 if (!chip->block_bad)
4032 chip->block_bad = nand_block_bad;
4033 if (!chip->block_markbad)
4034 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004035 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004036 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004037 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4038 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004039 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004040 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004041 if (!chip->scan_bbt)
4042 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004043
4044 if (!chip->controller) {
4045 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02004046 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004047 }
4048
Masahiro Yamada477544c2017-03-30 17:15:05 +09004049 if (!chip->buf_align)
4050 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004051}
4052
Brian Norris8b6e50c2011-05-25 14:59:01 -07004053/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004054static void sanitize_string(uint8_t *s, size_t len)
4055{
4056 ssize_t i;
4057
Brian Norris8b6e50c2011-05-25 14:59:01 -07004058 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004059 s[len - 1] = 0;
4060
Brian Norris8b6e50c2011-05-25 14:59:01 -07004061 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004062 for (i = 0; i < len - 1; i++) {
4063 if (s[i] < ' ' || s[i] > 127)
4064 s[i] = '?';
4065 }
4066
Brian Norris8b6e50c2011-05-25 14:59:01 -07004067 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004068 strim(s);
4069}
4070
4071static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4072{
4073 int i;
4074 while (len--) {
4075 crc ^= *p++ << 8;
4076 for (i = 0; i < 8; i++)
4077 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
4078 }
4079
4080 return crc;
4081}
4082
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004083/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004084static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
4085 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004086{
4087 struct onfi_ext_param_page *ep;
4088 struct onfi_ext_section *s;
4089 struct onfi_ext_ecc_info *ecc;
4090 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004091 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004092 int len;
4093 int i;
4094
4095 len = le16_to_cpu(p->ext_param_page_length) * 16;
4096 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07004097 if (!ep)
4098 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004099
4100 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004101 ret = nand_read_param_page_op(chip, 0, NULL, 0);
4102 if (ret)
4103 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004104
4105 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004106 ret = nand_change_read_column_op(chip,
4107 sizeof(*p) * p->num_of_param_pages,
4108 ep, len, true);
4109 if (ret)
4110 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004111
Boris Brezillon97d90da2017-11-30 18:01:29 +01004112 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004113 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
4114 != le16_to_cpu(ep->crc))) {
4115 pr_debug("fail in the CRC.\n");
4116 goto ext_out;
4117 }
4118
4119 /*
4120 * Check the signature.
4121 * Do not strictly follow the ONFI spec, maybe changed in future.
4122 */
4123 if (strncmp(ep->sig, "EPPS", 4)) {
4124 pr_debug("The signature is invalid.\n");
4125 goto ext_out;
4126 }
4127
4128 /* find the ECC section. */
4129 cursor = (uint8_t *)(ep + 1);
4130 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
4131 s = ep->sections + i;
4132 if (s->type == ONFI_SECTION_TYPE_2)
4133 break;
4134 cursor += s->length * 16;
4135 }
4136 if (i == ONFI_EXT_SECTION_MAX) {
4137 pr_debug("We can not find the ECC section.\n");
4138 goto ext_out;
4139 }
4140
4141 /* get the info we want. */
4142 ecc = (struct onfi_ext_ecc_info *)cursor;
4143
Brian Norris4ae7d222013-09-16 18:20:21 -07004144 if (!ecc->codeword_size) {
4145 pr_debug("Invalid codeword size\n");
4146 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004147 }
4148
Brian Norris4ae7d222013-09-16 18:20:21 -07004149 chip->ecc_strength_ds = ecc->ecc_bits;
4150 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07004151 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004152
4153ext_out:
4154 kfree(ep);
4155 return ret;
4156}
4157
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004158/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004159 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004160 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004161static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004162{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004163 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004164 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004165 char id[4];
4166 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004167
Brian Norris7854d3f2011-06-23 14:12:08 -07004168 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004169 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
4170 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004171 return 0;
4172
Boris Brezillon97d90da2017-11-30 18:01:29 +01004173 ret = nand_read_param_page_op(chip, 0, NULL, 0);
4174 if (ret)
4175 return 0;
4176
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004177 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004178 ret = nand_read_data_op(chip, p, sizeof(*p), true);
4179 if (ret)
4180 return 0;
4181
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004182 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
4183 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004184 break;
4185 }
4186 }
4187
Brian Norrisc7f23a72013-08-13 10:51:55 -07004188 if (i == 3) {
4189 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004190 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07004191 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004192
Brian Norris8b6e50c2011-05-25 14:59:01 -07004193 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004194 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08004195 if (val & (1 << 5))
4196 chip->onfi_version = 23;
4197 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004198 chip->onfi_version = 22;
4199 else if (val & (1 << 3))
4200 chip->onfi_version = 21;
4201 else if (val & (1 << 2))
4202 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08004203 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004204 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08004205
4206 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004207 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08004208 return 0;
4209 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004210
4211 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4212 sanitize_string(p->model, sizeof(p->model));
4213 if (!mtd->name)
4214 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07004215
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004216 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07004217
4218 /*
4219 * pages_per_block and blocks_per_lun may not be a power-of-2 size
4220 * (don't ask me who thought of this...). MTD assumes that these
4221 * dimensions will be power-of-2, so just truncate the remaining area.
4222 */
4223 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4224 mtd->erasesize *= mtd->writesize;
4225
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004226 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07004227
4228 /* See erasesize comment */
4229 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01004230 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08004231 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08004232
Zach Brown34da5f52017-01-10 13:30:21 -06004233 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
4234 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
4235
Huang Shijiee2985fc2013-05-17 11:17:30 +08004236 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02004237 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004238
Huang Shijie10c86ba2013-05-17 11:17:26 +08004239 if (p->ecc_bits != 0xff) {
4240 chip->ecc_strength_ds = p->ecc_bits;
4241 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004242 } else if (chip->onfi_version >= 21 &&
4243 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
4244
4245 /*
4246 * The nand_flash_detect_ext_param_page() uses the
4247 * Change Read Column command which maybe not supported
4248 * by the chip->cmdfunc. So try to update the chip->cmdfunc
4249 * now. We do not replace user supplied command function.
4250 */
4251 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4252 chip->cmdfunc = nand_command_lp;
4253
4254 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004255 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07004256 pr_warn("Failed to detect ONFI extended param page\n");
4257 } else {
4258 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08004259 }
4260
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004261 return 1;
4262}
4263
4264/*
Huang Shijie91361812014-02-21 13:39:40 +08004265 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
4266 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004267static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08004268{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004269 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08004270 struct nand_jedec_params *p = &chip->jedec_params;
4271 struct jedec_ecc_info *ecc;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004272 char id[5];
4273 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08004274
4275 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004276 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
4277 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08004278 return 0;
4279
Boris Brezillon97d90da2017-11-30 18:01:29 +01004280 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
4281 if (ret)
4282 return 0;
4283
Huang Shijie91361812014-02-21 13:39:40 +08004284 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004285 ret = nand_read_data_op(chip, p, sizeof(*p), true);
4286 if (ret)
4287 return 0;
Huang Shijie91361812014-02-21 13:39:40 +08004288
4289 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
4290 le16_to_cpu(p->crc))
4291 break;
4292 }
4293
4294 if (i == 3) {
4295 pr_err("Could not find valid JEDEC parameter page; aborting\n");
4296 return 0;
4297 }
4298
4299 /* Check version */
4300 val = le16_to_cpu(p->revision);
4301 if (val & (1 << 2))
4302 chip->jedec_version = 10;
4303 else if (val & (1 << 1))
4304 chip->jedec_version = 1; /* vendor specific version */
4305
4306 if (!chip->jedec_version) {
4307 pr_info("unsupported JEDEC version: %d\n", val);
4308 return 0;
4309 }
4310
4311 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4312 sanitize_string(p->model, sizeof(p->model));
4313 if (!mtd->name)
4314 mtd->name = p->model;
4315
4316 mtd->writesize = le32_to_cpu(p->byte_per_page);
4317
4318 /* Please reference to the comment for nand_flash_detect_onfi. */
4319 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4320 mtd->erasesize *= mtd->writesize;
4321
4322 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4323
4324 /* Please reference to the comment for nand_flash_detect_onfi. */
4325 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4326 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4327 chip->bits_per_cell = p->bits_per_cell;
4328
4329 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02004330 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08004331
4332 /* ECC info */
4333 ecc = &p->ecc_info[0];
4334
4335 if (ecc->codeword_size >= 9) {
4336 chip->ecc_strength_ds = ecc->ecc_bits;
4337 chip->ecc_step_ds = 1 << ecc->codeword_size;
4338 } else {
4339 pr_warn("Invalid codeword size\n");
4340 }
4341
4342 return 1;
4343}
4344
4345/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07004346 * nand_id_has_period - Check if an ID string has a given wraparound period
4347 * @id_data: the ID string
4348 * @arrlen: the length of the @id_data array
4349 * @period: the period of repitition
4350 *
4351 * Check if an ID string is repeated within a given sequence of bytes at
4352 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08004353 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07004354 * if the repetition has a period of @period; otherwise, returns zero.
4355 */
4356static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4357{
4358 int i, j;
4359 for (i = 0; i < period; i++)
4360 for (j = i + period; j < arrlen; j += period)
4361 if (id_data[i] != id_data[j])
4362 return 0;
4363 return 1;
4364}
4365
4366/*
4367 * nand_id_len - Get the length of an ID string returned by CMD_READID
4368 * @id_data: the ID string
4369 * @arrlen: the length of the @id_data array
4370
4371 * Returns the length of the ID string, according to known wraparound/trailing
4372 * zero patterns. If no pattern exists, returns the length of the array.
4373 */
4374static int nand_id_len(u8 *id_data, int arrlen)
4375{
4376 int last_nonzero, period;
4377
4378 /* Find last non-zero byte */
4379 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4380 if (id_data[last_nonzero])
4381 break;
4382
4383 /* All zeros */
4384 if (last_nonzero < 0)
4385 return 0;
4386
4387 /* Calculate wraparound period */
4388 for (period = 1; period < arrlen; period++)
4389 if (nand_id_has_period(id_data, arrlen, period))
4390 break;
4391
4392 /* There's a repeated pattern */
4393 if (period < arrlen)
4394 return period;
4395
4396 /* There are trailing zeros */
4397 if (last_nonzero < arrlen - 1)
4398 return last_nonzero + 1;
4399
4400 /* No pattern detected */
4401 return arrlen;
4402}
4403
Huang Shijie7db906b2013-09-25 14:58:11 +08004404/* Extract the bits of per cell from the 3rd byte of the extended ID */
4405static int nand_get_bits_per_cell(u8 cellinfo)
4406{
4407 int bits;
4408
4409 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4410 bits >>= NAND_CI_CELLTYPE_SHIFT;
4411 return bits + 1;
4412}
4413
Brian Norrise3b88bd2012-09-24 20:40:52 -07004414/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004415 * Many new NAND share similar device ID codes, which represent the size of the
4416 * chip. The rest of the parameters must be decoded according to generic or
4417 * manufacturer-specific "extended ID" decoding patterns.
4418 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004419void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004420{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004421 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02004422 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004423 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004424 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08004425 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004426 /* The 4th id byte is the important one */
4427 extid = id_data[3];
4428
Boris Brezillon01389b62016-06-08 10:30:18 +02004429 /* Calc pagesize */
4430 mtd->writesize = 1024 << (extid & 0x03);
4431 extid >>= 2;
4432 /* Calc oobsize */
4433 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
4434 extid >>= 2;
4435 /* Calc blocksize. Blocksize is multiples of 64KiB */
4436 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4437 extid >>= 2;
4438 /* Get buswidth information */
4439 if (extid & 0x1)
4440 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004441}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004442EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004443
4444/*
Brian Norrisf23a4812012-09-24 20:40:51 -07004445 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4446 * decodes a matching ID table entry and assigns the MTD size parameters for
4447 * the chip.
4448 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004449static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07004450{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004451 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07004452
4453 mtd->erasesize = type->erasesize;
4454 mtd->writesize = type->pagesize;
4455 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07004456
Huang Shijie1c195e92013-09-25 14:58:12 +08004457 /* All legacy ID NAND are small-page, SLC */
4458 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07004459}
4460
4461/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004462 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4463 * heuristic patterns using various detected parameters (e.g., manufacturer,
4464 * page size, cell-type information).
4465 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004466static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07004467{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004468 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004469
4470 /* Set the bad block position */
4471 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4472 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4473 else
4474 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07004475}
4476
Huang Shijieec6e87e2013-03-15 11:01:00 +08004477static inline bool is_full_id_nand(struct nand_flash_dev *type)
4478{
4479 return type->id_len;
4480}
4481
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004482static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02004483 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004484{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004485 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02004486 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004487
Huang Shijieec6e87e2013-03-15 11:01:00 +08004488 if (!strncmp(type->id, id_data, type->id_len)) {
4489 mtd->writesize = type->pagesize;
4490 mtd->erasesize = type->erasesize;
4491 mtd->oobsize = type->oobsize;
4492
Huang Shijie7db906b2013-09-25 14:58:11 +08004493 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004494 chip->chipsize = (uint64_t)type->chipsize << 20;
4495 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004496 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4497 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004498 chip->onfi_timing_mode_default =
4499 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004500
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004501 if (!mtd->name)
4502 mtd->name = type->name;
4503
Huang Shijieec6e87e2013-03-15 11:01:00 +08004504 return true;
4505 }
4506 return false;
4507}
4508
Brian Norris7e74c2d2012-09-24 20:40:49 -07004509/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004510 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4511 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4512 * table.
4513 */
4514static void nand_manufacturer_detect(struct nand_chip *chip)
4515{
4516 /*
4517 * Try manufacturer detection if available and use
4518 * nand_decode_ext_id() otherwise.
4519 */
4520 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004521 chip->manufacturer.desc->ops->detect) {
4522 /* The 3rd id byte holds MLC / multichip data */
4523 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004524 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004525 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004526 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02004527 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004528}
4529
4530/*
4531 * Manufacturer initialization. This function is called for all NANDs including
4532 * ONFI and JEDEC compliant ones.
4533 * Manufacturer drivers should put all their specific initialization code in
4534 * their ->init() hook.
4535 */
4536static int nand_manufacturer_init(struct nand_chip *chip)
4537{
4538 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4539 !chip->manufacturer.desc->ops->init)
4540 return 0;
4541
4542 return chip->manufacturer.desc->ops->init(chip);
4543}
4544
4545/*
4546 * Manufacturer cleanup. This function is called for all NANDs including
4547 * ONFI and JEDEC compliant ones.
4548 * Manufacturer drivers should put all their specific cleanup code in their
4549 * ->cleanup() hook.
4550 */
4551static void nand_manufacturer_cleanup(struct nand_chip *chip)
4552{
4553 /* Release manufacturer private data */
4554 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4555 chip->manufacturer.desc->ops->cleanup)
4556 chip->manufacturer.desc->ops->cleanup(chip);
4557}
4558
4559/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004560 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004561 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004562static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004563{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004564 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004565 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004566 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004567 u8 *id_data = chip->id.data;
4568 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Karl Beldanef89a882008-09-15 14:37:29 +02004570 /*
4571 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004572 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004573 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004574 ret = nand_reset(chip, 0);
4575 if (ret)
4576 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004577
4578 /* Select the device */
4579 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004582 ret = nand_readid_op(chip, 0, id_data, 2);
4583 if (ret)
4584 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
4586 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004587 maf_id = id_data[0];
4588 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Brian Norris8b6e50c2011-05-25 14:59:01 -07004590 /*
4591 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004592 * interface concerns can cause random data which looks like a
4593 * possibly credible NAND flash to appear. If the two results do
4594 * not match, ignore the device completely.
4595 */
4596
Brian Norris4aef9b72012-09-24 20:40:48 -07004597 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004598 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
4599 if (ret)
4600 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01004601
Boris Brezillon7f501f02016-05-24 19:20:05 +02004602 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004603 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004604 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004605 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004606 }
4607
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02004608 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02004609
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004610 /* Try to identify manufacturer */
4611 manufacturer = nand_get_manufacturer(maf_id);
4612 chip->manufacturer.desc = manufacturer;
4613
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004614 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004615 type = nand_flash_ids;
4616
Boris Brezillon29a198a2016-05-24 20:17:48 +02004617 /*
4618 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4619 * override it.
4620 * This is required to make sure initial NAND bus width set by the
4621 * NAND controller driver is coherent with the real NAND bus width
4622 * (extracted by auto-detection code).
4623 */
4624 busw = chip->options & NAND_BUSWIDTH_16;
4625
4626 /*
4627 * The flag is only set (never cleared), reset it to its default value
4628 * before starting auto-detection.
4629 */
4630 chip->options &= ~NAND_BUSWIDTH_16;
4631
Huang Shijieec6e87e2013-03-15 11:01:00 +08004632 for (; type->name != NULL; type++) {
4633 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004634 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004635 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004636 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004637 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004638 }
4639 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004640
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004641 chip->onfi_version = 0;
4642 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004643 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004644 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004645 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004646
4647 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004648 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08004649 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004650 }
4651
David Woodhouse5e81e882010-02-26 18:32:56 +00004652 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004653 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004654
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004655 if (!mtd->name)
4656 mtd->name = type->name;
4657
Adrian Hunter69423d92008-12-10 13:37:21 +00004658 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004659
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004660 if (!type->pagesize)
4661 nand_manufacturer_detect(chip);
4662 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004663 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004664
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004665 /* Get chip options */
4666 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004667
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004668ident_done:
4669
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004670 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004671 WARN_ON(busw & NAND_BUSWIDTH_16);
4672 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004673 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4674 /*
4675 * Check, if buswidth is correct. Hardware drivers should set
4676 * chip correct!
4677 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004678 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004679 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004680 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4681 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004682 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4683 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004684 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004685 }
4686
Boris Brezillon7f501f02016-05-24 19:20:05 +02004687 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004688
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004689 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004690 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004691 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004692 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004693
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004694 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004695 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004696 if (chip->chipsize & 0xffffffff)
4697 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004698 else {
4699 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4700 chip->chip_shift += 32 - 1;
4701 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004702
Masahiro Yamada14157f82017-09-13 11:05:50 +09004703 if (chip->chip_shift - chip->page_shift > 16)
4704 chip->options |= NAND_ROW_ADDR_3;
4705
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004706 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004707 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004708
Brian Norris8b6e50c2011-05-25 14:59:01 -07004709 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004710 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4711 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004712
Ezequiel Garcia20171642013-11-25 08:30:31 -03004713 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004714 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004715
4716 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004717 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4718 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004719 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004720 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4721 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004722 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004723 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4724 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004725
Rafał Miłecki3755a992014-10-21 00:01:04 +02004726 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004727 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004728 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004729 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004730}
4731
Boris Brezillond48f62b2016-04-01 14:54:32 +02004732static const char * const nand_ecc_modes[] = {
4733 [NAND_ECC_NONE] = "none",
4734 [NAND_ECC_SOFT] = "soft",
4735 [NAND_ECC_HW] = "hw",
4736 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4737 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004738 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004739};
4740
4741static int of_get_nand_ecc_mode(struct device_node *np)
4742{
4743 const char *pm;
4744 int err, i;
4745
4746 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4747 if (err < 0)
4748 return err;
4749
4750 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4751 if (!strcasecmp(pm, nand_ecc_modes[i]))
4752 return i;
4753
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004754 /*
4755 * For backward compatibility we support few obsoleted values that don't
4756 * have their mappings into nand_ecc_modes_t anymore (they were merged
4757 * with other enums).
4758 */
4759 if (!strcasecmp(pm, "soft_bch"))
4760 return NAND_ECC_SOFT;
4761
Boris Brezillond48f62b2016-04-01 14:54:32 +02004762 return -ENODEV;
4763}
4764
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004765static const char * const nand_ecc_algos[] = {
4766 [NAND_ECC_HAMMING] = "hamming",
4767 [NAND_ECC_BCH] = "bch",
4768};
4769
Boris Brezillond48f62b2016-04-01 14:54:32 +02004770static int of_get_nand_ecc_algo(struct device_node *np)
4771{
4772 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004773 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004774
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004775 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4776 if (!err) {
4777 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4778 if (!strcasecmp(pm, nand_ecc_algos[i]))
4779 return i;
4780 return -ENODEV;
4781 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004782
4783 /*
4784 * For backward compatibility we also read "nand-ecc-mode" checking
4785 * for some obsoleted values that were specifying ECC algorithm.
4786 */
4787 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4788 if (err < 0)
4789 return err;
4790
4791 if (!strcasecmp(pm, "soft"))
4792 return NAND_ECC_HAMMING;
4793 else if (!strcasecmp(pm, "soft_bch"))
4794 return NAND_ECC_BCH;
4795
4796 return -ENODEV;
4797}
4798
4799static int of_get_nand_ecc_step_size(struct device_node *np)
4800{
4801 int ret;
4802 u32 val;
4803
4804 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4805 return ret ? ret : val;
4806}
4807
4808static int of_get_nand_ecc_strength(struct device_node *np)
4809{
4810 int ret;
4811 u32 val;
4812
4813 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4814 return ret ? ret : val;
4815}
4816
4817static int of_get_nand_bus_width(struct device_node *np)
4818{
4819 u32 val;
4820
4821 if (of_property_read_u32(np, "nand-bus-width", &val))
4822 return 8;
4823
4824 switch (val) {
4825 case 8:
4826 case 16:
4827 return val;
4828 default:
4829 return -EIO;
4830 }
4831}
4832
4833static bool of_get_nand_on_flash_bbt(struct device_node *np)
4834{
4835 return of_property_read_bool(np, "nand-on-flash-bbt");
4836}
4837
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004838static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004839{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004840 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004841 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004842
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004843 if (!dn)
4844 return 0;
4845
Brian Norris5844fee2015-01-23 00:22:27 -08004846 if (of_get_nand_bus_width(dn) == 16)
4847 chip->options |= NAND_BUSWIDTH_16;
4848
4849 if (of_get_nand_on_flash_bbt(dn))
4850 chip->bbt_options |= NAND_BBT_USE_FLASH;
4851
4852 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004853 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004854 ecc_strength = of_get_nand_ecc_strength(dn);
4855 ecc_step = of_get_nand_ecc_step_size(dn);
4856
Brian Norris5844fee2015-01-23 00:22:27 -08004857 if (ecc_mode >= 0)
4858 chip->ecc.mode = ecc_mode;
4859
Rafał Miłecki79082452016-03-23 11:19:02 +01004860 if (ecc_algo >= 0)
4861 chip->ecc.algo = ecc_algo;
4862
Brian Norris5844fee2015-01-23 00:22:27 -08004863 if (ecc_strength >= 0)
4864 chip->ecc.strength = ecc_strength;
4865
4866 if (ecc_step > 0)
4867 chip->ecc.size = ecc_step;
4868
Boris Brezillonba78ee02016-06-08 17:04:22 +02004869 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4870 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4871
Brian Norris5844fee2015-01-23 00:22:27 -08004872 return 0;
4873}
4874
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004875/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004876 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004877 * @mtd: MTD device structure
4878 * @maxchips: number of chips to scan for
4879 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004880 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004881 * This is the first phase of the normal nand_scan() function. It reads the
4882 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004883 *
4884 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004885int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4886 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004887{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004888 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004889 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004890 int ret;
4891
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004892 ret = nand_dt_init(chip);
4893 if (ret)
4894 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004895
Brian Norrisf7a8e382016-01-05 10:39:45 -08004896 if (!mtd->name && mtd->dev.parent)
4897 mtd->name = dev_name(mtd->dev.parent);
4898
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004899 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4900 /*
4901 * Default functions assigned for chip_select() and
4902 * cmdfunc() both expect cmd_ctrl() to be populated,
4903 * so we need to check that that's the case
4904 */
4905 pr_err("chip.cmd_ctrl() callback is not provided");
4906 return -EINVAL;
4907 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004908 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004909 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004910
4911 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004912 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004913 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004914 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004915 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004916 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004917 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 }
4919
Boris Brezillon7f501f02016-05-24 19:20:05 +02004920 nand_maf_id = chip->id.data[0];
4921 nand_dev_id = chip->id.data[1];
4922
Huang Shijie07300162012-11-09 16:23:45 +08004923 chip->select_chip(mtd, -1);
4924
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004925 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004926 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004927 u8 id[2];
4928
Karl Beldanef89a882008-09-15 14:37:29 +02004929 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004930 nand_reset(chip, i);
4931
4932 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004934 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004936 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08004937 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 break;
Huang Shijie07300162012-11-09 16:23:45 +08004939 }
4940 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 }
4942 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004943 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004946 chip->numchips = i;
4947 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
David Woodhouse3b85c322006-09-25 17:06:53 +01004949 return 0;
4950}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004951EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004952
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004953static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4954{
4955 struct nand_chip *chip = mtd_to_nand(mtd);
4956 struct nand_ecc_ctrl *ecc = &chip->ecc;
4957
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004958 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004959 return -EINVAL;
4960
4961 switch (ecc->algo) {
4962 case NAND_ECC_HAMMING:
4963 ecc->calculate = nand_calculate_ecc;
4964 ecc->correct = nand_correct_data;
4965 ecc->read_page = nand_read_page_swecc;
4966 ecc->read_subpage = nand_read_subpage;
4967 ecc->write_page = nand_write_page_swecc;
4968 ecc->read_page_raw = nand_read_page_raw;
4969 ecc->write_page_raw = nand_write_page_raw;
4970 ecc->read_oob = nand_read_oob_std;
4971 ecc->write_oob = nand_write_oob_std;
4972 if (!ecc->size)
4973 ecc->size = 256;
4974 ecc->bytes = 3;
4975 ecc->strength = 1;
4976 return 0;
4977 case NAND_ECC_BCH:
4978 if (!mtd_nand_has_bch()) {
4979 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4980 return -EINVAL;
4981 }
4982 ecc->calculate = nand_bch_calculate_ecc;
4983 ecc->correct = nand_bch_correct_data;
4984 ecc->read_page = nand_read_page_swecc;
4985 ecc->read_subpage = nand_read_subpage;
4986 ecc->write_page = nand_write_page_swecc;
4987 ecc->read_page_raw = nand_read_page_raw;
4988 ecc->write_page_raw = nand_write_page_raw;
4989 ecc->read_oob = nand_read_oob_std;
4990 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004991
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004992 /*
4993 * Board driver should supply ecc.size and ecc.strength
4994 * values to select how many bits are correctable.
4995 * Otherwise, default to 4 bits for large page devices.
4996 */
4997 if (!ecc->size && (mtd->oobsize >= 64)) {
4998 ecc->size = 512;
4999 ecc->strength = 4;
5000 }
5001
5002 /*
5003 * if no ecc placement scheme was provided pickup the default
5004 * large page one.
5005 */
5006 if (!mtd->ooblayout) {
5007 /* handle large page devices only */
5008 if (mtd->oobsize < 64) {
5009 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5010 return -EINVAL;
5011 }
5012
5013 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005014
5015 }
5016
5017 /*
5018 * We can only maximize ECC config when the default layout is
5019 * used, otherwise we don't know how many bytes can really be
5020 * used.
5021 */
5022 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5023 ecc->options & NAND_ECC_MAXIMIZE) {
5024 int steps, bytes;
5025
5026 /* Always prefer 1k blocks over 512bytes ones */
5027 ecc->size = 1024;
5028 steps = mtd->writesize / ecc->size;
5029
5030 /* Reserve 2 bytes for the BBM */
5031 bytes = (mtd->oobsize - 2) / steps;
5032 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005033 }
5034
5035 /* See nand_bch_init() for details. */
5036 ecc->bytes = 0;
5037 ecc->priv = nand_bch_init(mtd);
5038 if (!ecc->priv) {
5039 WARN(1, "BCH ECC initialization failed!\n");
5040 return -EINVAL;
5041 }
5042 return 0;
5043 default:
5044 WARN(1, "Unsupported ECC algorithm!\n");
5045 return -EINVAL;
5046 }
5047}
5048
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005049/**
5050 * nand_check_ecc_caps - check the sanity of preset ECC settings
5051 * @chip: nand chip info structure
5052 * @caps: ECC caps info structure
5053 * @oobavail: OOB size that the ECC engine can use
5054 *
5055 * When ECC step size and strength are already set, check if they are supported
5056 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5057 * On success, the calculated ECC bytes is set.
5058 */
5059int nand_check_ecc_caps(struct nand_chip *chip,
5060 const struct nand_ecc_caps *caps, int oobavail)
5061{
5062 struct mtd_info *mtd = nand_to_mtd(chip);
5063 const struct nand_ecc_step_info *stepinfo;
5064 int preset_step = chip->ecc.size;
5065 int preset_strength = chip->ecc.strength;
5066 int nsteps, ecc_bytes;
5067 int i, j;
5068
5069 if (WARN_ON(oobavail < 0))
5070 return -EINVAL;
5071
5072 if (!preset_step || !preset_strength)
5073 return -ENODATA;
5074
5075 nsteps = mtd->writesize / preset_step;
5076
5077 for (i = 0; i < caps->nstepinfos; i++) {
5078 stepinfo = &caps->stepinfos[i];
5079
5080 if (stepinfo->stepsize != preset_step)
5081 continue;
5082
5083 for (j = 0; j < stepinfo->nstrengths; j++) {
5084 if (stepinfo->strengths[j] != preset_strength)
5085 continue;
5086
5087 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5088 preset_strength);
5089 if (WARN_ON_ONCE(ecc_bytes < 0))
5090 return ecc_bytes;
5091
5092 if (ecc_bytes * nsteps > oobavail) {
5093 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5094 preset_step, preset_strength);
5095 return -ENOSPC;
5096 }
5097
5098 chip->ecc.bytes = ecc_bytes;
5099
5100 return 0;
5101 }
5102 }
5103
5104 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
5105 preset_step, preset_strength);
5106
5107 return -ENOTSUPP;
5108}
5109EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
5110
5111/**
5112 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
5113 * @chip: nand chip info structure
5114 * @caps: ECC engine caps info structure
5115 * @oobavail: OOB size that the ECC engine can use
5116 *
5117 * If a chip's ECC requirement is provided, try to meet it with the least
5118 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
5119 * On success, the chosen ECC settings are set.
5120 */
5121int nand_match_ecc_req(struct nand_chip *chip,
5122 const struct nand_ecc_caps *caps, int oobavail)
5123{
5124 struct mtd_info *mtd = nand_to_mtd(chip);
5125 const struct nand_ecc_step_info *stepinfo;
5126 int req_step = chip->ecc_step_ds;
5127 int req_strength = chip->ecc_strength_ds;
5128 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
5129 int best_step, best_strength, best_ecc_bytes;
5130 int best_ecc_bytes_total = INT_MAX;
5131 int i, j;
5132
5133 if (WARN_ON(oobavail < 0))
5134 return -EINVAL;
5135
5136 /* No information provided by the NAND chip */
5137 if (!req_step || !req_strength)
5138 return -ENOTSUPP;
5139
5140 /* number of correctable bits the chip requires in a page */
5141 req_corr = mtd->writesize / req_step * req_strength;
5142
5143 for (i = 0; i < caps->nstepinfos; i++) {
5144 stepinfo = &caps->stepinfos[i];
5145 step_size = stepinfo->stepsize;
5146
5147 for (j = 0; j < stepinfo->nstrengths; j++) {
5148 strength = stepinfo->strengths[j];
5149
5150 /*
5151 * If both step size and strength are smaller than the
5152 * chip's requirement, it is not easy to compare the
5153 * resulted reliability.
5154 */
5155 if (step_size < req_step && strength < req_strength)
5156 continue;
5157
5158 if (mtd->writesize % step_size)
5159 continue;
5160
5161 nsteps = mtd->writesize / step_size;
5162
5163 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5164 if (WARN_ON_ONCE(ecc_bytes < 0))
5165 continue;
5166 ecc_bytes_total = ecc_bytes * nsteps;
5167
5168 if (ecc_bytes_total > oobavail ||
5169 strength * nsteps < req_corr)
5170 continue;
5171
5172 /*
5173 * We assume the best is to meet the chip's requrement
5174 * with the least number of ECC bytes.
5175 */
5176 if (ecc_bytes_total < best_ecc_bytes_total) {
5177 best_ecc_bytes_total = ecc_bytes_total;
5178 best_step = step_size;
5179 best_strength = strength;
5180 best_ecc_bytes = ecc_bytes;
5181 }
5182 }
5183 }
5184
5185 if (best_ecc_bytes_total == INT_MAX)
5186 return -ENOTSUPP;
5187
5188 chip->ecc.size = best_step;
5189 chip->ecc.strength = best_strength;
5190 chip->ecc.bytes = best_ecc_bytes;
5191
5192 return 0;
5193}
5194EXPORT_SYMBOL_GPL(nand_match_ecc_req);
5195
5196/**
5197 * nand_maximize_ecc - choose the max ECC strength available
5198 * @chip: nand chip info structure
5199 * @caps: ECC engine caps info structure
5200 * @oobavail: OOB size that the ECC engine can use
5201 *
5202 * Choose the max ECC strength that is supported on the controller, and can fit
5203 * within the chip's OOB. On success, the chosen ECC settings are set.
5204 */
5205int nand_maximize_ecc(struct nand_chip *chip,
5206 const struct nand_ecc_caps *caps, int oobavail)
5207{
5208 struct mtd_info *mtd = nand_to_mtd(chip);
5209 const struct nand_ecc_step_info *stepinfo;
5210 int step_size, strength, nsteps, ecc_bytes, corr;
5211 int best_corr = 0;
5212 int best_step = 0;
5213 int best_strength, best_ecc_bytes;
5214 int i, j;
5215
5216 if (WARN_ON(oobavail < 0))
5217 return -EINVAL;
5218
5219 for (i = 0; i < caps->nstepinfos; i++) {
5220 stepinfo = &caps->stepinfos[i];
5221 step_size = stepinfo->stepsize;
5222
5223 /* If chip->ecc.size is already set, respect it */
5224 if (chip->ecc.size && step_size != chip->ecc.size)
5225 continue;
5226
5227 for (j = 0; j < stepinfo->nstrengths; j++) {
5228 strength = stepinfo->strengths[j];
5229
5230 if (mtd->writesize % step_size)
5231 continue;
5232
5233 nsteps = mtd->writesize / step_size;
5234
5235 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
5236 if (WARN_ON_ONCE(ecc_bytes < 0))
5237 continue;
5238
5239 if (ecc_bytes * nsteps > oobavail)
5240 continue;
5241
5242 corr = strength * nsteps;
5243
5244 /*
5245 * If the number of correctable bits is the same,
5246 * bigger step_size has more reliability.
5247 */
5248 if (corr > best_corr ||
5249 (corr == best_corr && step_size > best_step)) {
5250 best_corr = corr;
5251 best_step = step_size;
5252 best_strength = strength;
5253 best_ecc_bytes = ecc_bytes;
5254 }
5255 }
5256 }
5257
5258 if (!best_corr)
5259 return -ENOTSUPP;
5260
5261 chip->ecc.size = best_step;
5262 chip->ecc.strength = best_strength;
5263 chip->ecc.bytes = best_ecc_bytes;
5264
5265 return 0;
5266}
5267EXPORT_SYMBOL_GPL(nand_maximize_ecc);
5268
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005269/*
5270 * Check if the chip configuration meet the datasheet requirements.
5271
5272 * If our configuration corrects A bits per B bytes and the minimum
5273 * required correction level is X bits per Y bytes, then we must ensure
5274 * both of the following are true:
5275 *
5276 * (1) A / B >= X / Y
5277 * (2) A >= X
5278 *
5279 * Requirement (1) ensures we can correct for the required bitflip density.
5280 * Requirement (2) ensures we can correct even when all bitflips are clumped
5281 * in the same sector.
5282 */
5283static bool nand_ecc_strength_good(struct mtd_info *mtd)
5284{
Boris BREZILLON862eba52015-12-01 12:03:03 +01005285 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005286 struct nand_ecc_ctrl *ecc = &chip->ecc;
5287 int corr, ds_corr;
5288
5289 if (ecc->size == 0 || chip->ecc_step_ds == 0)
5290 /* Not enough information */
5291 return true;
5292
5293 /*
5294 * We get the number of corrected bits per page to compare
5295 * the correction density.
5296 */
5297 corr = (mtd->writesize * ecc->strength) / ecc->size;
5298 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
5299
5300 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
5301}
David Woodhouse3b85c322006-09-25 17:06:53 +01005302
5303/**
5304 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005305 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01005306 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005307 * This is the second phase of the normal nand_scan() function. It fills out
5308 * all the uninitialized function pointers with the defaults and scans for a
5309 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01005310 */
5311int nand_scan_tail(struct mtd_info *mtd)
5312{
Boris BREZILLON862eba52015-12-01 12:03:03 +01005313 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08005314 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005315 struct nand_buffers *nbuf = NULL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005316 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01005317
Brian Norrise2414f42012-02-06 13:44:00 -08005318 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005319 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07005320 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02005321 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07005322 }
Brian Norrise2414f42012-02-06 13:44:00 -08005323
Huang Shijief02ea4e2014-01-13 14:27:12 +08005324 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005325 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Boris Brezillonf84674b2017-06-02 12:18:24 +02005326 if (!nbuf)
5327 return -ENOMEM;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005328
5329 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
5330 if (!nbuf->ecccalc) {
5331 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005332 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005333 }
5334
5335 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
5336 if (!nbuf->ecccode) {
5337 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005338 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005339 }
5340
5341 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
5342 GFP_KERNEL);
5343 if (!nbuf->databuf) {
5344 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005345 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005346 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08005347
5348 chip->buffers = nbuf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005349 } else if (!chip->buffers) {
5350 return -ENOMEM;
Huang Shijief02ea4e2014-01-13 14:27:12 +08005351 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005352
Boris Brezillonf84674b2017-06-02 12:18:24 +02005353 /*
5354 * FIXME: some NAND manufacturer drivers expect the first die to be
5355 * selected when manufacturer->init() is called. They should be fixed
5356 * to explictly select the relevant die when interacting with the NAND
5357 * chip.
5358 */
5359 chip->select_chip(mtd, 0);
5360 ret = nand_manufacturer_init(chip);
5361 chip->select_chip(mtd, -1);
5362 if (ret)
5363 goto err_free_nbuf;
5364
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01005365 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01005366 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005367
5368 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005369 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005370 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005371 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005372 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005373 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01005376 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 break;
5378 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005379 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02005380 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005381 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02005383 /*
5384 * Expose the whole OOB area to users if ECC_NONE
5385 * is passed. We could do that for all kind of
5386 * ->oobsize, but we must keep the old large/small
5387 * page with ECC layout when ->oobsize <= 128 for
5388 * compatibility reasons.
5389 */
5390 if (ecc->mode == NAND_ECC_NONE) {
5391 mtd_set_ooblayout(mtd,
5392 &nand_ooblayout_lp_ops);
5393 break;
5394 }
5395
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005396 WARN(1, "No oob scheme defined for oobsize %d\n",
5397 mtd->oobsize);
5398 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005399 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 }
5401 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005402
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005403 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005404 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005405 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01005406 */
David Woodhouse956e9442006-09-25 17:12:39 +01005407
Huang Shijie97de79e02013-10-18 14:20:53 +08005408 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005409 case NAND_ECC_HW_OOB_FIRST:
5410 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08005411 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005412 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5413 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005414 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005415 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005416 if (!ecc->read_page)
5417 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07005418
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005419 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07005420 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005421 if (!ecc->read_page)
5422 ecc->read_page = nand_read_page_hwecc;
5423 if (!ecc->write_page)
5424 ecc->write_page = nand_write_page_hwecc;
5425 if (!ecc->read_page_raw)
5426 ecc->read_page_raw = nand_read_page_raw;
5427 if (!ecc->write_page_raw)
5428 ecc->write_page_raw = nand_write_page_raw;
5429 if (!ecc->read_oob)
5430 ecc->read_oob = nand_read_oob_std;
5431 if (!ecc->write_oob)
5432 ecc->write_oob = nand_write_oob_std;
5433 if (!ecc->read_subpage)
5434 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02005435 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08005436 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005437
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005438 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08005439 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5440 (!ecc->read_page ||
5441 ecc->read_page == nand_read_page_hwecc ||
5442 !ecc->write_page ||
5443 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005444 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
5445 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005446 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005447 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07005448 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08005449 if (!ecc->read_page)
5450 ecc->read_page = nand_read_page_syndrome;
5451 if (!ecc->write_page)
5452 ecc->write_page = nand_write_page_syndrome;
5453 if (!ecc->read_page_raw)
5454 ecc->read_page_raw = nand_read_page_raw_syndrome;
5455 if (!ecc->write_page_raw)
5456 ecc->write_page_raw = nand_write_page_raw_syndrome;
5457 if (!ecc->read_oob)
5458 ecc->read_oob = nand_read_oob_syndrome;
5459 if (!ecc->write_oob)
5460 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02005461
Huang Shijie97de79e02013-10-18 14:20:53 +08005462 if (mtd->writesize >= ecc->size) {
5463 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005464 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
5465 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005466 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07005467 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005468 break;
Mike Dunne2788c92012-04-25 12:06:10 -07005469 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005470 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5471 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08005472 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02005473 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02005475 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005476 ret = nand_set_ecc_soft_ops(mtd);
5477 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005478 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005479 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01005480 }
5481 break;
5482
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005483 case NAND_ECC_ON_DIE:
5484 if (!ecc->read_page || !ecc->write_page) {
5485 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
5486 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005487 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005488 }
5489 if (!ecc->read_oob)
5490 ecc->read_oob = nand_read_oob_std;
5491 if (!ecc->write_oob)
5492 ecc->write_oob = nand_write_oob_std;
5493 break;
5494
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005495 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02005496 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08005497 ecc->read_page = nand_read_page_raw;
5498 ecc->write_page = nand_write_page_raw;
5499 ecc->read_oob = nand_read_oob_std;
5500 ecc->read_page_raw = nand_read_page_raw;
5501 ecc->write_page_raw = nand_write_page_raw;
5502 ecc->write_oob = nand_write_oob_std;
5503 ecc->size = mtd->writesize;
5504 ecc->bytes = 0;
5505 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005507
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005509 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5510 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005511 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
Brian Norris9ce244b2011-08-30 18:45:37 -07005514 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005515 if (!ecc->read_oob_raw)
5516 ecc->read_oob_raw = ecc->read_oob;
5517 if (!ecc->write_oob_raw)
5518 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005519
Boris Brezillon846031d2016-02-03 20:11:00 +01005520 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005521 mtd->ecc_strength = ecc->strength;
5522 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005523
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005524 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005525 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005526 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005527 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005528 ecc->steps = mtd->writesize / ecc->size;
5529 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005530 WARN(1, "Invalid ECC parameters\n");
5531 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005532 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005534 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005535 if (ecc->total > mtd->oobsize) {
5536 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5537 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02005538 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005539 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005540
Boris Brezillon846031d2016-02-03 20:11:00 +01005541 /*
5542 * The number of bytes available for a client to place data into
5543 * the out of band area.
5544 */
5545 ret = mtd_ooblayout_count_freebytes(mtd);
5546 if (ret < 0)
5547 ret = 0;
5548
5549 mtd->oobavail = ret;
5550
5551 /* ECC sanity check: warn if it's too weak */
5552 if (!nand_ecc_strength_good(mtd))
5553 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5554 mtd->name);
5555
Brian Norris8b6e50c2011-05-25 14:59:01 -07005556 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005557 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005558 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005559 case 2:
5560 mtd->subpage_sft = 1;
5561 break;
5562 case 4:
5563 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005564 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005565 mtd->subpage_sft = 2;
5566 break;
5567 }
5568 }
5569 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5570
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02005571 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005572 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005575 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005577 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305578 switch (ecc->mode) {
5579 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305580 if (chip->page_shift > 9)
5581 chip->options |= NAND_SUBPAGE_READ;
5582 break;
5583
5584 default:
5585 break;
5586 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08005589 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02005590 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5591 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005592 mtd->_erase = nand_erase;
5593 mtd->_point = NULL;
5594 mtd->_unpoint = NULL;
5595 mtd->_read = nand_read;
5596 mtd->_write = nand_write;
5597 mtd->_panic_write = panic_nand_write;
5598 mtd->_read_oob = nand_read_oob;
5599 mtd->_write_oob = nand_write_oob;
5600 mtd->_sync = nand_sync;
5601 mtd->_lock = NULL;
5602 mtd->_unlock = NULL;
5603 mtd->_suspend = nand_suspend;
5604 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005605 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005606 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005607 mtd->_block_isbad = nand_block_isbad;
5608 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06005609 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01005610 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005612 /*
5613 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5614 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5615 * properly set.
5616 */
5617 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005618 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
Boris Brezillonf84674b2017-06-02 12:18:24 +02005620 /* Initialize the ->data_interface field. */
5621 ret = nand_init_data_interface(chip);
5622 if (ret)
5623 goto err_nand_manuf_cleanup;
5624
5625 /* Enter fastest possible mode on all dies. */
5626 for (i = 0; i < chip->numchips; i++) {
5627 chip->select_chip(mtd, i);
5628 ret = nand_setup_data_interface(chip, i);
5629 chip->select_chip(mtd, -1);
5630
5631 if (ret)
5632 goto err_nand_data_iface_cleanup;
5633 }
5634
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005635 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005636 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
5639 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07005640 ret = chip->scan_bbt(mtd);
5641 if (ret)
Boris Brezillonf84674b2017-06-02 12:18:24 +02005642 goto err_nand_data_iface_cleanup;
5643
Brian Norris44d41822017-05-01 17:04:50 -07005644 return 0;
5645
Boris Brezillonf84674b2017-06-02 12:18:24 +02005646err_nand_data_iface_cleanup:
5647 nand_release_data_interface(chip);
5648
5649err_nand_manuf_cleanup:
5650 nand_manufacturer_cleanup(chip);
5651
5652err_free_nbuf:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005653 if (nbuf) {
5654 kfree(nbuf->databuf);
5655 kfree(nbuf->ecccode);
5656 kfree(nbuf->ecccalc);
5657 kfree(nbuf);
5658 }
Brian Norris78771042017-05-01 17:04:53 -07005659
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005660 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005662EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
Brian Norris8b6e50c2011-05-25 14:59:01 -07005664/*
5665 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005666 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07005667 * to call us from in-kernel code if the core NAND support is modular.
5668 */
David Woodhouse3b85c322006-09-25 17:06:53 +01005669#ifdef MODULE
5670#define caller_is_module() (1)
5671#else
5672#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06005673 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01005674#endif
5675
5676/**
5677 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005678 * @mtd: MTD device structure
5679 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01005680 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005681 * This fills out all the uninitialized function pointers with the defaults.
5682 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005683 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005684 */
5685int nand_scan(struct mtd_info *mtd, int maxchips)
5686{
5687 int ret;
5688
David Woodhouse5e81e882010-02-26 18:32:56 +00005689 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01005690 if (!ret)
5691 ret = nand_scan_tail(mtd);
5692 return ret;
5693}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005694EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01005695
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005697 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5698 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005699 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005700void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005702 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005703 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005704 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5705
Boris Brezillond8e725d2016-09-15 10:32:50 +02005706 nand_release_data_interface(chip);
5707
Jesper Juhlfa671642005-11-07 01:01:27 -08005708 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005709 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005710 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
5711 kfree(chip->buffers->databuf);
5712 kfree(chip->buffers->ecccode);
5713 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005714 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005715 }
Brian Norris58373ff2010-07-15 12:15:44 -07005716
5717 /* Free bad block descriptor memory */
5718 if (chip->badblock_pattern && chip->badblock_pattern->options
5719 & NAND_BBT_DYNAMICSTRUCT)
5720 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005721
5722 /* Free manufacturer priv data. */
5723 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005725EXPORT_SYMBOL_GPL(nand_cleanup);
5726
5727/**
5728 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5729 * held by the NAND device
5730 * @mtd: MTD device structure
5731 */
5732void nand_release(struct mtd_info *mtd)
5733{
5734 mtd_device_unregister(mtd);
5735 nand_cleanup(mtd_to_nand(mtd));
5736}
David Woodhousee0c7d762006-05-13 18:07:53 +01005737EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005738
David Woodhousee0c7d762006-05-13 18:07:53 +01005739MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005740MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5741MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005742MODULE_DESCRIPTION("Generic NAND flash driver code");