blob: 4c867115c53b82a04620ce0d5a503bf298025449 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010039#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/types.h>
41#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020042#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010044#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020047#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020049#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010050
Huang Shijie6a8214a2012-11-19 14:43:30 +080051static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020053static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
Boris Brezillon41b207a2016-02-03 19:06:15 +010056/* Define default oob placement schemes for large and small page devices */
57static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59{
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020068 if (mtd->oobsize == 16)
69 oobregion->length = 4;
70 else
71 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010072 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020073 if (mtd->oobsize == 8)
74 return -ERANGE;
75
Boris Brezillon41b207a2016-02-03 19:06:15 +010076 oobregion->offset = 6;
77 oobregion->length = ecc->total - 4;
78 }
79
80 return 0;
81}
82
83static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85{
86 if (section > 1)
87 return -ERANGE;
88
89 if (mtd->oobsize == 16) {
90 if (section)
91 return -ERANGE;
92
93 oobregion->length = 8;
94 oobregion->offset = 8;
95 } else {
96 oobregion->length = 2;
97 if (!section)
98 oobregion->offset = 3;
99 else
100 oobregion->offset = 6;
101 }
102
103 return 0;
104}
105
106const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107 .ecc = nand_ooblayout_ecc_sp,
108 .free = nand_ooblayout_free_sp,
109};
110EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
111
112static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113 struct mtd_oob_region *oobregion)
114{
115 struct nand_chip *chip = mtd_to_nand(mtd);
116 struct nand_ecc_ctrl *ecc = &chip->ecc;
117
Miquel Raynal882fd152017-08-26 17:19:15 +0200118 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100119 return -ERANGE;
120
121 oobregion->length = ecc->total;
122 oobregion->offset = mtd->oobsize - oobregion->length;
123
124 return 0;
125}
126
127static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128 struct mtd_oob_region *oobregion)
129{
130 struct nand_chip *chip = mtd_to_nand(mtd);
131 struct nand_ecc_ctrl *ecc = &chip->ecc;
132
133 if (section)
134 return -ERANGE;
135
136 oobregion->length = mtd->oobsize - ecc->total - 2;
137 oobregion->offset = 2;
138
139 return 0;
140}
141
142const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143 .ecc = nand_ooblayout_ecc_lp,
144 .free = nand_ooblayout_free_lp,
145};
146EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200147
Alexander Couzens6a623e02017-05-02 12:19:00 +0200148/*
149 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
150 * are placed at a fixed offset.
151 */
152static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153 struct mtd_oob_region *oobregion)
154{
155 struct nand_chip *chip = mtd_to_nand(mtd);
156 struct nand_ecc_ctrl *ecc = &chip->ecc;
157
158 if (section)
159 return -ERANGE;
160
161 switch (mtd->oobsize) {
162 case 64:
163 oobregion->offset = 40;
164 break;
165 case 128:
166 oobregion->offset = 80;
167 break;
168 default:
169 return -EINVAL;
170 }
171
172 oobregion->length = ecc->total;
173 if (oobregion->offset + oobregion->length > mtd->oobsize)
174 return -ERANGE;
175
176 return 0;
177}
178
179static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180 struct mtd_oob_region *oobregion)
181{
182 struct nand_chip *chip = mtd_to_nand(mtd);
183 struct nand_ecc_ctrl *ecc = &chip->ecc;
184 int ecc_offset = 0;
185
186 if (section < 0 || section > 1)
187 return -ERANGE;
188
189 switch (mtd->oobsize) {
190 case 64:
191 ecc_offset = 40;
192 break;
193 case 128:
194 ecc_offset = 80;
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 if (section == 0) {
201 oobregion->offset = 2;
202 oobregion->length = ecc_offset - 2;
203 } else {
204 oobregion->offset = ecc_offset + ecc->total;
205 oobregion->length = mtd->oobsize - oobregion->offset;
206 }
207
208 return 0;
209}
210
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100211static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200212 .ecc = nand_ooblayout_ecc_lp_hamming,
213 .free = nand_ooblayout_free_lp_hamming,
214};
215
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530216static int check_offs_len(struct mtd_info *mtd,
217 loff_t ofs, uint64_t len)
218{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100219 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530220 int ret = 0;
221
222 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
228 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300229 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700230 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530231 ret = -EINVAL;
232 }
233
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530234 return ret;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700239 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800241 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100243static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100245 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200247 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 spin_lock(&chip->controller->lock);
249 chip->controller->active = NULL;
250 chip->state = FL_READY;
251 wake_up(&chip->controller->wq);
252 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700257 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700259 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200261static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100263 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200264 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/**
Masanari Iida064a7692012-11-09 23:20:58 +0900268 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700269 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700271 * Default read function for 16bit buswidth with endianness conversion.
272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200274static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100276 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700282 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700284 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286static u16 nand_read_word(struct mtd_info *mtd)
287{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100288 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200289 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700294 * @mtd: MTD device structure
295 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * Default select function for 1 chip devices.
298 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200299static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100301 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200302
303 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200305 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309
310 default:
311 BUG();
312 }
313}
314
315/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100316 * nand_write_byte - [DEFAULT] write single byte to chip
317 * @mtd: MTD device structure
318 * @byte: value to write
319 *
320 * Default function to write a byte to I/O[7:0]
321 */
322static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
323{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100324 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100325
326 chip->write_buf(mtd, &byte, 1);
327}
328
329/**
330 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
331 * @mtd: MTD device structure
332 * @byte: value to write
333 *
334 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
335 */
336static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
337{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100338 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100339 uint16_t word = byte;
340
341 /*
342 * It's not entirely clear what should happen to I/O[15:8] when writing
343 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
344 *
345 * When the host supports a 16-bit bus width, only data is
346 * transferred at the 16-bit width. All address and command line
347 * transfers shall use only the lower 8-bits of the data bus. During
348 * command transfers, the host may place any value on the upper
349 * 8-bits of the data bus. During address transfers, the host shall
350 * set the upper 8-bits of the data bus to 00h.
351 *
352 * One user of the write_byte callback is nand_onfi_set_features. The
353 * four parameters are specified to be written to I/O[7:0], but this is
354 * neither an address nor a command transfer. Let's assume a 0 on the
355 * upper I/O lines is OK.
356 */
357 chip->write_buf(mtd, (uint8_t *)&word, 2);
358}
359
360/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700362 * @mtd: MTD device structure
363 * @buf: data buffer
364 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700366 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200368static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100370 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Alexander Shiyan76413832013-04-13 09:32:13 +0400372 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000376 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700377 * @mtd: MTD device structure
378 * @buf: buffer to store date
379 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700381 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200383static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100385 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alexander Shiyan76413832013-04-13 09:32:13 +0400387 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700392 * @mtd: MTD device structure
393 * @buf: data buffer
394 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700396 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200398static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100400 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000402
Alexander Shiyan76413832013-04-13 09:32:13 +0400403 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000407 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700408 * @mtd: MTD device structure
409 * @buf: buffer to store date
410 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700412 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200414static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100416 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Alexander Shiyan76413832013-04-13 09:32:13 +0400419 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700424 * @mtd: MTD device structure
425 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000427 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530429static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Masahiro Yamadac120e752017-03-23 05:07:01 +0900431 int page, page_end, res;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100432 struct nand_chip *chip = mtd_to_nand(mtd);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900433 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Brian Norris5fb15492011-05-31 16:31:21 -0700435 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700436 ofs += mtd->erasesize - mtd->writesize;
437
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100438 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900439 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100440
Masahiro Yamadac120e752017-03-23 05:07:01 +0900441 for (; page < page_end; page++) {
442 res = chip->ecc.read_oob(mtd, chip, page);
443 if (res)
444 return res;
445
446 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000447
Brian Norriscdbec052012-01-13 18:11:48 -0800448 if (likely(chip->badblockbits == 8))
449 res = bad != 0xFF;
450 else
451 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900452 if (res)
453 return res;
454 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200455
Masahiro Yamadac120e752017-03-23 05:07:01 +0900456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700460 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700461 * @mtd: MTD device structure
462 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700464 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700465 * specific driver. It provides the details for writing a bad block marker to a
466 * block.
467 */
468static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
469{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100470 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700471 struct mtd_oob_ops ops;
472 uint8_t buf[2] = { 0, 0 };
473 int ret = 0, res, i = 0;
474
Brian Norris0ec56dc2015-02-28 02:02:30 -0800475 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700476 ops.oobbuf = buf;
477 ops.ooboffs = chip->badblockpos;
478 if (chip->options & NAND_BUSWIDTH_16) {
479 ops.ooboffs &= ~0x01;
480 ops.len = ops.ooblen = 2;
481 } else {
482 ops.len = ops.ooblen = 1;
483 }
484 ops.mode = MTD_OPS_PLACE_OOB;
485
486 /* Write to first/last page(s) if necessary */
487 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
488 ofs += mtd->erasesize - mtd->writesize;
489 do {
490 res = nand_do_write_oob(mtd, ofs, &ops);
491 if (!ret)
492 ret = res;
493
494 i++;
495 ofs += mtd->writesize;
496 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
497
498 return ret;
499}
500
501/**
502 * nand_block_markbad_lowlevel - mark a block bad
503 * @mtd: MTD device structure
504 * @ofs: offset from device start
505 *
506 * This function performs the generic NAND bad block marking steps (i.e., bad
507 * block table(s) and/or marker(s)). We only allow the hardware driver to
508 * specify how to write bad block markers to OOB (chip->block_markbad).
509 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700510 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300511 *
Brian Norrise2414f42012-02-06 13:44:00 -0800512 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700513 * (2) write bad block marker to OOB area of affected block (unless flag
514 * NAND_BBT_NO_OOB_BBM is present)
515 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300516 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700517 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800518 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700520static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100522 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700523 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000524
Brian Norrisb32843b2013-07-30 17:52:59 -0700525 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800526 struct erase_info einfo;
527
528 /* Attempt erase before marking OOB */
529 memset(&einfo, 0, sizeof(einfo));
530 einfo.mtd = mtd;
531 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300532 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800533 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800534
Brian Norrisb32843b2013-07-30 17:52:59 -0700535 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800536 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700537 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300538 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200539 }
Brian Norrise2414f42012-02-06 13:44:00 -0800540
Brian Norrisb32843b2013-07-30 17:52:59 -0700541 /* Mark block bad in BBT */
542 if (chip->bbt) {
543 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800544 if (!ret)
545 ret = res;
546 }
547
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200548 if (!ret)
549 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300550
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000554/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700556 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700558 * Check, if the device is write protected. The function expects, that the
559 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100561static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100563 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200564
Brian Norris8b6e50c2011-05-25 14:59:01 -0700565 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200566 if (chip->options & NAND_BROKEN_XD)
567 return 0;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200570 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
571 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800575 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700576 * @mtd: MTD device structure
577 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300578 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800579 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300580 */
581static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
582{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100583 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300584
585 if (!chip->bbt)
586 return 0;
587 /* Return info from the table */
588 return nand_isreserved_bbt(mtd, ofs);
589}
590
591/**
592 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
593 * @mtd: MTD device structure
594 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700595 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
597 * Check, if the block is bad. Either by reading the bad block table or
598 * calling of the scan function.
599 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530600static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100602 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000603
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200604 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530605 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100608 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200611/**
612 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700613 * @mtd: MTD device structure
614 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200615 *
616 * Helper function for nand_wait_ready used when needing to wait in interrupt
617 * context.
618 */
619static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
620{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100621 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200622 int i;
623
624 /* Wait for the device to get ready */
625 for (i = 0; i < timeo; i++) {
626 if (chip->dev_ready(mtd))
627 break;
628 touch_softlockup_watchdog();
629 mdelay(1);
630 }
631}
632
Alex Smithb70af9b2015-10-06 14:52:07 +0100633/**
634 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
635 * @mtd: MTD device structure
636 *
637 * Wait for the ready pin after a command, and warn if a timeout occurs.
638 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100639void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000640{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100641 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100642 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000643
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200644 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100645 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200646
Brian Norris7854d3f2011-06-23 14:12:08 -0700647 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100648 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000649 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200650 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300651 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100652 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000653 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100654
Brian Norris9ebfdf52016-03-04 17:19:23 -0800655 if (!chip->dev_ready(mtd))
656 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000657}
David Woodhouse4b648b02006-09-25 17:05:24 +0100658EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200661 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
662 * @mtd: MTD device structure
663 * @timeo: Timeout in ms
664 *
665 * Wait for status ready (i.e. command done) or timeout.
666 */
667static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
668{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100669 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200670
671 timeo = jiffies + msecs_to_jiffies(timeo);
672 do {
673 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
674 break;
675 touch_softlockup_watchdog();
676 } while (time_before(jiffies, timeo));
677};
678
679/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700681 * @mtd: MTD device structure
682 * @command: the command to be sent
683 * @column: the column address for this command, -1 if none
684 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700686 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200687 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200689static void nand_command(struct mtd_info *mtd, unsigned int command,
690 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100692 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200693 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Brian Norris8b6e50c2011-05-25 14:59:01 -0700695 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (command == NAND_CMD_SEQIN) {
697 int readcmd;
698
Joern Engel28318772006-05-22 23:18:05 +0200699 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200701 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 readcmd = NAND_CMD_READOOB;
703 } else if (column < 256) {
704 /* First 256 bytes --> READ0 */
705 readcmd = NAND_CMD_READ0;
706 } else {
707 column -= 256;
708 readcmd = NAND_CMD_READ1;
709 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200710 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200711 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200713 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Brian Norris8b6e50c2011-05-25 14:59:01 -0700715 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200716 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
717 /* Serially input address */
718 if (column != -1) {
719 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800720 if (chip->options & NAND_BUSWIDTH_16 &&
721 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200722 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200723 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200724 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200726 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200727 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200728 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200729 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900730 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200732 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200733 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000734
735 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700736 * Program and erase have their own busy handlers status and sequential
737 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 case NAND_CMD_PAGEPROG:
742 case NAND_CMD_ERASE1:
743 case NAND_CMD_ERASE2:
744 case NAND_CMD_SEQIN:
745 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900746 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900747 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return;
749
750 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200751 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200753 udelay(chip->chip_delay);
754 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200755 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200756 chip->cmd_ctrl(mtd,
757 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200758 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
759 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return;
761
David Woodhousee0c7d762006-05-13 18:07:53 +0100762 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200763 case NAND_CMD_READ0:
764 /*
765 * READ0 is sometimes used to exit GET STATUS mode. When this
766 * is the case no address cycles are requested, and we can use
767 * this information to detect that we should not wait for the
768 * device to be ready.
769 */
770 if (column == -1 && page_addr == -1)
771 return;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000774 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * If we don't have access to the busy pin, we apply the given
776 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100777 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200778 if (!chip->dev_ready) {
779 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700783 /*
784 * Apply this short delay always to ensure that we do wait tWB in
785 * any case on any machine.
786 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100787 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000788
789 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200792static void nand_ccs_delay(struct nand_chip *chip)
793{
794 /*
795 * The controller already takes care of waiting for tCCS when the RNDIN
796 * or RNDOUT command is sent, return directly.
797 */
798 if (!(chip->options & NAND_WAIT_TCCS))
799 return;
800
801 /*
802 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
803 * (which should be safe for all NANDs).
804 */
805 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
806 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
807 else
808 ndelay(500);
809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/**
812 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700813 * @mtd: MTD device structure
814 * @command: the command to be sent
815 * @column: the column address for this command, -1 if none
816 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200818 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700819 * devices. We don't have the separate regions as we have in the small page
820 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200822static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
823 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100825 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /* Emulate NAND_CMD_READOOB */
828 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200829 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 command = NAND_CMD_READ0;
831 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000832
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200833 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400834 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200837 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /* Serially input address */
840 if (column != -1) {
841 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800842 if (chip->options & NAND_BUSWIDTH_16 &&
843 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200845 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200846 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200847
Brian Norrisf5b88de2016-10-03 09:49:35 -0700848 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200849 if (!nand_opcode_8bits(command))
850 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200853 chip->cmd_ctrl(mtd, page_addr, ctrl);
854 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200855 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900856 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200857 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200858 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200861 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000862
863 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700864 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100865 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 case NAND_CMD_CACHEDPROG:
870 case NAND_CMD_PAGEPROG:
871 case NAND_CMD_ERASE1:
872 case NAND_CMD_ERASE2:
873 case NAND_CMD_SEQIN:
874 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900875 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900876 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000877 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200879 case NAND_CMD_RNDIN:
880 nand_ccs_delay(chip);
881 return;
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200884 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200886 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200887 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
888 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
889 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
890 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200891 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
892 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return;
894
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200895 case NAND_CMD_RNDOUT:
896 /* No ready / busy check necessary */
897 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
898 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
899 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
900 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200901
902 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200903 return;
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200906 /*
907 * READ0 is sometimes used to exit GET STATUS mode. When this
908 * is the case no address cycles are requested, and we can use
909 * this information to detect that READSTART should not be
910 * issued.
911 */
912 if (column == -1 && page_addr == -1)
913 return;
914
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200915 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
916 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
917 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
918 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000919
David Woodhousee0c7d762006-05-13 18:07:53 +0100920 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000922 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700924 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100925 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200926 if (!chip->dev_ready) {
927 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000931
Brian Norris8b6e50c2011-05-25 14:59:01 -0700932 /*
933 * Apply this short delay always to ensure that we do wait tWB in
934 * any case on any machine.
935 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100936 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000937
938 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200942 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700943 * @chip: the nand chip descriptor
944 * @mtd: MTD device structure
945 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200946 *
947 * Used when in panic, no locks are taken.
948 */
949static void panic_nand_get_device(struct nand_chip *chip,
950 struct mtd_info *mtd, int new_state)
951{
Brian Norris7854d3f2011-06-23 14:12:08 -0700952 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200953 chip->controller->active = chip;
954 chip->state = new_state;
955}
956
957/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700959 * @mtd: MTD device structure
960 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 *
962 * Get the device and lock it for exclusive access
963 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200964static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800965nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100967 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200968 spinlock_t *lock = &chip->controller->lock;
969 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100970 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200971retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100972 spin_lock(lock);
973
vimal singhb8b3ee92009-07-09 20:41:22 +0530974 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200975 if (!chip->controller->active)
976 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200977
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200978 if (chip->controller->active == chip && chip->state == FL_READY) {
979 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100980 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100981 return 0;
982 }
983 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800984 if (chip->controller->active->state == FL_PM_SUSPENDED) {
985 chip->state = FL_PM_SUSPENDED;
986 spin_unlock(lock);
987 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800988 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100989 }
990 set_current_state(TASK_UNINTERRUPTIBLE);
991 add_wait_queue(wq, &wait);
992 spin_unlock(lock);
993 schedule();
994 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto retry;
996}
997
998/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700999 * panic_nand_wait - [GENERIC] wait until the command is done
1000 * @mtd: MTD device structure
1001 * @chip: NAND chip structure
1002 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001003 *
1004 * Wait for command done. This is a helper function for nand_wait used when
1005 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001006 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001007 */
1008static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1009 unsigned long timeo)
1010{
1011 int i;
1012 for (i = 0; i < timeo; i++) {
1013 if (chip->dev_ready) {
1014 if (chip->dev_ready(mtd))
1015 break;
1016 } else {
1017 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1018 break;
1019 }
1020 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001021 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001022}
1023
1024/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001025 * nand_wait - [DEFAULT] wait until the command is done
1026 * @mtd: MTD device structure
1027 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001029 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001030 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001031static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033
Alex Smithb70af9b2015-10-06 14:52:07 +01001034 int status;
1035 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Brian Norris8b6e50c2011-05-25 14:59:01 -07001037 /*
1038 * Apply this short delay always to ensure that we do wait tWB in any
1039 * case on any machine.
1040 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001041 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Artem Bityutskiy14c65782013-03-04 14:21:34 +02001043 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001045 if (in_interrupt() || oops_in_progress)
1046 panic_nand_wait(mtd, chip, timeo);
1047 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001048 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001049 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001050 if (chip->dev_ready) {
1051 if (chip->dev_ready(mtd))
1052 break;
1053 } else {
1054 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1055 break;
1056 }
1057 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001058 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001060
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001061 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001062 /* This can happen if in case of timeout or buggy dev_ready */
1063 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return status;
1065}
1066
1067/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001068 * nand_reset_data_interface - Reset data interface and timings
1069 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001070 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001071 *
1072 * Reset the Data interface and timings to ONFI mode 0.
1073 *
1074 * Returns 0 for success or negative error code otherwise.
1075 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001076static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001077{
1078 struct mtd_info *mtd = nand_to_mtd(chip);
1079 const struct nand_data_interface *conf;
1080 int ret;
1081
1082 if (!chip->setup_data_interface)
1083 return 0;
1084
1085 /*
1086 * The ONFI specification says:
1087 * "
1088 * To transition from NV-DDR or NV-DDR2 to the SDR data
1089 * interface, the host shall use the Reset (FFh) command
1090 * using SDR timing mode 0. A device in any timing mode is
1091 * required to recognize Reset (FFh) command issued in SDR
1092 * timing mode 0.
1093 * "
1094 *
1095 * Configure the data interface in SDR mode and set the
1096 * timings to timing mode 0.
1097 */
1098
1099 conf = nand_get_default_data_interface();
Boris Brezillon104e4422017-03-16 09:35:58 +01001100 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001101 if (ret)
1102 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1103
1104 return ret;
1105}
1106
1107/**
1108 * nand_setup_data_interface - Setup the best data interface and timings
1109 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001110 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001111 *
1112 * Find and configure the best data interface and NAND timings supported by
1113 * the chip and the driver.
1114 * First tries to retrieve supported timing modes from ONFI information,
1115 * and if the NAND chip does not support ONFI, relies on the
1116 * ->onfi_timing_mode_default specified in the nand_ids table.
1117 *
1118 * Returns 0 for success or negative error code otherwise.
1119 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001120static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001121{
1122 struct mtd_info *mtd = nand_to_mtd(chip);
1123 int ret;
1124
1125 if (!chip->setup_data_interface || !chip->data_interface)
1126 return 0;
1127
1128 /*
1129 * Ensure the timing mode has been changed on the chip side
1130 * before changing timings on the controller side.
1131 */
Boris Brezillona11bf5e2017-07-31 10:29:56 +02001132 if (chip->onfi_version &&
1133 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1134 ONFI_OPT_CMD_SET_GET_FEATURES)) {
Boris Brezillond8e725d2016-09-15 10:32:50 +02001135 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1136 chip->onfi_timing_mode_default,
1137 };
1138
1139 ret = chip->onfi_set_features(mtd, chip,
1140 ONFI_FEATURE_ADDR_TIMING_MODE,
1141 tmode_param);
1142 if (ret)
1143 goto err;
1144 }
1145
Boris Brezillon104e4422017-03-16 09:35:58 +01001146 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001147err:
1148 return ret;
1149}
1150
1151/**
1152 * nand_init_data_interface - find the best data interface and timings
1153 * @chip: The NAND chip
1154 *
1155 * Find the best data interface and NAND timings supported by the chip
1156 * and the driver.
1157 * First tries to retrieve supported timing modes from ONFI information,
1158 * and if the NAND chip does not support ONFI, relies on the
1159 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1160 * function nand_chip->data_interface is initialized with the best timing mode
1161 * available.
1162 *
1163 * Returns 0 for success or negative error code otherwise.
1164 */
1165static int nand_init_data_interface(struct nand_chip *chip)
1166{
1167 struct mtd_info *mtd = nand_to_mtd(chip);
1168 int modes, mode, ret;
1169
1170 if (!chip->setup_data_interface)
1171 return 0;
1172
1173 /*
1174 * First try to identify the best timings from ONFI parameters and
1175 * if the NAND does not support ONFI, fallback to the default ONFI
1176 * timing mode.
1177 */
1178 modes = onfi_get_async_timing_mode(chip);
1179 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1180 if (!chip->onfi_timing_mode_default)
1181 return 0;
1182
1183 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1184 }
1185
1186 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1187 GFP_KERNEL);
1188 if (!chip->data_interface)
1189 return -ENOMEM;
1190
1191 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1192 ret = onfi_init_data_interface(chip, chip->data_interface,
1193 NAND_SDR_IFACE, mode);
1194 if (ret)
1195 continue;
1196
Boris Brezillon104e4422017-03-16 09:35:58 +01001197 /* Pass -1 to only */
1198 ret = chip->setup_data_interface(mtd,
1199 NAND_DATA_IFACE_CHECK_ONLY,
1200 chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001201 if (!ret) {
1202 chip->onfi_timing_mode_default = mode;
1203 break;
1204 }
1205 }
1206
1207 return 0;
1208}
1209
1210static void nand_release_data_interface(struct nand_chip *chip)
1211{
1212 kfree(chip->data_interface);
1213}
1214
1215/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001216 * nand_reset - Reset and initialize a NAND device
1217 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001218 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001219 *
1220 * Returns 0 for success or negative error code otherwise
1221 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001222int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001223{
1224 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001225 int ret;
1226
Boris Brezillon104e4422017-03-16 09:35:58 +01001227 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001228 if (ret)
1229 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001230
Boris Brezillon73f907f2016-10-24 16:46:20 +02001231 /*
1232 * The CS line has to be released before we can apply the new NAND
1233 * interface settings, hence this weird ->select_chip() dance.
1234 */
1235 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001236 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001237 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001238
Boris Brezillon73f907f2016-10-24 16:46:20 +02001239 chip->select_chip(mtd, chipnr);
Boris Brezillon104e4422017-03-16 09:35:58 +01001240 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001241 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001242 if (ret)
1243 return ret;
1244
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001245 return 0;
1246}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02001247EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001248
1249/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001250 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1251 * @buf: buffer to test
1252 * @len: buffer length
1253 * @bitflips_threshold: maximum number of bitflips
1254 *
1255 * Check if a buffer contains only 0xff, which means the underlying region
1256 * has been erased and is ready to be programmed.
1257 * The bitflips_threshold specify the maximum number of bitflips before
1258 * considering the region is not erased.
1259 * Note: The logic of this function has been extracted from the memweight
1260 * implementation, except that nand_check_erased_buf function exit before
1261 * testing the whole buffer if the number of bitflips exceed the
1262 * bitflips_threshold value.
1263 *
1264 * Returns a positive number of bitflips less than or equal to
1265 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1266 * threshold.
1267 */
1268static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1269{
1270 const unsigned char *bitmap = buf;
1271 int bitflips = 0;
1272 int weight;
1273
1274 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1275 len--, bitmap++) {
1276 weight = hweight8(*bitmap);
1277 bitflips += BITS_PER_BYTE - weight;
1278 if (unlikely(bitflips > bitflips_threshold))
1279 return -EBADMSG;
1280 }
1281
1282 for (; len >= sizeof(long);
1283 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001284 unsigned long d = *((unsigned long *)bitmap);
1285 if (d == ~0UL)
1286 continue;
1287 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001288 bitflips += BITS_PER_LONG - weight;
1289 if (unlikely(bitflips > bitflips_threshold))
1290 return -EBADMSG;
1291 }
1292
1293 for (; len > 0; len--, bitmap++) {
1294 weight = hweight8(*bitmap);
1295 bitflips += BITS_PER_BYTE - weight;
1296 if (unlikely(bitflips > bitflips_threshold))
1297 return -EBADMSG;
1298 }
1299
1300 return bitflips;
1301}
1302
1303/**
1304 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1305 * 0xff data
1306 * @data: data buffer to test
1307 * @datalen: data length
1308 * @ecc: ECC buffer
1309 * @ecclen: ECC length
1310 * @extraoob: extra OOB buffer
1311 * @extraooblen: extra OOB length
1312 * @bitflips_threshold: maximum number of bitflips
1313 *
1314 * Check if a data buffer and its associated ECC and OOB data contains only
1315 * 0xff pattern, which means the underlying region has been erased and is
1316 * ready to be programmed.
1317 * The bitflips_threshold specify the maximum number of bitflips before
1318 * considering the region as not erased.
1319 *
1320 * Note:
1321 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1322 * different from the NAND page size. When fixing bitflips, ECC engines will
1323 * report the number of errors per chunk, and the NAND core infrastructure
1324 * expect you to return the maximum number of bitflips for the whole page.
1325 * This is why you should always use this function on a single chunk and
1326 * not on the whole page. After checking each chunk you should update your
1327 * max_bitflips value accordingly.
1328 * 2/ When checking for bitflips in erased pages you should not only check
1329 * the payload data but also their associated ECC data, because a user might
1330 * have programmed almost all bits to 1 but a few. In this case, we
1331 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1332 * this case.
1333 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1334 * data are protected by the ECC engine.
1335 * It could also be used if you support subpages and want to attach some
1336 * extra OOB data to an ECC chunk.
1337 *
1338 * Returns a positive number of bitflips less than or equal to
1339 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1340 * threshold. In case of success, the passed buffers are filled with 0xff.
1341 */
1342int nand_check_erased_ecc_chunk(void *data, int datalen,
1343 void *ecc, int ecclen,
1344 void *extraoob, int extraooblen,
1345 int bitflips_threshold)
1346{
1347 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1348
1349 data_bitflips = nand_check_erased_buf(data, datalen,
1350 bitflips_threshold);
1351 if (data_bitflips < 0)
1352 return data_bitflips;
1353
1354 bitflips_threshold -= data_bitflips;
1355
1356 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1357 if (ecc_bitflips < 0)
1358 return ecc_bitflips;
1359
1360 bitflips_threshold -= ecc_bitflips;
1361
1362 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1363 bitflips_threshold);
1364 if (extraoob_bitflips < 0)
1365 return extraoob_bitflips;
1366
1367 if (data_bitflips)
1368 memset(data, 0xff, datalen);
1369
1370 if (ecc_bitflips)
1371 memset(ecc, 0xff, ecclen);
1372
1373 if (extraoob_bitflips)
1374 memset(extraoob, 0xff, extraooblen);
1375
1376 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1377}
1378EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1379
1380/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001381 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001382 * @mtd: mtd info structure
1383 * @chip: nand chip info structure
1384 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001385 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001386 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001387 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001388 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001389 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001390int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1391 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001392{
1393 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001394 if (oob_required)
1395 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001396 return 0;
1397}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001398EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001399
1400/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001401 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001402 * @mtd: mtd info structure
1403 * @chip: nand chip info structure
1404 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001405 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001406 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001407 *
1408 * We need a special oob layout and handling even when OOB isn't used.
1409 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001410static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001411 struct nand_chip *chip, uint8_t *buf,
1412 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001413{
1414 int eccsize = chip->ecc.size;
1415 int eccbytes = chip->ecc.bytes;
1416 uint8_t *oob = chip->oob_poi;
1417 int steps, size;
1418
1419 for (steps = chip->ecc.steps; steps > 0; steps--) {
1420 chip->read_buf(mtd, buf, eccsize);
1421 buf += eccsize;
1422
1423 if (chip->ecc.prepad) {
1424 chip->read_buf(mtd, oob, chip->ecc.prepad);
1425 oob += chip->ecc.prepad;
1426 }
1427
1428 chip->read_buf(mtd, oob, eccbytes);
1429 oob += eccbytes;
1430
1431 if (chip->ecc.postpad) {
1432 chip->read_buf(mtd, oob, chip->ecc.postpad);
1433 oob += chip->ecc.postpad;
1434 }
1435 }
1436
1437 size = mtd->oobsize - (oob - chip->oob_poi);
1438 if (size)
1439 chip->read_buf(mtd, oob, size);
1440
1441 return 0;
1442}
1443
1444/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001445 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001446 * @mtd: mtd info structure
1447 * @chip: nand chip info structure
1448 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001449 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001450 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001451 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001452static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001453 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Boris Brezillon846031d2016-02-03 20:11:00 +01001455 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001456 int eccbytes = chip->ecc.bytes;
1457 int eccsteps = chip->ecc.steps;
1458 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001459 uint8_t *ecc_calc = chip->buffers->ecccalc;
1460 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001461 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001462
Brian Norris1fbb9382012-05-02 10:14:55 -07001463 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001464
1465 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1466 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1467
Boris Brezillon846031d2016-02-03 20:11:00 +01001468 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1469 chip->ecc.total);
1470 if (ret)
1471 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001472
1473 eccsteps = chip->ecc.steps;
1474 p = buf;
1475
1476 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1477 int stat;
1478
1479 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001480 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001481 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001482 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001483 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001484 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1485 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001486 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001487 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001488}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301491 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001492 * @mtd: mtd info structure
1493 * @chip: nand chip info structure
1494 * @data_offs: offset of requested data within the page
1495 * @readlen: data length
1496 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001497 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001498 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001499static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001500 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1501 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001502{
Boris Brezillon846031d2016-02-03 20:11:00 +01001503 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001504 uint8_t *p;
1505 int data_col_addr, i, gaps = 0;
1506 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1507 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001508 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001509 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001510 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001511
Brian Norris7854d3f2011-06-23 14:12:08 -07001512 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001513 start_step = data_offs / chip->ecc.size;
1514 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1515 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301516 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001517
Brian Norris8b6e50c2011-05-25 14:59:01 -07001518 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001519 datafrag_len = num_steps * chip->ecc.size;
1520 eccfrag_len = num_steps * chip->ecc.bytes;
1521
1522 data_col_addr = start_step * chip->ecc.size;
1523 /* If we read not a page aligned data */
1524 if (data_col_addr != 0)
1525 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1526
1527 p = bufpoi + data_col_addr;
1528 chip->read_buf(mtd, p, datafrag_len);
1529
Brian Norris8b6e50c2011-05-25 14:59:01 -07001530 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001531 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1532 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1533
Brian Norris8b6e50c2011-05-25 14:59:01 -07001534 /*
1535 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001536 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001537 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001538 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1539 if (ret)
1540 return ret;
1541
1542 if (oobregion.length < eccfrag_len)
1543 gaps = 1;
1544
Alexey Korolev3d459552008-05-15 17:23:18 +01001545 if (gaps) {
1546 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1547 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1548 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001549 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001550 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001551 * about buswidth alignment in read_buf.
1552 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001553 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001554 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001555 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001556 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001557 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1558 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001559 aligned_len++;
1560
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001561 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001562 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001563 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1564 }
1565
Boris Brezillon846031d2016-02-03 20:11:00 +01001566 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1567 chip->oob_poi, index, eccfrag_len);
1568 if (ret)
1569 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001570
1571 p = bufpoi + data_col_addr;
1572 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1573 int stat;
1574
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001575 stat = chip->ecc.correct(mtd, p,
1576 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001577 if (stat == -EBADMSG &&
1578 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1579 /* check for empty pages with bitflips */
1580 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1581 &chip->buffers->ecccode[i],
1582 chip->ecc.bytes,
1583 NULL, 0,
1584 chip->ecc.strength);
1585 }
1586
Mike Dunn3f91e942012-04-25 12:06:09 -07001587 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001588 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001589 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001590 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001591 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1592 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001593 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001594 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001595}
1596
1597/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001598 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001599 * @mtd: mtd info structure
1600 * @chip: nand chip info structure
1601 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001602 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001603 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001604 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001605 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001606 */
1607static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001608 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001609{
Boris Brezillon846031d2016-02-03 20:11:00 +01001610 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001611 int eccbytes = chip->ecc.bytes;
1612 int eccsteps = chip->ecc.steps;
1613 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001614 uint8_t *ecc_calc = chip->buffers->ecccalc;
1615 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001616 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001617
1618 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1619 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1620 chip->read_buf(mtd, p, eccsize);
1621 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1622 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001623 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001624
Boris Brezillon846031d2016-02-03 20:11:00 +01001625 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1626 chip->ecc.total);
1627 if (ret)
1628 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001629
1630 eccsteps = chip->ecc.steps;
1631 p = buf;
1632
1633 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1634 int stat;
1635
1636 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001637 if (stat == -EBADMSG &&
1638 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1639 /* check for empty pages with bitflips */
1640 stat = nand_check_erased_ecc_chunk(p, eccsize,
1641 &ecc_code[i], eccbytes,
1642 NULL, 0,
1643 chip->ecc.strength);
1644 }
1645
Mike Dunn3f91e942012-04-25 12:06:09 -07001646 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001647 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001648 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001649 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001650 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1651 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001652 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001653 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001654}
1655
1656/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001657 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001658 * @mtd: mtd info structure
1659 * @chip: nand chip info structure
1660 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001661 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001662 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001663 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001664 * Hardware ECC for large page chips, require OOB to be read first. For this
1665 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1666 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1667 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1668 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001669 */
1670static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001671 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001672{
Boris Brezillon846031d2016-02-03 20:11:00 +01001673 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001674 int eccbytes = chip->ecc.bytes;
1675 int eccsteps = chip->ecc.steps;
1676 uint8_t *p = buf;
1677 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001678 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001679 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001680
1681 /* Read the OOB area first */
1682 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1683 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1684 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1685
Boris Brezillon846031d2016-02-03 20:11:00 +01001686 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1687 chip->ecc.total);
1688 if (ret)
1689 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001690
1691 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1692 int stat;
1693
1694 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1695 chip->read_buf(mtd, p, eccsize);
1696 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1697
1698 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001699 if (stat == -EBADMSG &&
1700 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1701 /* check for empty pages with bitflips */
1702 stat = nand_check_erased_ecc_chunk(p, eccsize,
1703 &ecc_code[i], eccbytes,
1704 NULL, 0,
1705 chip->ecc.strength);
1706 }
1707
Mike Dunn3f91e942012-04-25 12:06:09 -07001708 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001709 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001710 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001711 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001712 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1713 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001714 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001715 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001716}
1717
1718/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001719 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001720 * @mtd: mtd info structure
1721 * @chip: nand chip info structure
1722 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001723 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001724 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001725 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001726 * The hw generator calculates the error syndrome automatically. Therefore we
1727 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001728 */
1729static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001730 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001731{
1732 int i, eccsize = chip->ecc.size;
1733 int eccbytes = chip->ecc.bytes;
1734 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001735 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001736 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001737 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001738 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001739
1740 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1741 int stat;
1742
1743 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1744 chip->read_buf(mtd, p, eccsize);
1745
1746 if (chip->ecc.prepad) {
1747 chip->read_buf(mtd, oob, chip->ecc.prepad);
1748 oob += chip->ecc.prepad;
1749 }
1750
1751 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1752 chip->read_buf(mtd, oob, eccbytes);
1753 stat = chip->ecc.correct(mtd, p, oob, NULL);
1754
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001755 oob += eccbytes;
1756
1757 if (chip->ecc.postpad) {
1758 chip->read_buf(mtd, oob, chip->ecc.postpad);
1759 oob += chip->ecc.postpad;
1760 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001761
1762 if (stat == -EBADMSG &&
1763 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1764 /* check for empty pages with bitflips */
1765 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1766 oob - eccpadbytes,
1767 eccpadbytes,
1768 NULL, 0,
1769 chip->ecc.strength);
1770 }
1771
1772 if (stat < 0) {
1773 mtd->ecc_stats.failed++;
1774 } else {
1775 mtd->ecc_stats.corrected += stat;
1776 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1777 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001778 }
1779
1780 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001781 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001782 if (i)
1783 chip->read_buf(mtd, oob, i);
1784
Mike Dunn3f91e942012-04-25 12:06:09 -07001785 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001786}
1787
1788/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001789 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001790 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001791 * @oob: oob destination address
1792 * @ops: oob ops structure
1793 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001794 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001795static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001796 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001797{
Boris Brezillon846031d2016-02-03 20:11:00 +01001798 struct nand_chip *chip = mtd_to_nand(mtd);
1799 int ret;
1800
Florian Fainellif8ac0412010-09-07 13:23:43 +02001801 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001802
Brian Norris0612b9d2011-08-30 18:45:40 -07001803 case MTD_OPS_PLACE_OOB:
1804 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001805 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1806 return oob + len;
1807
Boris Brezillon846031d2016-02-03 20:11:00 +01001808 case MTD_OPS_AUTO_OOB:
1809 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1810 ops->ooboffs, len);
1811 BUG_ON(ret);
1812 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001813
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001814 default:
1815 BUG();
1816 }
1817 return NULL;
1818}
1819
1820/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001821 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1822 * @mtd: MTD device structure
1823 * @retry_mode: the retry mode to use
1824 *
1825 * Some vendors supply a special command to shift the Vt threshold, to be used
1826 * when there are too many bitflips in a page (i.e., ECC error). After setting
1827 * a new threshold, the host should retry reading the page.
1828 */
1829static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1830{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001831 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001832
1833 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1834
1835 if (retry_mode >= chip->read_retries)
1836 return -EINVAL;
1837
1838 if (!chip->setup_read_retry)
1839 return -EOPNOTSUPP;
1840
1841 return chip->setup_read_retry(mtd, retry_mode);
1842}
1843
1844/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001845 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001846 * @mtd: MTD device structure
1847 * @from: offset to read from
1848 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001849 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001850 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001851 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001852static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1853 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001854{
Brian Norrise47f3db2012-05-02 10:14:56 -07001855 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001856 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001857 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001858 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001859 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001860 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001861
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001862 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001863 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001864 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001865 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001866 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001868 chipnr = (int)(from >> chip->chip_shift);
1869 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001871 realpage = (int)(from >> chip->page_shift);
1872 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001874 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001876 buf = ops->datbuf;
1877 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001878 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001879
Florian Fainellif8ac0412010-09-07 13:23:43 +02001880 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001881 unsigned int ecc_failures = mtd->ecc_stats.failed;
1882
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001883 bytes = min(mtd->writesize - col, readlen);
1884 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001885
Kamal Dasu66507c72014-05-01 20:51:19 -04001886 if (!aligned)
1887 use_bufpoi = 1;
1888 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09001889 use_bufpoi = !virt_addr_valid(buf) ||
1890 !IS_ALIGNED((unsigned long)buf,
1891 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04001892 else
1893 use_bufpoi = 0;
1894
Brian Norris8b6e50c2011-05-25 14:59:01 -07001895 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001896 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001897 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1898
1899 if (use_bufpoi && aligned)
1900 pr_debug("%s: using read bounce buffer for buf@%p\n",
1901 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Brian Norrisba84fb52014-01-03 15:13:33 -08001903read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01001904 if (nand_standard_page_accessors(&chip->ecc))
1905 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Mike Dunnedbc45402012-04-25 12:06:11 -07001907 /*
1908 * Now read the page into the buffer. Absent an error,
1909 * the read methods return max bitflips per ecc step.
1910 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001911 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001912 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001913 oob_required,
1914 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001915 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1916 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001917 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001918 col, bytes, bufpoi,
1919 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001920 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001921 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001922 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001923 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001924 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001925 /* Invalidate page cache */
1926 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001927 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001928 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001929
1930 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001931 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001932 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001933 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001934 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001935 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001936 chip->pagebuf_bitflips = ret;
1937 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001938 /* Invalidate page cache */
1939 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001940 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001941 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001943
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001944 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001945 int toread = min(oobreadlen, max_oobsize);
1946
1947 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01001948 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001949 oob, ops, toread);
1950 oobreadlen -= toread;
1951 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001952 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001953
1954 if (chip->options & NAND_NEED_READRDY) {
1955 /* Apply delay or wait for ready/busy pin */
1956 if (!chip->dev_ready)
1957 udelay(chip->chip_delay);
1958 else
1959 nand_wait_ready(mtd);
1960 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001961
Brian Norrisba84fb52014-01-03 15:13:33 -08001962 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001963 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001964 retry_mode++;
1965 ret = nand_setup_read_retry(mtd,
1966 retry_mode);
1967 if (ret < 0)
1968 break;
1969
1970 /* Reset failures; retry */
1971 mtd->ecc_stats.failed = ecc_failures;
1972 goto read_retry;
1973 } else {
1974 /* No more retry modes; real failure */
1975 ecc_fail = true;
1976 }
1977 }
1978
1979 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09001980 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001981 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001982 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001983 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001984 max_bitflips = max_t(unsigned int, max_bitflips,
1985 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001988 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001989
Brian Norrisba84fb52014-01-03 15:13:33 -08001990 /* Reset to retry mode 0 */
1991 if (retry_mode) {
1992 ret = nand_setup_read_retry(mtd, 0);
1993 if (ret < 0)
1994 break;
1995 retry_mode = 0;
1996 }
1997
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001998 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001999 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Brian Norris8b6e50c2011-05-25 14:59:01 -07002001 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 col = 0;
2003 /* Increment page address */
2004 realpage++;
2005
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002006 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /* Check, if we cross a chip boundary */
2008 if (!page) {
2009 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002010 chip->select_chip(mtd, -1);
2011 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002014 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002016 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002017 if (oob)
2018 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Mike Dunn3f91e942012-04-25 12:06:09 -07002020 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002021 return ret;
2022
Brian Norrisb72f3df2013-12-03 11:04:14 -08002023 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002024 return -EBADMSG;
2025
Mike Dunnedbc45402012-04-25 12:06:11 -07002026 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002027}
2028
2029/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002030 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002031 * @mtd: MTD device structure
2032 * @from: offset to read from
2033 * @len: number of bytes to read
2034 * @retlen: pointer to variable to store the number of read bytes
2035 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002036 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002037 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002038 */
2039static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2040 size_t *retlen, uint8_t *buf)
2041{
Brian Norris4a89ff82011-08-30 18:45:45 -07002042 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002043 int ret;
2044
Huang Shijie6a8214a2012-11-19 14:43:30 +08002045 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002046 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002047 ops.len = len;
2048 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002049 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002050 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002051 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002052 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002053 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
2056/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002057 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002058 * @mtd: mtd info structure
2059 * @chip: nand chip info structure
2060 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002061 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002062int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002063{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002064 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002065 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002066 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002067}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002068EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002069
2070/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002071 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002072 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002073 * @mtd: mtd info structure
2074 * @chip: nand chip info structure
2075 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002076 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002077int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2078 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002079{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002080 int length = mtd->oobsize;
2081 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2082 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002083 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002084 int i, toread, sndrnd = 0, pos;
2085
2086 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2087 for (i = 0; i < chip->ecc.steps; i++) {
2088 if (sndrnd) {
2089 pos = eccsize + i * (eccsize + chunk);
2090 if (mtd->writesize > 512)
2091 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2092 else
2093 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2094 } else
2095 sndrnd = 1;
2096 toread = min_t(int, length, chunk);
2097 chip->read_buf(mtd, bufpoi, toread);
2098 bufpoi += toread;
2099 length -= toread;
2100 }
2101 if (length > 0)
2102 chip->read_buf(mtd, bufpoi, length);
2103
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002104 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002105}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002106EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002107
2108/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002109 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002110 * @mtd: mtd info structure
2111 * @chip: nand chip info structure
2112 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002113 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002114int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002115{
2116 int status = 0;
2117 const uint8_t *buf = chip->oob_poi;
2118 int length = mtd->oobsize;
2119
2120 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2121 chip->write_buf(mtd, buf, length);
2122 /* Send command to program the OOB data */
2123 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2124
2125 status = chip->waitfunc(mtd, chip);
2126
Savin Zlobec0d420f92006-06-21 11:51:20 +02002127 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002128}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002129EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002130
2131/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002132 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002133 * with syndrome - only for large page flash
2134 * @mtd: mtd info structure
2135 * @chip: nand chip info structure
2136 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002137 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002138int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2139 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002140{
2141 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2142 int eccsize = chip->ecc.size, length = mtd->oobsize;
2143 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2144 const uint8_t *bufpoi = chip->oob_poi;
2145
2146 /*
2147 * data-ecc-data-ecc ... ecc-oob
2148 * or
2149 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2150 */
2151 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2152 pos = steps * (eccsize + chunk);
2153 steps = 0;
2154 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002155 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002156
2157 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2158 for (i = 0; i < steps; i++) {
2159 if (sndcmd) {
2160 if (mtd->writesize <= 512) {
2161 uint32_t fill = 0xFFFFFFFF;
2162
2163 len = eccsize;
2164 while (len > 0) {
2165 int num = min_t(int, len, 4);
2166 chip->write_buf(mtd, (uint8_t *)&fill,
2167 num);
2168 len -= num;
2169 }
2170 } else {
2171 pos = eccsize + i * (eccsize + chunk);
2172 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2173 }
2174 } else
2175 sndcmd = 1;
2176 len = min_t(int, length, chunk);
2177 chip->write_buf(mtd, bufpoi, len);
2178 bufpoi += len;
2179 length -= len;
2180 }
2181 if (length > 0)
2182 chip->write_buf(mtd, bufpoi, length);
2183
2184 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2185 status = chip->waitfunc(mtd, chip);
2186
2187 return status & NAND_STATUS_FAIL ? -EIO : 0;
2188}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002189EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002190
2191/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002192 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002193 * @mtd: MTD device structure
2194 * @from: offset to read from
2195 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002197 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002199static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2200 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Brian Norrisc00a0992012-05-01 17:12:54 -07002202 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002203 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002204 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002205 int readlen = ops->ooblen;
2206 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002207 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002208 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Brian Norris289c0522011-07-19 10:06:09 -07002210 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302211 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Brian Norris041e4572011-06-23 16:45:24 -07002213 stats = mtd->ecc_stats;
2214
Boris BREZILLON29f10582016-03-07 10:46:52 +01002215 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002216
2217 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002218 pr_debug("%s: attempt to start read outside oob\n",
2219 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002220 return -EINVAL;
2221 }
2222
2223 /* Do not allow reads past end of device */
2224 if (unlikely(from >= mtd->size ||
2225 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2226 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002227 pr_debug("%s: attempt to read beyond end of device\n",
2228 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002229 return -EINVAL;
2230 }
Vitaly Wool70145682006-11-03 18:20:38 +03002231
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002232 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002233 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002235 /* Shift to get page */
2236 realpage = (int)(from >> chip->page_shift);
2237 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Florian Fainellif8ac0412010-09-07 13:23:43 +02002239 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002240 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002241 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002242 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002243 ret = chip->ecc.read_oob(mtd, chip, page);
2244
2245 if (ret < 0)
2246 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002247
2248 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002249 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002250
Brian Norris5bc7c332013-03-13 09:51:31 -07002251 if (chip->options & NAND_NEED_READRDY) {
2252 /* Apply delay or wait for ready/busy pin */
2253 if (!chip->dev_ready)
2254 udelay(chip->chip_delay);
2255 else
2256 nand_wait_ready(mtd);
2257 }
2258
Vitaly Wool70145682006-11-03 18:20:38 +03002259 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002260 if (!readlen)
2261 break;
2262
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002263 /* Increment page address */
2264 realpage++;
2265
2266 page = realpage & chip->pagemask;
2267 /* Check, if we cross a chip boundary */
2268 if (!page) {
2269 chipnr++;
2270 chip->select_chip(mtd, -1);
2271 chip->select_chip(mtd, chipnr);
2272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002274 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002276 ops->oobretlen = ops->ooblen - readlen;
2277
2278 if (ret < 0)
2279 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002280
2281 if (mtd->ecc_stats.failed - stats.failed)
2282 return -EBADMSG;
2283
2284 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
2287/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002288 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002289 * @mtd: MTD device structure
2290 * @from: offset to read from
2291 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002293 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002295static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2296 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002298 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002299
2300 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002303 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002304 pr_debug("%s: attempt to read beyond end of device\n",
2305 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return -EINVAL;
2307 }
2308
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002309 if (ops->mode != MTD_OPS_PLACE_OOB &&
2310 ops->mode != MTD_OPS_AUTO_OOB &&
2311 ops->mode != MTD_OPS_RAW)
2312 return -ENOTSUPP;
2313
Huang Shijie6a8214a2012-11-19 14:43:30 +08002314 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002316 if (!ops->datbuf)
2317 ret = nand_do_read_oob(mtd, from, ops);
2318 else
2319 ret = nand_do_read_ops(mtd, from, ops);
2320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002322 return ret;
2323}
2324
2325
2326/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002327 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002328 * @mtd: mtd info structure
2329 * @chip: nand chip info structure
2330 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002331 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002332 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002333 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002334 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002335 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002336int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2337 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002338{
2339 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002340 if (oob_required)
2341 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002342
2343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002345EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002347/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002348 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002349 * @mtd: mtd info structure
2350 * @chip: nand chip info structure
2351 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002352 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002353 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002354 *
2355 * We need a special oob layout and handling even when ECC isn't checked.
2356 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002357static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002358 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002359 const uint8_t *buf, int oob_required,
2360 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002361{
2362 int eccsize = chip->ecc.size;
2363 int eccbytes = chip->ecc.bytes;
2364 uint8_t *oob = chip->oob_poi;
2365 int steps, size;
2366
2367 for (steps = chip->ecc.steps; steps > 0; steps--) {
2368 chip->write_buf(mtd, buf, eccsize);
2369 buf += eccsize;
2370
2371 if (chip->ecc.prepad) {
2372 chip->write_buf(mtd, oob, chip->ecc.prepad);
2373 oob += chip->ecc.prepad;
2374 }
2375
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002376 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002377 oob += eccbytes;
2378
2379 if (chip->ecc.postpad) {
2380 chip->write_buf(mtd, oob, chip->ecc.postpad);
2381 oob += chip->ecc.postpad;
2382 }
2383 }
2384
2385 size = mtd->oobsize - (oob - chip->oob_poi);
2386 if (size)
2387 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002388
2389 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002390}
2391/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002392 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002393 * @mtd: mtd info structure
2394 * @chip: nand chip info structure
2395 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002396 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002397 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002398 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002399static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002400 const uint8_t *buf, int oob_required,
2401 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002402{
Boris Brezillon846031d2016-02-03 20:11:00 +01002403 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002404 int eccbytes = chip->ecc.bytes;
2405 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002406 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002407 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002408
Brian Norris7854d3f2011-06-23 14:12:08 -07002409 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002410 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2411 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002412
Boris Brezillon846031d2016-02-03 20:11:00 +01002413 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2414 chip->ecc.total);
2415 if (ret)
2416 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002417
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002418 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002419}
2420
2421/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002422 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002423 * @mtd: mtd info structure
2424 * @chip: nand chip info structure
2425 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002426 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002427 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002428 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002429static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002430 const uint8_t *buf, int oob_required,
2431 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002432{
Boris Brezillon846031d2016-02-03 20:11:00 +01002433 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002434 int eccbytes = chip->ecc.bytes;
2435 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002436 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002437 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002438
2439 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2440 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002441 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002442 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2443 }
2444
Boris Brezillon846031d2016-02-03 20:11:00 +01002445 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2446 chip->ecc.total);
2447 if (ret)
2448 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002449
2450 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002451
2452 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002453}
2454
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302455
2456/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002457 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302458 * @mtd: mtd info structure
2459 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002460 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302461 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002462 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302463 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002464 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302465 */
2466static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2467 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002468 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002469 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302470{
2471 uint8_t *oob_buf = chip->oob_poi;
2472 uint8_t *ecc_calc = chip->buffers->ecccalc;
2473 int ecc_size = chip->ecc.size;
2474 int ecc_bytes = chip->ecc.bytes;
2475 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302476 uint32_t start_step = offset / ecc_size;
2477 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2478 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002479 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302480
2481 for (step = 0; step < ecc_steps; step++) {
2482 /* configure controller for WRITE access */
2483 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2484
2485 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002486 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302487
2488 /* mask ECC of un-touched subpages by padding 0xFF */
2489 if ((step < start_step) || (step > end_step))
2490 memset(ecc_calc, 0xff, ecc_bytes);
2491 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002492 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302493
2494 /* mask OOB of un-touched subpages by padding 0xFF */
2495 /* if oob_required, preserve OOB metadata of written subpage */
2496 if (!oob_required || (step < start_step) || (step > end_step))
2497 memset(oob_buf, 0xff, oob_bytes);
2498
Brian Norrisd6a950802013-08-08 17:16:36 -07002499 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302500 ecc_calc += ecc_bytes;
2501 oob_buf += oob_bytes;
2502 }
2503
2504 /* copy calculated ECC for whole page to chip->buffer->oob */
2505 /* this include masked-value(0xFF) for unwritten subpages */
2506 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002507 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2508 chip->ecc.total);
2509 if (ret)
2510 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302511
2512 /* write OOB buffer to NAND device */
2513 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2514
2515 return 0;
2516}
2517
2518
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002519/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002520 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002521 * @mtd: mtd info structure
2522 * @chip: nand chip info structure
2523 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002524 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002525 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002526 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002527 * The hw generator calculates the error syndrome automatically. Therefore we
2528 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002529 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002530static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002531 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002532 const uint8_t *buf, int oob_required,
2533 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002534{
2535 int i, eccsize = chip->ecc.size;
2536 int eccbytes = chip->ecc.bytes;
2537 int eccsteps = chip->ecc.steps;
2538 const uint8_t *p = buf;
2539 uint8_t *oob = chip->oob_poi;
2540
2541 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2542
2543 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2544 chip->write_buf(mtd, p, eccsize);
2545
2546 if (chip->ecc.prepad) {
2547 chip->write_buf(mtd, oob, chip->ecc.prepad);
2548 oob += chip->ecc.prepad;
2549 }
2550
2551 chip->ecc.calculate(mtd, p, oob);
2552 chip->write_buf(mtd, oob, eccbytes);
2553 oob += eccbytes;
2554
2555 if (chip->ecc.postpad) {
2556 chip->write_buf(mtd, oob, chip->ecc.postpad);
2557 oob += chip->ecc.postpad;
2558 }
2559 }
2560
2561 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002562 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002563 if (i)
2564 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002565
2566 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002567}
2568
2569/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002570 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002571 * @mtd: MTD device structure
2572 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302573 * @offset: address offset within the page
2574 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002575 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002576 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002577 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002578 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002579 */
2580static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302581 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002582 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002583{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302584 int status, subpage;
2585
2586 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2587 chip->ecc.write_subpage)
2588 subpage = offset || (data_len < mtd->writesize);
2589 else
2590 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002591
Marc Gonzalez3371d662016-11-15 10:56:20 +01002592 if (nand_standard_page_accessors(&chip->ecc))
2593 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002594
David Woodhouse956e9442006-09-25 17:12:39 +01002595 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302596 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002597 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302598 else if (subpage)
2599 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002600 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002601 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002602 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2603 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002604
2605 if (status < 0)
2606 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002607
Boris Brezillon41145642017-05-16 18:27:49 +02002608 if (nand_standard_page_accessors(&chip->ecc)) {
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002609 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002610
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002611 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002612 if (status & NAND_STATUS_FAIL)
2613 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002614 }
2615
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002616 return 0;
2617}
2618
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002619/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002620 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002621 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002622 * @oob: oob data buffer
2623 * @len: oob data write length
2624 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002625 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002626static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2627 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002628{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002629 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002630 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002631
2632 /*
2633 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2634 * data from a previous OOB read.
2635 */
2636 memset(chip->oob_poi, 0xff, mtd->oobsize);
2637
Florian Fainellif8ac0412010-09-07 13:23:43 +02002638 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002639
Brian Norris0612b9d2011-08-30 18:45:40 -07002640 case MTD_OPS_PLACE_OOB:
2641 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002642 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2643 return oob + len;
2644
Boris Brezillon846031d2016-02-03 20:11:00 +01002645 case MTD_OPS_AUTO_OOB:
2646 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2647 ops->ooboffs, len);
2648 BUG_ON(ret);
2649 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002650
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002651 default:
2652 BUG();
2653 }
2654 return NULL;
2655}
2656
Florian Fainellif8ac0412010-09-07 13:23:43 +02002657#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002658
2659/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002660 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002661 * @mtd: MTD device structure
2662 * @to: offset to write to
2663 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002664 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002665 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002666 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002667static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2668 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002669{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002670 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002671 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002672 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002673
2674 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002675 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002676
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002677 uint8_t *oob = ops->oobbuf;
2678 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302679 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002680 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002681
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002682 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002683 if (!writelen)
2684 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002685
Brian Norris8b6e50c2011-05-25 14:59:01 -07002686 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002687 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002688 pr_notice("%s: attempt to write non page aligned data\n",
2689 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002690 return -EINVAL;
2691 }
2692
Thomas Gleixner29072b92006-09-28 15:38:36 +02002693 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002694
Thomas Gleixner6a930962006-06-28 00:11:45 +02002695 chipnr = (int)(to >> chip->chip_shift);
2696 chip->select_chip(mtd, chipnr);
2697
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002698 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002699 if (nand_check_wp(mtd)) {
2700 ret = -EIO;
2701 goto err_out;
2702 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002703
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002704 realpage = (int)(to >> chip->page_shift);
2705 page = realpage & chip->pagemask;
2706 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2707
2708 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002709 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2710 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002711 chip->pagebuf = -1;
2712
Maxim Levitsky782ce792010-02-22 20:39:36 +02002713 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002714 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2715 ret = -EINVAL;
2716 goto err_out;
2717 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002718
Florian Fainellif8ac0412010-09-07 13:23:43 +02002719 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002720 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002721 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002722 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002723 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002724
Kamal Dasu66507c72014-05-01 20:51:19 -04002725 if (part_pagewr)
2726 use_bufpoi = 1;
2727 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002728 use_bufpoi = !virt_addr_valid(buf) ||
2729 !IS_ALIGNED((unsigned long)buf,
2730 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002731 else
2732 use_bufpoi = 0;
2733
2734 /* Partial page write?, or need to use bounce buffer */
2735 if (use_bufpoi) {
2736 pr_debug("%s: using write bounce buffer for buf@%p\n",
2737 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04002738 if (part_pagewr)
2739 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002740 chip->pagebuf = -1;
2741 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2742 memcpy(&chip->buffers->databuf[column], buf, bytes);
2743 wbuf = chip->buffers->databuf;
2744 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002745
Maxim Levitsky782ce792010-02-22 20:39:36 +02002746 if (unlikely(oob)) {
2747 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002748 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002749 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002750 } else {
2751 /* We still need to erase leftover OOB data */
2752 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002753 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002754
2755 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002756 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002757 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002758 if (ret)
2759 break;
2760
2761 writelen -= bytes;
2762 if (!writelen)
2763 break;
2764
Thomas Gleixner29072b92006-09-28 15:38:36 +02002765 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002766 buf += bytes;
2767 realpage++;
2768
2769 page = realpage & chip->pagemask;
2770 /* Check, if we cross a chip boundary */
2771 if (!page) {
2772 chipnr++;
2773 chip->select_chip(mtd, -1);
2774 chip->select_chip(mtd, chipnr);
2775 }
2776 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002777
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002778 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002779 if (unlikely(oob))
2780 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002781
2782err_out:
2783 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002784 return ret;
2785}
2786
2787/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002788 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002789 * @mtd: MTD device structure
2790 * @to: offset to write to
2791 * @len: number of bytes to write
2792 * @retlen: pointer to variable to store the number of written bytes
2793 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002794 *
2795 * NAND write with ECC. Used when performing writes in interrupt context, this
2796 * may for example be called by mtdoops when writing an oops while in panic.
2797 */
2798static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2799 size_t *retlen, const uint8_t *buf)
2800{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002801 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002802 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002803 int ret;
2804
Brian Norris8b6e50c2011-05-25 14:59:01 -07002805 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002806 panic_nand_wait(mtd, chip, 400);
2807
Brian Norris8b6e50c2011-05-25 14:59:01 -07002808 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002809 panic_nand_get_device(chip, mtd, FL_WRITING);
2810
Brian Norris0ec56dc2015-02-28 02:02:30 -08002811 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002812 ops.len = len;
2813 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002814 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002815
Brian Norris4a89ff82011-08-30 18:45:45 -07002816 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002817
Brian Norris4a89ff82011-08-30 18:45:45 -07002818 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002819 return ret;
2820}
2821
2822/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002823 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002824 * @mtd: MTD device structure
2825 * @to: offset to write to
2826 * @len: number of bytes to write
2827 * @retlen: pointer to variable to store the number of written bytes
2828 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002830 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002832static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002833 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Brian Norris4a89ff82011-08-30 18:45:45 -07002835 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002836 int ret;
2837
Huang Shijie6a8214a2012-11-19 14:43:30 +08002838 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002839 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002840 ops.len = len;
2841 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002842 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002843 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002844 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002845 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002846 return ret;
2847}
2848
2849/**
2850 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002851 * @mtd: MTD device structure
2852 * @to: offset to write to
2853 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002854 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002855 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002856 */
2857static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2858 struct mtd_oob_ops *ops)
2859{
Adrian Hunter03736152007-01-31 17:58:29 +02002860 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002861 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Brian Norris289c0522011-07-19 10:06:09 -07002863 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302864 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Boris BREZILLON29f10582016-03-07 10:46:52 +01002866 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002869 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002870 pr_debug("%s: attempt to write past end of page\n",
2871 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 return -EINVAL;
2873 }
2874
Adrian Hunter03736152007-01-31 17:58:29 +02002875 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002876 pr_debug("%s: attempt to start write outside oob\n",
2877 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002878 return -EINVAL;
2879 }
2880
Jason Liu775adc3d42011-02-25 13:06:18 +08002881 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002882 if (unlikely(to >= mtd->size ||
2883 ops->ooboffs + ops->ooblen >
2884 ((mtd->size >> chip->page_shift) -
2885 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002886 pr_debug("%s: attempt to write beyond end of device\n",
2887 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002888 return -EINVAL;
2889 }
2890
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002891 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002892
2893 /*
2894 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2895 * of my DiskOnChip 2000 test units) will clear the whole data page too
2896 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2897 * it in the doc2000 driver in August 1999. dwmw2.
2898 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002899 nand_reset(chip, chipnr);
2900
2901 chip->select_chip(mtd, chipnr);
2902
2903 /* Shift to get page */
2904 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
2906 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002907 if (nand_check_wp(mtd)) {
2908 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002909 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002910 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002913 if (page == chip->pagebuf)
2914 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002916 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002917
Brian Norris0612b9d2011-08-30 18:45:40 -07002918 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002919 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2920 else
2921 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002922
Huang Shijieb0bb6902012-11-19 14:43:29 +08002923 chip->select_chip(mtd, -1);
2924
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002925 if (status)
2926 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Vitaly Wool70145682006-11-03 18:20:38 +03002928 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002930 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002931}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002933/**
2934 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002935 * @mtd: MTD device structure
2936 * @to: offset to write to
2937 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002938 */
2939static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2940 struct mtd_oob_ops *ops)
2941{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002942 int ret = -ENOTSUPP;
2943
2944 ops->retlen = 0;
2945
2946 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002947 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002948 pr_debug("%s: attempt to write beyond end of device\n",
2949 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002950 return -EINVAL;
2951 }
2952
Huang Shijie6a8214a2012-11-19 14:43:30 +08002953 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002954
Florian Fainellif8ac0412010-09-07 13:23:43 +02002955 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002956 case MTD_OPS_PLACE_OOB:
2957 case MTD_OPS_AUTO_OOB:
2958 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002959 break;
2960
2961 default:
2962 goto out;
2963 }
2964
2965 if (!ops->datbuf)
2966 ret = nand_do_write_oob(mtd, to, ops);
2967 else
2968 ret = nand_do_write_ops(mtd, to, ops);
2969
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002970out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002971 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return ret;
2973}
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975/**
Brian Norris49c50b92014-05-06 16:02:19 -07002976 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002977 * @mtd: MTD device structure
2978 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 *
Brian Norris49c50b92014-05-06 16:02:19 -07002980 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 */
Brian Norris49c50b92014-05-06 16:02:19 -07002982static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002984 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002986 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2987 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002988
2989 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
2992/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002994 * @mtd: MTD device structure
2995 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002997 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002999static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
David Woodhousee0c7d762006-05-13 18:07:53 +01003001 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003005 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003006 * @mtd: MTD device structure
3007 * @instr: erase instruction
3008 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003010 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003012int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3013 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
Adrian Hunter69423d92008-12-10 13:37:21 +00003015 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003016 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003017 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Brian Norris289c0522011-07-19 10:06:09 -07003019 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3020 __func__, (unsigned long long)instr->addr,
3021 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303023 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003027 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003030 page = (int)(instr->addr >> chip->page_shift);
3031 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
3033 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003034 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003037 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 /* Check, if it is write protected */
3040 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003041 pr_debug("%s: device is write protected!\n",
3042 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 instr->state = MTD_ERASE_FAILED;
3044 goto erase_exit;
3045 }
3046
3047 /* Loop through the pages */
3048 len = instr->len;
3049
3050 instr->state = MTD_ERASING;
3051
3052 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003053 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003054 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303055 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003056 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3057 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 instr->state = MTD_ERASE_FAILED;
3059 goto erase_exit;
3060 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003061
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003062 /*
3063 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003064 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003065 */
3066 if (page <= chip->pagebuf && chip->pagebuf <
3067 (page + pages_per_block))
3068 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Brian Norris49c50b92014-05-06 16:02:19 -07003070 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003073 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003074 pr_debug("%s: failed erase, page 0x%08x\n",
3075 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003077 instr->fail_addr =
3078 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 goto erase_exit;
3080 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003083 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 page += pages_per_block;
3085
3086 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003087 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003089 chip->select_chip(mtd, -1);
3090 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
3092 }
3093 instr->state = MTD_ERASE_DONE;
3094
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003095erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
3097 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003100 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 nand_release_device(mtd);
3102
David Woodhouse49defc02007-10-06 15:01:59 -04003103 /* Do call back function */
3104 if (!ret)
3105 mtd_erase_callback(instr);
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 /* Return more or less happy */
3108 return ret;
3109}
3110
3111/**
3112 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003113 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003115 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003117static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
Brian Norris289c0522011-07-19 10:06:09 -07003119 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003122 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003124 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003128 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003129 * @mtd: MTD device structure
3130 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003132static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303134 struct nand_chip *chip = mtd_to_nand(mtd);
3135 int chipnr = (int)(offs >> chip->chip_shift);
3136 int ret;
3137
3138 /* Select the NAND device */
3139 nand_get_device(mtd, FL_READING);
3140 chip->select_chip(mtd, chipnr);
3141
3142 ret = nand_block_checkbad(mtd, offs, 0);
3143
3144 chip->select_chip(mtd, -1);
3145 nand_release_device(mtd);
3146
3147 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
3150/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003151 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003152 * @mtd: MTD device structure
3153 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003155static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 int ret;
3158
Florian Fainellif8ac0412010-09-07 13:23:43 +02003159 ret = nand_block_isbad(mtd, ofs);
3160 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003161 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if (ret > 0)
3163 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003164 return ret;
3165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Brian Norris5a0edb22013-07-30 17:52:58 -07003167 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
3170/**
Zach Brown56718422017-01-10 13:30:20 -06003171 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3172 * @mtd: MTD device structure
3173 * @ofs: offset relative to mtd start
3174 * @len: length of mtd
3175 */
3176static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3177{
3178 struct nand_chip *chip = mtd_to_nand(mtd);
3179 u32 part_start_block;
3180 u32 part_end_block;
3181 u32 part_start_die;
3182 u32 part_end_die;
3183
3184 /*
3185 * max_bb_per_die and blocks_per_die used to determine
3186 * the maximum bad block count.
3187 */
3188 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3189 return -ENOTSUPP;
3190
3191 /* Get the start and end of the partition in erase blocks. */
3192 part_start_block = mtd_div_by_eb(ofs, mtd);
3193 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3194
3195 /* Get the start and end LUNs of the partition. */
3196 part_start_die = part_start_block / chip->blocks_per_die;
3197 part_end_die = part_end_block / chip->blocks_per_die;
3198
3199 /*
3200 * Look up the bad blocks per unit and multiply by the number of units
3201 * that the partition spans.
3202 */
3203 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3204}
3205
3206/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003207 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3208 * @mtd: MTD device structure
3209 * @chip: nand chip info structure
3210 * @addr: feature address.
3211 * @subfeature_param: the subfeature parameters, a four bytes array.
3212 */
3213static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3214 int addr, uint8_t *subfeature_param)
3215{
3216 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003217 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003218
David Mosbergerd914c932013-05-29 15:30:13 +03003219 if (!chip->onfi_version ||
3220 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3221 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003222 return -EINVAL;
3223
3224 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003225 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3226 chip->write_byte(mtd, subfeature_param[i]);
3227
Huang Shijie7db03ec2012-09-13 14:57:52 +08003228 status = chip->waitfunc(mtd, chip);
3229 if (status & NAND_STATUS_FAIL)
3230 return -EIO;
3231 return 0;
3232}
3233
3234/**
3235 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3236 * @mtd: MTD device structure
3237 * @chip: nand chip info structure
3238 * @addr: feature address.
3239 * @subfeature_param: the subfeature parameters, a four bytes array.
3240 */
3241static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3242 int addr, uint8_t *subfeature_param)
3243{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003244 int i;
3245
David Mosbergerd914c932013-05-29 15:30:13 +03003246 if (!chip->onfi_version ||
3247 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3248 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003249 return -EINVAL;
3250
Huang Shijie7db03ec2012-09-13 14:57:52 +08003251 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003252 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3253 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003254 return 0;
3255}
3256
3257/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003258 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3259 * -ENOTSUPP
3260 * @mtd: MTD device structure
3261 * @chip: nand chip info structure
3262 * @addr: feature address.
3263 * @subfeature_param: the subfeature parameters, a four bytes array.
3264 *
3265 * Should be used by NAND controller drivers that do not support the SET/GET
3266 * FEATURES operations.
3267 */
3268int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3269 struct nand_chip *chip, int addr,
3270 u8 *subfeature_param)
3271{
3272 return -ENOTSUPP;
3273}
3274EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3275
3276/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003277 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003278 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003279 */
3280static int nand_suspend(struct mtd_info *mtd)
3281{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003282 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003283}
3284
3285/**
3286 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003287 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003288 */
3289static void nand_resume(struct mtd_info *mtd)
3290{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003291 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003292
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003293 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003294 nand_release_device(mtd);
3295 else
Brian Norrisd0370212011-07-19 10:06:08 -07003296 pr_err("%s called for a chip which is not in suspended state\n",
3297 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003298}
3299
Scott Branden72ea4032014-11-20 11:18:05 -08003300/**
3301 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3302 * prevent further operations
3303 * @mtd: MTD device structure
3304 */
3305static void nand_shutdown(struct mtd_info *mtd)
3306{
Brian Norris9ca641b2015-11-09 16:37:28 -08003307 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003308}
3309
Brian Norris8b6e50c2011-05-25 14:59:01 -07003310/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003311static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003312{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003313 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003316 if (!chip->chip_delay)
3317 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
3319 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003320 if (chip->cmdfunc == NULL)
3321 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
3323 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003324 if (chip->waitfunc == NULL)
3325 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003327 if (!chip->select_chip)
3328 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003329
Huang Shijie4204ccc2013-08-16 10:10:07 +08003330 /* set for ONFI nand */
3331 if (!chip->onfi_set_features)
3332 chip->onfi_set_features = nand_onfi_set_features;
3333 if (!chip->onfi_get_features)
3334 chip->onfi_get_features = nand_onfi_get_features;
3335
Brian Norris68e80782013-07-18 01:17:02 -07003336 /* If called twice, pointers that depend on busw may need to be reset */
3337 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003338 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3339 if (!chip->read_word)
3340 chip->read_word = nand_read_word;
3341 if (!chip->block_bad)
3342 chip->block_bad = nand_block_bad;
3343 if (!chip->block_markbad)
3344 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003345 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003346 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003347 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3348 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003349 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003350 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003351 if (!chip->scan_bbt)
3352 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003353
3354 if (!chip->controller) {
3355 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003356 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003357 }
3358
Masahiro Yamada477544c2017-03-30 17:15:05 +09003359 if (!chip->buf_align)
3360 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003361}
3362
Brian Norris8b6e50c2011-05-25 14:59:01 -07003363/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003364static void sanitize_string(uint8_t *s, size_t len)
3365{
3366 ssize_t i;
3367
Brian Norris8b6e50c2011-05-25 14:59:01 -07003368 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003369 s[len - 1] = 0;
3370
Brian Norris8b6e50c2011-05-25 14:59:01 -07003371 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003372 for (i = 0; i < len - 1; i++) {
3373 if (s[i] < ' ' || s[i] > 127)
3374 s[i] = '?';
3375 }
3376
Brian Norris8b6e50c2011-05-25 14:59:01 -07003377 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003378 strim(s);
3379}
3380
3381static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3382{
3383 int i;
3384 while (len--) {
3385 crc ^= *p++ << 8;
3386 for (i = 0; i < 8; i++)
3387 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3388 }
3389
3390 return crc;
3391}
3392
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003393/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003394static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3395 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003396{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003397 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003398 struct onfi_ext_param_page *ep;
3399 struct onfi_ext_section *s;
3400 struct onfi_ext_ecc_info *ecc;
3401 uint8_t *cursor;
3402 int ret = -EINVAL;
3403 int len;
3404 int i;
3405
3406 len = le16_to_cpu(p->ext_param_page_length) * 16;
3407 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003408 if (!ep)
3409 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003410
3411 /* Send our own NAND_CMD_PARAM. */
3412 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3413
3414 /* Use the Change Read Column command to skip the ONFI param pages. */
3415 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3416 sizeof(*p) * p->num_of_param_pages , -1);
3417
3418 /* Read out the Extended Parameter Page. */
3419 chip->read_buf(mtd, (uint8_t *)ep, len);
3420 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3421 != le16_to_cpu(ep->crc))) {
3422 pr_debug("fail in the CRC.\n");
3423 goto ext_out;
3424 }
3425
3426 /*
3427 * Check the signature.
3428 * Do not strictly follow the ONFI spec, maybe changed in future.
3429 */
3430 if (strncmp(ep->sig, "EPPS", 4)) {
3431 pr_debug("The signature is invalid.\n");
3432 goto ext_out;
3433 }
3434
3435 /* find the ECC section. */
3436 cursor = (uint8_t *)(ep + 1);
3437 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3438 s = ep->sections + i;
3439 if (s->type == ONFI_SECTION_TYPE_2)
3440 break;
3441 cursor += s->length * 16;
3442 }
3443 if (i == ONFI_EXT_SECTION_MAX) {
3444 pr_debug("We can not find the ECC section.\n");
3445 goto ext_out;
3446 }
3447
3448 /* get the info we want. */
3449 ecc = (struct onfi_ext_ecc_info *)cursor;
3450
Brian Norris4ae7d222013-09-16 18:20:21 -07003451 if (!ecc->codeword_size) {
3452 pr_debug("Invalid codeword size\n");
3453 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003454 }
3455
Brian Norris4ae7d222013-09-16 18:20:21 -07003456 chip->ecc_strength_ds = ecc->ecc_bits;
3457 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003458 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003459
3460ext_out:
3461 kfree(ep);
3462 return ret;
3463}
3464
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003465/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003466 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003467 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003468static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003469{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003470 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003471 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003472 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003473 int val;
3474
Brian Norris7854d3f2011-06-23 14:12:08 -07003475 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003476 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3477 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3478 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3479 return 0;
3480
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003481 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3482 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003483 for (j = 0; j < sizeof(*p); j++)
3484 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003485 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3486 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003487 break;
3488 }
3489 }
3490
Brian Norrisc7f23a72013-08-13 10:51:55 -07003491 if (i == 3) {
3492 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003493 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003494 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003495
Brian Norris8b6e50c2011-05-25 14:59:01 -07003496 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003497 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003498 if (val & (1 << 5))
3499 chip->onfi_version = 23;
3500 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003501 chip->onfi_version = 22;
3502 else if (val & (1 << 3))
3503 chip->onfi_version = 21;
3504 else if (val & (1 << 2))
3505 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003506 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003507 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003508
3509 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003510 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003511 return 0;
3512 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003513
3514 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3515 sanitize_string(p->model, sizeof(p->model));
3516 if (!mtd->name)
3517 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003518
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003519 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003520
3521 /*
3522 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3523 * (don't ask me who thought of this...). MTD assumes that these
3524 * dimensions will be power-of-2, so just truncate the remaining area.
3525 */
3526 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3527 mtd->erasesize *= mtd->writesize;
3528
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003529 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003530
3531 /* See erasesize comment */
3532 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003533 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003534 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003535
Zach Brown34da5f52017-01-10 13:30:21 -06003536 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3537 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3538
Huang Shijiee2985fc2013-05-17 11:17:30 +08003539 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003540 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003541
Huang Shijie10c86ba2013-05-17 11:17:26 +08003542 if (p->ecc_bits != 0xff) {
3543 chip->ecc_strength_ds = p->ecc_bits;
3544 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003545 } else if (chip->onfi_version >= 21 &&
3546 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3547
3548 /*
3549 * The nand_flash_detect_ext_param_page() uses the
3550 * Change Read Column command which maybe not supported
3551 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3552 * now. We do not replace user supplied command function.
3553 */
3554 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3555 chip->cmdfunc = nand_command_lp;
3556
3557 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003558 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003559 pr_warn("Failed to detect ONFI extended param page\n");
3560 } else {
3561 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003562 }
3563
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003564 return 1;
3565}
3566
3567/*
Huang Shijie91361812014-02-21 13:39:40 +08003568 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3569 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003570static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003571{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003572 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003573 struct nand_jedec_params *p = &chip->jedec_params;
3574 struct jedec_ecc_info *ecc;
3575 int val;
3576 int i, j;
3577
3578 /* Try JEDEC for unknown chip or LP */
3579 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3580 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3581 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3582 chip->read_byte(mtd) != 'C')
3583 return 0;
3584
3585 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3586 for (i = 0; i < 3; i++) {
3587 for (j = 0; j < sizeof(*p); j++)
3588 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3589
3590 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3591 le16_to_cpu(p->crc))
3592 break;
3593 }
3594
3595 if (i == 3) {
3596 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3597 return 0;
3598 }
3599
3600 /* Check version */
3601 val = le16_to_cpu(p->revision);
3602 if (val & (1 << 2))
3603 chip->jedec_version = 10;
3604 else if (val & (1 << 1))
3605 chip->jedec_version = 1; /* vendor specific version */
3606
3607 if (!chip->jedec_version) {
3608 pr_info("unsupported JEDEC version: %d\n", val);
3609 return 0;
3610 }
3611
3612 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3613 sanitize_string(p->model, sizeof(p->model));
3614 if (!mtd->name)
3615 mtd->name = p->model;
3616
3617 mtd->writesize = le32_to_cpu(p->byte_per_page);
3618
3619 /* Please reference to the comment for nand_flash_detect_onfi. */
3620 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3621 mtd->erasesize *= mtd->writesize;
3622
3623 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3624
3625 /* Please reference to the comment for nand_flash_detect_onfi. */
3626 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3627 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3628 chip->bits_per_cell = p->bits_per_cell;
3629
3630 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003631 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003632
3633 /* ECC info */
3634 ecc = &p->ecc_info[0];
3635
3636 if (ecc->codeword_size >= 9) {
3637 chip->ecc_strength_ds = ecc->ecc_bits;
3638 chip->ecc_step_ds = 1 << ecc->codeword_size;
3639 } else {
3640 pr_warn("Invalid codeword size\n");
3641 }
3642
3643 return 1;
3644}
3645
3646/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003647 * nand_id_has_period - Check if an ID string has a given wraparound period
3648 * @id_data: the ID string
3649 * @arrlen: the length of the @id_data array
3650 * @period: the period of repitition
3651 *
3652 * Check if an ID string is repeated within a given sequence of bytes at
3653 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003654 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003655 * if the repetition has a period of @period; otherwise, returns zero.
3656 */
3657static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3658{
3659 int i, j;
3660 for (i = 0; i < period; i++)
3661 for (j = i + period; j < arrlen; j += period)
3662 if (id_data[i] != id_data[j])
3663 return 0;
3664 return 1;
3665}
3666
3667/*
3668 * nand_id_len - Get the length of an ID string returned by CMD_READID
3669 * @id_data: the ID string
3670 * @arrlen: the length of the @id_data array
3671
3672 * Returns the length of the ID string, according to known wraparound/trailing
3673 * zero patterns. If no pattern exists, returns the length of the array.
3674 */
3675static int nand_id_len(u8 *id_data, int arrlen)
3676{
3677 int last_nonzero, period;
3678
3679 /* Find last non-zero byte */
3680 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3681 if (id_data[last_nonzero])
3682 break;
3683
3684 /* All zeros */
3685 if (last_nonzero < 0)
3686 return 0;
3687
3688 /* Calculate wraparound period */
3689 for (period = 1; period < arrlen; period++)
3690 if (nand_id_has_period(id_data, arrlen, period))
3691 break;
3692
3693 /* There's a repeated pattern */
3694 if (period < arrlen)
3695 return period;
3696
3697 /* There are trailing zeros */
3698 if (last_nonzero < arrlen - 1)
3699 return last_nonzero + 1;
3700
3701 /* No pattern detected */
3702 return arrlen;
3703}
3704
Huang Shijie7db906b2013-09-25 14:58:11 +08003705/* Extract the bits of per cell from the 3rd byte of the extended ID */
3706static int nand_get_bits_per_cell(u8 cellinfo)
3707{
3708 int bits;
3709
3710 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3711 bits >>= NAND_CI_CELLTYPE_SHIFT;
3712 return bits + 1;
3713}
3714
Brian Norrise3b88bd2012-09-24 20:40:52 -07003715/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003716 * Many new NAND share similar device ID codes, which represent the size of the
3717 * chip. The rest of the parameters must be decoded according to generic or
3718 * manufacturer-specific "extended ID" decoding patterns.
3719 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003720void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003721{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003722 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003723 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003724 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003725 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003726 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003727 /* The 4th id byte is the important one */
3728 extid = id_data[3];
3729
Boris Brezillon01389b62016-06-08 10:30:18 +02003730 /* Calc pagesize */
3731 mtd->writesize = 1024 << (extid & 0x03);
3732 extid >>= 2;
3733 /* Calc oobsize */
3734 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3735 extid >>= 2;
3736 /* Calc blocksize. Blocksize is multiples of 64KiB */
3737 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3738 extid >>= 2;
3739 /* Get buswidth information */
3740 if (extid & 0x1)
3741 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003742}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003743EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003744
3745/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003746 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3747 * decodes a matching ID table entry and assigns the MTD size parameters for
3748 * the chip.
3749 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003750static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003751{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003752 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003753
3754 mtd->erasesize = type->erasesize;
3755 mtd->writesize = type->pagesize;
3756 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003757
Huang Shijie1c195e92013-09-25 14:58:12 +08003758 /* All legacy ID NAND are small-page, SLC */
3759 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003760}
3761
3762/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003763 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3764 * heuristic patterns using various detected parameters (e.g., manufacturer,
3765 * page size, cell-type information).
3766 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003767static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003768{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003769 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003770
3771 /* Set the bad block position */
3772 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3773 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3774 else
3775 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003776}
3777
Huang Shijieec6e87e2013-03-15 11:01:00 +08003778static inline bool is_full_id_nand(struct nand_flash_dev *type)
3779{
3780 return type->id_len;
3781}
3782
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003783static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003784 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003785{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003786 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003787 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003788
Huang Shijieec6e87e2013-03-15 11:01:00 +08003789 if (!strncmp(type->id, id_data, type->id_len)) {
3790 mtd->writesize = type->pagesize;
3791 mtd->erasesize = type->erasesize;
3792 mtd->oobsize = type->oobsize;
3793
Huang Shijie7db906b2013-09-25 14:58:11 +08003794 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003795 chip->chipsize = (uint64_t)type->chipsize << 20;
3796 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003797 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3798 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003799 chip->onfi_timing_mode_default =
3800 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003801
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003802 if (!mtd->name)
3803 mtd->name = type->name;
3804
Huang Shijieec6e87e2013-03-15 11:01:00 +08003805 return true;
3806 }
3807 return false;
3808}
3809
Brian Norris7e74c2d2012-09-24 20:40:49 -07003810/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003811 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3812 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3813 * table.
3814 */
3815static void nand_manufacturer_detect(struct nand_chip *chip)
3816{
3817 /*
3818 * Try manufacturer detection if available and use
3819 * nand_decode_ext_id() otherwise.
3820 */
3821 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003822 chip->manufacturer.desc->ops->detect) {
3823 /* The 3rd id byte holds MLC / multichip data */
3824 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003825 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003826 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003827 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003828 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003829}
3830
3831/*
3832 * Manufacturer initialization. This function is called for all NANDs including
3833 * ONFI and JEDEC compliant ones.
3834 * Manufacturer drivers should put all their specific initialization code in
3835 * their ->init() hook.
3836 */
3837static int nand_manufacturer_init(struct nand_chip *chip)
3838{
3839 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
3840 !chip->manufacturer.desc->ops->init)
3841 return 0;
3842
3843 return chip->manufacturer.desc->ops->init(chip);
3844}
3845
3846/*
3847 * Manufacturer cleanup. This function is called for all NANDs including
3848 * ONFI and JEDEC compliant ones.
3849 * Manufacturer drivers should put all their specific cleanup code in their
3850 * ->cleanup() hook.
3851 */
3852static void nand_manufacturer_cleanup(struct nand_chip *chip)
3853{
3854 /* Release manufacturer private data */
3855 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3856 chip->manufacturer.desc->ops->cleanup)
3857 chip->manufacturer.desc->ops->cleanup(chip);
3858}
3859
3860/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003861 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003862 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02003863static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003864{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003865 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003866 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08003867 int busw;
Boris Brezillonf84674b2017-06-02 12:18:24 +02003868 int i;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003869 u8 *id_data = chip->id.data;
3870 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
Karl Beldanef89a882008-09-15 14:37:29 +02003872 /*
3873 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003874 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003875 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003876 nand_reset(chip, 0);
3877
3878 /* Select the device */
3879 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02003880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003882 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
3884 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003885 maf_id = chip->read_byte(mtd);
3886 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Brian Norris8b6e50c2011-05-25 14:59:01 -07003888 /*
3889 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003890 * interface concerns can cause random data which looks like a
3891 * possibly credible NAND flash to appear. If the two results do
3892 * not match, ignore the device completely.
3893 */
3894
3895 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3896
Brian Norris4aef9b72012-09-24 20:40:48 -07003897 /* Read entire ID string */
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003898 for (i = 0; i < ARRAY_SIZE(chip->id.data); i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003899 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003900
Boris Brezillon7f501f02016-05-24 19:20:05 +02003901 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003902 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003903 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003904 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01003905 }
3906
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003907 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02003908
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003909 /* Try to identify manufacturer */
3910 manufacturer = nand_get_manufacturer(maf_id);
3911 chip->manufacturer.desc = manufacturer;
3912
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003913 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003914 type = nand_flash_ids;
3915
Boris Brezillon29a198a2016-05-24 20:17:48 +02003916 /*
3917 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
3918 * override it.
3919 * This is required to make sure initial NAND bus width set by the
3920 * NAND controller driver is coherent with the real NAND bus width
3921 * (extracted by auto-detection code).
3922 */
3923 busw = chip->options & NAND_BUSWIDTH_16;
3924
3925 /*
3926 * The flag is only set (never cleared), reset it to its default value
3927 * before starting auto-detection.
3928 */
3929 chip->options &= ~NAND_BUSWIDTH_16;
3930
Huang Shijieec6e87e2013-03-15 11:01:00 +08003931 for (; type->name != NULL; type++) {
3932 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003933 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08003934 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003935 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003936 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003937 }
3938 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003939
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003940 chip->onfi_version = 0;
3941 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003942 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003943 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003944 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003945
3946 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003947 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08003948 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003949 }
3950
David Woodhouse5e81e882010-02-26 18:32:56 +00003951 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003952 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003953
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003954 if (!mtd->name)
3955 mtd->name = type->name;
3956
Adrian Hunter69423d92008-12-10 13:37:21 +00003957 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003958
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003959 if (!type->pagesize)
3960 nand_manufacturer_detect(chip);
3961 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02003962 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003963
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003964 /* Get chip options */
3965 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003966
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003967ident_done:
3968
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003969 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003970 WARN_ON(busw & NAND_BUSWIDTH_16);
3971 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003972 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3973 /*
3974 * Check, if buswidth is correct. Hardware drivers should set
3975 * chip correct!
3976 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003977 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003978 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003979 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
3980 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02003981 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
3982 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003983 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003984 }
3985
Boris Brezillon7f501f02016-05-24 19:20:05 +02003986 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003987
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003988 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003989 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003990 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003991 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003992
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003993 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003994 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003995 if (chip->chipsize & 0xffffffff)
3996 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003997 else {
3998 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3999 chip->chip_shift += 32 - 1;
4000 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004001
Masahiro Yamada14157f82017-09-13 11:05:50 +09004002 if (chip->chip_shift - chip->page_shift > 16)
4003 chip->options |= NAND_ROW_ADDR_3;
4004
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004005 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004006 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004007
Brian Norris8b6e50c2011-05-25 14:59:01 -07004008 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004009 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4010 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004011
Ezequiel Garcia20171642013-11-25 08:30:31 -03004012 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004013 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004014
4015 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004016 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4017 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004018 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004019 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4020 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004021 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004022 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4023 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004024
Rafał Miłecki3755a992014-10-21 00:01:04 +02004025 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004026 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004027 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004028 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004029}
4030
Boris Brezillond48f62b2016-04-01 14:54:32 +02004031static const char * const nand_ecc_modes[] = {
4032 [NAND_ECC_NONE] = "none",
4033 [NAND_ECC_SOFT] = "soft",
4034 [NAND_ECC_HW] = "hw",
4035 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4036 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004037 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004038};
4039
4040static int of_get_nand_ecc_mode(struct device_node *np)
4041{
4042 const char *pm;
4043 int err, i;
4044
4045 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4046 if (err < 0)
4047 return err;
4048
4049 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4050 if (!strcasecmp(pm, nand_ecc_modes[i]))
4051 return i;
4052
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004053 /*
4054 * For backward compatibility we support few obsoleted values that don't
4055 * have their mappings into nand_ecc_modes_t anymore (they were merged
4056 * with other enums).
4057 */
4058 if (!strcasecmp(pm, "soft_bch"))
4059 return NAND_ECC_SOFT;
4060
Boris Brezillond48f62b2016-04-01 14:54:32 +02004061 return -ENODEV;
4062}
4063
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004064static const char * const nand_ecc_algos[] = {
4065 [NAND_ECC_HAMMING] = "hamming",
4066 [NAND_ECC_BCH] = "bch",
4067};
4068
Boris Brezillond48f62b2016-04-01 14:54:32 +02004069static int of_get_nand_ecc_algo(struct device_node *np)
4070{
4071 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004072 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004073
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004074 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4075 if (!err) {
4076 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4077 if (!strcasecmp(pm, nand_ecc_algos[i]))
4078 return i;
4079 return -ENODEV;
4080 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004081
4082 /*
4083 * For backward compatibility we also read "nand-ecc-mode" checking
4084 * for some obsoleted values that were specifying ECC algorithm.
4085 */
4086 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4087 if (err < 0)
4088 return err;
4089
4090 if (!strcasecmp(pm, "soft"))
4091 return NAND_ECC_HAMMING;
4092 else if (!strcasecmp(pm, "soft_bch"))
4093 return NAND_ECC_BCH;
4094
4095 return -ENODEV;
4096}
4097
4098static int of_get_nand_ecc_step_size(struct device_node *np)
4099{
4100 int ret;
4101 u32 val;
4102
4103 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4104 return ret ? ret : val;
4105}
4106
4107static int of_get_nand_ecc_strength(struct device_node *np)
4108{
4109 int ret;
4110 u32 val;
4111
4112 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4113 return ret ? ret : val;
4114}
4115
4116static int of_get_nand_bus_width(struct device_node *np)
4117{
4118 u32 val;
4119
4120 if (of_property_read_u32(np, "nand-bus-width", &val))
4121 return 8;
4122
4123 switch (val) {
4124 case 8:
4125 case 16:
4126 return val;
4127 default:
4128 return -EIO;
4129 }
4130}
4131
4132static bool of_get_nand_on_flash_bbt(struct device_node *np)
4133{
4134 return of_property_read_bool(np, "nand-on-flash-bbt");
4135}
4136
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004137static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004138{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004139 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004140 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004141
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004142 if (!dn)
4143 return 0;
4144
Brian Norris5844fee2015-01-23 00:22:27 -08004145 if (of_get_nand_bus_width(dn) == 16)
4146 chip->options |= NAND_BUSWIDTH_16;
4147
4148 if (of_get_nand_on_flash_bbt(dn))
4149 chip->bbt_options |= NAND_BBT_USE_FLASH;
4150
4151 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004152 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004153 ecc_strength = of_get_nand_ecc_strength(dn);
4154 ecc_step = of_get_nand_ecc_step_size(dn);
4155
Brian Norris5844fee2015-01-23 00:22:27 -08004156 if (ecc_mode >= 0)
4157 chip->ecc.mode = ecc_mode;
4158
Rafał Miłecki79082452016-03-23 11:19:02 +01004159 if (ecc_algo >= 0)
4160 chip->ecc.algo = ecc_algo;
4161
Brian Norris5844fee2015-01-23 00:22:27 -08004162 if (ecc_strength >= 0)
4163 chip->ecc.strength = ecc_strength;
4164
4165 if (ecc_step > 0)
4166 chip->ecc.size = ecc_step;
4167
Boris Brezillonba78ee02016-06-08 17:04:22 +02004168 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4169 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4170
Brian Norris5844fee2015-01-23 00:22:27 -08004171 return 0;
4172}
4173
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004174/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004175 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004176 * @mtd: MTD device structure
4177 * @maxchips: number of chips to scan for
4178 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004179 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004180 * This is the first phase of the normal nand_scan() function. It reads the
4181 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004182 *
4183 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004184int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4185 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004186{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004187 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004188 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004189 int ret;
4190
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004191 ret = nand_dt_init(chip);
4192 if (ret)
4193 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004194
Brian Norrisf7a8e382016-01-05 10:39:45 -08004195 if (!mtd->name && mtd->dev.parent)
4196 mtd->name = dev_name(mtd->dev.parent);
4197
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004198 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4199 /*
4200 * Default functions assigned for chip_select() and
4201 * cmdfunc() both expect cmd_ctrl() to be populated,
4202 * so we need to check that that's the case
4203 */
4204 pr_err("chip.cmd_ctrl() callback is not provided");
4205 return -EINVAL;
4206 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004207 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004208 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004209
4210 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004211 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004212 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004213 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004214 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004215 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004216 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 }
4218
Boris Brezillon7f501f02016-05-24 19:20:05 +02004219 nand_maf_id = chip->id.data[0];
4220 nand_dev_id = chip->id.data[1];
4221
Huang Shijie07300162012-11-09 16:23:45 +08004222 chip->select_chip(mtd, -1);
4223
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004224 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004225 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004226 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004227 nand_reset(chip, i);
4228
4229 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004231 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004233 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004234 nand_dev_id != chip->read_byte(mtd)) {
4235 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 break;
Huang Shijie07300162012-11-09 16:23:45 +08004237 }
4238 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 }
4240 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004241 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004244 chip->numchips = i;
4245 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
David Woodhouse3b85c322006-09-25 17:06:53 +01004247 return 0;
4248}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004249EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004250
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004251static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4252{
4253 struct nand_chip *chip = mtd_to_nand(mtd);
4254 struct nand_ecc_ctrl *ecc = &chip->ecc;
4255
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004256 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004257 return -EINVAL;
4258
4259 switch (ecc->algo) {
4260 case NAND_ECC_HAMMING:
4261 ecc->calculate = nand_calculate_ecc;
4262 ecc->correct = nand_correct_data;
4263 ecc->read_page = nand_read_page_swecc;
4264 ecc->read_subpage = nand_read_subpage;
4265 ecc->write_page = nand_write_page_swecc;
4266 ecc->read_page_raw = nand_read_page_raw;
4267 ecc->write_page_raw = nand_write_page_raw;
4268 ecc->read_oob = nand_read_oob_std;
4269 ecc->write_oob = nand_write_oob_std;
4270 if (!ecc->size)
4271 ecc->size = 256;
4272 ecc->bytes = 3;
4273 ecc->strength = 1;
4274 return 0;
4275 case NAND_ECC_BCH:
4276 if (!mtd_nand_has_bch()) {
4277 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4278 return -EINVAL;
4279 }
4280 ecc->calculate = nand_bch_calculate_ecc;
4281 ecc->correct = nand_bch_correct_data;
4282 ecc->read_page = nand_read_page_swecc;
4283 ecc->read_subpage = nand_read_subpage;
4284 ecc->write_page = nand_write_page_swecc;
4285 ecc->read_page_raw = nand_read_page_raw;
4286 ecc->write_page_raw = nand_write_page_raw;
4287 ecc->read_oob = nand_read_oob_std;
4288 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004289
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004290 /*
4291 * Board driver should supply ecc.size and ecc.strength
4292 * values to select how many bits are correctable.
4293 * Otherwise, default to 4 bits for large page devices.
4294 */
4295 if (!ecc->size && (mtd->oobsize >= 64)) {
4296 ecc->size = 512;
4297 ecc->strength = 4;
4298 }
4299
4300 /*
4301 * if no ecc placement scheme was provided pickup the default
4302 * large page one.
4303 */
4304 if (!mtd->ooblayout) {
4305 /* handle large page devices only */
4306 if (mtd->oobsize < 64) {
4307 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4308 return -EINVAL;
4309 }
4310
4311 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004312
4313 }
4314
4315 /*
4316 * We can only maximize ECC config when the default layout is
4317 * used, otherwise we don't know how many bytes can really be
4318 * used.
4319 */
4320 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4321 ecc->options & NAND_ECC_MAXIMIZE) {
4322 int steps, bytes;
4323
4324 /* Always prefer 1k blocks over 512bytes ones */
4325 ecc->size = 1024;
4326 steps = mtd->writesize / ecc->size;
4327
4328 /* Reserve 2 bytes for the BBM */
4329 bytes = (mtd->oobsize - 2) / steps;
4330 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004331 }
4332
4333 /* See nand_bch_init() for details. */
4334 ecc->bytes = 0;
4335 ecc->priv = nand_bch_init(mtd);
4336 if (!ecc->priv) {
4337 WARN(1, "BCH ECC initialization failed!\n");
4338 return -EINVAL;
4339 }
4340 return 0;
4341 default:
4342 WARN(1, "Unsupported ECC algorithm!\n");
4343 return -EINVAL;
4344 }
4345}
4346
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09004347/**
4348 * nand_check_ecc_caps - check the sanity of preset ECC settings
4349 * @chip: nand chip info structure
4350 * @caps: ECC caps info structure
4351 * @oobavail: OOB size that the ECC engine can use
4352 *
4353 * When ECC step size and strength are already set, check if they are supported
4354 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4355 * On success, the calculated ECC bytes is set.
4356 */
4357int nand_check_ecc_caps(struct nand_chip *chip,
4358 const struct nand_ecc_caps *caps, int oobavail)
4359{
4360 struct mtd_info *mtd = nand_to_mtd(chip);
4361 const struct nand_ecc_step_info *stepinfo;
4362 int preset_step = chip->ecc.size;
4363 int preset_strength = chip->ecc.strength;
4364 int nsteps, ecc_bytes;
4365 int i, j;
4366
4367 if (WARN_ON(oobavail < 0))
4368 return -EINVAL;
4369
4370 if (!preset_step || !preset_strength)
4371 return -ENODATA;
4372
4373 nsteps = mtd->writesize / preset_step;
4374
4375 for (i = 0; i < caps->nstepinfos; i++) {
4376 stepinfo = &caps->stepinfos[i];
4377
4378 if (stepinfo->stepsize != preset_step)
4379 continue;
4380
4381 for (j = 0; j < stepinfo->nstrengths; j++) {
4382 if (stepinfo->strengths[j] != preset_strength)
4383 continue;
4384
4385 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4386 preset_strength);
4387 if (WARN_ON_ONCE(ecc_bytes < 0))
4388 return ecc_bytes;
4389
4390 if (ecc_bytes * nsteps > oobavail) {
4391 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4392 preset_step, preset_strength);
4393 return -ENOSPC;
4394 }
4395
4396 chip->ecc.bytes = ecc_bytes;
4397
4398 return 0;
4399 }
4400 }
4401
4402 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4403 preset_step, preset_strength);
4404
4405 return -ENOTSUPP;
4406}
4407EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4408
4409/**
4410 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4411 * @chip: nand chip info structure
4412 * @caps: ECC engine caps info structure
4413 * @oobavail: OOB size that the ECC engine can use
4414 *
4415 * If a chip's ECC requirement is provided, try to meet it with the least
4416 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4417 * On success, the chosen ECC settings are set.
4418 */
4419int nand_match_ecc_req(struct nand_chip *chip,
4420 const struct nand_ecc_caps *caps, int oobavail)
4421{
4422 struct mtd_info *mtd = nand_to_mtd(chip);
4423 const struct nand_ecc_step_info *stepinfo;
4424 int req_step = chip->ecc_step_ds;
4425 int req_strength = chip->ecc_strength_ds;
4426 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4427 int best_step, best_strength, best_ecc_bytes;
4428 int best_ecc_bytes_total = INT_MAX;
4429 int i, j;
4430
4431 if (WARN_ON(oobavail < 0))
4432 return -EINVAL;
4433
4434 /* No information provided by the NAND chip */
4435 if (!req_step || !req_strength)
4436 return -ENOTSUPP;
4437
4438 /* number of correctable bits the chip requires in a page */
4439 req_corr = mtd->writesize / req_step * req_strength;
4440
4441 for (i = 0; i < caps->nstepinfos; i++) {
4442 stepinfo = &caps->stepinfos[i];
4443 step_size = stepinfo->stepsize;
4444
4445 for (j = 0; j < stepinfo->nstrengths; j++) {
4446 strength = stepinfo->strengths[j];
4447
4448 /*
4449 * If both step size and strength are smaller than the
4450 * chip's requirement, it is not easy to compare the
4451 * resulted reliability.
4452 */
4453 if (step_size < req_step && strength < req_strength)
4454 continue;
4455
4456 if (mtd->writesize % step_size)
4457 continue;
4458
4459 nsteps = mtd->writesize / step_size;
4460
4461 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4462 if (WARN_ON_ONCE(ecc_bytes < 0))
4463 continue;
4464 ecc_bytes_total = ecc_bytes * nsteps;
4465
4466 if (ecc_bytes_total > oobavail ||
4467 strength * nsteps < req_corr)
4468 continue;
4469
4470 /*
4471 * We assume the best is to meet the chip's requrement
4472 * with the least number of ECC bytes.
4473 */
4474 if (ecc_bytes_total < best_ecc_bytes_total) {
4475 best_ecc_bytes_total = ecc_bytes_total;
4476 best_step = step_size;
4477 best_strength = strength;
4478 best_ecc_bytes = ecc_bytes;
4479 }
4480 }
4481 }
4482
4483 if (best_ecc_bytes_total == INT_MAX)
4484 return -ENOTSUPP;
4485
4486 chip->ecc.size = best_step;
4487 chip->ecc.strength = best_strength;
4488 chip->ecc.bytes = best_ecc_bytes;
4489
4490 return 0;
4491}
4492EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4493
4494/**
4495 * nand_maximize_ecc - choose the max ECC strength available
4496 * @chip: nand chip info structure
4497 * @caps: ECC engine caps info structure
4498 * @oobavail: OOB size that the ECC engine can use
4499 *
4500 * Choose the max ECC strength that is supported on the controller, and can fit
4501 * within the chip's OOB. On success, the chosen ECC settings are set.
4502 */
4503int nand_maximize_ecc(struct nand_chip *chip,
4504 const struct nand_ecc_caps *caps, int oobavail)
4505{
4506 struct mtd_info *mtd = nand_to_mtd(chip);
4507 const struct nand_ecc_step_info *stepinfo;
4508 int step_size, strength, nsteps, ecc_bytes, corr;
4509 int best_corr = 0;
4510 int best_step = 0;
4511 int best_strength, best_ecc_bytes;
4512 int i, j;
4513
4514 if (WARN_ON(oobavail < 0))
4515 return -EINVAL;
4516
4517 for (i = 0; i < caps->nstepinfos; i++) {
4518 stepinfo = &caps->stepinfos[i];
4519 step_size = stepinfo->stepsize;
4520
4521 /* If chip->ecc.size is already set, respect it */
4522 if (chip->ecc.size && step_size != chip->ecc.size)
4523 continue;
4524
4525 for (j = 0; j < stepinfo->nstrengths; j++) {
4526 strength = stepinfo->strengths[j];
4527
4528 if (mtd->writesize % step_size)
4529 continue;
4530
4531 nsteps = mtd->writesize / step_size;
4532
4533 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4534 if (WARN_ON_ONCE(ecc_bytes < 0))
4535 continue;
4536
4537 if (ecc_bytes * nsteps > oobavail)
4538 continue;
4539
4540 corr = strength * nsteps;
4541
4542 /*
4543 * If the number of correctable bits is the same,
4544 * bigger step_size has more reliability.
4545 */
4546 if (corr > best_corr ||
4547 (corr == best_corr && step_size > best_step)) {
4548 best_corr = corr;
4549 best_step = step_size;
4550 best_strength = strength;
4551 best_ecc_bytes = ecc_bytes;
4552 }
4553 }
4554 }
4555
4556 if (!best_corr)
4557 return -ENOTSUPP;
4558
4559 chip->ecc.size = best_step;
4560 chip->ecc.strength = best_strength;
4561 chip->ecc.bytes = best_ecc_bytes;
4562
4563 return 0;
4564}
4565EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4566
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004567/*
4568 * Check if the chip configuration meet the datasheet requirements.
4569
4570 * If our configuration corrects A bits per B bytes and the minimum
4571 * required correction level is X bits per Y bytes, then we must ensure
4572 * both of the following are true:
4573 *
4574 * (1) A / B >= X / Y
4575 * (2) A >= X
4576 *
4577 * Requirement (1) ensures we can correct for the required bitflip density.
4578 * Requirement (2) ensures we can correct even when all bitflips are clumped
4579 * in the same sector.
4580 */
4581static bool nand_ecc_strength_good(struct mtd_info *mtd)
4582{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004583 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004584 struct nand_ecc_ctrl *ecc = &chip->ecc;
4585 int corr, ds_corr;
4586
4587 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4588 /* Not enough information */
4589 return true;
4590
4591 /*
4592 * We get the number of corrected bits per page to compare
4593 * the correction density.
4594 */
4595 corr = (mtd->writesize * ecc->strength) / ecc->size;
4596 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4597
4598 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4599}
David Woodhouse3b85c322006-09-25 17:06:53 +01004600
Marc Gonzalez3371d662016-11-15 10:56:20 +01004601static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4602{
4603 struct nand_ecc_ctrl *ecc = &chip->ecc;
4604
4605 if (nand_standard_page_accessors(ecc))
4606 return false;
4607
4608 /*
4609 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4610 * controller driver implements all the page accessors because
4611 * default helpers are not suitable when the core does not
4612 * send the READ0/PAGEPROG commands.
4613 */
4614 return (!ecc->read_page || !ecc->write_page ||
4615 !ecc->read_page_raw || !ecc->write_page_raw ||
4616 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4617 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4618 ecc->hwctl && ecc->calculate));
4619}
4620
David Woodhouse3b85c322006-09-25 17:06:53 +01004621/**
4622 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004623 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004624 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004625 * This is the second phase of the normal nand_scan() function. It fills out
4626 * all the uninitialized function pointers with the defaults and scans for a
4627 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004628 */
4629int nand_scan_tail(struct mtd_info *mtd)
4630{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004631 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004632 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004633 struct nand_buffers *nbuf = NULL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004634 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01004635
Brian Norrise2414f42012-02-06 13:44:00 -08004636 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004637 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07004638 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02004639 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07004640 }
Brian Norrise2414f42012-02-06 13:44:00 -08004641
Marc Gonzalez3371d662016-11-15 10:56:20 +01004642 if (invalid_ecc_page_accessors(chip)) {
4643 pr_err("Invalid ECC page accessors setup\n");
Boris Brezillonf84674b2017-06-02 12:18:24 +02004644 return -EINVAL;
Marc Gonzalez3371d662016-11-15 10:56:20 +01004645 }
4646
Huang Shijief02ea4e2014-01-13 14:27:12 +08004647 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004648 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Boris Brezillonf84674b2017-06-02 12:18:24 +02004649 if (!nbuf)
4650 return -ENOMEM;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004651
4652 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4653 if (!nbuf->ecccalc) {
4654 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004655 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004656 }
4657
4658 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4659 if (!nbuf->ecccode) {
4660 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004661 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004662 }
4663
4664 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4665 GFP_KERNEL);
4666 if (!nbuf->databuf) {
4667 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004668 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004669 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004670
4671 chip->buffers = nbuf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004672 } else if (!chip->buffers) {
4673 return -ENOMEM;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004674 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004675
Boris Brezillonf84674b2017-06-02 12:18:24 +02004676 /*
4677 * FIXME: some NAND manufacturer drivers expect the first die to be
4678 * selected when manufacturer->init() is called. They should be fixed
4679 * to explictly select the relevant die when interacting with the NAND
4680 * chip.
4681 */
4682 chip->select_chip(mtd, 0);
4683 ret = nand_manufacturer_init(chip);
4684 chip->select_chip(mtd, -1);
4685 if (ret)
4686 goto err_free_nbuf;
4687
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004688 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004689 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004690
4691 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004692 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004693 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004694 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004695 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004696 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004699 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 break;
4701 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004702 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02004703 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004704 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02004706 /*
4707 * Expose the whole OOB area to users if ECC_NONE
4708 * is passed. We could do that for all kind of
4709 * ->oobsize, but we must keep the old large/small
4710 * page with ECC layout when ->oobsize <= 128 for
4711 * compatibility reasons.
4712 */
4713 if (ecc->mode == NAND_ECC_NONE) {
4714 mtd_set_ooblayout(mtd,
4715 &nand_ooblayout_lp_ops);
4716 break;
4717 }
4718
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004719 WARN(1, "No oob scheme defined for oobsize %d\n",
4720 mtd->oobsize);
4721 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004722 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 }
4724 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004725
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004726 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004727 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004728 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004729 */
David Woodhouse956e9442006-09-25 17:12:39 +01004730
Huang Shijie97de79e02013-10-18 14:20:53 +08004731 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004732 case NAND_ECC_HW_OOB_FIRST:
4733 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004734 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004735 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4736 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004737 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004738 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004739 if (!ecc->read_page)
4740 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004741
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004742 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004743 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004744 if (!ecc->read_page)
4745 ecc->read_page = nand_read_page_hwecc;
4746 if (!ecc->write_page)
4747 ecc->write_page = nand_write_page_hwecc;
4748 if (!ecc->read_page_raw)
4749 ecc->read_page_raw = nand_read_page_raw;
4750 if (!ecc->write_page_raw)
4751 ecc->write_page_raw = nand_write_page_raw;
4752 if (!ecc->read_oob)
4753 ecc->read_oob = nand_read_oob_std;
4754 if (!ecc->write_oob)
4755 ecc->write_oob = nand_write_oob_std;
4756 if (!ecc->read_subpage)
4757 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004758 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004759 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004760
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004761 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004762 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4763 (!ecc->read_page ||
4764 ecc->read_page == nand_read_page_hwecc ||
4765 !ecc->write_page ||
4766 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004767 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4768 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004769 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004770 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004771 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004772 if (!ecc->read_page)
4773 ecc->read_page = nand_read_page_syndrome;
4774 if (!ecc->write_page)
4775 ecc->write_page = nand_write_page_syndrome;
4776 if (!ecc->read_page_raw)
4777 ecc->read_page_raw = nand_read_page_raw_syndrome;
4778 if (!ecc->write_page_raw)
4779 ecc->write_page_raw = nand_write_page_raw_syndrome;
4780 if (!ecc->read_oob)
4781 ecc->read_oob = nand_read_oob_syndrome;
4782 if (!ecc->write_oob)
4783 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004784
Huang Shijie97de79e02013-10-18 14:20:53 +08004785 if (mtd->writesize >= ecc->size) {
4786 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004787 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4788 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004789 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07004790 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004791 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004792 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004793 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4794 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004795 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004796 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004798 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004799 ret = nand_set_ecc_soft_ops(mtd);
4800 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004801 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004802 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01004803 }
4804 break;
4805
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004806 case NAND_ECC_ON_DIE:
4807 if (!ecc->read_page || !ecc->write_page) {
4808 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4809 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004810 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004811 }
4812 if (!ecc->read_oob)
4813 ecc->read_oob = nand_read_oob_std;
4814 if (!ecc->write_oob)
4815 ecc->write_oob = nand_write_oob_std;
4816 break;
4817
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004818 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004819 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004820 ecc->read_page = nand_read_page_raw;
4821 ecc->write_page = nand_write_page_raw;
4822 ecc->read_oob = nand_read_oob_std;
4823 ecc->read_page_raw = nand_read_page_raw;
4824 ecc->write_page_raw = nand_write_page_raw;
4825 ecc->write_oob = nand_write_oob_std;
4826 ecc->size = mtd->writesize;
4827 ecc->bytes = 0;
4828 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004832 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4833 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004834 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Brian Norris9ce244b2011-08-30 18:45:37 -07004837 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004838 if (!ecc->read_oob_raw)
4839 ecc->read_oob_raw = ecc->read_oob;
4840 if (!ecc->write_oob_raw)
4841 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004842
Boris Brezillon846031d2016-02-03 20:11:00 +01004843 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004844 mtd->ecc_strength = ecc->strength;
4845 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004846
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004847 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004848 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004849 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004850 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004851 ecc->steps = mtd->writesize / ecc->size;
4852 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004853 WARN(1, "Invalid ECC parameters\n");
4854 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004855 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004857 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004858 if (ecc->total > mtd->oobsize) {
4859 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
4860 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004861 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004862 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004863
Boris Brezillon846031d2016-02-03 20:11:00 +01004864 /*
4865 * The number of bytes available for a client to place data into
4866 * the out of band area.
4867 */
4868 ret = mtd_ooblayout_count_freebytes(mtd);
4869 if (ret < 0)
4870 ret = 0;
4871
4872 mtd->oobavail = ret;
4873
4874 /* ECC sanity check: warn if it's too weak */
4875 if (!nand_ecc_strength_good(mtd))
4876 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4877 mtd->name);
4878
Brian Norris8b6e50c2011-05-25 14:59:01 -07004879 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004880 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004881 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004882 case 2:
4883 mtd->subpage_sft = 1;
4884 break;
4885 case 4:
4886 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004887 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004888 mtd->subpage_sft = 2;
4889 break;
4890 }
4891 }
4892 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4893
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004894 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004895 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004898 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004900 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304901 switch (ecc->mode) {
4902 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304903 if (chip->page_shift > 9)
4904 chip->options |= NAND_SUBPAGE_READ;
4905 break;
4906
4907 default:
4908 break;
4909 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004910
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004912 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004913 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4914 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004915 mtd->_erase = nand_erase;
4916 mtd->_point = NULL;
4917 mtd->_unpoint = NULL;
4918 mtd->_read = nand_read;
4919 mtd->_write = nand_write;
4920 mtd->_panic_write = panic_nand_write;
4921 mtd->_read_oob = nand_read_oob;
4922 mtd->_write_oob = nand_write_oob;
4923 mtd->_sync = nand_sync;
4924 mtd->_lock = NULL;
4925 mtd->_unlock = NULL;
4926 mtd->_suspend = nand_suspend;
4927 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004928 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004929 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004930 mtd->_block_isbad = nand_block_isbad;
4931 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004932 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004933 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004935 /*
4936 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4937 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4938 * properly set.
4939 */
4940 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004941 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
Boris Brezillonf84674b2017-06-02 12:18:24 +02004943 /* Initialize the ->data_interface field. */
4944 ret = nand_init_data_interface(chip);
4945 if (ret)
4946 goto err_nand_manuf_cleanup;
4947
4948 /* Enter fastest possible mode on all dies. */
4949 for (i = 0; i < chip->numchips; i++) {
4950 chip->select_chip(mtd, i);
4951 ret = nand_setup_data_interface(chip, i);
4952 chip->select_chip(mtd, -1);
4953
4954 if (ret)
4955 goto err_nand_data_iface_cleanup;
4956 }
4957
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004958 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004959 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004960 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
4962 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07004963 ret = chip->scan_bbt(mtd);
4964 if (ret)
Boris Brezillonf84674b2017-06-02 12:18:24 +02004965 goto err_nand_data_iface_cleanup;
4966
Brian Norris44d41822017-05-01 17:04:50 -07004967 return 0;
4968
Boris Brezillonf84674b2017-06-02 12:18:24 +02004969err_nand_data_iface_cleanup:
4970 nand_release_data_interface(chip);
4971
4972err_nand_manuf_cleanup:
4973 nand_manufacturer_cleanup(chip);
4974
4975err_free_nbuf:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004976 if (nbuf) {
4977 kfree(nbuf->databuf);
4978 kfree(nbuf->ecccode);
4979 kfree(nbuf->ecccalc);
4980 kfree(nbuf);
4981 }
Brian Norris78771042017-05-01 17:04:53 -07004982
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004985EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
Brian Norris8b6e50c2011-05-25 14:59:01 -07004987/*
4988 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004989 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004990 * to call us from in-kernel code if the core NAND support is modular.
4991 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004992#ifdef MODULE
4993#define caller_is_module() (1)
4994#else
4995#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004996 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004997#endif
4998
4999/**
5000 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005001 * @mtd: MTD device structure
5002 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01005003 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005004 * This fills out all the uninitialized function pointers with the defaults.
5005 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005006 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005007 */
5008int nand_scan(struct mtd_info *mtd, int maxchips)
5009{
5010 int ret;
5011
David Woodhouse5e81e882010-02-26 18:32:56 +00005012 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01005013 if (!ret)
5014 ret = nand_scan_tail(mtd);
5015 return ret;
5016}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005017EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01005018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005020 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5021 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005022 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005023void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005025 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005026 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005027 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5028
Boris Brezillond8e725d2016-09-15 10:32:50 +02005029 nand_release_data_interface(chip);
5030
Jesper Juhlfa671642005-11-07 01:01:27 -08005031 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005032 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005033 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
5034 kfree(chip->buffers->databuf);
5035 kfree(chip->buffers->ecccode);
5036 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005037 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005038 }
Brian Norris58373ff2010-07-15 12:15:44 -07005039
5040 /* Free bad block descriptor memory */
5041 if (chip->badblock_pattern && chip->badblock_pattern->options
5042 & NAND_BBT_DYNAMICSTRUCT)
5043 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005044
5045 /* Free manufacturer priv data. */
5046 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005048EXPORT_SYMBOL_GPL(nand_cleanup);
5049
5050/**
5051 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5052 * held by the NAND device
5053 * @mtd: MTD device structure
5054 */
5055void nand_release(struct mtd_info *mtd)
5056{
5057 mtd_device_unregister(mtd);
5058 nand_cleanup(mtd_to_nand(mtd));
5059}
David Woodhousee0c7d762006-05-13 18:07:53 +01005060EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005061
David Woodhousee0c7d762006-05-13 18:07:53 +01005062MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005063MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5064MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005065MODULE_DESCRIPTION("Generic NAND flash driver code");