blob: fae28ec5627791774b3871b8402d51b04423401e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010039#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/types.h>
41#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020042#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010044#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020047#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020049#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010050
Huang Shijie6a8214a2012-11-19 14:43:30 +080051static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020053static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
Boris Brezillon41b207a2016-02-03 19:06:15 +010056/* Define default oob placement schemes for large and small page devices */
57static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59{
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020068 if (mtd->oobsize == 16)
69 oobregion->length = 4;
70 else
71 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010072 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020073 if (mtd->oobsize == 8)
74 return -ERANGE;
75
Boris Brezillon41b207a2016-02-03 19:06:15 +010076 oobregion->offset = 6;
77 oobregion->length = ecc->total - 4;
78 }
79
80 return 0;
81}
82
83static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85{
86 if (section > 1)
87 return -ERANGE;
88
89 if (mtd->oobsize == 16) {
90 if (section)
91 return -ERANGE;
92
93 oobregion->length = 8;
94 oobregion->offset = 8;
95 } else {
96 oobregion->length = 2;
97 if (!section)
98 oobregion->offset = 3;
99 else
100 oobregion->offset = 6;
101 }
102
103 return 0;
104}
105
106const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107 .ecc = nand_ooblayout_ecc_sp,
108 .free = nand_ooblayout_free_sp,
109};
110EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
111
112static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113 struct mtd_oob_region *oobregion)
114{
115 struct nand_chip *chip = mtd_to_nand(mtd);
116 struct nand_ecc_ctrl *ecc = &chip->ecc;
117
Miquel Raynal882fd152017-08-26 17:19:15 +0200118 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100119 return -ERANGE;
120
121 oobregion->length = ecc->total;
122 oobregion->offset = mtd->oobsize - oobregion->length;
123
124 return 0;
125}
126
127static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128 struct mtd_oob_region *oobregion)
129{
130 struct nand_chip *chip = mtd_to_nand(mtd);
131 struct nand_ecc_ctrl *ecc = &chip->ecc;
132
133 if (section)
134 return -ERANGE;
135
136 oobregion->length = mtd->oobsize - ecc->total - 2;
137 oobregion->offset = 2;
138
139 return 0;
140}
141
142const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143 .ecc = nand_ooblayout_ecc_lp,
144 .free = nand_ooblayout_free_lp,
145};
146EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200147
Alexander Couzens6a623e02017-05-02 12:19:00 +0200148/*
149 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
150 * are placed at a fixed offset.
151 */
152static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153 struct mtd_oob_region *oobregion)
154{
155 struct nand_chip *chip = mtd_to_nand(mtd);
156 struct nand_ecc_ctrl *ecc = &chip->ecc;
157
158 if (section)
159 return -ERANGE;
160
161 switch (mtd->oobsize) {
162 case 64:
163 oobregion->offset = 40;
164 break;
165 case 128:
166 oobregion->offset = 80;
167 break;
168 default:
169 return -EINVAL;
170 }
171
172 oobregion->length = ecc->total;
173 if (oobregion->offset + oobregion->length > mtd->oobsize)
174 return -ERANGE;
175
176 return 0;
177}
178
179static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180 struct mtd_oob_region *oobregion)
181{
182 struct nand_chip *chip = mtd_to_nand(mtd);
183 struct nand_ecc_ctrl *ecc = &chip->ecc;
184 int ecc_offset = 0;
185
186 if (section < 0 || section > 1)
187 return -ERANGE;
188
189 switch (mtd->oobsize) {
190 case 64:
191 ecc_offset = 40;
192 break;
193 case 128:
194 ecc_offset = 80;
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 if (section == 0) {
201 oobregion->offset = 2;
202 oobregion->length = ecc_offset - 2;
203 } else {
204 oobregion->offset = ecc_offset + ecc->total;
205 oobregion->length = mtd->oobsize - oobregion->offset;
206 }
207
208 return 0;
209}
210
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100211static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200212 .ecc = nand_ooblayout_ecc_lp_hamming,
213 .free = nand_ooblayout_free_lp_hamming,
214};
215
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530216static int check_offs_len(struct mtd_info *mtd,
217 loff_t ofs, uint64_t len)
218{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100219 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530220 int ret = 0;
221
222 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
228 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300229 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700230 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530231 ret = -EINVAL;
232 }
233
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530234 return ret;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700239 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800241 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100243static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100245 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200247 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 spin_lock(&chip->controller->lock);
249 chip->controller->active = NULL;
250 chip->state = FL_READY;
251 wake_up(&chip->controller->wq);
252 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700257 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700259 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200261static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100263 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200264 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/**
Masanari Iida064a7692012-11-09 23:20:58 +0900268 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700269 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700271 * Default read function for 16bit buswidth with endianness conversion.
272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200274static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100276 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700282 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700284 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286static u16 nand_read_word(struct mtd_info *mtd)
287{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100288 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200289 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700294 * @mtd: MTD device structure
295 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * Default select function for 1 chip devices.
298 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200299static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100301 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200302
303 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200305 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309
310 default:
311 BUG();
312 }
313}
314
315/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100316 * nand_write_byte - [DEFAULT] write single byte to chip
317 * @mtd: MTD device structure
318 * @byte: value to write
319 *
320 * Default function to write a byte to I/O[7:0]
321 */
322static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
323{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100324 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100325
326 chip->write_buf(mtd, &byte, 1);
327}
328
329/**
330 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
331 * @mtd: MTD device structure
332 * @byte: value to write
333 *
334 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
335 */
336static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
337{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100338 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100339 uint16_t word = byte;
340
341 /*
342 * It's not entirely clear what should happen to I/O[15:8] when writing
343 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
344 *
345 * When the host supports a 16-bit bus width, only data is
346 * transferred at the 16-bit width. All address and command line
347 * transfers shall use only the lower 8-bits of the data bus. During
348 * command transfers, the host may place any value on the upper
349 * 8-bits of the data bus. During address transfers, the host shall
350 * set the upper 8-bits of the data bus to 00h.
351 *
352 * One user of the write_byte callback is nand_onfi_set_features. The
353 * four parameters are specified to be written to I/O[7:0], but this is
354 * neither an address nor a command transfer. Let's assume a 0 on the
355 * upper I/O lines is OK.
356 */
357 chip->write_buf(mtd, (uint8_t *)&word, 2);
358}
359
360/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700362 * @mtd: MTD device structure
363 * @buf: data buffer
364 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700366 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200368static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100370 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Alexander Shiyan76413832013-04-13 09:32:13 +0400372 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000376 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700377 * @mtd: MTD device structure
378 * @buf: buffer to store date
379 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700381 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200383static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100385 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alexander Shiyan76413832013-04-13 09:32:13 +0400387 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700392 * @mtd: MTD device structure
393 * @buf: data buffer
394 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700396 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200398static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100400 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000402
Alexander Shiyan76413832013-04-13 09:32:13 +0400403 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000407 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700408 * @mtd: MTD device structure
409 * @buf: buffer to store date
410 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700412 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200414static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100416 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Alexander Shiyan76413832013-04-13 09:32:13 +0400419 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700424 * @mtd: MTD device structure
425 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000427 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530429static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Masahiro Yamadac120e752017-03-23 05:07:01 +0900431 int page, page_end, res;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100432 struct nand_chip *chip = mtd_to_nand(mtd);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900433 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Brian Norris5fb15492011-05-31 16:31:21 -0700435 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700436 ofs += mtd->erasesize - mtd->writesize;
437
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100438 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900439 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100440
Masahiro Yamadac120e752017-03-23 05:07:01 +0900441 for (; page < page_end; page++) {
442 res = chip->ecc.read_oob(mtd, chip, page);
443 if (res)
444 return res;
445
446 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000447
Brian Norriscdbec052012-01-13 18:11:48 -0800448 if (likely(chip->badblockbits == 8))
449 res = bad != 0xFF;
450 else
451 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900452 if (res)
453 return res;
454 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200455
Masahiro Yamadac120e752017-03-23 05:07:01 +0900456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700460 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700461 * @mtd: MTD device structure
462 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700464 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700465 * specific driver. It provides the details for writing a bad block marker to a
466 * block.
467 */
468static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
469{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100470 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700471 struct mtd_oob_ops ops;
472 uint8_t buf[2] = { 0, 0 };
473 int ret = 0, res, i = 0;
474
Brian Norris0ec56dc2015-02-28 02:02:30 -0800475 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700476 ops.oobbuf = buf;
477 ops.ooboffs = chip->badblockpos;
478 if (chip->options & NAND_BUSWIDTH_16) {
479 ops.ooboffs &= ~0x01;
480 ops.len = ops.ooblen = 2;
481 } else {
482 ops.len = ops.ooblen = 1;
483 }
484 ops.mode = MTD_OPS_PLACE_OOB;
485
486 /* Write to first/last page(s) if necessary */
487 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
488 ofs += mtd->erasesize - mtd->writesize;
489 do {
490 res = nand_do_write_oob(mtd, ofs, &ops);
491 if (!ret)
492 ret = res;
493
494 i++;
495 ofs += mtd->writesize;
496 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
497
498 return ret;
499}
500
501/**
502 * nand_block_markbad_lowlevel - mark a block bad
503 * @mtd: MTD device structure
504 * @ofs: offset from device start
505 *
506 * This function performs the generic NAND bad block marking steps (i.e., bad
507 * block table(s) and/or marker(s)). We only allow the hardware driver to
508 * specify how to write bad block markers to OOB (chip->block_markbad).
509 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700510 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300511 *
Brian Norrise2414f42012-02-06 13:44:00 -0800512 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700513 * (2) write bad block marker to OOB area of affected block (unless flag
514 * NAND_BBT_NO_OOB_BBM is present)
515 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300516 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700517 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800518 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700520static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100522 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700523 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000524
Brian Norrisb32843b2013-07-30 17:52:59 -0700525 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800526 struct erase_info einfo;
527
528 /* Attempt erase before marking OOB */
529 memset(&einfo, 0, sizeof(einfo));
530 einfo.mtd = mtd;
531 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300532 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800533 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800534
Brian Norrisb32843b2013-07-30 17:52:59 -0700535 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800536 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700537 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300538 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200539 }
Brian Norrise2414f42012-02-06 13:44:00 -0800540
Brian Norrisb32843b2013-07-30 17:52:59 -0700541 /* Mark block bad in BBT */
542 if (chip->bbt) {
543 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800544 if (!ret)
545 ret = res;
546 }
547
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200548 if (!ret)
549 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300550
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000554/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700556 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700558 * Check, if the device is write protected. The function expects, that the
559 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100561static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100563 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200564
Brian Norris8b6e50c2011-05-25 14:59:01 -0700565 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200566 if (chip->options & NAND_BROKEN_XD)
567 return 0;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200570 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
571 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800575 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700576 * @mtd: MTD device structure
577 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300578 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800579 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300580 */
581static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
582{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100583 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300584
585 if (!chip->bbt)
586 return 0;
587 /* Return info from the table */
588 return nand_isreserved_bbt(mtd, ofs);
589}
590
591/**
592 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
593 * @mtd: MTD device structure
594 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700595 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
597 * Check, if the block is bad. Either by reading the bad block table or
598 * calling of the scan function.
599 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530600static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100602 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000603
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200604 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530605 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100608 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200611/**
612 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700613 * @mtd: MTD device structure
614 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200615 *
616 * Helper function for nand_wait_ready used when needing to wait in interrupt
617 * context.
618 */
619static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
620{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100621 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200622 int i;
623
624 /* Wait for the device to get ready */
625 for (i = 0; i < timeo; i++) {
626 if (chip->dev_ready(mtd))
627 break;
628 touch_softlockup_watchdog();
629 mdelay(1);
630 }
631}
632
Alex Smithb70af9b2015-10-06 14:52:07 +0100633/**
634 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
635 * @mtd: MTD device structure
636 *
637 * Wait for the ready pin after a command, and warn if a timeout occurs.
638 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100639void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000640{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100641 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100642 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000643
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200644 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100645 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200646
Brian Norris7854d3f2011-06-23 14:12:08 -0700647 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100648 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000649 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200650 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300651 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100652 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000653 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100654
Brian Norris9ebfdf52016-03-04 17:19:23 -0800655 if (!chip->dev_ready(mtd))
656 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000657}
David Woodhouse4b648b02006-09-25 17:05:24 +0100658EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200661 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
662 * @mtd: MTD device structure
663 * @timeo: Timeout in ms
664 *
665 * Wait for status ready (i.e. command done) or timeout.
666 */
667static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
668{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100669 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200670
671 timeo = jiffies + msecs_to_jiffies(timeo);
672 do {
673 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
674 break;
675 touch_softlockup_watchdog();
676 } while (time_before(jiffies, timeo));
677};
678
679/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700681 * @mtd: MTD device structure
682 * @command: the command to be sent
683 * @column: the column address for this command, -1 if none
684 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700686 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200687 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200689static void nand_command(struct mtd_info *mtd, unsigned int command,
690 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100692 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200693 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Brian Norris8b6e50c2011-05-25 14:59:01 -0700695 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (command == NAND_CMD_SEQIN) {
697 int readcmd;
698
Joern Engel28318772006-05-22 23:18:05 +0200699 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200701 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 readcmd = NAND_CMD_READOOB;
703 } else if (column < 256) {
704 /* First 256 bytes --> READ0 */
705 readcmd = NAND_CMD_READ0;
706 } else {
707 column -= 256;
708 readcmd = NAND_CMD_READ1;
709 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200710 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200711 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200713 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Brian Norris8b6e50c2011-05-25 14:59:01 -0700715 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200716 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
717 /* Serially input address */
718 if (column != -1) {
719 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800720 if (chip->options & NAND_BUSWIDTH_16 &&
721 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200722 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200723 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200724 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200726 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200727 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200728 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200729 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200730 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 if (chip->chipsize > (32 << 20))
732 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200733 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200734 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000735
736 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700737 * Program and erase have their own busy handlers status and sequential
738 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100739 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 case NAND_CMD_PAGEPROG:
743 case NAND_CMD_ERASE1:
744 case NAND_CMD_ERASE2:
745 case NAND_CMD_SEQIN:
746 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900747 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900748 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return;
750
751 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200752 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200754 udelay(chip->chip_delay);
755 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200756 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200757 chip->cmd_ctrl(mtd,
758 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200759 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
760 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return;
762
David Woodhousee0c7d762006-05-13 18:07:53 +0100763 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200764 case NAND_CMD_READ0:
765 /*
766 * READ0 is sometimes used to exit GET STATUS mode. When this
767 * is the case no address cycles are requested, and we can use
768 * this information to detect that we should not wait for the
769 * device to be ready.
770 */
771 if (column == -1 && page_addr == -1)
772 return;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000775 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 * If we don't have access to the busy pin, we apply the given
777 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100778 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200779 if (!chip->dev_ready) {
780 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700784 /*
785 * Apply this short delay always to ensure that we do wait tWB in
786 * any case on any machine.
787 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100788 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000789
790 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200793static void nand_ccs_delay(struct nand_chip *chip)
794{
795 /*
796 * The controller already takes care of waiting for tCCS when the RNDIN
797 * or RNDOUT command is sent, return directly.
798 */
799 if (!(chip->options & NAND_WAIT_TCCS))
800 return;
801
802 /*
803 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
804 * (which should be safe for all NANDs).
805 */
806 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
807 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
808 else
809 ndelay(500);
810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/**
813 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700814 * @mtd: MTD device structure
815 * @command: the command to be sent
816 * @column: the column address for this command, -1 if none
817 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200819 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700820 * devices. We don't have the separate regions as we have in the small page
821 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200823static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
824 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100826 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 /* Emulate NAND_CMD_READOOB */
829 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200830 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 command = NAND_CMD_READ0;
832 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000833
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200834 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400835 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200838 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /* Serially input address */
841 if (column != -1) {
842 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800843 if (chip->options & NAND_BUSWIDTH_16 &&
844 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200846 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200847 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200848
Brian Norrisf5b88de2016-10-03 09:49:35 -0700849 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200850 if (!nand_opcode_8bits(command))
851 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200854 chip->cmd_ctrl(mtd, page_addr, ctrl);
855 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200856 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200858 if (chip->chipsize > (128 << 20))
859 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200860 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200863 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000864
865 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700866 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100867 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000868 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 case NAND_CMD_CACHEDPROG:
872 case NAND_CMD_PAGEPROG:
873 case NAND_CMD_ERASE1:
874 case NAND_CMD_ERASE2:
875 case NAND_CMD_SEQIN:
876 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900877 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900878 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000879 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200881 case NAND_CMD_RNDIN:
882 nand_ccs_delay(chip);
883 return;
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200886 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200888 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200889 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
890 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
891 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
892 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200893 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
894 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return;
896
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200897 case NAND_CMD_RNDOUT:
898 /* No ready / busy check necessary */
899 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
900 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
901 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
902 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200903
904 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200905 return;
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200908 /*
909 * READ0 is sometimes used to exit GET STATUS mode. When this
910 * is the case no address cycles are requested, and we can use
911 * this information to detect that READSTART should not be
912 * issued.
913 */
914 if (column == -1 && page_addr == -1)
915 return;
916
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200917 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
918 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
919 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
920 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000921
David Woodhousee0c7d762006-05-13 18:07:53 +0100922 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000924 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700926 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100927 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200928 if (!chip->dev_ready) {
929 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000933
Brian Norris8b6e50c2011-05-25 14:59:01 -0700934 /*
935 * Apply this short delay always to ensure that we do wait tWB in
936 * any case on any machine.
937 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100938 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000939
940 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200944 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700945 * @chip: the nand chip descriptor
946 * @mtd: MTD device structure
947 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200948 *
949 * Used when in panic, no locks are taken.
950 */
951static void panic_nand_get_device(struct nand_chip *chip,
952 struct mtd_info *mtd, int new_state)
953{
Brian Norris7854d3f2011-06-23 14:12:08 -0700954 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200955 chip->controller->active = chip;
956 chip->state = new_state;
957}
958
959/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700961 * @mtd: MTD device structure
962 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 *
964 * Get the device and lock it for exclusive access
965 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200966static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800967nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100969 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200970 spinlock_t *lock = &chip->controller->lock;
971 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100972 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200973retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100974 spin_lock(lock);
975
vimal singhb8b3ee92009-07-09 20:41:22 +0530976 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200977 if (!chip->controller->active)
978 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200979
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200980 if (chip->controller->active == chip && chip->state == FL_READY) {
981 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100982 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100983 return 0;
984 }
985 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800986 if (chip->controller->active->state == FL_PM_SUSPENDED) {
987 chip->state = FL_PM_SUSPENDED;
988 spin_unlock(lock);
989 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800990 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100991 }
992 set_current_state(TASK_UNINTERRUPTIBLE);
993 add_wait_queue(wq, &wait);
994 spin_unlock(lock);
995 schedule();
996 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 goto retry;
998}
999
1000/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001001 * panic_nand_wait - [GENERIC] wait until the command is done
1002 * @mtd: MTD device structure
1003 * @chip: NAND chip structure
1004 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001005 *
1006 * Wait for command done. This is a helper function for nand_wait used when
1007 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001008 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001009 */
1010static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1011 unsigned long timeo)
1012{
1013 int i;
1014 for (i = 0; i < timeo; i++) {
1015 if (chip->dev_ready) {
1016 if (chip->dev_ready(mtd))
1017 break;
1018 } else {
1019 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1020 break;
1021 }
1022 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001023 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001024}
1025
1026/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001027 * nand_wait - [DEFAULT] wait until the command is done
1028 * @mtd: MTD device structure
1029 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001031 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001032 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001033static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035
Alex Smithb70af9b2015-10-06 14:52:07 +01001036 int status;
1037 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Brian Norris8b6e50c2011-05-25 14:59:01 -07001039 /*
1040 * Apply this short delay always to ensure that we do wait tWB in any
1041 * case on any machine.
1042 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001043 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Artem Bityutskiy14c65782013-03-04 14:21:34 +02001045 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001047 if (in_interrupt() || oops_in_progress)
1048 panic_nand_wait(mtd, chip, timeo);
1049 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001050 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001051 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001052 if (chip->dev_ready) {
1053 if (chip->dev_ready(mtd))
1054 break;
1055 } else {
1056 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1057 break;
1058 }
1059 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001060 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001062
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001063 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001064 /* This can happen if in case of timeout or buggy dev_ready */
1065 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return status;
1067}
1068
1069/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001070 * nand_reset_data_interface - Reset data interface and timings
1071 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001072 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001073 *
1074 * Reset the Data interface and timings to ONFI mode 0.
1075 *
1076 * Returns 0 for success or negative error code otherwise.
1077 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001078static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001079{
1080 struct mtd_info *mtd = nand_to_mtd(chip);
1081 const struct nand_data_interface *conf;
1082 int ret;
1083
1084 if (!chip->setup_data_interface)
1085 return 0;
1086
1087 /*
1088 * The ONFI specification says:
1089 * "
1090 * To transition from NV-DDR or NV-DDR2 to the SDR data
1091 * interface, the host shall use the Reset (FFh) command
1092 * using SDR timing mode 0. A device in any timing mode is
1093 * required to recognize Reset (FFh) command issued in SDR
1094 * timing mode 0.
1095 * "
1096 *
1097 * Configure the data interface in SDR mode and set the
1098 * timings to timing mode 0.
1099 */
1100
1101 conf = nand_get_default_data_interface();
Boris Brezillon104e4422017-03-16 09:35:58 +01001102 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001103 if (ret)
1104 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1105
1106 return ret;
1107}
1108
1109/**
1110 * nand_setup_data_interface - Setup the best data interface and timings
1111 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001112 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001113 *
1114 * Find and configure the best data interface and NAND timings supported by
1115 * the chip and the driver.
1116 * First tries to retrieve supported timing modes from ONFI information,
1117 * and if the NAND chip does not support ONFI, relies on the
1118 * ->onfi_timing_mode_default specified in the nand_ids table.
1119 *
1120 * Returns 0 for success or negative error code otherwise.
1121 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001122static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001123{
1124 struct mtd_info *mtd = nand_to_mtd(chip);
1125 int ret;
1126
1127 if (!chip->setup_data_interface || !chip->data_interface)
1128 return 0;
1129
1130 /*
1131 * Ensure the timing mode has been changed on the chip side
1132 * before changing timings on the controller side.
1133 */
Boris Brezillona11bf5e2017-07-31 10:29:56 +02001134 if (chip->onfi_version &&
1135 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1136 ONFI_OPT_CMD_SET_GET_FEATURES)) {
Boris Brezillond8e725d2016-09-15 10:32:50 +02001137 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1138 chip->onfi_timing_mode_default,
1139 };
1140
1141 ret = chip->onfi_set_features(mtd, chip,
1142 ONFI_FEATURE_ADDR_TIMING_MODE,
1143 tmode_param);
1144 if (ret)
1145 goto err;
1146 }
1147
Boris Brezillon104e4422017-03-16 09:35:58 +01001148 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001149err:
1150 return ret;
1151}
1152
1153/**
1154 * nand_init_data_interface - find the best data interface and timings
1155 * @chip: The NAND chip
1156 *
1157 * Find the best data interface and NAND timings supported by the chip
1158 * and the driver.
1159 * First tries to retrieve supported timing modes from ONFI information,
1160 * and if the NAND chip does not support ONFI, relies on the
1161 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1162 * function nand_chip->data_interface is initialized with the best timing mode
1163 * available.
1164 *
1165 * Returns 0 for success or negative error code otherwise.
1166 */
1167static int nand_init_data_interface(struct nand_chip *chip)
1168{
1169 struct mtd_info *mtd = nand_to_mtd(chip);
1170 int modes, mode, ret;
1171
1172 if (!chip->setup_data_interface)
1173 return 0;
1174
1175 /*
1176 * First try to identify the best timings from ONFI parameters and
1177 * if the NAND does not support ONFI, fallback to the default ONFI
1178 * timing mode.
1179 */
1180 modes = onfi_get_async_timing_mode(chip);
1181 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1182 if (!chip->onfi_timing_mode_default)
1183 return 0;
1184
1185 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1186 }
1187
1188 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1189 GFP_KERNEL);
1190 if (!chip->data_interface)
1191 return -ENOMEM;
1192
1193 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1194 ret = onfi_init_data_interface(chip, chip->data_interface,
1195 NAND_SDR_IFACE, mode);
1196 if (ret)
1197 continue;
1198
Boris Brezillon104e4422017-03-16 09:35:58 +01001199 /* Pass -1 to only */
1200 ret = chip->setup_data_interface(mtd,
1201 NAND_DATA_IFACE_CHECK_ONLY,
1202 chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001203 if (!ret) {
1204 chip->onfi_timing_mode_default = mode;
1205 break;
1206 }
1207 }
1208
1209 return 0;
1210}
1211
1212static void nand_release_data_interface(struct nand_chip *chip)
1213{
1214 kfree(chip->data_interface);
1215}
1216
1217/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001218 * nand_reset - Reset and initialize a NAND device
1219 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001220 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001221 *
1222 * Returns 0 for success or negative error code otherwise
1223 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001224int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001225{
1226 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001227 int ret;
1228
Boris Brezillon104e4422017-03-16 09:35:58 +01001229 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001230 if (ret)
1231 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001232
Boris Brezillon73f907f2016-10-24 16:46:20 +02001233 /*
1234 * The CS line has to be released before we can apply the new NAND
1235 * interface settings, hence this weird ->select_chip() dance.
1236 */
1237 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001238 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001239 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001240
Boris Brezillon73f907f2016-10-24 16:46:20 +02001241 chip->select_chip(mtd, chipnr);
Boris Brezillon104e4422017-03-16 09:35:58 +01001242 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001243 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001244 if (ret)
1245 return ret;
1246
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001247 return 0;
1248}
1249
1250/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001251 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1252 * @buf: buffer to test
1253 * @len: buffer length
1254 * @bitflips_threshold: maximum number of bitflips
1255 *
1256 * Check if a buffer contains only 0xff, which means the underlying region
1257 * has been erased and is ready to be programmed.
1258 * The bitflips_threshold specify the maximum number of bitflips before
1259 * considering the region is not erased.
1260 * Note: The logic of this function has been extracted from the memweight
1261 * implementation, except that nand_check_erased_buf function exit before
1262 * testing the whole buffer if the number of bitflips exceed the
1263 * bitflips_threshold value.
1264 *
1265 * Returns a positive number of bitflips less than or equal to
1266 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1267 * threshold.
1268 */
1269static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1270{
1271 const unsigned char *bitmap = buf;
1272 int bitflips = 0;
1273 int weight;
1274
1275 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1276 len--, bitmap++) {
1277 weight = hweight8(*bitmap);
1278 bitflips += BITS_PER_BYTE - weight;
1279 if (unlikely(bitflips > bitflips_threshold))
1280 return -EBADMSG;
1281 }
1282
1283 for (; len >= sizeof(long);
1284 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001285 unsigned long d = *((unsigned long *)bitmap);
1286 if (d == ~0UL)
1287 continue;
1288 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001289 bitflips += BITS_PER_LONG - weight;
1290 if (unlikely(bitflips > bitflips_threshold))
1291 return -EBADMSG;
1292 }
1293
1294 for (; len > 0; len--, bitmap++) {
1295 weight = hweight8(*bitmap);
1296 bitflips += BITS_PER_BYTE - weight;
1297 if (unlikely(bitflips > bitflips_threshold))
1298 return -EBADMSG;
1299 }
1300
1301 return bitflips;
1302}
1303
1304/**
1305 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1306 * 0xff data
1307 * @data: data buffer to test
1308 * @datalen: data length
1309 * @ecc: ECC buffer
1310 * @ecclen: ECC length
1311 * @extraoob: extra OOB buffer
1312 * @extraooblen: extra OOB length
1313 * @bitflips_threshold: maximum number of bitflips
1314 *
1315 * Check if a data buffer and its associated ECC and OOB data contains only
1316 * 0xff pattern, which means the underlying region has been erased and is
1317 * ready to be programmed.
1318 * The bitflips_threshold specify the maximum number of bitflips before
1319 * considering the region as not erased.
1320 *
1321 * Note:
1322 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1323 * different from the NAND page size. When fixing bitflips, ECC engines will
1324 * report the number of errors per chunk, and the NAND core infrastructure
1325 * expect you to return the maximum number of bitflips for the whole page.
1326 * This is why you should always use this function on a single chunk and
1327 * not on the whole page. After checking each chunk you should update your
1328 * max_bitflips value accordingly.
1329 * 2/ When checking for bitflips in erased pages you should not only check
1330 * the payload data but also their associated ECC data, because a user might
1331 * have programmed almost all bits to 1 but a few. In this case, we
1332 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1333 * this case.
1334 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1335 * data are protected by the ECC engine.
1336 * It could also be used if you support subpages and want to attach some
1337 * extra OOB data to an ECC chunk.
1338 *
1339 * Returns a positive number of bitflips less than or equal to
1340 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1341 * threshold. In case of success, the passed buffers are filled with 0xff.
1342 */
1343int nand_check_erased_ecc_chunk(void *data, int datalen,
1344 void *ecc, int ecclen,
1345 void *extraoob, int extraooblen,
1346 int bitflips_threshold)
1347{
1348 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1349
1350 data_bitflips = nand_check_erased_buf(data, datalen,
1351 bitflips_threshold);
1352 if (data_bitflips < 0)
1353 return data_bitflips;
1354
1355 bitflips_threshold -= data_bitflips;
1356
1357 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1358 if (ecc_bitflips < 0)
1359 return ecc_bitflips;
1360
1361 bitflips_threshold -= ecc_bitflips;
1362
1363 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1364 bitflips_threshold);
1365 if (extraoob_bitflips < 0)
1366 return extraoob_bitflips;
1367
1368 if (data_bitflips)
1369 memset(data, 0xff, datalen);
1370
1371 if (ecc_bitflips)
1372 memset(ecc, 0xff, ecclen);
1373
1374 if (extraoob_bitflips)
1375 memset(extraoob, 0xff, extraooblen);
1376
1377 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1378}
1379EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1380
1381/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001382 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001383 * @mtd: mtd info structure
1384 * @chip: nand chip info structure
1385 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001386 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001387 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001388 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001389 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001390 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001391int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1392 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001393{
1394 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001395 if (oob_required)
1396 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001397 return 0;
1398}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001399EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001400
1401/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001402 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001403 * @mtd: mtd info structure
1404 * @chip: nand chip info structure
1405 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001406 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001407 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001408 *
1409 * We need a special oob layout and handling even when OOB isn't used.
1410 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001411static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001412 struct nand_chip *chip, uint8_t *buf,
1413 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001414{
1415 int eccsize = chip->ecc.size;
1416 int eccbytes = chip->ecc.bytes;
1417 uint8_t *oob = chip->oob_poi;
1418 int steps, size;
1419
1420 for (steps = chip->ecc.steps; steps > 0; steps--) {
1421 chip->read_buf(mtd, buf, eccsize);
1422 buf += eccsize;
1423
1424 if (chip->ecc.prepad) {
1425 chip->read_buf(mtd, oob, chip->ecc.prepad);
1426 oob += chip->ecc.prepad;
1427 }
1428
1429 chip->read_buf(mtd, oob, eccbytes);
1430 oob += eccbytes;
1431
1432 if (chip->ecc.postpad) {
1433 chip->read_buf(mtd, oob, chip->ecc.postpad);
1434 oob += chip->ecc.postpad;
1435 }
1436 }
1437
1438 size = mtd->oobsize - (oob - chip->oob_poi);
1439 if (size)
1440 chip->read_buf(mtd, oob, size);
1441
1442 return 0;
1443}
1444
1445/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001446 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001447 * @mtd: mtd info structure
1448 * @chip: nand chip info structure
1449 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001450 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001451 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001452 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001453static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001454 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Boris Brezillon846031d2016-02-03 20:11:00 +01001456 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001457 int eccbytes = chip->ecc.bytes;
1458 int eccsteps = chip->ecc.steps;
1459 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001460 uint8_t *ecc_calc = chip->buffers->ecccalc;
1461 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001462 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001463
Brian Norris1fbb9382012-05-02 10:14:55 -07001464 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001465
1466 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1467 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1468
Boris Brezillon846031d2016-02-03 20:11:00 +01001469 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1470 chip->ecc.total);
1471 if (ret)
1472 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001473
1474 eccsteps = chip->ecc.steps;
1475 p = buf;
1476
1477 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1478 int stat;
1479
1480 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001481 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001482 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001483 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001484 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001485 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1486 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001487 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001488 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301492 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001493 * @mtd: mtd info structure
1494 * @chip: nand chip info structure
1495 * @data_offs: offset of requested data within the page
1496 * @readlen: data length
1497 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001498 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001499 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001500static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001501 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1502 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001503{
Boris Brezillon846031d2016-02-03 20:11:00 +01001504 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001505 uint8_t *p;
1506 int data_col_addr, i, gaps = 0;
1507 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1508 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001509 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001510 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001511 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001512
Brian Norris7854d3f2011-06-23 14:12:08 -07001513 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001514 start_step = data_offs / chip->ecc.size;
1515 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1516 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301517 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001518
Brian Norris8b6e50c2011-05-25 14:59:01 -07001519 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001520 datafrag_len = num_steps * chip->ecc.size;
1521 eccfrag_len = num_steps * chip->ecc.bytes;
1522
1523 data_col_addr = start_step * chip->ecc.size;
1524 /* If we read not a page aligned data */
1525 if (data_col_addr != 0)
1526 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1527
1528 p = bufpoi + data_col_addr;
1529 chip->read_buf(mtd, p, datafrag_len);
1530
Brian Norris8b6e50c2011-05-25 14:59:01 -07001531 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001532 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1533 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1534
Brian Norris8b6e50c2011-05-25 14:59:01 -07001535 /*
1536 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001537 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001538 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001539 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1540 if (ret)
1541 return ret;
1542
1543 if (oobregion.length < eccfrag_len)
1544 gaps = 1;
1545
Alexey Korolev3d459552008-05-15 17:23:18 +01001546 if (gaps) {
1547 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1548 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1549 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001550 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001551 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001552 * about buswidth alignment in read_buf.
1553 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001554 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001555 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001556 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001557 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001558 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1559 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001560 aligned_len++;
1561
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001562 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001563 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001564 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1565 }
1566
Boris Brezillon846031d2016-02-03 20:11:00 +01001567 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1568 chip->oob_poi, index, eccfrag_len);
1569 if (ret)
1570 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001571
1572 p = bufpoi + data_col_addr;
1573 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1574 int stat;
1575
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001576 stat = chip->ecc.correct(mtd, p,
1577 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001578 if (stat == -EBADMSG &&
1579 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1580 /* check for empty pages with bitflips */
1581 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1582 &chip->buffers->ecccode[i],
1583 chip->ecc.bytes,
1584 NULL, 0,
1585 chip->ecc.strength);
1586 }
1587
Mike Dunn3f91e942012-04-25 12:06:09 -07001588 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001589 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001590 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001591 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001592 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1593 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001594 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001595 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001596}
1597
1598/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001599 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001600 * @mtd: mtd info structure
1601 * @chip: nand chip info structure
1602 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001603 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001604 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001605 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001606 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001607 */
1608static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001609 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001610{
Boris Brezillon846031d2016-02-03 20:11:00 +01001611 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001612 int eccbytes = chip->ecc.bytes;
1613 int eccsteps = chip->ecc.steps;
1614 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001615 uint8_t *ecc_calc = chip->buffers->ecccalc;
1616 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001617 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001618
1619 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1620 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1621 chip->read_buf(mtd, p, eccsize);
1622 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1623 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001624 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001625
Boris Brezillon846031d2016-02-03 20:11:00 +01001626 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1627 chip->ecc.total);
1628 if (ret)
1629 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001630
1631 eccsteps = chip->ecc.steps;
1632 p = buf;
1633
1634 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1635 int stat;
1636
1637 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001638 if (stat == -EBADMSG &&
1639 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1640 /* check for empty pages with bitflips */
1641 stat = nand_check_erased_ecc_chunk(p, eccsize,
1642 &ecc_code[i], eccbytes,
1643 NULL, 0,
1644 chip->ecc.strength);
1645 }
1646
Mike Dunn3f91e942012-04-25 12:06:09 -07001647 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001648 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001649 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001650 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001651 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1652 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001653 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001654 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001655}
1656
1657/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001658 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001659 * @mtd: mtd info structure
1660 * @chip: nand chip info structure
1661 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001662 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001663 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001664 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001665 * Hardware ECC for large page chips, require OOB to be read first. For this
1666 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1667 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1668 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1669 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001670 */
1671static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001672 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001673{
Boris Brezillon846031d2016-02-03 20:11:00 +01001674 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001675 int eccbytes = chip->ecc.bytes;
1676 int eccsteps = chip->ecc.steps;
1677 uint8_t *p = buf;
1678 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001679 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001680 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001681
1682 /* Read the OOB area first */
1683 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1684 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1685 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1686
Boris Brezillon846031d2016-02-03 20:11:00 +01001687 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1688 chip->ecc.total);
1689 if (ret)
1690 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001691
1692 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1693 int stat;
1694
1695 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1696 chip->read_buf(mtd, p, eccsize);
1697 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1698
1699 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001700 if (stat == -EBADMSG &&
1701 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1702 /* check for empty pages with bitflips */
1703 stat = nand_check_erased_ecc_chunk(p, eccsize,
1704 &ecc_code[i], eccbytes,
1705 NULL, 0,
1706 chip->ecc.strength);
1707 }
1708
Mike Dunn3f91e942012-04-25 12:06:09 -07001709 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001710 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001711 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001712 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001713 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1714 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001715 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001716 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001717}
1718
1719/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001720 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001721 * @mtd: mtd info structure
1722 * @chip: nand chip info structure
1723 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001724 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001725 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001726 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001727 * The hw generator calculates the error syndrome automatically. Therefore we
1728 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001729 */
1730static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001731 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001732{
1733 int i, eccsize = chip->ecc.size;
1734 int eccbytes = chip->ecc.bytes;
1735 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001736 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001737 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001738 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001739 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001740
1741 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1742 int stat;
1743
1744 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1745 chip->read_buf(mtd, p, eccsize);
1746
1747 if (chip->ecc.prepad) {
1748 chip->read_buf(mtd, oob, chip->ecc.prepad);
1749 oob += chip->ecc.prepad;
1750 }
1751
1752 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1753 chip->read_buf(mtd, oob, eccbytes);
1754 stat = chip->ecc.correct(mtd, p, oob, NULL);
1755
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001756 oob += eccbytes;
1757
1758 if (chip->ecc.postpad) {
1759 chip->read_buf(mtd, oob, chip->ecc.postpad);
1760 oob += chip->ecc.postpad;
1761 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001762
1763 if (stat == -EBADMSG &&
1764 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1765 /* check for empty pages with bitflips */
1766 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1767 oob - eccpadbytes,
1768 eccpadbytes,
1769 NULL, 0,
1770 chip->ecc.strength);
1771 }
1772
1773 if (stat < 0) {
1774 mtd->ecc_stats.failed++;
1775 } else {
1776 mtd->ecc_stats.corrected += stat;
1777 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1778 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001779 }
1780
1781 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001782 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001783 if (i)
1784 chip->read_buf(mtd, oob, i);
1785
Mike Dunn3f91e942012-04-25 12:06:09 -07001786 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001787}
1788
1789/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001790 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001791 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001792 * @oob: oob destination address
1793 * @ops: oob ops structure
1794 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001795 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001796static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001797 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001798{
Boris Brezillon846031d2016-02-03 20:11:00 +01001799 struct nand_chip *chip = mtd_to_nand(mtd);
1800 int ret;
1801
Florian Fainellif8ac0412010-09-07 13:23:43 +02001802 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001803
Brian Norris0612b9d2011-08-30 18:45:40 -07001804 case MTD_OPS_PLACE_OOB:
1805 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001806 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1807 return oob + len;
1808
Boris Brezillon846031d2016-02-03 20:11:00 +01001809 case MTD_OPS_AUTO_OOB:
1810 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1811 ops->ooboffs, len);
1812 BUG_ON(ret);
1813 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001814
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001815 default:
1816 BUG();
1817 }
1818 return NULL;
1819}
1820
1821/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001822 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1823 * @mtd: MTD device structure
1824 * @retry_mode: the retry mode to use
1825 *
1826 * Some vendors supply a special command to shift the Vt threshold, to be used
1827 * when there are too many bitflips in a page (i.e., ECC error). After setting
1828 * a new threshold, the host should retry reading the page.
1829 */
1830static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1831{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001832 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001833
1834 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1835
1836 if (retry_mode >= chip->read_retries)
1837 return -EINVAL;
1838
1839 if (!chip->setup_read_retry)
1840 return -EOPNOTSUPP;
1841
1842 return chip->setup_read_retry(mtd, retry_mode);
1843}
1844
1845/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001846 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001847 * @mtd: MTD device structure
1848 * @from: offset to read from
1849 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001850 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001851 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001852 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001853static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1854 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001855{
Brian Norrise47f3db2012-05-02 10:14:56 -07001856 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001857 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001858 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001859 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001860 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001861 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001862
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001863 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001864 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001865 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001866 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001867 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001869 chipnr = (int)(from >> chip->chip_shift);
1870 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001872 realpage = (int)(from >> chip->page_shift);
1873 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001875 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001877 buf = ops->datbuf;
1878 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001879 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001880
Florian Fainellif8ac0412010-09-07 13:23:43 +02001881 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001882 unsigned int ecc_failures = mtd->ecc_stats.failed;
1883
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001884 bytes = min(mtd->writesize - col, readlen);
1885 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001886
Kamal Dasu66507c72014-05-01 20:51:19 -04001887 if (!aligned)
1888 use_bufpoi = 1;
1889 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09001890 use_bufpoi = !virt_addr_valid(buf) ||
1891 !IS_ALIGNED((unsigned long)buf,
1892 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04001893 else
1894 use_bufpoi = 0;
1895
Brian Norris8b6e50c2011-05-25 14:59:01 -07001896 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001897 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001898 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1899
1900 if (use_bufpoi && aligned)
1901 pr_debug("%s: using read bounce buffer for buf@%p\n",
1902 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Brian Norrisba84fb52014-01-03 15:13:33 -08001904read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01001905 if (nand_standard_page_accessors(&chip->ecc))
1906 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Mike Dunnedbc45402012-04-25 12:06:11 -07001908 /*
1909 * Now read the page into the buffer. Absent an error,
1910 * the read methods return max bitflips per ecc step.
1911 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001912 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001913 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001914 oob_required,
1915 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001916 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1917 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001918 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001919 col, bytes, bufpoi,
1920 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001921 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001922 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001923 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001924 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001925 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001926 /* Invalidate page cache */
1927 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001928 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001929 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001930
1931 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001932 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001933 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001934 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001935 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001936 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001937 chip->pagebuf_bitflips = ret;
1938 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001939 /* Invalidate page cache */
1940 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001941 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001942 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001944
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001945 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001946 int toread = min(oobreadlen, max_oobsize);
1947
1948 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01001949 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001950 oob, ops, toread);
1951 oobreadlen -= toread;
1952 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001953 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001954
1955 if (chip->options & NAND_NEED_READRDY) {
1956 /* Apply delay or wait for ready/busy pin */
1957 if (!chip->dev_ready)
1958 udelay(chip->chip_delay);
1959 else
1960 nand_wait_ready(mtd);
1961 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001962
Brian Norrisba84fb52014-01-03 15:13:33 -08001963 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001964 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001965 retry_mode++;
1966 ret = nand_setup_read_retry(mtd,
1967 retry_mode);
1968 if (ret < 0)
1969 break;
1970
1971 /* Reset failures; retry */
1972 mtd->ecc_stats.failed = ecc_failures;
1973 goto read_retry;
1974 } else {
1975 /* No more retry modes; real failure */
1976 ecc_fail = true;
1977 }
1978 }
1979
1980 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09001981 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001982 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001983 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001984 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001985 max_bitflips = max_t(unsigned int, max_bitflips,
1986 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001989 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001990
Brian Norrisba84fb52014-01-03 15:13:33 -08001991 /* Reset to retry mode 0 */
1992 if (retry_mode) {
1993 ret = nand_setup_read_retry(mtd, 0);
1994 if (ret < 0)
1995 break;
1996 retry_mode = 0;
1997 }
1998
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001999 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002000 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Brian Norris8b6e50c2011-05-25 14:59:01 -07002002 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 col = 0;
2004 /* Increment page address */
2005 realpage++;
2006
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002007 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 /* Check, if we cross a chip boundary */
2009 if (!page) {
2010 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002011 chip->select_chip(mtd, -1);
2012 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002015 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002017 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002018 if (oob)
2019 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Mike Dunn3f91e942012-04-25 12:06:09 -07002021 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002022 return ret;
2023
Brian Norrisb72f3df2013-12-03 11:04:14 -08002024 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002025 return -EBADMSG;
2026
Mike Dunnedbc45402012-04-25 12:06:11 -07002027 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002028}
2029
2030/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002031 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002032 * @mtd: MTD device structure
2033 * @from: offset to read from
2034 * @len: number of bytes to read
2035 * @retlen: pointer to variable to store the number of read bytes
2036 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002037 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002038 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002039 */
2040static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2041 size_t *retlen, uint8_t *buf)
2042{
Brian Norris4a89ff82011-08-30 18:45:45 -07002043 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002044 int ret;
2045
Huang Shijie6a8214a2012-11-19 14:43:30 +08002046 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002047 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002048 ops.len = len;
2049 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002050 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002051 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002052 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002053 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002054 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
2057/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002058 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002059 * @mtd: mtd info structure
2060 * @chip: nand chip info structure
2061 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002062 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002063int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002064{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002065 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002066 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002067 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002068}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002069EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002070
2071/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002072 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002073 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002074 * @mtd: mtd info structure
2075 * @chip: nand chip info structure
2076 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002077 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002078int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2079 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002080{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002081 int length = mtd->oobsize;
2082 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2083 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002084 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002085 int i, toread, sndrnd = 0, pos;
2086
2087 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2088 for (i = 0; i < chip->ecc.steps; i++) {
2089 if (sndrnd) {
2090 pos = eccsize + i * (eccsize + chunk);
2091 if (mtd->writesize > 512)
2092 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2093 else
2094 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2095 } else
2096 sndrnd = 1;
2097 toread = min_t(int, length, chunk);
2098 chip->read_buf(mtd, bufpoi, toread);
2099 bufpoi += toread;
2100 length -= toread;
2101 }
2102 if (length > 0)
2103 chip->read_buf(mtd, bufpoi, length);
2104
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002105 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002106}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002107EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002108
2109/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002110 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002111 * @mtd: mtd info structure
2112 * @chip: nand chip info structure
2113 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002114 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002115int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002116{
2117 int status = 0;
2118 const uint8_t *buf = chip->oob_poi;
2119 int length = mtd->oobsize;
2120
2121 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2122 chip->write_buf(mtd, buf, length);
2123 /* Send command to program the OOB data */
2124 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2125
2126 status = chip->waitfunc(mtd, chip);
2127
Savin Zlobec0d420f92006-06-21 11:51:20 +02002128 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002129}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002130EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002131
2132/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002133 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002134 * with syndrome - only for large page flash
2135 * @mtd: mtd info structure
2136 * @chip: nand chip info structure
2137 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002138 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002139int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2140 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002141{
2142 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2143 int eccsize = chip->ecc.size, length = mtd->oobsize;
2144 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2145 const uint8_t *bufpoi = chip->oob_poi;
2146
2147 /*
2148 * data-ecc-data-ecc ... ecc-oob
2149 * or
2150 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2151 */
2152 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2153 pos = steps * (eccsize + chunk);
2154 steps = 0;
2155 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002156 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002157
2158 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2159 for (i = 0; i < steps; i++) {
2160 if (sndcmd) {
2161 if (mtd->writesize <= 512) {
2162 uint32_t fill = 0xFFFFFFFF;
2163
2164 len = eccsize;
2165 while (len > 0) {
2166 int num = min_t(int, len, 4);
2167 chip->write_buf(mtd, (uint8_t *)&fill,
2168 num);
2169 len -= num;
2170 }
2171 } else {
2172 pos = eccsize + i * (eccsize + chunk);
2173 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2174 }
2175 } else
2176 sndcmd = 1;
2177 len = min_t(int, length, chunk);
2178 chip->write_buf(mtd, bufpoi, len);
2179 bufpoi += len;
2180 length -= len;
2181 }
2182 if (length > 0)
2183 chip->write_buf(mtd, bufpoi, length);
2184
2185 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2186 status = chip->waitfunc(mtd, chip);
2187
2188 return status & NAND_STATUS_FAIL ? -EIO : 0;
2189}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002190EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002191
2192/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002193 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002194 * @mtd: MTD device structure
2195 * @from: offset to read from
2196 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002198 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002200static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2201 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Brian Norrisc00a0992012-05-01 17:12:54 -07002203 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002204 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002205 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002206 int readlen = ops->ooblen;
2207 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002208 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002209 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Brian Norris289c0522011-07-19 10:06:09 -07002211 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302212 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Brian Norris041e4572011-06-23 16:45:24 -07002214 stats = mtd->ecc_stats;
2215
Boris BREZILLON29f10582016-03-07 10:46:52 +01002216 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002217
2218 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002219 pr_debug("%s: attempt to start read outside oob\n",
2220 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002221 return -EINVAL;
2222 }
2223
2224 /* Do not allow reads past end of device */
2225 if (unlikely(from >= mtd->size ||
2226 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2227 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002228 pr_debug("%s: attempt to read beyond end of device\n",
2229 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002230 return -EINVAL;
2231 }
Vitaly Wool70145682006-11-03 18:20:38 +03002232
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002233 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002234 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002236 /* Shift to get page */
2237 realpage = (int)(from >> chip->page_shift);
2238 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Florian Fainellif8ac0412010-09-07 13:23:43 +02002240 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002241 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002242 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002243 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002244 ret = chip->ecc.read_oob(mtd, chip, page);
2245
2246 if (ret < 0)
2247 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002248
2249 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002250 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002251
Brian Norris5bc7c332013-03-13 09:51:31 -07002252 if (chip->options & NAND_NEED_READRDY) {
2253 /* Apply delay or wait for ready/busy pin */
2254 if (!chip->dev_ready)
2255 udelay(chip->chip_delay);
2256 else
2257 nand_wait_ready(mtd);
2258 }
2259
Vitaly Wool70145682006-11-03 18:20:38 +03002260 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002261 if (!readlen)
2262 break;
2263
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002264 /* Increment page address */
2265 realpage++;
2266
2267 page = realpage & chip->pagemask;
2268 /* Check, if we cross a chip boundary */
2269 if (!page) {
2270 chipnr++;
2271 chip->select_chip(mtd, -1);
2272 chip->select_chip(mtd, chipnr);
2273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002275 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002277 ops->oobretlen = ops->ooblen - readlen;
2278
2279 if (ret < 0)
2280 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002281
2282 if (mtd->ecc_stats.failed - stats.failed)
2283 return -EBADMSG;
2284
2285 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
2288/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002289 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002290 * @mtd: MTD device structure
2291 * @from: offset to read from
2292 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002294 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002296static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2297 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002299 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002300
2301 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002304 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002305 pr_debug("%s: attempt to read beyond end of device\n",
2306 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return -EINVAL;
2308 }
2309
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002310 if (ops->mode != MTD_OPS_PLACE_OOB &&
2311 ops->mode != MTD_OPS_AUTO_OOB &&
2312 ops->mode != MTD_OPS_RAW)
2313 return -ENOTSUPP;
2314
Huang Shijie6a8214a2012-11-19 14:43:30 +08002315 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002317 if (!ops->datbuf)
2318 ret = nand_do_read_oob(mtd, from, ops);
2319 else
2320 ret = nand_do_read_ops(mtd, from, ops);
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002323 return ret;
2324}
2325
2326
2327/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002328 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002329 * @mtd: mtd info structure
2330 * @chip: nand chip info structure
2331 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002332 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002333 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002334 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002335 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002336 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002337int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2338 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002339{
2340 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002341 if (oob_required)
2342 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002343
2344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002346EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002348/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002349 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002350 * @mtd: mtd info structure
2351 * @chip: nand chip info structure
2352 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002353 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002354 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002355 *
2356 * We need a special oob layout and handling even when ECC isn't checked.
2357 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002358static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002359 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002360 const uint8_t *buf, int oob_required,
2361 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002362{
2363 int eccsize = chip->ecc.size;
2364 int eccbytes = chip->ecc.bytes;
2365 uint8_t *oob = chip->oob_poi;
2366 int steps, size;
2367
2368 for (steps = chip->ecc.steps; steps > 0; steps--) {
2369 chip->write_buf(mtd, buf, eccsize);
2370 buf += eccsize;
2371
2372 if (chip->ecc.prepad) {
2373 chip->write_buf(mtd, oob, chip->ecc.prepad);
2374 oob += chip->ecc.prepad;
2375 }
2376
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002377 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002378 oob += eccbytes;
2379
2380 if (chip->ecc.postpad) {
2381 chip->write_buf(mtd, oob, chip->ecc.postpad);
2382 oob += chip->ecc.postpad;
2383 }
2384 }
2385
2386 size = mtd->oobsize - (oob - chip->oob_poi);
2387 if (size)
2388 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002389
2390 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002391}
2392/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002393 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002394 * @mtd: mtd info structure
2395 * @chip: nand chip info structure
2396 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002397 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002398 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002399 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002400static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002401 const uint8_t *buf, int oob_required,
2402 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002403{
Boris Brezillon846031d2016-02-03 20:11:00 +01002404 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002405 int eccbytes = chip->ecc.bytes;
2406 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002407 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002408 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002409
Brian Norris7854d3f2011-06-23 14:12:08 -07002410 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002411 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2412 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002413
Boris Brezillon846031d2016-02-03 20:11:00 +01002414 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2415 chip->ecc.total);
2416 if (ret)
2417 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002418
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002419 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002420}
2421
2422/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002423 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002424 * @mtd: mtd info structure
2425 * @chip: nand chip info structure
2426 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002427 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002428 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002429 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002430static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002431 const uint8_t *buf, int oob_required,
2432 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002433{
Boris Brezillon846031d2016-02-03 20:11:00 +01002434 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002435 int eccbytes = chip->ecc.bytes;
2436 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002437 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002438 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002439
2440 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2441 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002442 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002443 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2444 }
2445
Boris Brezillon846031d2016-02-03 20:11:00 +01002446 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2447 chip->ecc.total);
2448 if (ret)
2449 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002450
2451 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002452
2453 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002454}
2455
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302456
2457/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002458 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302459 * @mtd: mtd info structure
2460 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002461 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302462 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002463 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302464 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002465 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302466 */
2467static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2468 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002469 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002470 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302471{
2472 uint8_t *oob_buf = chip->oob_poi;
2473 uint8_t *ecc_calc = chip->buffers->ecccalc;
2474 int ecc_size = chip->ecc.size;
2475 int ecc_bytes = chip->ecc.bytes;
2476 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302477 uint32_t start_step = offset / ecc_size;
2478 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2479 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002480 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302481
2482 for (step = 0; step < ecc_steps; step++) {
2483 /* configure controller for WRITE access */
2484 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2485
2486 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002487 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302488
2489 /* mask ECC of un-touched subpages by padding 0xFF */
2490 if ((step < start_step) || (step > end_step))
2491 memset(ecc_calc, 0xff, ecc_bytes);
2492 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002493 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302494
2495 /* mask OOB of un-touched subpages by padding 0xFF */
2496 /* if oob_required, preserve OOB metadata of written subpage */
2497 if (!oob_required || (step < start_step) || (step > end_step))
2498 memset(oob_buf, 0xff, oob_bytes);
2499
Brian Norrisd6a950802013-08-08 17:16:36 -07002500 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302501 ecc_calc += ecc_bytes;
2502 oob_buf += oob_bytes;
2503 }
2504
2505 /* copy calculated ECC for whole page to chip->buffer->oob */
2506 /* this include masked-value(0xFF) for unwritten subpages */
2507 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002508 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2509 chip->ecc.total);
2510 if (ret)
2511 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302512
2513 /* write OOB buffer to NAND device */
2514 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2515
2516 return 0;
2517}
2518
2519
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002520/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002521 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002522 * @mtd: mtd info structure
2523 * @chip: nand chip info structure
2524 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002525 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002526 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002527 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002528 * The hw generator calculates the error syndrome automatically. Therefore we
2529 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002530 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002531static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002532 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002533 const uint8_t *buf, int oob_required,
2534 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002535{
2536 int i, eccsize = chip->ecc.size;
2537 int eccbytes = chip->ecc.bytes;
2538 int eccsteps = chip->ecc.steps;
2539 const uint8_t *p = buf;
2540 uint8_t *oob = chip->oob_poi;
2541
2542 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2543
2544 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2545 chip->write_buf(mtd, p, eccsize);
2546
2547 if (chip->ecc.prepad) {
2548 chip->write_buf(mtd, oob, chip->ecc.prepad);
2549 oob += chip->ecc.prepad;
2550 }
2551
2552 chip->ecc.calculate(mtd, p, oob);
2553 chip->write_buf(mtd, oob, eccbytes);
2554 oob += eccbytes;
2555
2556 if (chip->ecc.postpad) {
2557 chip->write_buf(mtd, oob, chip->ecc.postpad);
2558 oob += chip->ecc.postpad;
2559 }
2560 }
2561
2562 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002563 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002564 if (i)
2565 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002566
2567 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002568}
2569
2570/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002571 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002572 * @mtd: MTD device structure
2573 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302574 * @offset: address offset within the page
2575 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002576 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002577 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002578 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002579 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002580 */
2581static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302582 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002583 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002584{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302585 int status, subpage;
2586
2587 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2588 chip->ecc.write_subpage)
2589 subpage = offset || (data_len < mtd->writesize);
2590 else
2591 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002592
Marc Gonzalez3371d662016-11-15 10:56:20 +01002593 if (nand_standard_page_accessors(&chip->ecc))
2594 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002595
David Woodhouse956e9442006-09-25 17:12:39 +01002596 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302597 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002598 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302599 else if (subpage)
2600 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002601 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002602 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002603 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2604 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002605
2606 if (status < 0)
2607 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002608
Boris Brezillon41145642017-05-16 18:27:49 +02002609 if (nand_standard_page_accessors(&chip->ecc)) {
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002610 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002611
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002612 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002613 if (status & NAND_STATUS_FAIL)
2614 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002615 }
2616
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002617 return 0;
2618}
2619
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002620/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002621 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002622 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002623 * @oob: oob data buffer
2624 * @len: oob data write length
2625 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002626 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002627static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2628 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002629{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002630 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002631 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002632
2633 /*
2634 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2635 * data from a previous OOB read.
2636 */
2637 memset(chip->oob_poi, 0xff, mtd->oobsize);
2638
Florian Fainellif8ac0412010-09-07 13:23:43 +02002639 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002640
Brian Norris0612b9d2011-08-30 18:45:40 -07002641 case MTD_OPS_PLACE_OOB:
2642 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002643 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2644 return oob + len;
2645
Boris Brezillon846031d2016-02-03 20:11:00 +01002646 case MTD_OPS_AUTO_OOB:
2647 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2648 ops->ooboffs, len);
2649 BUG_ON(ret);
2650 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002651
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002652 default:
2653 BUG();
2654 }
2655 return NULL;
2656}
2657
Florian Fainellif8ac0412010-09-07 13:23:43 +02002658#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002659
2660/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002661 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002662 * @mtd: MTD device structure
2663 * @to: offset to write to
2664 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002665 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002666 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002667 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002668static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2669 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002670{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002671 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002672 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002673 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002674
2675 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002676 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002677
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002678 uint8_t *oob = ops->oobbuf;
2679 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302680 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002681 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002682
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002683 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002684 if (!writelen)
2685 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002686
Brian Norris8b6e50c2011-05-25 14:59:01 -07002687 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002688 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002689 pr_notice("%s: attempt to write non page aligned data\n",
2690 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002691 return -EINVAL;
2692 }
2693
Thomas Gleixner29072b92006-09-28 15:38:36 +02002694 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002695
Thomas Gleixner6a930962006-06-28 00:11:45 +02002696 chipnr = (int)(to >> chip->chip_shift);
2697 chip->select_chip(mtd, chipnr);
2698
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002699 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002700 if (nand_check_wp(mtd)) {
2701 ret = -EIO;
2702 goto err_out;
2703 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002704
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002705 realpage = (int)(to >> chip->page_shift);
2706 page = realpage & chip->pagemask;
2707 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2708
2709 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002710 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2711 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002712 chip->pagebuf = -1;
2713
Maxim Levitsky782ce792010-02-22 20:39:36 +02002714 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002715 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2716 ret = -EINVAL;
2717 goto err_out;
2718 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002719
Florian Fainellif8ac0412010-09-07 13:23:43 +02002720 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002721 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002722 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002723 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002724 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002725
Kamal Dasu66507c72014-05-01 20:51:19 -04002726 if (part_pagewr)
2727 use_bufpoi = 1;
2728 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002729 use_bufpoi = !virt_addr_valid(buf) ||
2730 !IS_ALIGNED((unsigned long)buf,
2731 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002732 else
2733 use_bufpoi = 0;
2734
2735 /* Partial page write?, or need to use bounce buffer */
2736 if (use_bufpoi) {
2737 pr_debug("%s: using write bounce buffer for buf@%p\n",
2738 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04002739 if (part_pagewr)
2740 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002741 chip->pagebuf = -1;
2742 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2743 memcpy(&chip->buffers->databuf[column], buf, bytes);
2744 wbuf = chip->buffers->databuf;
2745 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002746
Maxim Levitsky782ce792010-02-22 20:39:36 +02002747 if (unlikely(oob)) {
2748 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002749 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002750 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002751 } else {
2752 /* We still need to erase leftover OOB data */
2753 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002754 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002755
2756 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002757 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002758 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002759 if (ret)
2760 break;
2761
2762 writelen -= bytes;
2763 if (!writelen)
2764 break;
2765
Thomas Gleixner29072b92006-09-28 15:38:36 +02002766 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002767 buf += bytes;
2768 realpage++;
2769
2770 page = realpage & chip->pagemask;
2771 /* Check, if we cross a chip boundary */
2772 if (!page) {
2773 chipnr++;
2774 chip->select_chip(mtd, -1);
2775 chip->select_chip(mtd, chipnr);
2776 }
2777 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002778
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002779 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002780 if (unlikely(oob))
2781 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002782
2783err_out:
2784 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002785 return ret;
2786}
2787
2788/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002789 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002790 * @mtd: MTD device structure
2791 * @to: offset to write to
2792 * @len: number of bytes to write
2793 * @retlen: pointer to variable to store the number of written bytes
2794 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002795 *
2796 * NAND write with ECC. Used when performing writes in interrupt context, this
2797 * may for example be called by mtdoops when writing an oops while in panic.
2798 */
2799static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2800 size_t *retlen, const uint8_t *buf)
2801{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002802 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002803 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002804 int ret;
2805
Brian Norris8b6e50c2011-05-25 14:59:01 -07002806 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002807 panic_nand_wait(mtd, chip, 400);
2808
Brian Norris8b6e50c2011-05-25 14:59:01 -07002809 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002810 panic_nand_get_device(chip, mtd, FL_WRITING);
2811
Brian Norris0ec56dc2015-02-28 02:02:30 -08002812 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002813 ops.len = len;
2814 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002815 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002816
Brian Norris4a89ff82011-08-30 18:45:45 -07002817 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002818
Brian Norris4a89ff82011-08-30 18:45:45 -07002819 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002820 return ret;
2821}
2822
2823/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002824 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002825 * @mtd: MTD device structure
2826 * @to: offset to write to
2827 * @len: number of bytes to write
2828 * @retlen: pointer to variable to store the number of written bytes
2829 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002831 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002833static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002834 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
Brian Norris4a89ff82011-08-30 18:45:45 -07002836 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002837 int ret;
2838
Huang Shijie6a8214a2012-11-19 14:43:30 +08002839 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002840 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002841 ops.len = len;
2842 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002843 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002844 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002845 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002846 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002847 return ret;
2848}
2849
2850/**
2851 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002852 * @mtd: MTD device structure
2853 * @to: offset to write to
2854 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002855 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002856 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002857 */
2858static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2859 struct mtd_oob_ops *ops)
2860{
Adrian Hunter03736152007-01-31 17:58:29 +02002861 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002862 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Brian Norris289c0522011-07-19 10:06:09 -07002864 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302865 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Boris BREZILLON29f10582016-03-07 10:46:52 +01002867 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002870 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002871 pr_debug("%s: attempt to write past end of page\n",
2872 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 return -EINVAL;
2874 }
2875
Adrian Hunter03736152007-01-31 17:58:29 +02002876 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002877 pr_debug("%s: attempt to start write outside oob\n",
2878 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002879 return -EINVAL;
2880 }
2881
Jason Liu775adc3d42011-02-25 13:06:18 +08002882 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002883 if (unlikely(to >= mtd->size ||
2884 ops->ooboffs + ops->ooblen >
2885 ((mtd->size >> chip->page_shift) -
2886 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002887 pr_debug("%s: attempt to write beyond end of device\n",
2888 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002889 return -EINVAL;
2890 }
2891
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002892 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002893
2894 /*
2895 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2896 * of my DiskOnChip 2000 test units) will clear the whole data page too
2897 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2898 * it in the doc2000 driver in August 1999. dwmw2.
2899 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002900 nand_reset(chip, chipnr);
2901
2902 chip->select_chip(mtd, chipnr);
2903
2904 /* Shift to get page */
2905 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002908 if (nand_check_wp(mtd)) {
2909 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002910 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002911 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002914 if (page == chip->pagebuf)
2915 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002917 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002918
Brian Norris0612b9d2011-08-30 18:45:40 -07002919 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002920 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2921 else
2922 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002923
Huang Shijieb0bb6902012-11-19 14:43:29 +08002924 chip->select_chip(mtd, -1);
2925
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002926 if (status)
2927 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Vitaly Wool70145682006-11-03 18:20:38 +03002929 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002931 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002932}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002934/**
2935 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002936 * @mtd: MTD device structure
2937 * @to: offset to write to
2938 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002939 */
2940static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2941 struct mtd_oob_ops *ops)
2942{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002943 int ret = -ENOTSUPP;
2944
2945 ops->retlen = 0;
2946
2947 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002948 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002949 pr_debug("%s: attempt to write beyond end of device\n",
2950 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002951 return -EINVAL;
2952 }
2953
Huang Shijie6a8214a2012-11-19 14:43:30 +08002954 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002955
Florian Fainellif8ac0412010-09-07 13:23:43 +02002956 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002957 case MTD_OPS_PLACE_OOB:
2958 case MTD_OPS_AUTO_OOB:
2959 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002960 break;
2961
2962 default:
2963 goto out;
2964 }
2965
2966 if (!ops->datbuf)
2967 ret = nand_do_write_oob(mtd, to, ops);
2968 else
2969 ret = nand_do_write_ops(mtd, to, ops);
2970
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002971out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002972 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return ret;
2974}
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976/**
Brian Norris49c50b92014-05-06 16:02:19 -07002977 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002978 * @mtd: MTD device structure
2979 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 *
Brian Norris49c50b92014-05-06 16:02:19 -07002981 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 */
Brian Norris49c50b92014-05-06 16:02:19 -07002983static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002985 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002987 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2988 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002989
2990 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
2992
2993/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002995 * @mtd: MTD device structure
2996 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002998 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003000static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
David Woodhousee0c7d762006-05-13 18:07:53 +01003002 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003004
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003006 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003007 * @mtd: MTD device structure
3008 * @instr: erase instruction
3009 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003011 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003013int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3014 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Adrian Hunter69423d92008-12-10 13:37:21 +00003016 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003017 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003018 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Brian Norris289c0522011-07-19 10:06:09 -07003020 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3021 __func__, (unsigned long long)instr->addr,
3022 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303024 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003028 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003031 page = (int)(instr->addr >> chip->page_shift);
3032 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003035 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003038 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 /* Check, if it is write protected */
3041 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003042 pr_debug("%s: device is write protected!\n",
3043 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 instr->state = MTD_ERASE_FAILED;
3045 goto erase_exit;
3046 }
3047
3048 /* Loop through the pages */
3049 len = instr->len;
3050
3051 instr->state = MTD_ERASING;
3052
3053 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003054 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003055 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303056 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003057 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3058 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 instr->state = MTD_ERASE_FAILED;
3060 goto erase_exit;
3061 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003062
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003063 /*
3064 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003065 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003066 */
3067 if (page <= chip->pagebuf && chip->pagebuf <
3068 (page + pages_per_block))
3069 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Brian Norris49c50b92014-05-06 16:02:19 -07003071 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003074 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003075 pr_debug("%s: failed erase, page 0x%08x\n",
3076 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003078 instr->fail_addr =
3079 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 goto erase_exit;
3081 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003084 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 page += pages_per_block;
3086
3087 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003088 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003090 chip->select_chip(mtd, -1);
3091 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093 }
3094 instr->state = MTD_ERASE_DONE;
3095
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003096erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003101 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 nand_release_device(mtd);
3103
David Woodhouse49defc02007-10-06 15:01:59 -04003104 /* Do call back function */
3105 if (!ret)
3106 mtd_erase_callback(instr);
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 /* Return more or less happy */
3109 return ret;
3110}
3111
3112/**
3113 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003114 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003116 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003118static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Brian Norris289c0522011-07-19 10:06:09 -07003120 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
3122 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003123 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003125 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003129 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003130 * @mtd: MTD device structure
3131 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003133static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303135 struct nand_chip *chip = mtd_to_nand(mtd);
3136 int chipnr = (int)(offs >> chip->chip_shift);
3137 int ret;
3138
3139 /* Select the NAND device */
3140 nand_get_device(mtd, FL_READING);
3141 chip->select_chip(mtd, chipnr);
3142
3143 ret = nand_block_checkbad(mtd, offs, 0);
3144
3145 chip->select_chip(mtd, -1);
3146 nand_release_device(mtd);
3147
3148 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
3151/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003152 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003153 * @mtd: MTD device structure
3154 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003156static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 int ret;
3159
Florian Fainellif8ac0412010-09-07 13:23:43 +02003160 ret = nand_block_isbad(mtd, ofs);
3161 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003162 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 if (ret > 0)
3164 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003165 return ret;
3166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Brian Norris5a0edb22013-07-30 17:52:58 -07003168 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
3171/**
Zach Brown56718422017-01-10 13:30:20 -06003172 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3173 * @mtd: MTD device structure
3174 * @ofs: offset relative to mtd start
3175 * @len: length of mtd
3176 */
3177static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3178{
3179 struct nand_chip *chip = mtd_to_nand(mtd);
3180 u32 part_start_block;
3181 u32 part_end_block;
3182 u32 part_start_die;
3183 u32 part_end_die;
3184
3185 /*
3186 * max_bb_per_die and blocks_per_die used to determine
3187 * the maximum bad block count.
3188 */
3189 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3190 return -ENOTSUPP;
3191
3192 /* Get the start and end of the partition in erase blocks. */
3193 part_start_block = mtd_div_by_eb(ofs, mtd);
3194 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3195
3196 /* Get the start and end LUNs of the partition. */
3197 part_start_die = part_start_block / chip->blocks_per_die;
3198 part_end_die = part_end_block / chip->blocks_per_die;
3199
3200 /*
3201 * Look up the bad blocks per unit and multiply by the number of units
3202 * that the partition spans.
3203 */
3204 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3205}
3206
3207/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003208 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3209 * @mtd: MTD device structure
3210 * @chip: nand chip info structure
3211 * @addr: feature address.
3212 * @subfeature_param: the subfeature parameters, a four bytes array.
3213 */
3214static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3215 int addr, uint8_t *subfeature_param)
3216{
3217 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003218 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003219
David Mosbergerd914c932013-05-29 15:30:13 +03003220 if (!chip->onfi_version ||
3221 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3222 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003223 return -EINVAL;
3224
3225 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003226 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3227 chip->write_byte(mtd, subfeature_param[i]);
3228
Huang Shijie7db03ec2012-09-13 14:57:52 +08003229 status = chip->waitfunc(mtd, chip);
3230 if (status & NAND_STATUS_FAIL)
3231 return -EIO;
3232 return 0;
3233}
3234
3235/**
3236 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3237 * @mtd: MTD device structure
3238 * @chip: nand chip info structure
3239 * @addr: feature address.
3240 * @subfeature_param: the subfeature parameters, a four bytes array.
3241 */
3242static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3243 int addr, uint8_t *subfeature_param)
3244{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003245 int i;
3246
David Mosbergerd914c932013-05-29 15:30:13 +03003247 if (!chip->onfi_version ||
3248 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3249 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003250 return -EINVAL;
3251
Huang Shijie7db03ec2012-09-13 14:57:52 +08003252 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003253 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3254 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003255 return 0;
3256}
3257
3258/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003259 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3260 * -ENOTSUPP
3261 * @mtd: MTD device structure
3262 * @chip: nand chip info structure
3263 * @addr: feature address.
3264 * @subfeature_param: the subfeature parameters, a four bytes array.
3265 *
3266 * Should be used by NAND controller drivers that do not support the SET/GET
3267 * FEATURES operations.
3268 */
3269int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3270 struct nand_chip *chip, int addr,
3271 u8 *subfeature_param)
3272{
3273 return -ENOTSUPP;
3274}
3275EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3276
3277/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003278 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003279 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003280 */
3281static int nand_suspend(struct mtd_info *mtd)
3282{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003283 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003284}
3285
3286/**
3287 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003288 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003289 */
3290static void nand_resume(struct mtd_info *mtd)
3291{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003292 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003293
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003294 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003295 nand_release_device(mtd);
3296 else
Brian Norrisd0370212011-07-19 10:06:08 -07003297 pr_err("%s called for a chip which is not in suspended state\n",
3298 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003299}
3300
Scott Branden72ea4032014-11-20 11:18:05 -08003301/**
3302 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3303 * prevent further operations
3304 * @mtd: MTD device structure
3305 */
3306static void nand_shutdown(struct mtd_info *mtd)
3307{
Brian Norris9ca641b2015-11-09 16:37:28 -08003308 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003309}
3310
Brian Norris8b6e50c2011-05-25 14:59:01 -07003311/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003312static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003313{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003314 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003317 if (!chip->chip_delay)
3318 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
3320 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003321 if (chip->cmdfunc == NULL)
3322 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
3324 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003325 if (chip->waitfunc == NULL)
3326 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003328 if (!chip->select_chip)
3329 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003330
Huang Shijie4204ccc2013-08-16 10:10:07 +08003331 /* set for ONFI nand */
3332 if (!chip->onfi_set_features)
3333 chip->onfi_set_features = nand_onfi_set_features;
3334 if (!chip->onfi_get_features)
3335 chip->onfi_get_features = nand_onfi_get_features;
3336
Brian Norris68e80782013-07-18 01:17:02 -07003337 /* If called twice, pointers that depend on busw may need to be reset */
3338 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003339 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3340 if (!chip->read_word)
3341 chip->read_word = nand_read_word;
3342 if (!chip->block_bad)
3343 chip->block_bad = nand_block_bad;
3344 if (!chip->block_markbad)
3345 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003346 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003347 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003348 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3349 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003350 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003351 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003352 if (!chip->scan_bbt)
3353 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003354
3355 if (!chip->controller) {
3356 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003357 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003358 }
3359
Masahiro Yamada477544c2017-03-30 17:15:05 +09003360 if (!chip->buf_align)
3361 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003362}
3363
Brian Norris8b6e50c2011-05-25 14:59:01 -07003364/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003365static void sanitize_string(uint8_t *s, size_t len)
3366{
3367 ssize_t i;
3368
Brian Norris8b6e50c2011-05-25 14:59:01 -07003369 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003370 s[len - 1] = 0;
3371
Brian Norris8b6e50c2011-05-25 14:59:01 -07003372 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003373 for (i = 0; i < len - 1; i++) {
3374 if (s[i] < ' ' || s[i] > 127)
3375 s[i] = '?';
3376 }
3377
Brian Norris8b6e50c2011-05-25 14:59:01 -07003378 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003379 strim(s);
3380}
3381
3382static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3383{
3384 int i;
3385 while (len--) {
3386 crc ^= *p++ << 8;
3387 for (i = 0; i < 8; i++)
3388 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3389 }
3390
3391 return crc;
3392}
3393
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003394/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003395static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3396 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003397{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003398 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003399 struct onfi_ext_param_page *ep;
3400 struct onfi_ext_section *s;
3401 struct onfi_ext_ecc_info *ecc;
3402 uint8_t *cursor;
3403 int ret = -EINVAL;
3404 int len;
3405 int i;
3406
3407 len = le16_to_cpu(p->ext_param_page_length) * 16;
3408 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003409 if (!ep)
3410 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003411
3412 /* Send our own NAND_CMD_PARAM. */
3413 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3414
3415 /* Use the Change Read Column command to skip the ONFI param pages. */
3416 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3417 sizeof(*p) * p->num_of_param_pages , -1);
3418
3419 /* Read out the Extended Parameter Page. */
3420 chip->read_buf(mtd, (uint8_t *)ep, len);
3421 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3422 != le16_to_cpu(ep->crc))) {
3423 pr_debug("fail in the CRC.\n");
3424 goto ext_out;
3425 }
3426
3427 /*
3428 * Check the signature.
3429 * Do not strictly follow the ONFI spec, maybe changed in future.
3430 */
3431 if (strncmp(ep->sig, "EPPS", 4)) {
3432 pr_debug("The signature is invalid.\n");
3433 goto ext_out;
3434 }
3435
3436 /* find the ECC section. */
3437 cursor = (uint8_t *)(ep + 1);
3438 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3439 s = ep->sections + i;
3440 if (s->type == ONFI_SECTION_TYPE_2)
3441 break;
3442 cursor += s->length * 16;
3443 }
3444 if (i == ONFI_EXT_SECTION_MAX) {
3445 pr_debug("We can not find the ECC section.\n");
3446 goto ext_out;
3447 }
3448
3449 /* get the info we want. */
3450 ecc = (struct onfi_ext_ecc_info *)cursor;
3451
Brian Norris4ae7d222013-09-16 18:20:21 -07003452 if (!ecc->codeword_size) {
3453 pr_debug("Invalid codeword size\n");
3454 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003455 }
3456
Brian Norris4ae7d222013-09-16 18:20:21 -07003457 chip->ecc_strength_ds = ecc->ecc_bits;
3458 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003459 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003460
3461ext_out:
3462 kfree(ep);
3463 return ret;
3464}
3465
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003466/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003467 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003468 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003469static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003470{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003471 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003472 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003473 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003474 int val;
3475
Brian Norris7854d3f2011-06-23 14:12:08 -07003476 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003477 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3478 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3479 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3480 return 0;
3481
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003482 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3483 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003484 for (j = 0; j < sizeof(*p); j++)
3485 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003486 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3487 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003488 break;
3489 }
3490 }
3491
Brian Norrisc7f23a72013-08-13 10:51:55 -07003492 if (i == 3) {
3493 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003494 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003495 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003496
Brian Norris8b6e50c2011-05-25 14:59:01 -07003497 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003498 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003499 if (val & (1 << 5))
3500 chip->onfi_version = 23;
3501 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003502 chip->onfi_version = 22;
3503 else if (val & (1 << 3))
3504 chip->onfi_version = 21;
3505 else if (val & (1 << 2))
3506 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003507 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003508 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003509
3510 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003511 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003512 return 0;
3513 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003514
3515 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3516 sanitize_string(p->model, sizeof(p->model));
3517 if (!mtd->name)
3518 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003519
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003520 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003521
3522 /*
3523 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3524 * (don't ask me who thought of this...). MTD assumes that these
3525 * dimensions will be power-of-2, so just truncate the remaining area.
3526 */
3527 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3528 mtd->erasesize *= mtd->writesize;
3529
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003530 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003531
3532 /* See erasesize comment */
3533 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003534 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003535 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003536
Zach Brown34da5f52017-01-10 13:30:21 -06003537 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3538 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3539
Huang Shijiee2985fc2013-05-17 11:17:30 +08003540 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003541 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003542
Huang Shijie10c86ba2013-05-17 11:17:26 +08003543 if (p->ecc_bits != 0xff) {
3544 chip->ecc_strength_ds = p->ecc_bits;
3545 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003546 } else if (chip->onfi_version >= 21 &&
3547 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3548
3549 /*
3550 * The nand_flash_detect_ext_param_page() uses the
3551 * Change Read Column command which maybe not supported
3552 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3553 * now. We do not replace user supplied command function.
3554 */
3555 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3556 chip->cmdfunc = nand_command_lp;
3557
3558 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003559 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003560 pr_warn("Failed to detect ONFI extended param page\n");
3561 } else {
3562 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003563 }
3564
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003565 return 1;
3566}
3567
3568/*
Huang Shijie91361812014-02-21 13:39:40 +08003569 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3570 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003571static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003572{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003573 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003574 struct nand_jedec_params *p = &chip->jedec_params;
3575 struct jedec_ecc_info *ecc;
3576 int val;
3577 int i, j;
3578
3579 /* Try JEDEC for unknown chip or LP */
3580 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3581 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3582 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3583 chip->read_byte(mtd) != 'C')
3584 return 0;
3585
3586 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3587 for (i = 0; i < 3; i++) {
3588 for (j = 0; j < sizeof(*p); j++)
3589 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3590
3591 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3592 le16_to_cpu(p->crc))
3593 break;
3594 }
3595
3596 if (i == 3) {
3597 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3598 return 0;
3599 }
3600
3601 /* Check version */
3602 val = le16_to_cpu(p->revision);
3603 if (val & (1 << 2))
3604 chip->jedec_version = 10;
3605 else if (val & (1 << 1))
3606 chip->jedec_version = 1; /* vendor specific version */
3607
3608 if (!chip->jedec_version) {
3609 pr_info("unsupported JEDEC version: %d\n", val);
3610 return 0;
3611 }
3612
3613 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3614 sanitize_string(p->model, sizeof(p->model));
3615 if (!mtd->name)
3616 mtd->name = p->model;
3617
3618 mtd->writesize = le32_to_cpu(p->byte_per_page);
3619
3620 /* Please reference to the comment for nand_flash_detect_onfi. */
3621 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3622 mtd->erasesize *= mtd->writesize;
3623
3624 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3625
3626 /* Please reference to the comment for nand_flash_detect_onfi. */
3627 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3628 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3629 chip->bits_per_cell = p->bits_per_cell;
3630
3631 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003632 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003633
3634 /* ECC info */
3635 ecc = &p->ecc_info[0];
3636
3637 if (ecc->codeword_size >= 9) {
3638 chip->ecc_strength_ds = ecc->ecc_bits;
3639 chip->ecc_step_ds = 1 << ecc->codeword_size;
3640 } else {
3641 pr_warn("Invalid codeword size\n");
3642 }
3643
3644 return 1;
3645}
3646
3647/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003648 * nand_id_has_period - Check if an ID string has a given wraparound period
3649 * @id_data: the ID string
3650 * @arrlen: the length of the @id_data array
3651 * @period: the period of repitition
3652 *
3653 * Check if an ID string is repeated within a given sequence of bytes at
3654 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003655 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003656 * if the repetition has a period of @period; otherwise, returns zero.
3657 */
3658static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3659{
3660 int i, j;
3661 for (i = 0; i < period; i++)
3662 for (j = i + period; j < arrlen; j += period)
3663 if (id_data[i] != id_data[j])
3664 return 0;
3665 return 1;
3666}
3667
3668/*
3669 * nand_id_len - Get the length of an ID string returned by CMD_READID
3670 * @id_data: the ID string
3671 * @arrlen: the length of the @id_data array
3672
3673 * Returns the length of the ID string, according to known wraparound/trailing
3674 * zero patterns. If no pattern exists, returns the length of the array.
3675 */
3676static int nand_id_len(u8 *id_data, int arrlen)
3677{
3678 int last_nonzero, period;
3679
3680 /* Find last non-zero byte */
3681 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3682 if (id_data[last_nonzero])
3683 break;
3684
3685 /* All zeros */
3686 if (last_nonzero < 0)
3687 return 0;
3688
3689 /* Calculate wraparound period */
3690 for (period = 1; period < arrlen; period++)
3691 if (nand_id_has_period(id_data, arrlen, period))
3692 break;
3693
3694 /* There's a repeated pattern */
3695 if (period < arrlen)
3696 return period;
3697
3698 /* There are trailing zeros */
3699 if (last_nonzero < arrlen - 1)
3700 return last_nonzero + 1;
3701
3702 /* No pattern detected */
3703 return arrlen;
3704}
3705
Huang Shijie7db906b2013-09-25 14:58:11 +08003706/* Extract the bits of per cell from the 3rd byte of the extended ID */
3707static int nand_get_bits_per_cell(u8 cellinfo)
3708{
3709 int bits;
3710
3711 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3712 bits >>= NAND_CI_CELLTYPE_SHIFT;
3713 return bits + 1;
3714}
3715
Brian Norrise3b88bd2012-09-24 20:40:52 -07003716/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003717 * Many new NAND share similar device ID codes, which represent the size of the
3718 * chip. The rest of the parameters must be decoded according to generic or
3719 * manufacturer-specific "extended ID" decoding patterns.
3720 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003721void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003722{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003723 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003724 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003725 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003726 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003727 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003728 /* The 4th id byte is the important one */
3729 extid = id_data[3];
3730
Boris Brezillon01389b62016-06-08 10:30:18 +02003731 /* Calc pagesize */
3732 mtd->writesize = 1024 << (extid & 0x03);
3733 extid >>= 2;
3734 /* Calc oobsize */
3735 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3736 extid >>= 2;
3737 /* Calc blocksize. Blocksize is multiples of 64KiB */
3738 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3739 extid >>= 2;
3740 /* Get buswidth information */
3741 if (extid & 0x1)
3742 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003743}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003744EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003745
3746/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003747 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3748 * decodes a matching ID table entry and assigns the MTD size parameters for
3749 * the chip.
3750 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003751static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003752{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003753 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003754
3755 mtd->erasesize = type->erasesize;
3756 mtd->writesize = type->pagesize;
3757 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003758
Huang Shijie1c195e92013-09-25 14:58:12 +08003759 /* All legacy ID NAND are small-page, SLC */
3760 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003761}
3762
3763/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003764 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3765 * heuristic patterns using various detected parameters (e.g., manufacturer,
3766 * page size, cell-type information).
3767 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003768static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003769{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003770 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003771
3772 /* Set the bad block position */
3773 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3774 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3775 else
3776 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003777}
3778
Huang Shijieec6e87e2013-03-15 11:01:00 +08003779static inline bool is_full_id_nand(struct nand_flash_dev *type)
3780{
3781 return type->id_len;
3782}
3783
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003784static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003785 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003786{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003787 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003788 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003789
Huang Shijieec6e87e2013-03-15 11:01:00 +08003790 if (!strncmp(type->id, id_data, type->id_len)) {
3791 mtd->writesize = type->pagesize;
3792 mtd->erasesize = type->erasesize;
3793 mtd->oobsize = type->oobsize;
3794
Huang Shijie7db906b2013-09-25 14:58:11 +08003795 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003796 chip->chipsize = (uint64_t)type->chipsize << 20;
3797 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003798 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3799 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003800 chip->onfi_timing_mode_default =
3801 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003802
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003803 if (!mtd->name)
3804 mtd->name = type->name;
3805
Huang Shijieec6e87e2013-03-15 11:01:00 +08003806 return true;
3807 }
3808 return false;
3809}
3810
Brian Norris7e74c2d2012-09-24 20:40:49 -07003811/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003812 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3813 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3814 * table.
3815 */
3816static void nand_manufacturer_detect(struct nand_chip *chip)
3817{
3818 /*
3819 * Try manufacturer detection if available and use
3820 * nand_decode_ext_id() otherwise.
3821 */
3822 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003823 chip->manufacturer.desc->ops->detect) {
3824 /* The 3rd id byte holds MLC / multichip data */
3825 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003826 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003827 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003828 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003829 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003830}
3831
3832/*
3833 * Manufacturer initialization. This function is called for all NANDs including
3834 * ONFI and JEDEC compliant ones.
3835 * Manufacturer drivers should put all their specific initialization code in
3836 * their ->init() hook.
3837 */
3838static int nand_manufacturer_init(struct nand_chip *chip)
3839{
3840 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
3841 !chip->manufacturer.desc->ops->init)
3842 return 0;
3843
3844 return chip->manufacturer.desc->ops->init(chip);
3845}
3846
3847/*
3848 * Manufacturer cleanup. This function is called for all NANDs including
3849 * ONFI and JEDEC compliant ones.
3850 * Manufacturer drivers should put all their specific cleanup code in their
3851 * ->cleanup() hook.
3852 */
3853static void nand_manufacturer_cleanup(struct nand_chip *chip)
3854{
3855 /* Release manufacturer private data */
3856 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3857 chip->manufacturer.desc->ops->cleanup)
3858 chip->manufacturer.desc->ops->cleanup(chip);
3859}
3860
3861/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003862 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003863 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02003864static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003865{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003866 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003867 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08003868 int busw;
Boris Brezillonf84674b2017-06-02 12:18:24 +02003869 int i;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003870 u8 *id_data = chip->id.data;
3871 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Karl Beldanef89a882008-09-15 14:37:29 +02003873 /*
3874 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003875 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003876 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003877 nand_reset(chip, 0);
3878
3879 /* Select the device */
3880 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02003881
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003883 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003886 maf_id = chip->read_byte(mtd);
3887 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Brian Norris8b6e50c2011-05-25 14:59:01 -07003889 /*
3890 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003891 * interface concerns can cause random data which looks like a
3892 * possibly credible NAND flash to appear. If the two results do
3893 * not match, ignore the device completely.
3894 */
3895
3896 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3897
Brian Norris4aef9b72012-09-24 20:40:48 -07003898 /* Read entire ID string */
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003899 for (i = 0; i < ARRAY_SIZE(chip->id.data); i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003900 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003901
Boris Brezillon7f501f02016-05-24 19:20:05 +02003902 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003903 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003904 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003905 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01003906 }
3907
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003908 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02003909
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003910 /* Try to identify manufacturer */
3911 manufacturer = nand_get_manufacturer(maf_id);
3912 chip->manufacturer.desc = manufacturer;
3913
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003914 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003915 type = nand_flash_ids;
3916
Boris Brezillon29a198a2016-05-24 20:17:48 +02003917 /*
3918 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
3919 * override it.
3920 * This is required to make sure initial NAND bus width set by the
3921 * NAND controller driver is coherent with the real NAND bus width
3922 * (extracted by auto-detection code).
3923 */
3924 busw = chip->options & NAND_BUSWIDTH_16;
3925
3926 /*
3927 * The flag is only set (never cleared), reset it to its default value
3928 * before starting auto-detection.
3929 */
3930 chip->options &= ~NAND_BUSWIDTH_16;
3931
Huang Shijieec6e87e2013-03-15 11:01:00 +08003932 for (; type->name != NULL; type++) {
3933 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003934 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08003935 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003936 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003937 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003938 }
3939 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003940
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003941 chip->onfi_version = 0;
3942 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003943 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003944 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003945 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003946
3947 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003948 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08003949 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003950 }
3951
David Woodhouse5e81e882010-02-26 18:32:56 +00003952 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003953 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003954
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02003955 if (!mtd->name)
3956 mtd->name = type->name;
3957
Adrian Hunter69423d92008-12-10 13:37:21 +00003958 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003959
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003960 if (!type->pagesize)
3961 nand_manufacturer_detect(chip);
3962 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02003963 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003964
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003965 /* Get chip options */
3966 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003967
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003968ident_done:
3969
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003970 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003971 WARN_ON(busw & NAND_BUSWIDTH_16);
3972 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003973 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3974 /*
3975 * Check, if buswidth is correct. Hardware drivers should set
3976 * chip correct!
3977 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003978 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003979 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003980 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
3981 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02003982 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
3983 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003984 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003985 }
3986
Boris Brezillon7f501f02016-05-24 19:20:05 +02003987 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003988
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003989 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003990 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003991 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003992 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003993
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003994 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003995 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003996 if (chip->chipsize & 0xffffffff)
3997 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003998 else {
3999 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4000 chip->chip_shift += 32 - 1;
4001 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004002
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004003 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004004 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004005
Brian Norris8b6e50c2011-05-25 14:59:01 -07004006 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004007 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4008 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004009
Ezequiel Garcia20171642013-11-25 08:30:31 -03004010 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004011 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004012
4013 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004014 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4015 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004016 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004017 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4018 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004019 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004020 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4021 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004022
Rafał Miłecki3755a992014-10-21 00:01:04 +02004023 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004024 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004025 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004026 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004027}
4028
Boris Brezillond48f62b2016-04-01 14:54:32 +02004029static const char * const nand_ecc_modes[] = {
4030 [NAND_ECC_NONE] = "none",
4031 [NAND_ECC_SOFT] = "soft",
4032 [NAND_ECC_HW] = "hw",
4033 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4034 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004035 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004036};
4037
4038static int of_get_nand_ecc_mode(struct device_node *np)
4039{
4040 const char *pm;
4041 int err, i;
4042
4043 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4044 if (err < 0)
4045 return err;
4046
4047 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4048 if (!strcasecmp(pm, nand_ecc_modes[i]))
4049 return i;
4050
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004051 /*
4052 * For backward compatibility we support few obsoleted values that don't
4053 * have their mappings into nand_ecc_modes_t anymore (they were merged
4054 * with other enums).
4055 */
4056 if (!strcasecmp(pm, "soft_bch"))
4057 return NAND_ECC_SOFT;
4058
Boris Brezillond48f62b2016-04-01 14:54:32 +02004059 return -ENODEV;
4060}
4061
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004062static const char * const nand_ecc_algos[] = {
4063 [NAND_ECC_HAMMING] = "hamming",
4064 [NAND_ECC_BCH] = "bch",
4065};
4066
Boris Brezillond48f62b2016-04-01 14:54:32 +02004067static int of_get_nand_ecc_algo(struct device_node *np)
4068{
4069 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004070 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004071
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004072 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4073 if (!err) {
4074 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4075 if (!strcasecmp(pm, nand_ecc_algos[i]))
4076 return i;
4077 return -ENODEV;
4078 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004079
4080 /*
4081 * For backward compatibility we also read "nand-ecc-mode" checking
4082 * for some obsoleted values that were specifying ECC algorithm.
4083 */
4084 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4085 if (err < 0)
4086 return err;
4087
4088 if (!strcasecmp(pm, "soft"))
4089 return NAND_ECC_HAMMING;
4090 else if (!strcasecmp(pm, "soft_bch"))
4091 return NAND_ECC_BCH;
4092
4093 return -ENODEV;
4094}
4095
4096static int of_get_nand_ecc_step_size(struct device_node *np)
4097{
4098 int ret;
4099 u32 val;
4100
4101 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4102 return ret ? ret : val;
4103}
4104
4105static int of_get_nand_ecc_strength(struct device_node *np)
4106{
4107 int ret;
4108 u32 val;
4109
4110 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4111 return ret ? ret : val;
4112}
4113
4114static int of_get_nand_bus_width(struct device_node *np)
4115{
4116 u32 val;
4117
4118 if (of_property_read_u32(np, "nand-bus-width", &val))
4119 return 8;
4120
4121 switch (val) {
4122 case 8:
4123 case 16:
4124 return val;
4125 default:
4126 return -EIO;
4127 }
4128}
4129
4130static bool of_get_nand_on_flash_bbt(struct device_node *np)
4131{
4132 return of_property_read_bool(np, "nand-on-flash-bbt");
4133}
4134
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004135static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004136{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004137 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004138 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004139
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004140 if (!dn)
4141 return 0;
4142
Brian Norris5844fee2015-01-23 00:22:27 -08004143 if (of_get_nand_bus_width(dn) == 16)
4144 chip->options |= NAND_BUSWIDTH_16;
4145
4146 if (of_get_nand_on_flash_bbt(dn))
4147 chip->bbt_options |= NAND_BBT_USE_FLASH;
4148
4149 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004150 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004151 ecc_strength = of_get_nand_ecc_strength(dn);
4152 ecc_step = of_get_nand_ecc_step_size(dn);
4153
Brian Norris5844fee2015-01-23 00:22:27 -08004154 if (ecc_mode >= 0)
4155 chip->ecc.mode = ecc_mode;
4156
Rafał Miłecki79082452016-03-23 11:19:02 +01004157 if (ecc_algo >= 0)
4158 chip->ecc.algo = ecc_algo;
4159
Brian Norris5844fee2015-01-23 00:22:27 -08004160 if (ecc_strength >= 0)
4161 chip->ecc.strength = ecc_strength;
4162
4163 if (ecc_step > 0)
4164 chip->ecc.size = ecc_step;
4165
Boris Brezillonba78ee02016-06-08 17:04:22 +02004166 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4167 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4168
Brian Norris5844fee2015-01-23 00:22:27 -08004169 return 0;
4170}
4171
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004172/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004173 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004174 * @mtd: MTD device structure
4175 * @maxchips: number of chips to scan for
4176 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004177 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004178 * This is the first phase of the normal nand_scan() function. It reads the
4179 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004180 *
4181 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004182int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4183 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004184{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004185 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004186 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004187 int ret;
4188
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004189 ret = nand_dt_init(chip);
4190 if (ret)
4191 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004192
Brian Norrisf7a8e382016-01-05 10:39:45 -08004193 if (!mtd->name && mtd->dev.parent)
4194 mtd->name = dev_name(mtd->dev.parent);
4195
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004196 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4197 /*
4198 * Default functions assigned for chip_select() and
4199 * cmdfunc() both expect cmd_ctrl() to be populated,
4200 * so we need to check that that's the case
4201 */
4202 pr_err("chip.cmd_ctrl() callback is not provided");
4203 return -EINVAL;
4204 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004205 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004206 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004207
4208 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004209 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004210 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004211 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004212 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004213 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004214 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 }
4216
Boris Brezillon7f501f02016-05-24 19:20:05 +02004217 nand_maf_id = chip->id.data[0];
4218 nand_dev_id = chip->id.data[1];
4219
Huang Shijie07300162012-11-09 16:23:45 +08004220 chip->select_chip(mtd, -1);
4221
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004222 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004223 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004224 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004225 nand_reset(chip, i);
4226
4227 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004229 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004231 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004232 nand_dev_id != chip->read_byte(mtd)) {
4233 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 break;
Huang Shijie07300162012-11-09 16:23:45 +08004235 }
4236 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 }
4238 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004239 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004242 chip->numchips = i;
4243 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
David Woodhouse3b85c322006-09-25 17:06:53 +01004245 return 0;
4246}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004247EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004248
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004249static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4250{
4251 struct nand_chip *chip = mtd_to_nand(mtd);
4252 struct nand_ecc_ctrl *ecc = &chip->ecc;
4253
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004254 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004255 return -EINVAL;
4256
4257 switch (ecc->algo) {
4258 case NAND_ECC_HAMMING:
4259 ecc->calculate = nand_calculate_ecc;
4260 ecc->correct = nand_correct_data;
4261 ecc->read_page = nand_read_page_swecc;
4262 ecc->read_subpage = nand_read_subpage;
4263 ecc->write_page = nand_write_page_swecc;
4264 ecc->read_page_raw = nand_read_page_raw;
4265 ecc->write_page_raw = nand_write_page_raw;
4266 ecc->read_oob = nand_read_oob_std;
4267 ecc->write_oob = nand_write_oob_std;
4268 if (!ecc->size)
4269 ecc->size = 256;
4270 ecc->bytes = 3;
4271 ecc->strength = 1;
4272 return 0;
4273 case NAND_ECC_BCH:
4274 if (!mtd_nand_has_bch()) {
4275 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4276 return -EINVAL;
4277 }
4278 ecc->calculate = nand_bch_calculate_ecc;
4279 ecc->correct = nand_bch_correct_data;
4280 ecc->read_page = nand_read_page_swecc;
4281 ecc->read_subpage = nand_read_subpage;
4282 ecc->write_page = nand_write_page_swecc;
4283 ecc->read_page_raw = nand_read_page_raw;
4284 ecc->write_page_raw = nand_write_page_raw;
4285 ecc->read_oob = nand_read_oob_std;
4286 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004287
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004288 /*
4289 * Board driver should supply ecc.size and ecc.strength
4290 * values to select how many bits are correctable.
4291 * Otherwise, default to 4 bits for large page devices.
4292 */
4293 if (!ecc->size && (mtd->oobsize >= 64)) {
4294 ecc->size = 512;
4295 ecc->strength = 4;
4296 }
4297
4298 /*
4299 * if no ecc placement scheme was provided pickup the default
4300 * large page one.
4301 */
4302 if (!mtd->ooblayout) {
4303 /* handle large page devices only */
4304 if (mtd->oobsize < 64) {
4305 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4306 return -EINVAL;
4307 }
4308
4309 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004310
4311 }
4312
4313 /*
4314 * We can only maximize ECC config when the default layout is
4315 * used, otherwise we don't know how many bytes can really be
4316 * used.
4317 */
4318 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4319 ecc->options & NAND_ECC_MAXIMIZE) {
4320 int steps, bytes;
4321
4322 /* Always prefer 1k blocks over 512bytes ones */
4323 ecc->size = 1024;
4324 steps = mtd->writesize / ecc->size;
4325
4326 /* Reserve 2 bytes for the BBM */
4327 bytes = (mtd->oobsize - 2) / steps;
4328 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004329 }
4330
4331 /* See nand_bch_init() for details. */
4332 ecc->bytes = 0;
4333 ecc->priv = nand_bch_init(mtd);
4334 if (!ecc->priv) {
4335 WARN(1, "BCH ECC initialization failed!\n");
4336 return -EINVAL;
4337 }
4338 return 0;
4339 default:
4340 WARN(1, "Unsupported ECC algorithm!\n");
4341 return -EINVAL;
4342 }
4343}
4344
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09004345/**
4346 * nand_check_ecc_caps - check the sanity of preset ECC settings
4347 * @chip: nand chip info structure
4348 * @caps: ECC caps info structure
4349 * @oobavail: OOB size that the ECC engine can use
4350 *
4351 * When ECC step size and strength are already set, check if they are supported
4352 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4353 * On success, the calculated ECC bytes is set.
4354 */
4355int nand_check_ecc_caps(struct nand_chip *chip,
4356 const struct nand_ecc_caps *caps, int oobavail)
4357{
4358 struct mtd_info *mtd = nand_to_mtd(chip);
4359 const struct nand_ecc_step_info *stepinfo;
4360 int preset_step = chip->ecc.size;
4361 int preset_strength = chip->ecc.strength;
4362 int nsteps, ecc_bytes;
4363 int i, j;
4364
4365 if (WARN_ON(oobavail < 0))
4366 return -EINVAL;
4367
4368 if (!preset_step || !preset_strength)
4369 return -ENODATA;
4370
4371 nsteps = mtd->writesize / preset_step;
4372
4373 for (i = 0; i < caps->nstepinfos; i++) {
4374 stepinfo = &caps->stepinfos[i];
4375
4376 if (stepinfo->stepsize != preset_step)
4377 continue;
4378
4379 for (j = 0; j < stepinfo->nstrengths; j++) {
4380 if (stepinfo->strengths[j] != preset_strength)
4381 continue;
4382
4383 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4384 preset_strength);
4385 if (WARN_ON_ONCE(ecc_bytes < 0))
4386 return ecc_bytes;
4387
4388 if (ecc_bytes * nsteps > oobavail) {
4389 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4390 preset_step, preset_strength);
4391 return -ENOSPC;
4392 }
4393
4394 chip->ecc.bytes = ecc_bytes;
4395
4396 return 0;
4397 }
4398 }
4399
4400 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4401 preset_step, preset_strength);
4402
4403 return -ENOTSUPP;
4404}
4405EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4406
4407/**
4408 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4409 * @chip: nand chip info structure
4410 * @caps: ECC engine caps info structure
4411 * @oobavail: OOB size that the ECC engine can use
4412 *
4413 * If a chip's ECC requirement is provided, try to meet it with the least
4414 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4415 * On success, the chosen ECC settings are set.
4416 */
4417int nand_match_ecc_req(struct nand_chip *chip,
4418 const struct nand_ecc_caps *caps, int oobavail)
4419{
4420 struct mtd_info *mtd = nand_to_mtd(chip);
4421 const struct nand_ecc_step_info *stepinfo;
4422 int req_step = chip->ecc_step_ds;
4423 int req_strength = chip->ecc_strength_ds;
4424 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4425 int best_step, best_strength, best_ecc_bytes;
4426 int best_ecc_bytes_total = INT_MAX;
4427 int i, j;
4428
4429 if (WARN_ON(oobavail < 0))
4430 return -EINVAL;
4431
4432 /* No information provided by the NAND chip */
4433 if (!req_step || !req_strength)
4434 return -ENOTSUPP;
4435
4436 /* number of correctable bits the chip requires in a page */
4437 req_corr = mtd->writesize / req_step * req_strength;
4438
4439 for (i = 0; i < caps->nstepinfos; i++) {
4440 stepinfo = &caps->stepinfos[i];
4441 step_size = stepinfo->stepsize;
4442
4443 for (j = 0; j < stepinfo->nstrengths; j++) {
4444 strength = stepinfo->strengths[j];
4445
4446 /*
4447 * If both step size and strength are smaller than the
4448 * chip's requirement, it is not easy to compare the
4449 * resulted reliability.
4450 */
4451 if (step_size < req_step && strength < req_strength)
4452 continue;
4453
4454 if (mtd->writesize % step_size)
4455 continue;
4456
4457 nsteps = mtd->writesize / step_size;
4458
4459 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4460 if (WARN_ON_ONCE(ecc_bytes < 0))
4461 continue;
4462 ecc_bytes_total = ecc_bytes * nsteps;
4463
4464 if (ecc_bytes_total > oobavail ||
4465 strength * nsteps < req_corr)
4466 continue;
4467
4468 /*
4469 * We assume the best is to meet the chip's requrement
4470 * with the least number of ECC bytes.
4471 */
4472 if (ecc_bytes_total < best_ecc_bytes_total) {
4473 best_ecc_bytes_total = ecc_bytes_total;
4474 best_step = step_size;
4475 best_strength = strength;
4476 best_ecc_bytes = ecc_bytes;
4477 }
4478 }
4479 }
4480
4481 if (best_ecc_bytes_total == INT_MAX)
4482 return -ENOTSUPP;
4483
4484 chip->ecc.size = best_step;
4485 chip->ecc.strength = best_strength;
4486 chip->ecc.bytes = best_ecc_bytes;
4487
4488 return 0;
4489}
4490EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4491
4492/**
4493 * nand_maximize_ecc - choose the max ECC strength available
4494 * @chip: nand chip info structure
4495 * @caps: ECC engine caps info structure
4496 * @oobavail: OOB size that the ECC engine can use
4497 *
4498 * Choose the max ECC strength that is supported on the controller, and can fit
4499 * within the chip's OOB. On success, the chosen ECC settings are set.
4500 */
4501int nand_maximize_ecc(struct nand_chip *chip,
4502 const struct nand_ecc_caps *caps, int oobavail)
4503{
4504 struct mtd_info *mtd = nand_to_mtd(chip);
4505 const struct nand_ecc_step_info *stepinfo;
4506 int step_size, strength, nsteps, ecc_bytes, corr;
4507 int best_corr = 0;
4508 int best_step = 0;
4509 int best_strength, best_ecc_bytes;
4510 int i, j;
4511
4512 if (WARN_ON(oobavail < 0))
4513 return -EINVAL;
4514
4515 for (i = 0; i < caps->nstepinfos; i++) {
4516 stepinfo = &caps->stepinfos[i];
4517 step_size = stepinfo->stepsize;
4518
4519 /* If chip->ecc.size is already set, respect it */
4520 if (chip->ecc.size && step_size != chip->ecc.size)
4521 continue;
4522
4523 for (j = 0; j < stepinfo->nstrengths; j++) {
4524 strength = stepinfo->strengths[j];
4525
4526 if (mtd->writesize % step_size)
4527 continue;
4528
4529 nsteps = mtd->writesize / step_size;
4530
4531 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4532 if (WARN_ON_ONCE(ecc_bytes < 0))
4533 continue;
4534
4535 if (ecc_bytes * nsteps > oobavail)
4536 continue;
4537
4538 corr = strength * nsteps;
4539
4540 /*
4541 * If the number of correctable bits is the same,
4542 * bigger step_size has more reliability.
4543 */
4544 if (corr > best_corr ||
4545 (corr == best_corr && step_size > best_step)) {
4546 best_corr = corr;
4547 best_step = step_size;
4548 best_strength = strength;
4549 best_ecc_bytes = ecc_bytes;
4550 }
4551 }
4552 }
4553
4554 if (!best_corr)
4555 return -ENOTSUPP;
4556
4557 chip->ecc.size = best_step;
4558 chip->ecc.strength = best_strength;
4559 chip->ecc.bytes = best_ecc_bytes;
4560
4561 return 0;
4562}
4563EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4564
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004565/*
4566 * Check if the chip configuration meet the datasheet requirements.
4567
4568 * If our configuration corrects A bits per B bytes and the minimum
4569 * required correction level is X bits per Y bytes, then we must ensure
4570 * both of the following are true:
4571 *
4572 * (1) A / B >= X / Y
4573 * (2) A >= X
4574 *
4575 * Requirement (1) ensures we can correct for the required bitflip density.
4576 * Requirement (2) ensures we can correct even when all bitflips are clumped
4577 * in the same sector.
4578 */
4579static bool nand_ecc_strength_good(struct mtd_info *mtd)
4580{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004581 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004582 struct nand_ecc_ctrl *ecc = &chip->ecc;
4583 int corr, ds_corr;
4584
4585 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4586 /* Not enough information */
4587 return true;
4588
4589 /*
4590 * We get the number of corrected bits per page to compare
4591 * the correction density.
4592 */
4593 corr = (mtd->writesize * ecc->strength) / ecc->size;
4594 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4595
4596 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4597}
David Woodhouse3b85c322006-09-25 17:06:53 +01004598
Marc Gonzalez3371d662016-11-15 10:56:20 +01004599static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4600{
4601 struct nand_ecc_ctrl *ecc = &chip->ecc;
4602
4603 if (nand_standard_page_accessors(ecc))
4604 return false;
4605
4606 /*
4607 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4608 * controller driver implements all the page accessors because
4609 * default helpers are not suitable when the core does not
4610 * send the READ0/PAGEPROG commands.
4611 */
4612 return (!ecc->read_page || !ecc->write_page ||
4613 !ecc->read_page_raw || !ecc->write_page_raw ||
4614 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4615 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4616 ecc->hwctl && ecc->calculate));
4617}
4618
David Woodhouse3b85c322006-09-25 17:06:53 +01004619/**
4620 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004621 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004622 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004623 * This is the second phase of the normal nand_scan() function. It fills out
4624 * all the uninitialized function pointers with the defaults and scans for a
4625 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004626 */
4627int nand_scan_tail(struct mtd_info *mtd)
4628{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004629 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004630 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004631 struct nand_buffers *nbuf = NULL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004632 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01004633
Brian Norrise2414f42012-02-06 13:44:00 -08004634 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004635 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07004636 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02004637 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07004638 }
Brian Norrise2414f42012-02-06 13:44:00 -08004639
Marc Gonzalez3371d662016-11-15 10:56:20 +01004640 if (invalid_ecc_page_accessors(chip)) {
4641 pr_err("Invalid ECC page accessors setup\n");
Boris Brezillonf84674b2017-06-02 12:18:24 +02004642 return -EINVAL;
Marc Gonzalez3371d662016-11-15 10:56:20 +01004643 }
4644
Huang Shijief02ea4e2014-01-13 14:27:12 +08004645 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004646 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Boris Brezillonf84674b2017-06-02 12:18:24 +02004647 if (!nbuf)
4648 return -ENOMEM;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004649
4650 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4651 if (!nbuf->ecccalc) {
4652 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004653 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004654 }
4655
4656 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4657 if (!nbuf->ecccode) {
4658 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004659 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004660 }
4661
4662 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4663 GFP_KERNEL);
4664 if (!nbuf->databuf) {
4665 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004666 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004667 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004668
4669 chip->buffers = nbuf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004670 } else if (!chip->buffers) {
4671 return -ENOMEM;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004672 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004673
Boris Brezillonf84674b2017-06-02 12:18:24 +02004674 /*
4675 * FIXME: some NAND manufacturer drivers expect the first die to be
4676 * selected when manufacturer->init() is called. They should be fixed
4677 * to explictly select the relevant die when interacting with the NAND
4678 * chip.
4679 */
4680 chip->select_chip(mtd, 0);
4681 ret = nand_manufacturer_init(chip);
4682 chip->select_chip(mtd, -1);
4683 if (ret)
4684 goto err_free_nbuf;
4685
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004686 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004687 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004688
4689 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004690 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004691 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004692 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004693 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004694 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004697 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 break;
4699 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004700 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02004701 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004702 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02004704 /*
4705 * Expose the whole OOB area to users if ECC_NONE
4706 * is passed. We could do that for all kind of
4707 * ->oobsize, but we must keep the old large/small
4708 * page with ECC layout when ->oobsize <= 128 for
4709 * compatibility reasons.
4710 */
4711 if (ecc->mode == NAND_ECC_NONE) {
4712 mtd_set_ooblayout(mtd,
4713 &nand_ooblayout_lp_ops);
4714 break;
4715 }
4716
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004717 WARN(1, "No oob scheme defined for oobsize %d\n",
4718 mtd->oobsize);
4719 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004720 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 }
4722 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004723
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004724 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004725 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004726 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004727 */
David Woodhouse956e9442006-09-25 17:12:39 +01004728
Huang Shijie97de79e02013-10-18 14:20:53 +08004729 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004730 case NAND_ECC_HW_OOB_FIRST:
4731 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004732 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004733 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4734 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004735 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004736 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004737 if (!ecc->read_page)
4738 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004739
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004740 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004741 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004742 if (!ecc->read_page)
4743 ecc->read_page = nand_read_page_hwecc;
4744 if (!ecc->write_page)
4745 ecc->write_page = nand_write_page_hwecc;
4746 if (!ecc->read_page_raw)
4747 ecc->read_page_raw = nand_read_page_raw;
4748 if (!ecc->write_page_raw)
4749 ecc->write_page_raw = nand_write_page_raw;
4750 if (!ecc->read_oob)
4751 ecc->read_oob = nand_read_oob_std;
4752 if (!ecc->write_oob)
4753 ecc->write_oob = nand_write_oob_std;
4754 if (!ecc->read_subpage)
4755 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004756 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004757 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004758
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004759 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004760 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4761 (!ecc->read_page ||
4762 ecc->read_page == nand_read_page_hwecc ||
4763 !ecc->write_page ||
4764 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004765 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4766 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004767 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004768 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004769 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004770 if (!ecc->read_page)
4771 ecc->read_page = nand_read_page_syndrome;
4772 if (!ecc->write_page)
4773 ecc->write_page = nand_write_page_syndrome;
4774 if (!ecc->read_page_raw)
4775 ecc->read_page_raw = nand_read_page_raw_syndrome;
4776 if (!ecc->write_page_raw)
4777 ecc->write_page_raw = nand_write_page_raw_syndrome;
4778 if (!ecc->read_oob)
4779 ecc->read_oob = nand_read_oob_syndrome;
4780 if (!ecc->write_oob)
4781 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004782
Huang Shijie97de79e02013-10-18 14:20:53 +08004783 if (mtd->writesize >= ecc->size) {
4784 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004785 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4786 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004787 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07004788 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004789 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004790 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004791 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4792 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004793 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004794 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004796 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004797 ret = nand_set_ecc_soft_ops(mtd);
4798 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004799 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004800 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01004801 }
4802 break;
4803
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004804 case NAND_ECC_ON_DIE:
4805 if (!ecc->read_page || !ecc->write_page) {
4806 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4807 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004808 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004809 }
4810 if (!ecc->read_oob)
4811 ecc->read_oob = nand_read_oob_std;
4812 if (!ecc->write_oob)
4813 ecc->write_oob = nand_write_oob_std;
4814 break;
4815
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004816 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004817 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004818 ecc->read_page = nand_read_page_raw;
4819 ecc->write_page = nand_write_page_raw;
4820 ecc->read_oob = nand_read_oob_std;
4821 ecc->read_page_raw = nand_read_page_raw;
4822 ecc->write_page_raw = nand_write_page_raw;
4823 ecc->write_oob = nand_write_oob_std;
4824 ecc->size = mtd->writesize;
4825 ecc->bytes = 0;
4826 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004830 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4831 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004832 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
Brian Norris9ce244b2011-08-30 18:45:37 -07004835 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004836 if (!ecc->read_oob_raw)
4837 ecc->read_oob_raw = ecc->read_oob;
4838 if (!ecc->write_oob_raw)
4839 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004840
Boris Brezillon846031d2016-02-03 20:11:00 +01004841 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004842 mtd->ecc_strength = ecc->strength;
4843 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004844
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004845 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004846 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004847 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004848 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004849 ecc->steps = mtd->writesize / ecc->size;
4850 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004851 WARN(1, "Invalid ECC parameters\n");
4852 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004853 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004855 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004856 if (ecc->total > mtd->oobsize) {
4857 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
4858 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004859 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004860 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004861
Boris Brezillon846031d2016-02-03 20:11:00 +01004862 /*
4863 * The number of bytes available for a client to place data into
4864 * the out of band area.
4865 */
4866 ret = mtd_ooblayout_count_freebytes(mtd);
4867 if (ret < 0)
4868 ret = 0;
4869
4870 mtd->oobavail = ret;
4871
4872 /* ECC sanity check: warn if it's too weak */
4873 if (!nand_ecc_strength_good(mtd))
4874 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4875 mtd->name);
4876
Brian Norris8b6e50c2011-05-25 14:59:01 -07004877 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004878 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004879 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004880 case 2:
4881 mtd->subpage_sft = 1;
4882 break;
4883 case 4:
4884 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004885 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004886 mtd->subpage_sft = 2;
4887 break;
4888 }
4889 }
4890 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4891
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004892 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004893 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004896 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004898 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304899 switch (ecc->mode) {
4900 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304901 if (chip->page_shift > 9)
4902 chip->options |= NAND_SUBPAGE_READ;
4903 break;
4904
4905 default:
4906 break;
4907 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004910 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004911 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4912 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004913 mtd->_erase = nand_erase;
4914 mtd->_point = NULL;
4915 mtd->_unpoint = NULL;
4916 mtd->_read = nand_read;
4917 mtd->_write = nand_write;
4918 mtd->_panic_write = panic_nand_write;
4919 mtd->_read_oob = nand_read_oob;
4920 mtd->_write_oob = nand_write_oob;
4921 mtd->_sync = nand_sync;
4922 mtd->_lock = NULL;
4923 mtd->_unlock = NULL;
4924 mtd->_suspend = nand_suspend;
4925 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004926 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004927 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004928 mtd->_block_isbad = nand_block_isbad;
4929 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004930 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004931 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004933 /*
4934 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4935 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4936 * properly set.
4937 */
4938 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004939 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
Boris Brezillonf84674b2017-06-02 12:18:24 +02004941 /* Initialize the ->data_interface field. */
4942 ret = nand_init_data_interface(chip);
4943 if (ret)
4944 goto err_nand_manuf_cleanup;
4945
4946 /* Enter fastest possible mode on all dies. */
4947 for (i = 0; i < chip->numchips; i++) {
4948 chip->select_chip(mtd, i);
4949 ret = nand_setup_data_interface(chip, i);
4950 chip->select_chip(mtd, -1);
4951
4952 if (ret)
4953 goto err_nand_data_iface_cleanup;
4954 }
4955
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004956 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004957 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
4960 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07004961 ret = chip->scan_bbt(mtd);
4962 if (ret)
Boris Brezillonf84674b2017-06-02 12:18:24 +02004963 goto err_nand_data_iface_cleanup;
4964
Brian Norris44d41822017-05-01 17:04:50 -07004965 return 0;
4966
Boris Brezillonf84674b2017-06-02 12:18:24 +02004967err_nand_data_iface_cleanup:
4968 nand_release_data_interface(chip);
4969
4970err_nand_manuf_cleanup:
4971 nand_manufacturer_cleanup(chip);
4972
4973err_free_nbuf:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004974 if (nbuf) {
4975 kfree(nbuf->databuf);
4976 kfree(nbuf->ecccode);
4977 kfree(nbuf->ecccalc);
4978 kfree(nbuf);
4979 }
Brian Norris78771042017-05-01 17:04:53 -07004980
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004981 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004983EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
Brian Norris8b6e50c2011-05-25 14:59:01 -07004985/*
4986 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004987 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004988 * to call us from in-kernel code if the core NAND support is modular.
4989 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004990#ifdef MODULE
4991#define caller_is_module() (1)
4992#else
4993#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004994 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004995#endif
4996
4997/**
4998 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004999 * @mtd: MTD device structure
5000 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01005001 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005002 * This fills out all the uninitialized function pointers with the defaults.
5003 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005004 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005005 */
5006int nand_scan(struct mtd_info *mtd, int maxchips)
5007{
5008 int ret;
5009
David Woodhouse5e81e882010-02-26 18:32:56 +00005010 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01005011 if (!ret)
5012 ret = nand_scan_tail(mtd);
5013 return ret;
5014}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005015EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01005016
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005018 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5019 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005020 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005021void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005023 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005024 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005025 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5026
Boris Brezillond8e725d2016-09-15 10:32:50 +02005027 nand_release_data_interface(chip);
5028
Jesper Juhlfa671642005-11-07 01:01:27 -08005029 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005030 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005031 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
5032 kfree(chip->buffers->databuf);
5033 kfree(chip->buffers->ecccode);
5034 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005035 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005036 }
Brian Norris58373ff2010-07-15 12:15:44 -07005037
5038 /* Free bad block descriptor memory */
5039 if (chip->badblock_pattern && chip->badblock_pattern->options
5040 & NAND_BBT_DYNAMICSTRUCT)
5041 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005042
5043 /* Free manufacturer priv data. */
5044 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005046EXPORT_SYMBOL_GPL(nand_cleanup);
5047
5048/**
5049 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5050 * held by the NAND device
5051 * @mtd: MTD device structure
5052 */
5053void nand_release(struct mtd_info *mtd)
5054{
5055 mtd_device_unregister(mtd);
5056 nand_cleanup(mtd_to_nand(mtd));
5057}
David Woodhousee0c7d762006-05-13 18:07:53 +01005058EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005059
David Woodhousee0c7d762006-05-13 18:07:53 +01005060MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005061MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5062MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005063MODULE_DESCRIPTION("Generic NAND flash driver code");