blob: 327c60b06f87c8f2c9292bce538bd7599ea32d6e [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 *
Miquel Raynalb9587582018-03-19 14:47:19 +0100352 * One user of the write_byte callback is nand_set_features. The
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100353 * 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));
Brian Norris00918422012-01-13 18:11:47 -0800530 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300531 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800532 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800533
Brian Norrisb32843b2013-07-30 17:52:59 -0700534 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800535 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700536 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300537 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200538 }
Brian Norrise2414f42012-02-06 13:44:00 -0800539
Brian Norrisb32843b2013-07-30 17:52:59 -0700540 /* Mark block bad in BBT */
541 if (chip->bbt) {
542 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800543 if (!ret)
544 ret = res;
545 }
546
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200547 if (!ret)
548 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300549
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200550 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000553/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700555 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700557 * Check, if the device is write protected. The function expects, that the
558 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100560static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100562 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100563 u8 status;
564 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200565
Brian Norris8b6e50c2011-05-25 14:59:01 -0700566 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200567 if (chip->options & NAND_BROKEN_XD)
568 return 0;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100571 ret = nand_status_op(chip, &status);
572 if (ret)
573 return ret;
574
575 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800579 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700580 * @mtd: MTD device structure
581 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300582 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800583 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300584 */
585static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
586{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100587 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300588
589 if (!chip->bbt)
590 return 0;
591 /* Return info from the table */
592 return nand_isreserved_bbt(mtd, ofs);
593}
594
595/**
596 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
597 * @mtd: MTD device structure
598 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700599 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *
601 * Check, if the block is bad. Either by reading the bad block table or
602 * calling of the scan function.
603 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530604static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100606 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000607
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200608 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530609 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100612 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200615/**
616 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700617 * @mtd: MTD device structure
618 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200619 *
620 * Helper function for nand_wait_ready used when needing to wait in interrupt
621 * context.
622 */
623static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
624{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100625 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200626 int i;
627
628 /* Wait for the device to get ready */
629 for (i = 0; i < timeo; i++) {
630 if (chip->dev_ready(mtd))
631 break;
632 touch_softlockup_watchdog();
633 mdelay(1);
634 }
635}
636
Alex Smithb70af9b2015-10-06 14:52:07 +0100637/**
638 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
639 * @mtd: MTD device structure
640 *
641 * Wait for the ready pin after a command, and warn if a timeout occurs.
642 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100643void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000644{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100645 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100646 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000647
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200648 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100649 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200650
Brian Norris7854d3f2011-06-23 14:12:08 -0700651 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100652 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000653 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200654 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300655 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100656 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000657 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100658
Brian Norris9ebfdf52016-03-04 17:19:23 -0800659 if (!chip->dev_ready(mtd))
660 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000661}
David Woodhouse4b648b02006-09-25 17:05:24 +0100662EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200665 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
666 * @mtd: MTD device structure
667 * @timeo: Timeout in ms
668 *
669 * Wait for status ready (i.e. command done) or timeout.
670 */
671static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
672{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100673 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100674 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200675
676 timeo = jiffies + msecs_to_jiffies(timeo);
677 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100678 u8 status;
679
680 ret = nand_read_data_op(chip, &status, sizeof(status), true);
681 if (ret)
682 return;
683
684 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200685 break;
686 touch_softlockup_watchdog();
687 } while (time_before(jiffies, timeo));
688};
689
690/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100691 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
692 * @chip: NAND chip structure
693 * @timeout_ms: Timeout in ms
694 *
695 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
696 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
697 * returned.
698 *
699 * This helper is intended to be used when the controller does not have access
700 * to the NAND R/B pin.
701 *
702 * Be aware that calling this helper from an ->exec_op() implementation means
703 * ->exec_op() must be re-entrant.
704 *
705 * Return 0 if the NAND chip is ready, a negative error otherwise.
706 */
707int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
708{
709 u8 status = 0;
710 int ret;
711
712 if (!chip->exec_op)
713 return -ENOTSUPP;
714
715 ret = nand_status_op(chip, NULL);
716 if (ret)
717 return ret;
718
719 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
720 do {
721 ret = nand_read_data_op(chip, &status, sizeof(status), true);
722 if (ret)
723 break;
724
725 if (status & NAND_STATUS_READY)
726 break;
727
728 /*
729 * Typical lowest execution time for a tR on most NANDs is 10us,
730 * use this as polling delay before doing something smarter (ie.
731 * deriving a delay from the timeout value, timeout_ms/ratio).
732 */
733 udelay(10);
734 } while (time_before(jiffies, timeout_ms));
735
736 /*
737 * We have to exit READ_STATUS mode in order to read real data on the
738 * bus in case the WAITRDY instruction is preceding a DATA_IN
739 * instruction.
740 */
741 nand_exit_status_op(chip);
742
743 if (ret)
744 return ret;
745
746 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
747};
748EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
749
750/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700752 * @mtd: MTD device structure
753 * @command: the command to be sent
754 * @column: the column address for this command, -1 if none
755 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700757 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200758 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200760static void nand_command(struct mtd_info *mtd, unsigned int command,
761 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100763 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200764 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Brian Norris8b6e50c2011-05-25 14:59:01 -0700766 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (command == NAND_CMD_SEQIN) {
768 int readcmd;
769
Joern Engel28318772006-05-22 23:18:05 +0200770 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200772 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 readcmd = NAND_CMD_READOOB;
774 } else if (column < 256) {
775 /* First 256 bytes --> READ0 */
776 readcmd = NAND_CMD_READ0;
777 } else {
778 column -= 256;
779 readcmd = NAND_CMD_READ1;
780 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200781 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200782 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100784 if (command != NAND_CMD_NONE)
785 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Brian Norris8b6e50c2011-05-25 14:59:01 -0700787 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200788 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
789 /* Serially input address */
790 if (column != -1) {
791 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800792 if (chip->options & NAND_BUSWIDTH_16 &&
793 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200794 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200795 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200796 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200798 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200799 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200800 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200801 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900802 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200803 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200804 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200805 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000806
807 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700808 * Program and erase have their own busy handlers status and sequential
809 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100810 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000812
Miquel Raynaldf467892017-11-08 17:00:27 +0100813 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 case NAND_CMD_PAGEPROG:
815 case NAND_CMD_ERASE1:
816 case NAND_CMD_ERASE2:
817 case NAND_CMD_SEQIN:
818 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900819 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900820 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return;
822
823 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200824 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200826 udelay(chip->chip_delay);
827 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200828 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200829 chip->cmd_ctrl(mtd,
830 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200831 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
832 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return;
834
David Woodhousee0c7d762006-05-13 18:07:53 +0100835 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200836 case NAND_CMD_READ0:
837 /*
838 * READ0 is sometimes used to exit GET STATUS mode. When this
839 * is the case no address cycles are requested, and we can use
840 * this information to detect that we should not wait for the
841 * device to be ready.
842 */
843 if (column == -1 && page_addr == -1)
844 return;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000847 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * If we don't have access to the busy pin, we apply the given
849 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100850 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200851 if (!chip->dev_ready) {
852 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700856 /*
857 * Apply this short delay always to ensure that we do wait tWB in
858 * any case on any machine.
859 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100860 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000861
862 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200865static void nand_ccs_delay(struct nand_chip *chip)
866{
867 /*
868 * The controller already takes care of waiting for tCCS when the RNDIN
869 * or RNDOUT command is sent, return directly.
870 */
871 if (!(chip->options & NAND_WAIT_TCCS))
872 return;
873
874 /*
875 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
876 * (which should be safe for all NANDs).
877 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100878 if (chip->setup_data_interface)
879 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200880 else
881 ndelay(500);
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/**
885 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700886 * @mtd: MTD device structure
887 * @command: the command to be sent
888 * @column: the column address for this command, -1 if none
889 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200891 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700892 * devices. We don't have the separate regions as we have in the small page
893 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200895static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
896 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100898 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /* Emulate NAND_CMD_READOOB */
901 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200902 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 command = NAND_CMD_READ0;
904 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000905
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200906 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100907 if (command != NAND_CMD_NONE)
908 chip->cmd_ctrl(mtd, command,
909 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200912 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /* Serially input address */
915 if (column != -1) {
916 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800917 if (chip->options & NAND_BUSWIDTH_16 &&
918 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200920 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200921 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200922
Brian Norrisf5b88de2016-10-03 09:49:35 -0700923 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200924 if (!nand_opcode_8bits(command))
925 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200928 chip->cmd_ctrl(mtd, page_addr, ctrl);
929 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200930 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900931 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200932 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200933 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200936 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000937
938 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700939 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100940 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000941 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000943
Miquel Raynaldf467892017-11-08 17:00:27 +0100944 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 case NAND_CMD_CACHEDPROG:
946 case NAND_CMD_PAGEPROG:
947 case NAND_CMD_ERASE1:
948 case NAND_CMD_ERASE2:
949 case NAND_CMD_SEQIN:
950 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900951 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900952 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000953 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200955 case NAND_CMD_RNDIN:
956 nand_ccs_delay(chip);
957 return;
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200960 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200962 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200963 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
964 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
965 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
966 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200967 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
968 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return;
970
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200971 case NAND_CMD_RNDOUT:
972 /* No ready / busy check necessary */
973 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
974 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
975 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
976 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200977
978 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200979 return;
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200982 /*
983 * READ0 is sometimes used to exit GET STATUS mode. When this
984 * is the case no address cycles are requested, and we can use
985 * this information to detect that READSTART should not be
986 * issued.
987 */
988 if (column == -1 && page_addr == -1)
989 return;
990
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200991 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
992 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
993 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
994 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000995
David Woodhousee0c7d762006-05-13 18:07:53 +0100996 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000998 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -07001000 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +01001001 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001002 if (!chip->dev_ready) {
1003 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Thomas Gleixner3b887752005-02-22 21:56:49 +00001007
Brian Norris8b6e50c2011-05-25 14:59:01 -07001008 /*
1009 * Apply this short delay always to ensure that we do wait tWB in
1010 * any case on any machine.
1011 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001012 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +00001013
1014 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001018 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001019 * @chip: the nand chip descriptor
1020 * @mtd: MTD device structure
1021 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001022 *
1023 * Used when in panic, no locks are taken.
1024 */
1025static void panic_nand_get_device(struct nand_chip *chip,
1026 struct mtd_info *mtd, int new_state)
1027{
Brian Norris7854d3f2011-06-23 14:12:08 -07001028 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001029 chip->controller->active = chip;
1030 chip->state = new_state;
1031}
1032
1033/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001035 * @mtd: MTD device structure
1036 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 *
1038 * Get the device and lock it for exclusive access
1039 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001040static int
Huang Shijie6a8214a2012-11-19 14:43:30 +08001041nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001043 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001044 spinlock_t *lock = &chip->controller->lock;
1045 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +01001046 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001047retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001048 spin_lock(lock);
1049
vimal singhb8b3ee92009-07-09 20:41:22 +05301050 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001051 if (!chip->controller->active)
1052 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001053
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001054 if (chip->controller->active == chip && chip->state == FL_READY) {
1055 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001056 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01001057 return 0;
1058 }
1059 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001060 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1061 chip->state = FL_PM_SUSPENDED;
1062 spin_unlock(lock);
1063 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001064 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001065 }
1066 set_current_state(TASK_UNINTERRUPTIBLE);
1067 add_wait_queue(wq, &wait);
1068 spin_unlock(lock);
1069 schedule();
1070 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto retry;
1072}
1073
1074/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001075 * panic_nand_wait - [GENERIC] wait until the command is done
1076 * @mtd: MTD device structure
1077 * @chip: NAND chip structure
1078 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001079 *
1080 * Wait for command done. This is a helper function for nand_wait used when
1081 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001082 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001083 */
1084static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1085 unsigned long timeo)
1086{
1087 int i;
1088 for (i = 0; i < timeo; i++) {
1089 if (chip->dev_ready) {
1090 if (chip->dev_ready(mtd))
1091 break;
1092 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001093 int ret;
1094 u8 status;
1095
1096 ret = nand_read_data_op(chip, &status, sizeof(status),
1097 true);
1098 if (ret)
1099 return;
1100
1101 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001102 break;
1103 }
1104 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001105 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001106}
1107
1108/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001109 * nand_wait - [DEFAULT] wait until the command is done
1110 * @mtd: MTD device structure
1111 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001113 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001114 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001115static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117
Alex Smithb70af9b2015-10-06 14:52:07 +01001118 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001119 u8 status;
1120 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Brian Norris8b6e50c2011-05-25 14:59:01 -07001122 /*
1123 * Apply this short delay always to ensure that we do wait tWB in any
1124 * case on any machine.
1125 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001126 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Boris Brezillon97d90da2017-11-30 18:01:29 +01001128 ret = nand_status_op(chip, NULL);
1129 if (ret)
1130 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001132 if (in_interrupt() || oops_in_progress)
1133 panic_nand_wait(mtd, chip, timeo);
1134 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001135 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001136 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001137 if (chip->dev_ready) {
1138 if (chip->dev_ready(mtd))
1139 break;
1140 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001141 ret = nand_read_data_op(chip, &status,
1142 sizeof(status), true);
1143 if (ret)
1144 return ret;
1145
1146 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001147 break;
1148 }
1149 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001150 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001152
Boris Brezillon97d90da2017-11-30 18:01:29 +01001153 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1154 if (ret)
1155 return ret;
1156
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001157 /* This can happen if in case of timeout or buggy dev_ready */
1158 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return status;
1160}
1161
Miquel Raynal789157e2018-03-19 14:47:28 +01001162static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +01001163{
Miquel Raynal789157e2018-03-19 14:47:28 +01001164 return (chip->parameters.supports_set_get_features &&
1165 test_bit(addr, chip->parameters.get_feature_list));
1166}
1167
1168static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1169{
1170 return (chip->parameters.supports_set_get_features &&
1171 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +01001172}
1173
1174/**
1175 * nand_get_features - wrapper to perform a GET_FEATURE
1176 * @chip: NAND chip info structure
1177 * @addr: feature address
1178 * @subfeature_param: the subfeature parameters, a four bytes array
1179 *
1180 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1181 * operation cannot be handled.
1182 */
1183int nand_get_features(struct nand_chip *chip, int addr,
1184 u8 *subfeature_param)
1185{
1186 struct mtd_info *mtd = nand_to_mtd(chip);
1187
Miquel Raynal789157e2018-03-19 14:47:28 +01001188 if (!nand_supports_get_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001189 return -ENOTSUPP;
1190
1191 return chip->get_features(mtd, chip, addr, subfeature_param);
1192}
1193EXPORT_SYMBOL_GPL(nand_get_features);
1194
1195/**
1196 * nand_set_features - wrapper to perform a SET_FEATURE
1197 * @chip: NAND chip info structure
1198 * @addr: feature address
1199 * @subfeature_param: the subfeature parameters, a four bytes array
1200 *
1201 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1202 * operation cannot be handled.
1203 */
1204int nand_set_features(struct nand_chip *chip, int addr,
1205 u8 *subfeature_param)
1206{
1207 struct mtd_info *mtd = nand_to_mtd(chip);
1208
Miquel Raynal789157e2018-03-19 14:47:28 +01001209 if (!nand_supports_set_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001210 return -ENOTSUPP;
1211
1212 return chip->set_features(mtd, chip, addr, subfeature_param);
1213}
1214EXPORT_SYMBOL_GPL(nand_set_features);
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001217 * nand_reset_data_interface - Reset data interface and timings
1218 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001219 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001220 *
1221 * Reset the Data interface and timings to ONFI mode 0.
1222 *
1223 * Returns 0 for success or negative error code otherwise.
1224 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001225static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001226{
1227 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001228 int ret;
1229
1230 if (!chip->setup_data_interface)
1231 return 0;
1232
1233 /*
1234 * The ONFI specification says:
1235 * "
1236 * To transition from NV-DDR or NV-DDR2 to the SDR data
1237 * interface, the host shall use the Reset (FFh) command
1238 * using SDR timing mode 0. A device in any timing mode is
1239 * required to recognize Reset (FFh) command issued in SDR
1240 * timing mode 0.
1241 * "
1242 *
1243 * Configure the data interface in SDR mode and set the
1244 * timings to timing mode 0.
1245 */
1246
Miquel Raynal17fa8042017-11-30 18:01:31 +01001247 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1248 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001249 if (ret)
1250 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1251
1252 return ret;
1253}
1254
1255/**
1256 * nand_setup_data_interface - Setup the best data interface and timings
1257 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001258 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001259 *
1260 * Find and configure the best data interface and NAND timings supported by
1261 * the chip and the driver.
1262 * First tries to retrieve supported timing modes from ONFI information,
1263 * and if the NAND chip does not support ONFI, relies on the
1264 * ->onfi_timing_mode_default specified in the nand_ids table.
1265 *
1266 * Returns 0 for success or negative error code otherwise.
1267 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001268static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001269{
1270 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal97baea12018-03-19 14:47:20 +01001271 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1272 chip->onfi_timing_mode_default,
1273 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001274 int ret;
1275
Miquel Raynal17fa8042017-11-30 18:01:31 +01001276 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001277 return 0;
1278
Miquel Raynal993447b2018-03-19 14:47:21 +01001279 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +01001280 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Miquel Raynal29714d62018-03-19 14:47:23 +01001281 chip->select_chip(mtd, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001282 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1283 tmode_param);
Miquel Raynal29714d62018-03-19 14:47:23 +01001284 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001285 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +01001286 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001287 }
1288
Miquel Raynal97baea12018-03-19 14:47:20 +01001289 /* Change the mode on the controller side */
Miquel Raynal17fa8042017-11-30 18:01:31 +01001290 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +01001291 if (ret)
1292 return ret;
1293
1294 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +01001295 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +01001296 return 0;
1297
1298 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
1299 chip->select_chip(mtd, chipnr);
1300 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1301 tmode_param);
1302 chip->select_chip(mtd, -1);
1303 if (ret)
1304 goto err_reset_chip;
1305
1306 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1307 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1308 chip->onfi_timing_mode_default);
1309 goto err_reset_chip;
1310 }
1311
1312 return 0;
1313
1314err_reset_chip:
1315 /*
1316 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1317 * timing mode.
1318 */
1319 nand_reset_data_interface(chip, chipnr);
1320 chip->select_chip(mtd, chipnr);
1321 nand_reset_op(chip);
1322 chip->select_chip(mtd, -1);
1323
Boris Brezillond8e725d2016-09-15 10:32:50 +02001324 return ret;
1325}
1326
1327/**
1328 * nand_init_data_interface - find the best data interface and timings
1329 * @chip: The NAND chip
1330 *
1331 * Find the best data interface and NAND timings supported by the chip
1332 * and the driver.
1333 * First tries to retrieve supported timing modes from ONFI information,
1334 * and if the NAND chip does not support ONFI, relies on the
1335 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1336 * function nand_chip->data_interface is initialized with the best timing mode
1337 * available.
1338 *
1339 * Returns 0 for success or negative error code otherwise.
1340 */
1341static int nand_init_data_interface(struct nand_chip *chip)
1342{
1343 struct mtd_info *mtd = nand_to_mtd(chip);
1344 int modes, mode, ret;
1345
1346 if (!chip->setup_data_interface)
1347 return 0;
1348
1349 /*
1350 * First try to identify the best timings from ONFI parameters and
1351 * if the NAND does not support ONFI, fallback to the default ONFI
1352 * timing mode.
1353 */
1354 modes = onfi_get_async_timing_mode(chip);
1355 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1356 if (!chip->onfi_timing_mode_default)
1357 return 0;
1358
1359 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1360 }
1361
Boris Brezillond8e725d2016-09-15 10:32:50 +02001362
1363 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001364 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001365 if (ret)
1366 continue;
1367
Miquel Raynald787b8b2017-12-22 18:12:41 +01001368 /*
1369 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1370 * controller supports the requested timings.
1371 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001372 ret = chip->setup_data_interface(mtd,
1373 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001374 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001375 if (!ret) {
1376 chip->onfi_timing_mode_default = mode;
1377 break;
1378 }
1379 }
1380
1381 return 0;
1382}
1383
Boris Brezillond8e725d2016-09-15 10:32:50 +02001384/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001385 * nand_fill_column_cycles - fill the column cycles of an address
1386 * @chip: The NAND chip
1387 * @addrs: Array of address cycles to fill
1388 * @offset_in_page: The offset in the page
1389 *
1390 * Fills the first or the first two bytes of the @addrs field depending
1391 * on the NAND bus width and the page size.
1392 *
1393 * Returns the number of cycles needed to encode the column, or a negative
1394 * error code in case one of the arguments is invalid.
1395 */
1396static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1397 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Miquel Raynal8878b122017-11-09 14:16:45 +01001399 struct mtd_info *mtd = nand_to_mtd(chip);
1400
1401 /* Make sure the offset is less than the actual page size. */
1402 if (offset_in_page > mtd->writesize + mtd->oobsize)
1403 return -EINVAL;
1404
1405 /*
1406 * On small page NANDs, there's a dedicated command to access the OOB
1407 * area, and the column address is relative to the start of the OOB
1408 * area, not the start of the page. Asjust the address accordingly.
1409 */
1410 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1411 offset_in_page -= mtd->writesize;
1412
1413 /*
1414 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1415 * wide, then it must be divided by 2.
1416 */
1417 if (chip->options & NAND_BUSWIDTH_16) {
1418 if (WARN_ON(offset_in_page % 2))
1419 return -EINVAL;
1420
1421 offset_in_page /= 2;
1422 }
1423
1424 addrs[0] = offset_in_page;
1425
1426 /*
1427 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1428 * need 2
1429 */
1430 if (mtd->writesize <= 512)
1431 return 1;
1432
1433 addrs[1] = offset_in_page >> 8;
1434
1435 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
Miquel Raynal8878b122017-11-09 14:16:45 +01001438static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1439 unsigned int offset_in_page, void *buf,
1440 unsigned int len)
1441{
1442 struct mtd_info *mtd = nand_to_mtd(chip);
1443 const struct nand_sdr_timings *sdr =
1444 nand_get_sdr_timings(&chip->data_interface);
1445 u8 addrs[4];
1446 struct nand_op_instr instrs[] = {
1447 NAND_OP_CMD(NAND_CMD_READ0, 0),
1448 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1449 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1450 PSEC_TO_NSEC(sdr->tRR_min)),
1451 NAND_OP_DATA_IN(len, buf, 0),
1452 };
1453 struct nand_operation op = NAND_OPERATION(instrs);
1454 int ret;
1455
1456 /* Drop the DATA_IN instruction if len is set to 0. */
1457 if (!len)
1458 op.ninstrs--;
1459
1460 if (offset_in_page >= mtd->writesize)
1461 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1462 else if (offset_in_page >= 256 &&
1463 !(chip->options & NAND_BUSWIDTH_16))
1464 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1465
1466 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1467 if (ret < 0)
1468 return ret;
1469
1470 addrs[1] = page;
1471 addrs[2] = page >> 8;
1472
1473 if (chip->options & NAND_ROW_ADDR_3) {
1474 addrs[3] = page >> 16;
1475 instrs[1].ctx.addr.naddrs++;
1476 }
1477
1478 return nand_exec_op(chip, &op);
1479}
1480
1481static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1482 unsigned int offset_in_page, void *buf,
1483 unsigned int len)
1484{
1485 const struct nand_sdr_timings *sdr =
1486 nand_get_sdr_timings(&chip->data_interface);
1487 u8 addrs[5];
1488 struct nand_op_instr instrs[] = {
1489 NAND_OP_CMD(NAND_CMD_READ0, 0),
1490 NAND_OP_ADDR(4, addrs, 0),
1491 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1492 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1493 PSEC_TO_NSEC(sdr->tRR_min)),
1494 NAND_OP_DATA_IN(len, buf, 0),
1495 };
1496 struct nand_operation op = NAND_OPERATION(instrs);
1497 int ret;
1498
1499 /* Drop the DATA_IN instruction if len is set to 0. */
1500 if (!len)
1501 op.ninstrs--;
1502
1503 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1504 if (ret < 0)
1505 return ret;
1506
1507 addrs[2] = page;
1508 addrs[3] = page >> 8;
1509
1510 if (chip->options & NAND_ROW_ADDR_3) {
1511 addrs[4] = page >> 16;
1512 instrs[1].ctx.addr.naddrs++;
1513 }
1514
1515 return nand_exec_op(chip, &op);
1516}
1517
1518/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001519 * nand_read_page_op - Do a READ PAGE operation
1520 * @chip: The NAND chip
1521 * @page: page to read
1522 * @offset_in_page: offset within the page
1523 * @buf: buffer used to store the data
1524 * @len: length of the buffer
1525 *
1526 * This function issues a READ PAGE operation.
1527 * This function does not select/unselect the CS line.
1528 *
1529 * Returns 0 on success, a negative error code otherwise.
1530 */
1531int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1532 unsigned int offset_in_page, void *buf, unsigned int len)
1533{
1534 struct mtd_info *mtd = nand_to_mtd(chip);
1535
1536 if (len && !buf)
1537 return -EINVAL;
1538
1539 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1540 return -EINVAL;
1541
Miquel Raynal8878b122017-11-09 14:16:45 +01001542 if (chip->exec_op) {
1543 if (mtd->writesize > 512)
1544 return nand_lp_exec_read_page_op(chip, page,
1545 offset_in_page, buf,
1546 len);
1547
1548 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1549 buf, len);
1550 }
1551
Boris Brezillon97d90da2017-11-30 18:01:29 +01001552 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1553 if (len)
1554 chip->read_buf(mtd, buf, len);
1555
1556 return 0;
1557}
1558EXPORT_SYMBOL_GPL(nand_read_page_op);
1559
1560/**
1561 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1562 * @chip: The NAND chip
1563 * @page: parameter page to read
1564 * @buf: buffer used to store the data
1565 * @len: length of the buffer
1566 *
1567 * This function issues a READ PARAMETER PAGE operation.
1568 * This function does not select/unselect the CS line.
1569 *
1570 * Returns 0 on success, a negative error code otherwise.
1571 */
1572static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1573 unsigned int len)
1574{
1575 struct mtd_info *mtd = nand_to_mtd(chip);
1576 unsigned int i;
1577 u8 *p = buf;
1578
1579 if (len && !buf)
1580 return -EINVAL;
1581
Miquel Raynal8878b122017-11-09 14:16:45 +01001582 if (chip->exec_op) {
1583 const struct nand_sdr_timings *sdr =
1584 nand_get_sdr_timings(&chip->data_interface);
1585 struct nand_op_instr instrs[] = {
1586 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1587 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1588 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1589 PSEC_TO_NSEC(sdr->tRR_min)),
1590 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1591 };
1592 struct nand_operation op = NAND_OPERATION(instrs);
1593
1594 /* Drop the DATA_IN instruction if len is set to 0. */
1595 if (!len)
1596 op.ninstrs--;
1597
1598 return nand_exec_op(chip, &op);
1599 }
1600
Boris Brezillon97d90da2017-11-30 18:01:29 +01001601 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1602 for (i = 0; i < len; i++)
1603 p[i] = chip->read_byte(mtd);
1604
1605 return 0;
1606}
1607
1608/**
1609 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1610 * @chip: The NAND chip
1611 * @offset_in_page: offset within the page
1612 * @buf: buffer used to store the data
1613 * @len: length of the buffer
1614 * @force_8bit: force 8-bit bus access
1615 *
1616 * This function issues a CHANGE READ COLUMN operation.
1617 * This function does not select/unselect the CS line.
1618 *
1619 * Returns 0 on success, a negative error code otherwise.
1620 */
1621int nand_change_read_column_op(struct nand_chip *chip,
1622 unsigned int offset_in_page, void *buf,
1623 unsigned int len, bool force_8bit)
1624{
1625 struct mtd_info *mtd = nand_to_mtd(chip);
1626
1627 if (len && !buf)
1628 return -EINVAL;
1629
1630 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1631 return -EINVAL;
1632
Miquel Raynal8878b122017-11-09 14:16:45 +01001633 /* Small page NANDs do not support column change. */
1634 if (mtd->writesize <= 512)
1635 return -ENOTSUPP;
1636
1637 if (chip->exec_op) {
1638 const struct nand_sdr_timings *sdr =
1639 nand_get_sdr_timings(&chip->data_interface);
1640 u8 addrs[2] = {};
1641 struct nand_op_instr instrs[] = {
1642 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1643 NAND_OP_ADDR(2, addrs, 0),
1644 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1645 PSEC_TO_NSEC(sdr->tCCS_min)),
1646 NAND_OP_DATA_IN(len, buf, 0),
1647 };
1648 struct nand_operation op = NAND_OPERATION(instrs);
1649 int ret;
1650
1651 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1652 if (ret < 0)
1653 return ret;
1654
1655 /* Drop the DATA_IN instruction if len is set to 0. */
1656 if (!len)
1657 op.ninstrs--;
1658
1659 instrs[3].ctx.data.force_8bit = force_8bit;
1660
1661 return nand_exec_op(chip, &op);
1662 }
1663
Boris Brezillon97d90da2017-11-30 18:01:29 +01001664 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1665 if (len)
1666 chip->read_buf(mtd, buf, len);
1667
1668 return 0;
1669}
1670EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1671
1672/**
1673 * nand_read_oob_op - Do a READ OOB operation
1674 * @chip: The NAND chip
1675 * @page: page to read
1676 * @offset_in_oob: offset within the OOB area
1677 * @buf: buffer used to store the data
1678 * @len: length of the buffer
1679 *
1680 * This function issues a READ OOB operation.
1681 * This function does not select/unselect the CS line.
1682 *
1683 * Returns 0 on success, a negative error code otherwise.
1684 */
1685int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1686 unsigned int offset_in_oob, void *buf, unsigned int len)
1687{
1688 struct mtd_info *mtd = nand_to_mtd(chip);
1689
1690 if (len && !buf)
1691 return -EINVAL;
1692
1693 if (offset_in_oob + len > mtd->oobsize)
1694 return -EINVAL;
1695
Miquel Raynal8878b122017-11-09 14:16:45 +01001696 if (chip->exec_op)
1697 return nand_read_page_op(chip, page,
1698 mtd->writesize + offset_in_oob,
1699 buf, len);
1700
Boris Brezillon97d90da2017-11-30 18:01:29 +01001701 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1702 if (len)
1703 chip->read_buf(mtd, buf, len);
1704
1705 return 0;
1706}
1707EXPORT_SYMBOL_GPL(nand_read_oob_op);
1708
Miquel Raynal8878b122017-11-09 14:16:45 +01001709static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1710 unsigned int offset_in_page, const void *buf,
1711 unsigned int len, bool prog)
1712{
1713 struct mtd_info *mtd = nand_to_mtd(chip);
1714 const struct nand_sdr_timings *sdr =
1715 nand_get_sdr_timings(&chip->data_interface);
1716 u8 addrs[5] = {};
1717 struct nand_op_instr instrs[] = {
1718 /*
1719 * The first instruction will be dropped if we're dealing
1720 * with a large page NAND and adjusted if we're dealing
1721 * with a small page NAND and the page offset is > 255.
1722 */
1723 NAND_OP_CMD(NAND_CMD_READ0, 0),
1724 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1725 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1726 NAND_OP_DATA_OUT(len, buf, 0),
1727 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1728 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1729 };
1730 struct nand_operation op = NAND_OPERATION(instrs);
1731 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1732 int ret;
1733 u8 status;
1734
1735 if (naddrs < 0)
1736 return naddrs;
1737
1738 addrs[naddrs++] = page;
1739 addrs[naddrs++] = page >> 8;
1740 if (chip->options & NAND_ROW_ADDR_3)
1741 addrs[naddrs++] = page >> 16;
1742
1743 instrs[2].ctx.addr.naddrs = naddrs;
1744
1745 /* Drop the last two instructions if we're not programming the page. */
1746 if (!prog) {
1747 op.ninstrs -= 2;
1748 /* Also drop the DATA_OUT instruction if empty. */
1749 if (!len)
1750 op.ninstrs--;
1751 }
1752
1753 if (mtd->writesize <= 512) {
1754 /*
1755 * Small pages need some more tweaking: we have to adjust the
1756 * first instruction depending on the page offset we're trying
1757 * to access.
1758 */
1759 if (offset_in_page >= mtd->writesize)
1760 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1761 else if (offset_in_page >= 256 &&
1762 !(chip->options & NAND_BUSWIDTH_16))
1763 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1764 } else {
1765 /*
1766 * Drop the first command if we're dealing with a large page
1767 * NAND.
1768 */
1769 op.instrs++;
1770 op.ninstrs--;
1771 }
1772
1773 ret = nand_exec_op(chip, &op);
1774 if (!prog || ret)
1775 return ret;
1776
1777 ret = nand_status_op(chip, &status);
1778 if (ret)
1779 return ret;
1780
1781 return status;
1782}
1783
Boris Brezillon97d90da2017-11-30 18:01:29 +01001784/**
1785 * nand_prog_page_begin_op - starts a PROG PAGE operation
1786 * @chip: The NAND chip
1787 * @page: page to write
1788 * @offset_in_page: offset within the page
1789 * @buf: buffer containing the data to write to the page
1790 * @len: length of the buffer
1791 *
1792 * This function issues the first half of a PROG PAGE operation.
1793 * This function does not select/unselect the CS line.
1794 *
1795 * Returns 0 on success, a negative error code otherwise.
1796 */
1797int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1798 unsigned int offset_in_page, const void *buf,
1799 unsigned int len)
1800{
1801 struct mtd_info *mtd = nand_to_mtd(chip);
1802
1803 if (len && !buf)
1804 return -EINVAL;
1805
1806 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1807 return -EINVAL;
1808
Miquel Raynal8878b122017-11-09 14:16:45 +01001809 if (chip->exec_op)
1810 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1811 len, false);
1812
Boris Brezillon97d90da2017-11-30 18:01:29 +01001813 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1814
1815 if (buf)
1816 chip->write_buf(mtd, buf, len);
1817
1818 return 0;
1819}
1820EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1821
1822/**
1823 * nand_prog_page_end_op - ends a PROG PAGE operation
1824 * @chip: The NAND chip
1825 *
1826 * This function issues the second half of a PROG PAGE operation.
1827 * This function does not select/unselect the CS line.
1828 *
1829 * Returns 0 on success, a negative error code otherwise.
1830 */
1831int nand_prog_page_end_op(struct nand_chip *chip)
1832{
1833 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001834 int ret;
1835 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001836
Miquel Raynal8878b122017-11-09 14:16:45 +01001837 if (chip->exec_op) {
1838 const struct nand_sdr_timings *sdr =
1839 nand_get_sdr_timings(&chip->data_interface);
1840 struct nand_op_instr instrs[] = {
1841 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1842 PSEC_TO_NSEC(sdr->tWB_max)),
1843 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1844 };
1845 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001846
Miquel Raynal8878b122017-11-09 14:16:45 +01001847 ret = nand_exec_op(chip, &op);
1848 if (ret)
1849 return ret;
1850
1851 ret = nand_status_op(chip, &status);
1852 if (ret)
1853 return ret;
1854 } else {
1855 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1856 ret = chip->waitfunc(mtd, chip);
1857 if (ret < 0)
1858 return ret;
1859
1860 status = ret;
1861 }
1862
Boris Brezillon97d90da2017-11-30 18:01:29 +01001863 if (status & NAND_STATUS_FAIL)
1864 return -EIO;
1865
1866 return 0;
1867}
1868EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1869
1870/**
1871 * nand_prog_page_op - Do a full PROG PAGE operation
1872 * @chip: The NAND chip
1873 * @page: page to write
1874 * @offset_in_page: offset within the page
1875 * @buf: buffer containing the data to write to the page
1876 * @len: length of the buffer
1877 *
1878 * This function issues a full PROG PAGE operation.
1879 * This function does not select/unselect the CS line.
1880 *
1881 * Returns 0 on success, a negative error code otherwise.
1882 */
1883int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1884 unsigned int offset_in_page, const void *buf,
1885 unsigned int len)
1886{
1887 struct mtd_info *mtd = nand_to_mtd(chip);
1888 int status;
1889
1890 if (!len || !buf)
1891 return -EINVAL;
1892
1893 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1894 return -EINVAL;
1895
Miquel Raynal8878b122017-11-09 14:16:45 +01001896 if (chip->exec_op) {
1897 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1898 len, true);
1899 } else {
1900 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1901 chip->write_buf(mtd, buf, len);
1902 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1903 status = chip->waitfunc(mtd, chip);
1904 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001905
Boris Brezillon97d90da2017-11-30 18:01:29 +01001906 if (status & NAND_STATUS_FAIL)
1907 return -EIO;
1908
1909 return 0;
1910}
1911EXPORT_SYMBOL_GPL(nand_prog_page_op);
1912
1913/**
1914 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1915 * @chip: The NAND chip
1916 * @offset_in_page: offset within the page
1917 * @buf: buffer containing the data to send to the NAND
1918 * @len: length of the buffer
1919 * @force_8bit: force 8-bit bus access
1920 *
1921 * This function issues a CHANGE WRITE COLUMN operation.
1922 * This function does not select/unselect the CS line.
1923 *
1924 * Returns 0 on success, a negative error code otherwise.
1925 */
1926int nand_change_write_column_op(struct nand_chip *chip,
1927 unsigned int offset_in_page,
1928 const void *buf, unsigned int len,
1929 bool force_8bit)
1930{
1931 struct mtd_info *mtd = nand_to_mtd(chip);
1932
1933 if (len && !buf)
1934 return -EINVAL;
1935
1936 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1937 return -EINVAL;
1938
Miquel Raynal8878b122017-11-09 14:16:45 +01001939 /* Small page NANDs do not support column change. */
1940 if (mtd->writesize <= 512)
1941 return -ENOTSUPP;
1942
1943 if (chip->exec_op) {
1944 const struct nand_sdr_timings *sdr =
1945 nand_get_sdr_timings(&chip->data_interface);
1946 u8 addrs[2];
1947 struct nand_op_instr instrs[] = {
1948 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1949 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1950 NAND_OP_DATA_OUT(len, buf, 0),
1951 };
1952 struct nand_operation op = NAND_OPERATION(instrs);
1953 int ret;
1954
1955 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1956 if (ret < 0)
1957 return ret;
1958
1959 instrs[2].ctx.data.force_8bit = force_8bit;
1960
1961 /* Drop the DATA_OUT instruction if len is set to 0. */
1962 if (!len)
1963 op.ninstrs--;
1964
1965 return nand_exec_op(chip, &op);
1966 }
1967
Boris Brezillon97d90da2017-11-30 18:01:29 +01001968 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1969 if (len)
1970 chip->write_buf(mtd, buf, len);
1971
1972 return 0;
1973}
1974EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1975
1976/**
1977 * nand_readid_op - Do a READID operation
1978 * @chip: The NAND chip
1979 * @addr: address cycle to pass after the READID command
1980 * @buf: buffer used to store the ID
1981 * @len: length of the buffer
1982 *
1983 * This function sends a READID command and reads back the ID returned by the
1984 * NAND.
1985 * This function does not select/unselect the CS line.
1986 *
1987 * Returns 0 on success, a negative error code otherwise.
1988 */
1989int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1990 unsigned int len)
1991{
1992 struct mtd_info *mtd = nand_to_mtd(chip);
1993 unsigned int i;
1994 u8 *id = buf;
1995
1996 if (len && !buf)
1997 return -EINVAL;
1998
Miquel Raynal8878b122017-11-09 14:16:45 +01001999 if (chip->exec_op) {
2000 const struct nand_sdr_timings *sdr =
2001 nand_get_sdr_timings(&chip->data_interface);
2002 struct nand_op_instr instrs[] = {
2003 NAND_OP_CMD(NAND_CMD_READID, 0),
2004 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
2005 NAND_OP_8BIT_DATA_IN(len, buf, 0),
2006 };
2007 struct nand_operation op = NAND_OPERATION(instrs);
2008
2009 /* Drop the DATA_IN instruction if len is set to 0. */
2010 if (!len)
2011 op.ninstrs--;
2012
2013 return nand_exec_op(chip, &op);
2014 }
2015
Boris Brezillon97d90da2017-11-30 18:01:29 +01002016 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
2017
2018 for (i = 0; i < len; i++)
2019 id[i] = chip->read_byte(mtd);
2020
2021 return 0;
2022}
2023EXPORT_SYMBOL_GPL(nand_readid_op);
2024
2025/**
2026 * nand_status_op - Do a STATUS operation
2027 * @chip: The NAND chip
2028 * @status: out variable to store the NAND status
2029 *
2030 * This function sends a STATUS command and reads back the status returned by
2031 * the NAND.
2032 * This function does not select/unselect the CS line.
2033 *
2034 * Returns 0 on success, a negative error code otherwise.
2035 */
2036int nand_status_op(struct nand_chip *chip, u8 *status)
2037{
2038 struct mtd_info *mtd = nand_to_mtd(chip);
2039
Miquel Raynal8878b122017-11-09 14:16:45 +01002040 if (chip->exec_op) {
2041 const struct nand_sdr_timings *sdr =
2042 nand_get_sdr_timings(&chip->data_interface);
2043 struct nand_op_instr instrs[] = {
2044 NAND_OP_CMD(NAND_CMD_STATUS,
2045 PSEC_TO_NSEC(sdr->tADL_min)),
2046 NAND_OP_8BIT_DATA_IN(1, status, 0),
2047 };
2048 struct nand_operation op = NAND_OPERATION(instrs);
2049
2050 if (!status)
2051 op.ninstrs--;
2052
2053 return nand_exec_op(chip, &op);
2054 }
2055
Boris Brezillon97d90da2017-11-30 18:01:29 +01002056 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
2057 if (status)
2058 *status = chip->read_byte(mtd);
2059
2060 return 0;
2061}
2062EXPORT_SYMBOL_GPL(nand_status_op);
2063
2064/**
2065 * nand_exit_status_op - Exit a STATUS operation
2066 * @chip: The NAND chip
2067 *
2068 * This function sends a READ0 command to cancel the effect of the STATUS
2069 * command to avoid reading only the status until a new read command is sent.
2070 *
2071 * This function does not select/unselect the CS line.
2072 *
2073 * Returns 0 on success, a negative error code otherwise.
2074 */
2075int nand_exit_status_op(struct nand_chip *chip)
2076{
2077 struct mtd_info *mtd = nand_to_mtd(chip);
2078
Miquel Raynal8878b122017-11-09 14:16:45 +01002079 if (chip->exec_op) {
2080 struct nand_op_instr instrs[] = {
2081 NAND_OP_CMD(NAND_CMD_READ0, 0),
2082 };
2083 struct nand_operation op = NAND_OPERATION(instrs);
2084
2085 return nand_exec_op(chip, &op);
2086 }
2087
Boris Brezillon97d90da2017-11-30 18:01:29 +01002088 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2089
2090 return 0;
2091}
2092EXPORT_SYMBOL_GPL(nand_exit_status_op);
2093
2094/**
2095 * nand_erase_op - Do an erase operation
2096 * @chip: The NAND chip
2097 * @eraseblock: block to erase
2098 *
2099 * This function sends an ERASE command and waits for the NAND to be ready
2100 * before returning.
2101 * This function does not select/unselect the CS line.
2102 *
2103 * Returns 0 on success, a negative error code otherwise.
2104 */
2105int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2106{
2107 struct mtd_info *mtd = nand_to_mtd(chip);
2108 unsigned int page = eraseblock <<
2109 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002110 int ret;
2111 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002112
Miquel Raynal8878b122017-11-09 14:16:45 +01002113 if (chip->exec_op) {
2114 const struct nand_sdr_timings *sdr =
2115 nand_get_sdr_timings(&chip->data_interface);
2116 u8 addrs[3] = { page, page >> 8, page >> 16 };
2117 struct nand_op_instr instrs[] = {
2118 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2119 NAND_OP_ADDR(2, addrs, 0),
2120 NAND_OP_CMD(NAND_CMD_ERASE2,
2121 PSEC_TO_MSEC(sdr->tWB_max)),
2122 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2123 };
2124 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002125
Miquel Raynal8878b122017-11-09 14:16:45 +01002126 if (chip->options & NAND_ROW_ADDR_3)
2127 instrs[1].ctx.addr.naddrs++;
2128
2129 ret = nand_exec_op(chip, &op);
2130 if (ret)
2131 return ret;
2132
2133 ret = nand_status_op(chip, &status);
2134 if (ret)
2135 return ret;
2136 } else {
2137 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2138 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2139
2140 ret = chip->waitfunc(mtd, chip);
2141 if (ret < 0)
2142 return ret;
2143
2144 status = ret;
2145 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002146
2147 if (status & NAND_STATUS_FAIL)
2148 return -EIO;
2149
2150 return 0;
2151}
2152EXPORT_SYMBOL_GPL(nand_erase_op);
2153
2154/**
2155 * nand_set_features_op - Do a SET FEATURES operation
2156 * @chip: The NAND chip
2157 * @feature: feature id
2158 * @data: 4 bytes of data
2159 *
2160 * This function sends a SET FEATURES command and waits for the NAND to be
2161 * ready before returning.
2162 * This function does not select/unselect the CS line.
2163 *
2164 * Returns 0 on success, a negative error code otherwise.
2165 */
2166static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2167 const void *data)
2168{
2169 struct mtd_info *mtd = nand_to_mtd(chip);
2170 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002171 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002172
Miquel Raynal8878b122017-11-09 14:16:45 +01002173 if (chip->exec_op) {
2174 const struct nand_sdr_timings *sdr =
2175 nand_get_sdr_timings(&chip->data_interface);
2176 struct nand_op_instr instrs[] = {
2177 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2178 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2179 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2180 PSEC_TO_NSEC(sdr->tWB_max)),
2181 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2182 };
2183 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002184
Boris Brezillon782d1962018-05-11 14:44:07 +02002185 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01002186 }
2187
Boris Brezillon782d1962018-05-11 14:44:07 +02002188 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
2189 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2190 chip->write_byte(mtd, params[i]);
2191
2192 ret = chip->waitfunc(mtd, chip);
2193 if (ret < 0)
2194 return ret;
2195
2196 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002197 return -EIO;
2198
2199 return 0;
2200}
2201
2202/**
2203 * nand_get_features_op - Do a GET FEATURES operation
2204 * @chip: The NAND chip
2205 * @feature: feature id
2206 * @data: 4 bytes of data
2207 *
2208 * This function sends a GET FEATURES command and waits for the NAND to be
2209 * ready before returning.
2210 * This function does not select/unselect the CS line.
2211 *
2212 * Returns 0 on success, a negative error code otherwise.
2213 */
2214static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2215 void *data)
2216{
2217 struct mtd_info *mtd = nand_to_mtd(chip);
2218 u8 *params = data;
2219 int i;
2220
Miquel Raynal8878b122017-11-09 14:16:45 +01002221 if (chip->exec_op) {
2222 const struct nand_sdr_timings *sdr =
2223 nand_get_sdr_timings(&chip->data_interface);
2224 struct nand_op_instr instrs[] = {
2225 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2226 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2227 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2228 PSEC_TO_NSEC(sdr->tRR_min)),
2229 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2230 data, 0),
2231 };
2232 struct nand_operation op = NAND_OPERATION(instrs);
2233
2234 return nand_exec_op(chip, &op);
2235 }
2236
Boris Brezillon97d90da2017-11-30 18:01:29 +01002237 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2238 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2239 params[i] = chip->read_byte(mtd);
2240
2241 return 0;
2242}
2243
2244/**
2245 * nand_reset_op - Do a reset operation
2246 * @chip: The NAND chip
2247 *
2248 * This function sends a RESET command and waits for the NAND to be ready
2249 * before returning.
2250 * This function does not select/unselect the CS line.
2251 *
2252 * Returns 0 on success, a negative error code otherwise.
2253 */
2254int nand_reset_op(struct nand_chip *chip)
2255{
2256 struct mtd_info *mtd = nand_to_mtd(chip);
2257
Miquel Raynal8878b122017-11-09 14:16:45 +01002258 if (chip->exec_op) {
2259 const struct nand_sdr_timings *sdr =
2260 nand_get_sdr_timings(&chip->data_interface);
2261 struct nand_op_instr instrs[] = {
2262 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2263 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2264 };
2265 struct nand_operation op = NAND_OPERATION(instrs);
2266
2267 return nand_exec_op(chip, &op);
2268 }
2269
Boris Brezillon97d90da2017-11-30 18:01:29 +01002270 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2271
2272 return 0;
2273}
2274EXPORT_SYMBOL_GPL(nand_reset_op);
2275
2276/**
2277 * nand_read_data_op - Read data from the NAND
2278 * @chip: The NAND chip
2279 * @buf: buffer used to store the data
2280 * @len: length of the buffer
2281 * @force_8bit: force 8-bit bus access
2282 *
2283 * This function does a raw data read on the bus. Usually used after launching
2284 * another NAND operation like nand_read_page_op().
2285 * This function does not select/unselect the CS line.
2286 *
2287 * Returns 0 on success, a negative error code otherwise.
2288 */
2289int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2290 bool force_8bit)
2291{
2292 struct mtd_info *mtd = nand_to_mtd(chip);
2293
2294 if (!len || !buf)
2295 return -EINVAL;
2296
Miquel Raynal8878b122017-11-09 14:16:45 +01002297 if (chip->exec_op) {
2298 struct nand_op_instr instrs[] = {
2299 NAND_OP_DATA_IN(len, buf, 0),
2300 };
2301 struct nand_operation op = NAND_OPERATION(instrs);
2302
2303 instrs[0].ctx.data.force_8bit = force_8bit;
2304
2305 return nand_exec_op(chip, &op);
2306 }
2307
Boris Brezillon97d90da2017-11-30 18:01:29 +01002308 if (force_8bit) {
2309 u8 *p = buf;
2310 unsigned int i;
2311
2312 for (i = 0; i < len; i++)
2313 p[i] = chip->read_byte(mtd);
2314 } else {
2315 chip->read_buf(mtd, buf, len);
2316 }
2317
2318 return 0;
2319}
2320EXPORT_SYMBOL_GPL(nand_read_data_op);
2321
2322/**
2323 * nand_write_data_op - Write data from the NAND
2324 * @chip: The NAND chip
2325 * @buf: buffer containing the data to send on the bus
2326 * @len: length of the buffer
2327 * @force_8bit: force 8-bit bus access
2328 *
2329 * This function does a raw data write on the bus. Usually used after launching
2330 * another NAND operation like nand_write_page_begin_op().
2331 * This function does not select/unselect the CS line.
2332 *
2333 * Returns 0 on success, a negative error code otherwise.
2334 */
2335int nand_write_data_op(struct nand_chip *chip, const void *buf,
2336 unsigned int len, bool force_8bit)
2337{
2338 struct mtd_info *mtd = nand_to_mtd(chip);
2339
2340 if (!len || !buf)
2341 return -EINVAL;
2342
Miquel Raynal8878b122017-11-09 14:16:45 +01002343 if (chip->exec_op) {
2344 struct nand_op_instr instrs[] = {
2345 NAND_OP_DATA_OUT(len, buf, 0),
2346 };
2347 struct nand_operation op = NAND_OPERATION(instrs);
2348
2349 instrs[0].ctx.data.force_8bit = force_8bit;
2350
2351 return nand_exec_op(chip, &op);
2352 }
2353
Boris Brezillon97d90da2017-11-30 18:01:29 +01002354 if (force_8bit) {
2355 const u8 *p = buf;
2356 unsigned int i;
2357
2358 for (i = 0; i < len; i++)
2359 chip->write_byte(mtd, p[i]);
2360 } else {
2361 chip->write_buf(mtd, buf, len);
2362 }
2363
2364 return 0;
2365}
2366EXPORT_SYMBOL_GPL(nand_write_data_op);
2367
2368/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002369 * struct nand_op_parser_ctx - Context used by the parser
2370 * @instrs: array of all the instructions that must be addressed
2371 * @ninstrs: length of the @instrs array
2372 * @subop: Sub-operation to be passed to the NAND controller
2373 *
2374 * This structure is used by the core to split NAND operations into
2375 * sub-operations that can be handled by the NAND controller.
2376 */
2377struct nand_op_parser_ctx {
2378 const struct nand_op_instr *instrs;
2379 unsigned int ninstrs;
2380 struct nand_subop subop;
2381};
2382
2383/**
2384 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2385 * @pat: the parser pattern element that matches @instr
2386 * @instr: pointer to the instruction to check
2387 * @start_offset: this is an in/out parameter. If @instr has already been
2388 * split, then @start_offset is the offset from which to start
2389 * (either an address cycle or an offset in the data buffer).
2390 * Conversely, if the function returns true (ie. instr must be
2391 * split), this parameter is updated to point to the first
2392 * data/address cycle that has not been taken care of.
2393 *
2394 * Some NAND controllers are limited and cannot send X address cycles with a
2395 * unique operation, or cannot read/write more than Y bytes at the same time.
2396 * In this case, split the instruction that does not fit in a single
2397 * controller-operation into two or more chunks.
2398 *
2399 * Returns true if the instruction must be split, false otherwise.
2400 * The @start_offset parameter is also updated to the offset at which the next
2401 * bundle of instruction must start (if an address or a data instruction).
2402 */
2403static bool
2404nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2405 const struct nand_op_instr *instr,
2406 unsigned int *start_offset)
2407{
2408 switch (pat->type) {
2409 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002410 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002411 break;
2412
2413 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002414 pat->ctx.addr.maxcycles) {
2415 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002416 return true;
2417 }
2418 break;
2419
2420 case NAND_OP_DATA_IN_INSTR:
2421 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002422 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002423 break;
2424
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002425 if (instr->ctx.data.len - *start_offset >
2426 pat->ctx.data.maxlen) {
2427 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002428 return true;
2429 }
2430 break;
2431
2432 default:
2433 break;
2434 }
2435
2436 return false;
2437}
2438
2439/**
2440 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2441 * remaining in the parser context
2442 * @pat: the pattern to test
2443 * @ctx: the parser context structure to match with the pattern @pat
2444 *
2445 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2446 * Returns true if this is the case, false ortherwise. When true is returned,
2447 * @ctx->subop is updated with the set of instructions to be passed to the
2448 * controller driver.
2449 */
2450static bool
2451nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2452 struct nand_op_parser_ctx *ctx)
2453{
2454 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2455 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2456 const struct nand_op_instr *instr = ctx->subop.instrs;
2457 unsigned int i, ninstrs;
2458
2459 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2460 /*
2461 * The pattern instruction does not match the operation
2462 * instruction. If the instruction is marked optional in the
2463 * pattern definition, we skip the pattern element and continue
2464 * to the next one. If the element is mandatory, there's no
2465 * match and we can return false directly.
2466 */
2467 if (instr->type != pat->elems[i].type) {
2468 if (!pat->elems[i].optional)
2469 return false;
2470
2471 continue;
2472 }
2473
2474 /*
2475 * Now check the pattern element constraints. If the pattern is
2476 * not able to handle the whole instruction in a single step,
2477 * we have to split it.
2478 * The last_instr_end_off value comes back updated to point to
2479 * the position where we have to split the instruction (the
2480 * start of the next subop chunk).
2481 */
2482 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2483 &instr_offset)) {
2484 ninstrs++;
2485 i++;
2486 break;
2487 }
2488
2489 instr++;
2490 ninstrs++;
2491 instr_offset = 0;
2492 }
2493
2494 /*
2495 * This can happen if all instructions of a pattern are optional.
2496 * Still, if there's not at least one instruction handled by this
2497 * pattern, this is not a match, and we should try the next one (if
2498 * any).
2499 */
2500 if (!ninstrs)
2501 return false;
2502
2503 /*
2504 * We had a match on the pattern head, but the pattern may be longer
2505 * than the instructions we're asked to execute. We need to make sure
2506 * there's no mandatory elements in the pattern tail.
2507 */
2508 for (; i < pat->nelems; i++) {
2509 if (!pat->elems[i].optional)
2510 return false;
2511 }
2512
2513 /*
2514 * We have a match: update the subop structure accordingly and return
2515 * true.
2516 */
2517 ctx->subop.ninstrs = ninstrs;
2518 ctx->subop.last_instr_end_off = instr_offset;
2519
2520 return true;
2521}
2522
2523#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2524static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2525{
2526 const struct nand_op_instr *instr;
2527 char *prefix = " ";
2528 unsigned int i;
2529
2530 pr_debug("executing subop:\n");
2531
2532 for (i = 0; i < ctx->ninstrs; i++) {
2533 instr = &ctx->instrs[i];
2534
2535 if (instr == &ctx->subop.instrs[0])
2536 prefix = " ->";
2537
2538 switch (instr->type) {
2539 case NAND_OP_CMD_INSTR:
2540 pr_debug("%sCMD [0x%02x]\n", prefix,
2541 instr->ctx.cmd.opcode);
2542 break;
2543 case NAND_OP_ADDR_INSTR:
2544 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2545 instr->ctx.addr.naddrs,
2546 instr->ctx.addr.naddrs < 64 ?
2547 instr->ctx.addr.naddrs : 64,
2548 instr->ctx.addr.addrs);
2549 break;
2550 case NAND_OP_DATA_IN_INSTR:
2551 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2552 instr->ctx.data.len,
2553 instr->ctx.data.force_8bit ?
2554 ", force 8-bit" : "");
2555 break;
2556 case NAND_OP_DATA_OUT_INSTR:
2557 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2558 instr->ctx.data.len,
2559 instr->ctx.data.force_8bit ?
2560 ", force 8-bit" : "");
2561 break;
2562 case NAND_OP_WAITRDY_INSTR:
2563 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2564 instr->ctx.waitrdy.timeout_ms);
2565 break;
2566 }
2567
2568 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2569 prefix = " ";
2570 }
2571}
2572#else
2573static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2574{
2575 /* NOP */
2576}
2577#endif
2578
2579/**
2580 * nand_op_parser_exec_op - exec_op parser
2581 * @chip: the NAND chip
2582 * @parser: patterns description provided by the controller driver
2583 * @op: the NAND operation to address
2584 * @check_only: when true, the function only checks if @op can be handled but
2585 * does not execute the operation
2586 *
2587 * Helper function designed to ease integration of NAND controller drivers that
2588 * only support a limited set of instruction sequences. The supported sequences
2589 * are described in @parser, and the framework takes care of splitting @op into
2590 * multiple sub-operations (if required) and pass them back to the ->exec()
2591 * callback of the matching pattern if @check_only is set to false.
2592 *
2593 * NAND controller drivers should call this function from their own ->exec_op()
2594 * implementation.
2595 *
2596 * Returns 0 on success, a negative error code otherwise. A failure can be
2597 * caused by an unsupported operation (none of the supported patterns is able
2598 * to handle the requested operation), or an error returned by one of the
2599 * matching pattern->exec() hook.
2600 */
2601int nand_op_parser_exec_op(struct nand_chip *chip,
2602 const struct nand_op_parser *parser,
2603 const struct nand_operation *op, bool check_only)
2604{
2605 struct nand_op_parser_ctx ctx = {
2606 .subop.instrs = op->instrs,
2607 .instrs = op->instrs,
2608 .ninstrs = op->ninstrs,
2609 };
2610 unsigned int i;
2611
2612 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2613 int ret;
2614
2615 for (i = 0; i < parser->npatterns; i++) {
2616 const struct nand_op_parser_pattern *pattern;
2617
2618 pattern = &parser->patterns[i];
2619 if (!nand_op_parser_match_pat(pattern, &ctx))
2620 continue;
2621
2622 nand_op_parser_trace(&ctx);
2623
2624 if (check_only)
2625 break;
2626
2627 ret = pattern->exec(chip, &ctx.subop);
2628 if (ret)
2629 return ret;
2630
2631 break;
2632 }
2633
2634 if (i == parser->npatterns) {
2635 pr_debug("->exec_op() parser: pattern not found!\n");
2636 return -ENOTSUPP;
2637 }
2638
2639 /*
2640 * Update the context structure by pointing to the start of the
2641 * next subop.
2642 */
2643 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2644 if (ctx.subop.last_instr_end_off)
2645 ctx.subop.instrs -= 1;
2646
2647 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2648 }
2649
2650 return 0;
2651}
2652EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2653
2654static bool nand_instr_is_data(const struct nand_op_instr *instr)
2655{
2656 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2657 instr->type == NAND_OP_DATA_OUT_INSTR);
2658}
2659
2660static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2661 unsigned int instr_idx)
2662{
2663 return subop && instr_idx < subop->ninstrs;
2664}
2665
2666static int nand_subop_get_start_off(const struct nand_subop *subop,
2667 unsigned int instr_idx)
2668{
2669 if (instr_idx)
2670 return 0;
2671
2672 return subop->first_instr_start_off;
2673}
2674
2675/**
2676 * nand_subop_get_addr_start_off - Get the start offset in an address array
2677 * @subop: The entire sub-operation
2678 * @instr_idx: Index of the instruction inside the sub-operation
2679 *
2680 * During driver development, one could be tempted to directly use the
2681 * ->addr.addrs field of address instructions. This is wrong as address
2682 * instructions might be split.
2683 *
2684 * Given an address instruction, returns the offset of the first cycle to issue.
2685 */
2686int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2687 unsigned int instr_idx)
2688{
2689 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2690 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2691 return -EINVAL;
2692
2693 return nand_subop_get_start_off(subop, instr_idx);
2694}
2695EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2696
2697/**
2698 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2699 * @subop: The entire sub-operation
2700 * @instr_idx: Index of the instruction inside the sub-operation
2701 *
2702 * During driver development, one could be tempted to directly use the
2703 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2704 * might be split.
2705 *
2706 * Given an address instruction, returns the number of address cycle to issue.
2707 */
2708int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2709 unsigned int instr_idx)
2710{
2711 int start_off, end_off;
2712
2713 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2714 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2715 return -EINVAL;
2716
2717 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2718
2719 if (instr_idx == subop->ninstrs - 1 &&
2720 subop->last_instr_end_off)
2721 end_off = subop->last_instr_end_off;
2722 else
2723 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2724
2725 return end_off - start_off;
2726}
2727EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2728
2729/**
2730 * nand_subop_get_data_start_off - Get the start offset in a data array
2731 * @subop: The entire sub-operation
2732 * @instr_idx: Index of the instruction inside the sub-operation
2733 *
2734 * During driver development, one could be tempted to directly use the
2735 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2736 * instructions might be split.
2737 *
2738 * Given a data instruction, returns the offset to start from.
2739 */
2740int nand_subop_get_data_start_off(const struct nand_subop *subop,
2741 unsigned int instr_idx)
2742{
2743 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2744 !nand_instr_is_data(&subop->instrs[instr_idx]))
2745 return -EINVAL;
2746
2747 return nand_subop_get_start_off(subop, instr_idx);
2748}
2749EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2750
2751/**
2752 * nand_subop_get_data_len - Get the number of bytes to retrieve
2753 * @subop: The entire sub-operation
2754 * @instr_idx: Index of the instruction inside the sub-operation
2755 *
2756 * During driver development, one could be tempted to directly use the
2757 * ->data->len field of a data instruction. This is wrong as data instructions
2758 * might be split.
2759 *
2760 * Returns the length of the chunk of data to send/receive.
2761 */
2762int nand_subop_get_data_len(const struct nand_subop *subop,
2763 unsigned int instr_idx)
2764{
2765 int start_off = 0, end_off;
2766
2767 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2768 !nand_instr_is_data(&subop->instrs[instr_idx]))
2769 return -EINVAL;
2770
2771 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2772
2773 if (instr_idx == subop->ninstrs - 1 &&
2774 subop->last_instr_end_off)
2775 end_off = subop->last_instr_end_off;
2776 else
2777 end_off = subop->instrs[instr_idx].ctx.data.len;
2778
2779 return end_off - start_off;
2780}
2781EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002784 * nand_reset - Reset and initialize a NAND device
2785 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002786 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002787 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002788 * Save the timings data structure, then apply SDR timings mode 0 (see
2789 * nand_reset_data_interface for details), do the reset operation, and
2790 * apply back the previous timings.
2791 *
2792 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002793 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002794int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002795{
2796 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal17fa8042017-11-30 18:01:31 +01002797 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002798 int ret;
2799
Boris Brezillon104e4422017-03-16 09:35:58 +01002800 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002801 if (ret)
2802 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002803
Boris Brezillon73f907f2016-10-24 16:46:20 +02002804 /*
2805 * The CS line has to be released before we can apply the new NAND
2806 * interface settings, hence this weird ->select_chip() dance.
2807 */
2808 chip->select_chip(mtd, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002809 ret = nand_reset_op(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02002810 chip->select_chip(mtd, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002811 if (ret)
2812 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002813
Miquel Raynal107b7d62018-03-19 14:47:25 +01002814 /*
2815 * A nand_reset_data_interface() put both the NAND chip and the NAND
2816 * controller in timings mode 0. If the default mode for this chip is
2817 * also 0, no need to proceed to the change again. Plus, at probe time,
2818 * nand_setup_data_interface() uses ->set/get_features() which would
2819 * fail anyway as the parameter page is not available yet.
2820 */
2821 if (!chip->onfi_timing_mode_default)
2822 return 0;
2823
Miquel Raynal17fa8042017-11-30 18:01:31 +01002824 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002825 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002826 if (ret)
2827 return ret;
2828
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002829 return 0;
2830}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002831EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002832
2833/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002834 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2835 * @buf: buffer to test
2836 * @len: buffer length
2837 * @bitflips_threshold: maximum number of bitflips
2838 *
2839 * Check if a buffer contains only 0xff, which means the underlying region
2840 * has been erased and is ready to be programmed.
2841 * The bitflips_threshold specify the maximum number of bitflips before
2842 * considering the region is not erased.
2843 * Note: The logic of this function has been extracted from the memweight
2844 * implementation, except that nand_check_erased_buf function exit before
2845 * testing the whole buffer if the number of bitflips exceed the
2846 * bitflips_threshold value.
2847 *
2848 * Returns a positive number of bitflips less than or equal to
2849 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2850 * threshold.
2851 */
2852static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2853{
2854 const unsigned char *bitmap = buf;
2855 int bitflips = 0;
2856 int weight;
2857
2858 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2859 len--, bitmap++) {
2860 weight = hweight8(*bitmap);
2861 bitflips += BITS_PER_BYTE - weight;
2862 if (unlikely(bitflips > bitflips_threshold))
2863 return -EBADMSG;
2864 }
2865
2866 for (; len >= sizeof(long);
2867 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002868 unsigned long d = *((unsigned long *)bitmap);
2869 if (d == ~0UL)
2870 continue;
2871 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002872 bitflips += BITS_PER_LONG - weight;
2873 if (unlikely(bitflips > bitflips_threshold))
2874 return -EBADMSG;
2875 }
2876
2877 for (; len > 0; len--, bitmap++) {
2878 weight = hweight8(*bitmap);
2879 bitflips += BITS_PER_BYTE - weight;
2880 if (unlikely(bitflips > bitflips_threshold))
2881 return -EBADMSG;
2882 }
2883
2884 return bitflips;
2885}
2886
2887/**
2888 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2889 * 0xff data
2890 * @data: data buffer to test
2891 * @datalen: data length
2892 * @ecc: ECC buffer
2893 * @ecclen: ECC length
2894 * @extraoob: extra OOB buffer
2895 * @extraooblen: extra OOB length
2896 * @bitflips_threshold: maximum number of bitflips
2897 *
2898 * Check if a data buffer and its associated ECC and OOB data contains only
2899 * 0xff pattern, which means the underlying region has been erased and is
2900 * ready to be programmed.
2901 * The bitflips_threshold specify the maximum number of bitflips before
2902 * considering the region as not erased.
2903 *
2904 * Note:
2905 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2906 * different from the NAND page size. When fixing bitflips, ECC engines will
2907 * report the number of errors per chunk, and the NAND core infrastructure
2908 * expect you to return the maximum number of bitflips for the whole page.
2909 * This is why you should always use this function on a single chunk and
2910 * not on the whole page. After checking each chunk you should update your
2911 * max_bitflips value accordingly.
2912 * 2/ When checking for bitflips in erased pages you should not only check
2913 * the payload data but also their associated ECC data, because a user might
2914 * have programmed almost all bits to 1 but a few. In this case, we
2915 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2916 * this case.
2917 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2918 * data are protected by the ECC engine.
2919 * It could also be used if you support subpages and want to attach some
2920 * extra OOB data to an ECC chunk.
2921 *
2922 * Returns a positive number of bitflips less than or equal to
2923 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2924 * threshold. In case of success, the passed buffers are filled with 0xff.
2925 */
2926int nand_check_erased_ecc_chunk(void *data, int datalen,
2927 void *ecc, int ecclen,
2928 void *extraoob, int extraooblen,
2929 int bitflips_threshold)
2930{
2931 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2932
2933 data_bitflips = nand_check_erased_buf(data, datalen,
2934 bitflips_threshold);
2935 if (data_bitflips < 0)
2936 return data_bitflips;
2937
2938 bitflips_threshold -= data_bitflips;
2939
2940 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2941 if (ecc_bitflips < 0)
2942 return ecc_bitflips;
2943
2944 bitflips_threshold -= ecc_bitflips;
2945
2946 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2947 bitflips_threshold);
2948 if (extraoob_bitflips < 0)
2949 return extraoob_bitflips;
2950
2951 if (data_bitflips)
2952 memset(data, 0xff, datalen);
2953
2954 if (ecc_bitflips)
2955 memset(ecc, 0xff, ecclen);
2956
2957 if (extraoob_bitflips)
2958 memset(extraoob, 0xff, extraooblen);
2959
2960 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2961}
2962EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2963
2964/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002965 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002966 * @mtd: mtd info structure
2967 * @chip: nand chip info structure
2968 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002969 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002970 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002971 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002972 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002973 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002974int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2975 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002976{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002977 int ret;
2978
Boris Brezillon25f815f2017-11-30 18:01:30 +01002979 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002980 if (ret)
2981 return ret;
2982
2983 if (oob_required) {
2984 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2985 false);
2986 if (ret)
2987 return ret;
2988 }
2989
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002990 return 0;
2991}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002992EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002993
2994/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002995 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002996 * @mtd: mtd info structure
2997 * @chip: nand chip info structure
2998 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002999 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003000 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003001 *
3002 * We need a special oob layout and handling even when OOB isn't used.
3003 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003004static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003005 struct nand_chip *chip, uint8_t *buf,
3006 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003007{
3008 int eccsize = chip->ecc.size;
3009 int eccbytes = chip->ecc.bytes;
3010 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003011 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003012
Boris Brezillon25f815f2017-11-30 18:01:30 +01003013 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3014 if (ret)
3015 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003016
3017 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003018 ret = nand_read_data_op(chip, buf, eccsize, false);
3019 if (ret)
3020 return ret;
3021
David Brownell52ff49d2009-03-04 12:01:36 -08003022 buf += eccsize;
3023
3024 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003025 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3026 false);
3027 if (ret)
3028 return ret;
3029
David Brownell52ff49d2009-03-04 12:01:36 -08003030 oob += chip->ecc.prepad;
3031 }
3032
Boris Brezillon97d90da2017-11-30 18:01:29 +01003033 ret = nand_read_data_op(chip, oob, eccbytes, false);
3034 if (ret)
3035 return ret;
3036
David Brownell52ff49d2009-03-04 12:01:36 -08003037 oob += eccbytes;
3038
3039 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003040 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3041 false);
3042 if (ret)
3043 return ret;
3044
David Brownell52ff49d2009-03-04 12:01:36 -08003045 oob += chip->ecc.postpad;
3046 }
3047 }
3048
3049 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003050 if (size) {
3051 ret = nand_read_data_op(chip, oob, size, false);
3052 if (ret)
3053 return ret;
3054 }
David Brownell52ff49d2009-03-04 12:01:36 -08003055
3056 return 0;
3057}
3058
3059/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003060 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003061 * @mtd: mtd info structure
3062 * @chip: nand chip info structure
3063 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003064 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003065 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003066 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003067static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003068 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Boris Brezillon846031d2016-02-03 20:11:00 +01003070 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003071 int eccbytes = chip->ecc.bytes;
3072 int eccsteps = chip->ecc.steps;
3073 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003074 uint8_t *ecc_calc = chip->ecc.calc_buf;
3075 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003076 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003077
Brian Norris1fbb9382012-05-02 10:14:55 -07003078 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003079
3080 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3081 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3082
Boris Brezillon846031d2016-02-03 20:11:00 +01003083 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3084 chip->ecc.total);
3085 if (ret)
3086 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003087
3088 eccsteps = chip->ecc.steps;
3089 p = buf;
3090
3091 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3092 int stat;
3093
3094 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003095 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003096 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003097 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003098 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003099 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3100 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003101 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003102 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003103}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303106 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003107 * @mtd: mtd info structure
3108 * @chip: nand chip info structure
3109 * @data_offs: offset of requested data within the page
3110 * @readlen: data length
3111 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003112 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003113 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003114static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003115 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3116 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003117{
Boris Brezillon846031d2016-02-03 20:11:00 +01003118 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003119 uint8_t *p;
3120 int data_col_addr, i, gaps = 0;
3121 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3122 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003123 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003124 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003125 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003126
Brian Norris7854d3f2011-06-23 14:12:08 -07003127 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003128 start_step = data_offs / chip->ecc.size;
3129 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3130 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303131 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003132
Brian Norris8b6e50c2011-05-25 14:59:01 -07003133 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003134 datafrag_len = num_steps * chip->ecc.size;
3135 eccfrag_len = num_steps * chip->ecc.bytes;
3136
3137 data_col_addr = start_step * chip->ecc.size;
3138 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003139 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003140 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003141 if (ret)
3142 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003143
Brian Norris8b6e50c2011-05-25 14:59:01 -07003144 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003145 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003146 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003147
Brian Norris8b6e50c2011-05-25 14:59:01 -07003148 /*
3149 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003150 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003151 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003152 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3153 if (ret)
3154 return ret;
3155
3156 if (oobregion.length < eccfrag_len)
3157 gaps = 1;
3158
Alexey Korolev3d459552008-05-15 17:23:18 +01003159 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003160 ret = nand_change_read_column_op(chip, mtd->writesize,
3161 chip->oob_poi, mtd->oobsize,
3162 false);
3163 if (ret)
3164 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003165 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003166 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003167 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003168 * about buswidth alignment in read_buf.
3169 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003170 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003171 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003172 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003173 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003174 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3175 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003176 aligned_len++;
3177
Boris Brezillon97d90da2017-11-30 18:01:29 +01003178 ret = nand_change_read_column_op(chip,
3179 mtd->writesize + aligned_pos,
3180 &chip->oob_poi[aligned_pos],
3181 aligned_len, false);
3182 if (ret)
3183 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003184 }
3185
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003186 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003187 chip->oob_poi, index, eccfrag_len);
3188 if (ret)
3189 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003190
3191 p = bufpoi + data_col_addr;
3192 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3193 int stat;
3194
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003195 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3196 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003197 if (stat == -EBADMSG &&
3198 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3199 /* check for empty pages with bitflips */
3200 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003201 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003202 chip->ecc.bytes,
3203 NULL, 0,
3204 chip->ecc.strength);
3205 }
3206
Mike Dunn3f91e942012-04-25 12:06:09 -07003207 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003208 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003209 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003210 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003211 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3212 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003213 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003214 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003215}
3216
3217/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003218 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003219 * @mtd: mtd info structure
3220 * @chip: nand chip info structure
3221 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003222 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003223 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003224 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003225 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003226 */
3227static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003228 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003229{
Boris Brezillon846031d2016-02-03 20:11:00 +01003230 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003231 int eccbytes = chip->ecc.bytes;
3232 int eccsteps = chip->ecc.steps;
3233 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003234 uint8_t *ecc_calc = chip->ecc.calc_buf;
3235 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003236 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003237
Boris Brezillon25f815f2017-11-30 18:01:30 +01003238 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3239 if (ret)
3240 return ret;
3241
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003242 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3243 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003244
3245 ret = nand_read_data_op(chip, p, eccsize, false);
3246 if (ret)
3247 return ret;
3248
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003249 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3250 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003251
3252 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3253 if (ret)
3254 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003255
Boris Brezillon846031d2016-02-03 20:11:00 +01003256 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3257 chip->ecc.total);
3258 if (ret)
3259 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003260
3261 eccsteps = chip->ecc.steps;
3262 p = buf;
3263
3264 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3265 int stat;
3266
3267 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003268 if (stat == -EBADMSG &&
3269 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3270 /* check for empty pages with bitflips */
3271 stat = nand_check_erased_ecc_chunk(p, eccsize,
3272 &ecc_code[i], eccbytes,
3273 NULL, 0,
3274 chip->ecc.strength);
3275 }
3276
Mike Dunn3f91e942012-04-25 12:06:09 -07003277 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003278 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003279 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003280 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003281 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3282 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003283 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003284 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003285}
3286
3287/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003288 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003289 * @mtd: mtd info structure
3290 * @chip: nand chip info structure
3291 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003292 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003293 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003294 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003295 * Hardware ECC for large page chips, require OOB to be read first. For this
3296 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3297 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3298 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3299 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003300 */
3301static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003302 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003303{
Boris Brezillon846031d2016-02-03 20:11:00 +01003304 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003305 int eccbytes = chip->ecc.bytes;
3306 int eccsteps = chip->ecc.steps;
3307 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003308 uint8_t *ecc_code = chip->ecc.code_buf;
3309 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003310 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003311
3312 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003313 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3314 if (ret)
3315 return ret;
3316
3317 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3318 if (ret)
3319 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003320
Boris Brezillon846031d2016-02-03 20:11:00 +01003321 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3322 chip->ecc.total);
3323 if (ret)
3324 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003325
3326 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3327 int stat;
3328
3329 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003330
3331 ret = nand_read_data_op(chip, p, eccsize, false);
3332 if (ret)
3333 return ret;
3334
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003335 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3336
3337 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003338 if (stat == -EBADMSG &&
3339 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3340 /* check for empty pages with bitflips */
3341 stat = nand_check_erased_ecc_chunk(p, eccsize,
3342 &ecc_code[i], eccbytes,
3343 NULL, 0,
3344 chip->ecc.strength);
3345 }
3346
Mike Dunn3f91e942012-04-25 12:06:09 -07003347 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003348 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003349 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003350 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003351 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3352 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003353 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003354 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003355}
3356
3357/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003358 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003359 * @mtd: mtd info structure
3360 * @chip: nand chip info structure
3361 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003362 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003363 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003364 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003365 * The hw generator calculates the error syndrome automatically. Therefore we
3366 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003367 */
3368static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003369 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003370{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003371 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003372 int eccbytes = chip->ecc.bytes;
3373 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003374 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003375 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003376 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003377 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003378
Boris Brezillon25f815f2017-11-30 18:01:30 +01003379 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3380 if (ret)
3381 return ret;
3382
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003383 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3384 int stat;
3385
3386 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003387
3388 ret = nand_read_data_op(chip, p, eccsize, false);
3389 if (ret)
3390 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003391
3392 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003393 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3394 false);
3395 if (ret)
3396 return ret;
3397
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003398 oob += chip->ecc.prepad;
3399 }
3400
3401 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003402
3403 ret = nand_read_data_op(chip, oob, eccbytes, false);
3404 if (ret)
3405 return ret;
3406
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003407 stat = chip->ecc.correct(mtd, p, oob, NULL);
3408
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003409 oob += eccbytes;
3410
3411 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003412 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3413 false);
3414 if (ret)
3415 return ret;
3416
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003417 oob += chip->ecc.postpad;
3418 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003419
3420 if (stat == -EBADMSG &&
3421 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3422 /* check for empty pages with bitflips */
3423 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3424 oob - eccpadbytes,
3425 eccpadbytes,
3426 NULL, 0,
3427 chip->ecc.strength);
3428 }
3429
3430 if (stat < 0) {
3431 mtd->ecc_stats.failed++;
3432 } else {
3433 mtd->ecc_stats.corrected += stat;
3434 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3435 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003436 }
3437
3438 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003439 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003440 if (i) {
3441 ret = nand_read_data_op(chip, oob, i, false);
3442 if (ret)
3443 return ret;
3444 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003445
Mike Dunn3f91e942012-04-25 12:06:09 -07003446 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003447}
3448
3449/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003450 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003451 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003452 * @oob: oob destination address
3453 * @ops: oob ops structure
3454 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003455 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003456static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003457 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003458{
Boris Brezillon846031d2016-02-03 20:11:00 +01003459 struct nand_chip *chip = mtd_to_nand(mtd);
3460 int ret;
3461
Florian Fainellif8ac0412010-09-07 13:23:43 +02003462 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003463
Brian Norris0612b9d2011-08-30 18:45:40 -07003464 case MTD_OPS_PLACE_OOB:
3465 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003466 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3467 return oob + len;
3468
Boris Brezillon846031d2016-02-03 20:11:00 +01003469 case MTD_OPS_AUTO_OOB:
3470 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3471 ops->ooboffs, len);
3472 BUG_ON(ret);
3473 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003474
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003475 default:
3476 BUG();
3477 }
3478 return NULL;
3479}
3480
3481/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003482 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3483 * @mtd: MTD device structure
3484 * @retry_mode: the retry mode to use
3485 *
3486 * Some vendors supply a special command to shift the Vt threshold, to be used
3487 * when there are too many bitflips in a page (i.e., ECC error). After setting
3488 * a new threshold, the host should retry reading the page.
3489 */
3490static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3491{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003492 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08003493
3494 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3495
3496 if (retry_mode >= chip->read_retries)
3497 return -EINVAL;
3498
3499 if (!chip->setup_read_retry)
3500 return -EOPNOTSUPP;
3501
3502 return chip->setup_read_retry(mtd, retry_mode);
3503}
3504
3505/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003506 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003507 * @mtd: MTD device structure
3508 * @from: offset to read from
3509 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003510 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003511 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003512 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003513static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3514 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003515{
Brian Norrise47f3db2012-05-02 10:14:56 -07003516 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003517 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003518 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003519 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003520 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003521 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003522
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003523 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003524 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003525 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003526 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003527 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003529 chipnr = (int)(from >> chip->chip_shift);
3530 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003532 realpage = (int)(from >> chip->page_shift);
3533 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003535 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003537 buf = ops->datbuf;
3538 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003539 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003540
Florian Fainellif8ac0412010-09-07 13:23:43 +02003541 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003542 unsigned int ecc_failures = mtd->ecc_stats.failed;
3543
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003544 bytes = min(mtd->writesize - col, readlen);
3545 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003546
Kamal Dasu66507c72014-05-01 20:51:19 -04003547 if (!aligned)
3548 use_bufpoi = 1;
3549 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003550 use_bufpoi = !virt_addr_valid(buf) ||
3551 !IS_ALIGNED((unsigned long)buf,
3552 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003553 else
3554 use_bufpoi = 0;
3555
Brian Norris8b6e50c2011-05-25 14:59:01 -07003556 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003557 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003558 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003559
3560 if (use_bufpoi && aligned)
3561 pr_debug("%s: using read bounce buffer for buf@%p\n",
3562 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Brian Norrisba84fb52014-01-03 15:13:33 -08003564read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003565 /*
3566 * Now read the page into the buffer. Absent an error,
3567 * the read methods return max bitflips per ecc step.
3568 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003569 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07003570 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003571 oob_required,
3572 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003573 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3574 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003575 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003576 col, bytes, bufpoi,
3577 page);
David Woodhouse956e9442006-09-25 17:12:39 +01003578 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07003579 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003580 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003581 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003582 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003583 /* Invalidate page cache */
3584 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003585 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003586 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003587
3588 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003589 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003590 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003591 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003592 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003593 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003594 chip->pagebuf_bitflips = ret;
3595 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003596 /* Invalidate page cache */
3597 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003598 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003599 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003601
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003602 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003603 int toread = min(oobreadlen, max_oobsize);
3604
3605 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003606 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003607 oob, ops, toread);
3608 oobreadlen -= toread;
3609 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003610 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003611
3612 if (chip->options & NAND_NEED_READRDY) {
3613 /* Apply delay or wait for ready/busy pin */
3614 if (!chip->dev_ready)
3615 udelay(chip->chip_delay);
3616 else
3617 nand_wait_ready(mtd);
3618 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08003619
Brian Norrisba84fb52014-01-03 15:13:33 -08003620 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003621 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003622 retry_mode++;
3623 ret = nand_setup_read_retry(mtd,
3624 retry_mode);
3625 if (ret < 0)
3626 break;
3627
3628 /* Reset failures; retry */
3629 mtd->ecc_stats.failed = ecc_failures;
3630 goto read_retry;
3631 } else {
3632 /* No more retry modes; real failure */
3633 ecc_fail = true;
3634 }
3635 }
3636
3637 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003638 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003639 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003640 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003641 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003642 max_bitflips = max_t(unsigned int, max_bitflips,
3643 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003646 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003647
Brian Norrisba84fb52014-01-03 15:13:33 -08003648 /* Reset to retry mode 0 */
3649 if (retry_mode) {
3650 ret = nand_setup_read_retry(mtd, 0);
3651 if (ret < 0)
3652 break;
3653 retry_mode = 0;
3654 }
3655
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003656 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003657 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
Brian Norris8b6e50c2011-05-25 14:59:01 -07003659 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 col = 0;
3661 /* Increment page address */
3662 realpage++;
3663
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003664 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 /* Check, if we cross a chip boundary */
3666 if (!page) {
3667 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003668 chip->select_chip(mtd, -1);
3669 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003672 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003674 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003675 if (oob)
3676 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Mike Dunn3f91e942012-04-25 12:06:09 -07003678 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003679 return ret;
3680
Brian Norrisb72f3df2013-12-03 11:04:14 -08003681 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003682 return -EBADMSG;
3683
Mike Dunnedbc45402012-04-25 12:06:11 -07003684 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003685}
3686
3687/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003688 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003689 * @mtd: mtd info structure
3690 * @chip: nand chip info structure
3691 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003692 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003693int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003694{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003695 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003696}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003697EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003698
3699/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003700 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003701 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003702 * @mtd: mtd info structure
3703 * @chip: nand chip info structure
3704 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003705 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003706int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3707 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003708{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003709 int length = mtd->oobsize;
3710 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3711 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003712 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003713 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003714
Boris Brezillon97d90da2017-11-30 18:01:29 +01003715 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3716 if (ret)
3717 return ret;
3718
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003719 for (i = 0; i < chip->ecc.steps; i++) {
3720 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003721 int ret;
3722
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003723 pos = eccsize + i * (eccsize + chunk);
3724 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003725 ret = nand_change_read_column_op(chip, pos,
3726 NULL, 0,
3727 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003728 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003729 ret = nand_read_page_op(chip, page, pos, NULL,
3730 0);
3731
3732 if (ret)
3733 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003734 } else
3735 sndrnd = 1;
3736 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003737
3738 ret = nand_read_data_op(chip, bufpoi, toread, false);
3739 if (ret)
3740 return ret;
3741
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003742 bufpoi += toread;
3743 length -= toread;
3744 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003745 if (length > 0) {
3746 ret = nand_read_data_op(chip, bufpoi, length, false);
3747 if (ret)
3748 return ret;
3749 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003750
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003751 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003752}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003753EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003754
3755/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003756 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003757 * @mtd: mtd info structure
3758 * @chip: nand chip info structure
3759 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003760 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003761int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003762{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003763 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3764 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003765}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003766EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003767
3768/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003769 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003770 * with syndrome - only for large page flash
3771 * @mtd: mtd info structure
3772 * @chip: nand chip info structure
3773 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003774 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003775int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3776 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003777{
3778 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3779 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003780 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003781 const uint8_t *bufpoi = chip->oob_poi;
3782
3783 /*
3784 * data-ecc-data-ecc ... ecc-oob
3785 * or
3786 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3787 */
3788 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3789 pos = steps * (eccsize + chunk);
3790 steps = 0;
3791 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003792 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003793
Boris Brezillon97d90da2017-11-30 18:01:29 +01003794 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3795 if (ret)
3796 return ret;
3797
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003798 for (i = 0; i < steps; i++) {
3799 if (sndcmd) {
3800 if (mtd->writesize <= 512) {
3801 uint32_t fill = 0xFFFFFFFF;
3802
3803 len = eccsize;
3804 while (len > 0) {
3805 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003806
3807 ret = nand_write_data_op(chip, &fill,
3808 num, false);
3809 if (ret)
3810 return ret;
3811
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003812 len -= num;
3813 }
3814 } else {
3815 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003816 ret = nand_change_write_column_op(chip, pos,
3817 NULL, 0,
3818 false);
3819 if (ret)
3820 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003821 }
3822 } else
3823 sndcmd = 1;
3824 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003825
3826 ret = nand_write_data_op(chip, bufpoi, len, false);
3827 if (ret)
3828 return ret;
3829
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003830 bufpoi += len;
3831 length -= len;
3832 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003833 if (length > 0) {
3834 ret = nand_write_data_op(chip, bufpoi, length, false);
3835 if (ret)
3836 return ret;
3837 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003838
Boris Brezillon97d90da2017-11-30 18:01:29 +01003839 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003840}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003841EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003842
3843/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003844 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003845 * @mtd: MTD device structure
3846 * @from: offset to read from
3847 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003849 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003851static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3852 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003854 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003855 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003856 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003857 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003858 int readlen = ops->ooblen;
3859 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003860 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003861 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
Brian Norris289c0522011-07-19 10:06:09 -07003863 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303864 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Brian Norris041e4572011-06-23 16:45:24 -07003866 stats = mtd->ecc_stats;
3867
Boris BREZILLON29f10582016-03-07 10:46:52 +01003868 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003869
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003870 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003871 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003873 /* Shift to get page */
3874 realpage = (int)(from >> chip->page_shift);
3875 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Florian Fainellif8ac0412010-09-07 13:23:43 +02003877 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003878 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003879 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003880 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003881 ret = chip->ecc.read_oob(mtd, chip, page);
3882
3883 if (ret < 0)
3884 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003885
3886 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003887 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003888
Brian Norris5bc7c332013-03-13 09:51:31 -07003889 if (chip->options & NAND_NEED_READRDY) {
3890 /* Apply delay or wait for ready/busy pin */
3891 if (!chip->dev_ready)
3892 udelay(chip->chip_delay);
3893 else
3894 nand_wait_ready(mtd);
3895 }
3896
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003897 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3898
Vitaly Wool70145682006-11-03 18:20:38 +03003899 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003900 if (!readlen)
3901 break;
3902
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003903 /* Increment page address */
3904 realpage++;
3905
3906 page = realpage & chip->pagemask;
3907 /* Check, if we cross a chip boundary */
3908 if (!page) {
3909 chipnr++;
3910 chip->select_chip(mtd, -1);
3911 chip->select_chip(mtd, chipnr);
3912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003914 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003916 ops->oobretlen = ops->ooblen - readlen;
3917
3918 if (ret < 0)
3919 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003920
3921 if (mtd->ecc_stats.failed - stats.failed)
3922 return -EBADMSG;
3923
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003924 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925}
3926
3927/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003928 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003929 * @mtd: MTD device structure
3930 * @from: offset to read from
3931 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003933 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003935static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3936 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003938 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003939
3940 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003942 if (ops->mode != MTD_OPS_PLACE_OOB &&
3943 ops->mode != MTD_OPS_AUTO_OOB &&
3944 ops->mode != MTD_OPS_RAW)
3945 return -ENOTSUPP;
3946
Huang Shijie6a8214a2012-11-19 14:43:30 +08003947 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003949 if (!ops->datbuf)
3950 ret = nand_do_read_oob(mtd, from, ops);
3951 else
3952 ret = nand_do_read_ops(mtd, from, ops);
3953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003955 return ret;
3956}
3957
3958
3959/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003960 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003961 * @mtd: mtd info structure
3962 * @chip: nand chip info structure
3963 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003964 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003965 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003966 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003967 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003968 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003969int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
3970 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003971{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003972 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003973
Boris Brezillon25f815f2017-11-30 18:01:30 +01003974 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003975 if (ret)
3976 return ret;
3977
3978 if (oob_required) {
3979 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3980 false);
3981 if (ret)
3982 return ret;
3983 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003984
Boris Brezillon25f815f2017-11-30 18:01:30 +01003985 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003987EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003989/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003990 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003991 * @mtd: mtd info structure
3992 * @chip: nand chip info structure
3993 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003994 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003995 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003996 *
3997 * We need a special oob layout and handling even when ECC isn't checked.
3998 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003999static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004000 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004001 const uint8_t *buf, int oob_required,
4002 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004003{
4004 int eccsize = chip->ecc.size;
4005 int eccbytes = chip->ecc.bytes;
4006 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004007 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004008
Boris Brezillon25f815f2017-11-30 18:01:30 +01004009 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4010 if (ret)
4011 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004012
4013 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004014 ret = nand_write_data_op(chip, buf, eccsize, false);
4015 if (ret)
4016 return ret;
4017
David Brownell52ff49d2009-03-04 12:01:36 -08004018 buf += eccsize;
4019
4020 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004021 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4022 false);
4023 if (ret)
4024 return ret;
4025
David Brownell52ff49d2009-03-04 12:01:36 -08004026 oob += chip->ecc.prepad;
4027 }
4028
Boris Brezillon97d90da2017-11-30 18:01:29 +01004029 ret = nand_write_data_op(chip, oob, eccbytes, false);
4030 if (ret)
4031 return ret;
4032
David Brownell52ff49d2009-03-04 12:01:36 -08004033 oob += eccbytes;
4034
4035 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004036 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4037 false);
4038 if (ret)
4039 return ret;
4040
David Brownell52ff49d2009-03-04 12:01:36 -08004041 oob += chip->ecc.postpad;
4042 }
4043 }
4044
4045 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004046 if (size) {
4047 ret = nand_write_data_op(chip, oob, size, false);
4048 if (ret)
4049 return ret;
4050 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004051
Boris Brezillon25f815f2017-11-30 18:01:30 +01004052 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004053}
4054/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004055 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004056 * @mtd: mtd info structure
4057 * @chip: nand chip info structure
4058 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004059 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004060 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004061 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004062static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004063 const uint8_t *buf, int oob_required,
4064 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004065{
Boris Brezillon846031d2016-02-03 20:11:00 +01004066 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004067 int eccbytes = chip->ecc.bytes;
4068 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004069 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004070 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004071
Brian Norris7854d3f2011-06-23 14:12:08 -07004072 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004073 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
4074 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004075
Boris Brezillon846031d2016-02-03 20:11:00 +01004076 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4077 chip->ecc.total);
4078 if (ret)
4079 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004080
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004081 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004082}
4083
4084/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004085 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004086 * @mtd: mtd info structure
4087 * @chip: nand chip info structure
4088 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004089 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004090 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004091 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004092static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004093 const uint8_t *buf, int oob_required,
4094 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004095{
Boris Brezillon846031d2016-02-03 20:11:00 +01004096 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004097 int eccbytes = chip->ecc.bytes;
4098 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004099 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004100 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004101
Boris Brezillon25f815f2017-11-30 18:01:30 +01004102 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4103 if (ret)
4104 return ret;
4105
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004106 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4107 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004108
4109 ret = nand_write_data_op(chip, p, eccsize, false);
4110 if (ret)
4111 return ret;
4112
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004113 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
4114 }
4115
Boris Brezillon846031d2016-02-03 20:11:00 +01004116 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4117 chip->ecc.total);
4118 if (ret)
4119 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004120
Boris Brezillon97d90da2017-11-30 18:01:29 +01004121 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4122 if (ret)
4123 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004124
Boris Brezillon25f815f2017-11-30 18:01:30 +01004125 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004126}
4127
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304128
4129/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004130 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304131 * @mtd: mtd info structure
4132 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004133 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304134 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004135 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304136 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004137 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304138 */
4139static int nand_write_subpage_hwecc(struct mtd_info *mtd,
4140 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07004141 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004142 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304143{
4144 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004145 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304146 int ecc_size = chip->ecc.size;
4147 int ecc_bytes = chip->ecc.bytes;
4148 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304149 uint32_t start_step = offset / ecc_size;
4150 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4151 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004152 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304153
Boris Brezillon25f815f2017-11-30 18:01:30 +01004154 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4155 if (ret)
4156 return ret;
4157
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304158 for (step = 0; step < ecc_steps; step++) {
4159 /* configure controller for WRITE access */
4160 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
4161
4162 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004163 ret = nand_write_data_op(chip, buf, ecc_size, false);
4164 if (ret)
4165 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304166
4167 /* mask ECC of un-touched subpages by padding 0xFF */
4168 if ((step < start_step) || (step > end_step))
4169 memset(ecc_calc, 0xff, ecc_bytes);
4170 else
Brian Norrisd6a950802013-08-08 17:16:36 -07004171 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304172
4173 /* mask OOB of un-touched subpages by padding 0xFF */
4174 /* if oob_required, preserve OOB metadata of written subpage */
4175 if (!oob_required || (step < start_step) || (step > end_step))
4176 memset(oob_buf, 0xff, oob_bytes);
4177
Brian Norrisd6a950802013-08-08 17:16:36 -07004178 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304179 ecc_calc += ecc_bytes;
4180 oob_buf += oob_bytes;
4181 }
4182
4183 /* copy calculated ECC for whole page to chip->buffer->oob */
4184 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004185 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004186 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4187 chip->ecc.total);
4188 if (ret)
4189 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304190
4191 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004192 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4193 if (ret)
4194 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304195
Boris Brezillon25f815f2017-11-30 18:01:30 +01004196 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304197}
4198
4199
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004200/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004201 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004202 * @mtd: mtd info structure
4203 * @chip: nand chip info structure
4204 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004205 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004206 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004207 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004208 * The hw generator calculates the error syndrome automatically. Therefore we
4209 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004210 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004211static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07004212 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004213 const uint8_t *buf, int oob_required,
4214 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004215{
4216 int i, eccsize = chip->ecc.size;
4217 int eccbytes = chip->ecc.bytes;
4218 int eccsteps = chip->ecc.steps;
4219 const uint8_t *p = buf;
4220 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004221 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004222
Boris Brezillon25f815f2017-11-30 18:01:30 +01004223 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4224 if (ret)
4225 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004226
4227 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004228 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004229
4230 ret = nand_write_data_op(chip, p, eccsize, false);
4231 if (ret)
4232 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004233
4234 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004235 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4236 false);
4237 if (ret)
4238 return ret;
4239
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004240 oob += chip->ecc.prepad;
4241 }
4242
4243 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004244
4245 ret = nand_write_data_op(chip, oob, eccbytes, false);
4246 if (ret)
4247 return ret;
4248
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004249 oob += eccbytes;
4250
4251 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004252 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4253 false);
4254 if (ret)
4255 return ret;
4256
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004257 oob += chip->ecc.postpad;
4258 }
4259 }
4260
4261 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004262 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004263 if (i) {
4264 ret = nand_write_data_op(chip, oob, i, false);
4265 if (ret)
4266 return ret;
4267 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004268
Boris Brezillon25f815f2017-11-30 18:01:30 +01004269 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004270}
4271
4272/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004273 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004274 * @mtd: MTD device structure
4275 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304276 * @offset: address offset within the page
4277 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004278 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004279 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004280 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004281 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004282 */
4283static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304284 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004285 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004286{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304287 int status, subpage;
4288
4289 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4290 chip->ecc.write_subpage)
4291 subpage = offset || (data_len < mtd->writesize);
4292 else
4293 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004294
David Woodhouse956e9442006-09-25 17:12:39 +01004295 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304296 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004297 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304298 else if (subpage)
4299 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004300 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004301 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004302 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
4303 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004304
4305 if (status < 0)
4306 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004307
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004308 return 0;
4309}
4310
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004311/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004312 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004313 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004314 * @oob: oob data buffer
4315 * @len: oob data write length
4316 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004317 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004318static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4319 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004320{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004321 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004322 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004323
4324 /*
4325 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4326 * data from a previous OOB read.
4327 */
4328 memset(chip->oob_poi, 0xff, mtd->oobsize);
4329
Florian Fainellif8ac0412010-09-07 13:23:43 +02004330 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004331
Brian Norris0612b9d2011-08-30 18:45:40 -07004332 case MTD_OPS_PLACE_OOB:
4333 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004334 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4335 return oob + len;
4336
Boris Brezillon846031d2016-02-03 20:11:00 +01004337 case MTD_OPS_AUTO_OOB:
4338 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4339 ops->ooboffs, len);
4340 BUG_ON(ret);
4341 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004342
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004343 default:
4344 BUG();
4345 }
4346 return NULL;
4347}
4348
Florian Fainellif8ac0412010-09-07 13:23:43 +02004349#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004350
4351/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004352 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004353 * @mtd: MTD device structure
4354 * @to: offset to write to
4355 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004356 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004357 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004358 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004359static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4360 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004361{
Corentin Labbe73600b62017-09-02 10:49:38 +02004362 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004363 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004364 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004365
4366 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004367 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004368
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004369 uint8_t *oob = ops->oobbuf;
4370 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304371 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004372 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004373
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004374 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004375 if (!writelen)
4376 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004377
Brian Norris8b6e50c2011-05-25 14:59:01 -07004378 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004379 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004380 pr_notice("%s: attempt to write non page aligned data\n",
4381 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004382 return -EINVAL;
4383 }
4384
Thomas Gleixner29072b92006-09-28 15:38:36 +02004385 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004386
Thomas Gleixner6a930962006-06-28 00:11:45 +02004387 chipnr = (int)(to >> chip->chip_shift);
4388 chip->select_chip(mtd, chipnr);
4389
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004390 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004391 if (nand_check_wp(mtd)) {
4392 ret = -EIO;
4393 goto err_out;
4394 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004395
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004396 realpage = (int)(to >> chip->page_shift);
4397 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004398
4399 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004400 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4401 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004402 chip->pagebuf = -1;
4403
Maxim Levitsky782ce792010-02-22 20:39:36 +02004404 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004405 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4406 ret = -EINVAL;
4407 goto err_out;
4408 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004409
Florian Fainellif8ac0412010-09-07 13:23:43 +02004410 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004411 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004412 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004413 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004414 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004415
Kamal Dasu66507c72014-05-01 20:51:19 -04004416 if (part_pagewr)
4417 use_bufpoi = 1;
4418 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004419 use_bufpoi = !virt_addr_valid(buf) ||
4420 !IS_ALIGNED((unsigned long)buf,
4421 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004422 else
4423 use_bufpoi = 0;
4424
4425 /* Partial page write?, or need to use bounce buffer */
4426 if (use_bufpoi) {
4427 pr_debug("%s: using write bounce buffer for buf@%p\n",
4428 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004429 if (part_pagewr)
4430 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004431 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004432 memset(chip->data_buf, 0xff, mtd->writesize);
4433 memcpy(&chip->data_buf[column], buf, bytes);
4434 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004435 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004436
Maxim Levitsky782ce792010-02-22 20:39:36 +02004437 if (unlikely(oob)) {
4438 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004439 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004440 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004441 } else {
4442 /* We still need to erase leftover OOB data */
4443 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004444 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004445
4446 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004447 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004448 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004449 if (ret)
4450 break;
4451
4452 writelen -= bytes;
4453 if (!writelen)
4454 break;
4455
Thomas Gleixner29072b92006-09-28 15:38:36 +02004456 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004457 buf += bytes;
4458 realpage++;
4459
4460 page = realpage & chip->pagemask;
4461 /* Check, if we cross a chip boundary */
4462 if (!page) {
4463 chipnr++;
4464 chip->select_chip(mtd, -1);
4465 chip->select_chip(mtd, chipnr);
4466 }
4467 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004468
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004469 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004470 if (unlikely(oob))
4471 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004472
4473err_out:
4474 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004475 return ret;
4476}
4477
4478/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004479 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004480 * @mtd: MTD device structure
4481 * @to: offset to write to
4482 * @len: number of bytes to write
4483 * @retlen: pointer to variable to store the number of written bytes
4484 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004485 *
4486 * NAND write with ECC. Used when performing writes in interrupt context, this
4487 * may for example be called by mtdoops when writing an oops while in panic.
4488 */
4489static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4490 size_t *retlen, const uint8_t *buf)
4491{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004492 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004493 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004494 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004495 int ret;
4496
Brian Norris8b6e50c2011-05-25 14:59:01 -07004497 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004498 panic_nand_get_device(chip, mtd, FL_WRITING);
4499
Brent Taylor30863e382017-10-30 22:32:45 -05004500 chip->select_chip(mtd, chipnr);
4501
4502 /* Wait for the device to get ready */
4503 panic_nand_wait(mtd, chip, 400);
4504
Brian Norris0ec56dc2015-02-28 02:02:30 -08004505 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004506 ops.len = len;
4507 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004508 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004509
Brian Norris4a89ff82011-08-30 18:45:45 -07004510 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004511
Brian Norris4a89ff82011-08-30 18:45:45 -07004512 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004513 return ret;
4514}
4515
4516/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004517 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004518 * @mtd: MTD device structure
4519 * @to: offset to write to
4520 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004521 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004522 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004523 */
4524static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4525 struct mtd_oob_ops *ops)
4526{
Adrian Hunter03736152007-01-31 17:58:29 +02004527 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004528 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Brian Norris289c0522011-07-19 10:06:09 -07004530 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304531 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Boris BREZILLON29f10582016-03-07 10:46:52 +01004533 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004534
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004536 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004537 pr_debug("%s: attempt to write past end of page\n",
4538 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 return -EINVAL;
4540 }
4541
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004542 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004543
4544 /*
4545 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4546 * of my DiskOnChip 2000 test units) will clear the whole data page too
4547 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4548 * it in the doc2000 driver in August 1999. dwmw2.
4549 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004550 nand_reset(chip, chipnr);
4551
4552 chip->select_chip(mtd, chipnr);
4553
4554 /* Shift to get page */
4555 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
4557 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004558 if (nand_check_wp(mtd)) {
4559 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004560 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004561 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004564 if (page == chip->pagebuf)
4565 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004567 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004568
Brian Norris0612b9d2011-08-30 18:45:40 -07004569 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07004570 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
4571 else
4572 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004573
Huang Shijieb0bb6902012-11-19 14:43:29 +08004574 chip->select_chip(mtd, -1);
4575
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004576 if (status)
4577 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Vitaly Wool70145682006-11-03 18:20:38 +03004579 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004581 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004582}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004584/**
4585 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004586 * @mtd: MTD device structure
4587 * @to: offset to write to
4588 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004589 */
4590static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4591 struct mtd_oob_ops *ops)
4592{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004593 int ret = -ENOTSUPP;
4594
4595 ops->retlen = 0;
4596
Huang Shijie6a8214a2012-11-19 14:43:30 +08004597 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004598
Florian Fainellif8ac0412010-09-07 13:23:43 +02004599 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004600 case MTD_OPS_PLACE_OOB:
4601 case MTD_OPS_AUTO_OOB:
4602 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004603 break;
4604
4605 default:
4606 goto out;
4607 }
4608
4609 if (!ops->datbuf)
4610 ret = nand_do_write_oob(mtd, to, ops);
4611 else
4612 ret = nand_do_write_ops(mtd, to, ops);
4613
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004614out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004615 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 return ret;
4617}
4618
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619/**
Brian Norris49c50b92014-05-06 16:02:19 -07004620 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004621 * @mtd: MTD device structure
4622 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 *
Brian Norris49c50b92014-05-06 16:02:19 -07004624 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 */
Brian Norris49c50b92014-05-06 16:02:19 -07004626static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004628 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004629 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004630
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004632 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004633
Boris Brezillon97d90da2017-11-30 18:01:29 +01004634 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635}
4636
4637/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004639 * @mtd: MTD device structure
4640 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004642 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004644static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
David Woodhousee0c7d762006-05-13 18:07:53 +01004646 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004648
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004650 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004651 * @mtd: MTD device structure
4652 * @instr: erase instruction
4653 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004655 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004657int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4658 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659{
Adrian Hunter69423d92008-12-10 13:37:21 +00004660 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004661 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00004662 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Brian Norris289c0522011-07-19 10:06:09 -07004664 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4665 __func__, (unsigned long long)instr->addr,
4666 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304668 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004672 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
4674 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004675 page = (int)(instr->addr >> chip->page_shift);
4676 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
4678 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004679 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004682 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 /* Check, if it is write protected */
4685 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004686 pr_debug("%s: device is write protected!\n",
4687 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004688 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 goto erase_exit;
4690 }
4691
4692 /* Loop through the pages */
4693 len = instr->len;
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004696 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004697 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304698 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004699 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4700 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004701 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 goto erase_exit;
4703 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004704
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004705 /*
4706 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004707 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004708 */
4709 if (page <= chip->pagebuf && chip->pagebuf <
4710 (page + pages_per_block))
4711 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
Brian Norris49c50b92014-05-06 16:02:19 -07004713 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
4715 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004716 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004717 pr_debug("%s: failed erase, page 0x%08x\n",
4718 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004719 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004720 instr->fail_addr =
4721 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 goto erase_exit;
4723 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004724
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004726 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 page += pages_per_block;
4728
4729 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004730 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004732 chip->select_chip(mtd, -1);
4733 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 }
4735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004737 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004738erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004741 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 nand_release_device(mtd);
4743
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 /* Return more or less happy */
4745 return ret;
4746}
4747
4748/**
4749 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004750 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004752 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004754static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755{
Brian Norris289c0522011-07-19 10:06:09 -07004756 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004759 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004761 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004765 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004766 * @mtd: MTD device structure
4767 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004769static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304771 struct nand_chip *chip = mtd_to_nand(mtd);
4772 int chipnr = (int)(offs >> chip->chip_shift);
4773 int ret;
4774
4775 /* Select the NAND device */
4776 nand_get_device(mtd, FL_READING);
4777 chip->select_chip(mtd, chipnr);
4778
4779 ret = nand_block_checkbad(mtd, offs, 0);
4780
4781 chip->select_chip(mtd, -1);
4782 nand_release_device(mtd);
4783
4784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785}
4786
4787/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004788 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004789 * @mtd: MTD device structure
4790 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004792static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 int ret;
4795
Florian Fainellif8ac0412010-09-07 13:23:43 +02004796 ret = nand_block_isbad(mtd, ofs);
4797 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004798 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 if (ret > 0)
4800 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004801 return ret;
4802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Brian Norris5a0edb22013-07-30 17:52:58 -07004804 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
4807/**
Zach Brown56718422017-01-10 13:30:20 -06004808 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4809 * @mtd: MTD device structure
4810 * @ofs: offset relative to mtd start
4811 * @len: length of mtd
4812 */
4813static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4814{
4815 struct nand_chip *chip = mtd_to_nand(mtd);
4816 u32 part_start_block;
4817 u32 part_end_block;
4818 u32 part_start_die;
4819 u32 part_end_die;
4820
4821 /*
4822 * max_bb_per_die and blocks_per_die used to determine
4823 * the maximum bad block count.
4824 */
4825 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4826 return -ENOTSUPP;
4827
4828 /* Get the start and end of the partition in erase blocks. */
4829 part_start_block = mtd_div_by_eb(ofs, mtd);
4830 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4831
4832 /* Get the start and end LUNs of the partition. */
4833 part_start_die = part_start_block / chip->blocks_per_die;
4834 part_end_die = part_end_block / chip->blocks_per_die;
4835
4836 /*
4837 * Look up the bad blocks per unit and multiply by the number of units
4838 * that the partition spans.
4839 */
4840 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4841}
4842
4843/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004844 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004845 * @mtd: MTD device structure
4846 * @chip: nand chip info structure
4847 * @addr: feature address.
4848 * @subfeature_param: the subfeature parameters, a four bytes array.
4849 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004850static int nand_default_set_features(struct mtd_info *mtd,
4851 struct nand_chip *chip, int addr,
4852 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004853{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004854 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004855}
4856
4857/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004858 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004859 * @mtd: MTD device structure
4860 * @chip: nand chip info structure
4861 * @addr: feature address.
4862 * @subfeature_param: the subfeature parameters, a four bytes array.
4863 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004864static int nand_default_get_features(struct mtd_info *mtd,
4865 struct nand_chip *chip, int addr,
4866 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004867{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004868 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004869}
4870
4871/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004872 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004873 * @mtd: MTD device structure
4874 * @chip: nand chip info structure
4875 * @addr: feature address.
4876 * @subfeature_param: the subfeature parameters, a four bytes array.
4877 *
4878 * Should be used by NAND controller drivers that do not support the SET/GET
4879 * FEATURES operations.
4880 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004881int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4882 int addr, u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004883{
4884 return -ENOTSUPP;
4885}
Miquel Raynalb9587582018-03-19 14:47:19 +01004886EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004887
4888/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004889 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004890 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004891 */
4892static int nand_suspend(struct mtd_info *mtd)
4893{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004894 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004895}
4896
4897/**
4898 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004899 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004900 */
4901static void nand_resume(struct mtd_info *mtd)
4902{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004903 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004904
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004905 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004906 nand_release_device(mtd);
4907 else
Brian Norrisd0370212011-07-19 10:06:08 -07004908 pr_err("%s called for a chip which is not in suspended state\n",
4909 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004910}
4911
Scott Branden72ea4032014-11-20 11:18:05 -08004912/**
4913 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4914 * prevent further operations
4915 * @mtd: MTD device structure
4916 */
4917static void nand_shutdown(struct mtd_info *mtd)
4918{
Brian Norris9ca641b2015-11-09 16:37:28 -08004919 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004920}
4921
Brian Norris8b6e50c2011-05-25 14:59:01 -07004922/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004923static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004924{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004925 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004928 if (!chip->chip_delay)
4929 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
4931 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004932 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004933 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
4935 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004936 if (chip->waitfunc == NULL)
4937 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004939 if (!chip->select_chip)
4940 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004941
Huang Shijie4204ccc2013-08-16 10:10:07 +08004942 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004943 if (!chip->set_features)
4944 chip->set_features = nand_default_set_features;
4945 if (!chip->get_features)
4946 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004947
Brian Norris68e80782013-07-18 01:17:02 -07004948 /* If called twice, pointers that depend on busw may need to be reset */
4949 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004950 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4951 if (!chip->read_word)
4952 chip->read_word = nand_read_word;
4953 if (!chip->block_bad)
4954 chip->block_bad = nand_block_bad;
4955 if (!chip->block_markbad)
4956 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004957 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004958 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004959 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4960 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004961 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004962 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004963 if (!chip->scan_bbt)
4964 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004965
4966 if (!chip->controller) {
4967 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02004968 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004969 }
4970
Masahiro Yamada477544c2017-03-30 17:15:05 +09004971 if (!chip->buf_align)
4972 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004973}
4974
Brian Norris8b6e50c2011-05-25 14:59:01 -07004975/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004976static void sanitize_string(uint8_t *s, size_t len)
4977{
4978 ssize_t i;
4979
Brian Norris8b6e50c2011-05-25 14:59:01 -07004980 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004981 s[len - 1] = 0;
4982
Brian Norris8b6e50c2011-05-25 14:59:01 -07004983 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004984 for (i = 0; i < len - 1; i++) {
4985 if (s[i] < ' ' || s[i] > 127)
4986 s[i] = '?';
4987 }
4988
Brian Norris8b6e50c2011-05-25 14:59:01 -07004989 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004990 strim(s);
4991}
4992
4993static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4994{
4995 int i;
4996 while (len--) {
4997 crc ^= *p++ << 8;
4998 for (i = 0; i < 8; i++)
4999 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
5000 }
5001
5002 return crc;
5003}
5004
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005005/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005006static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
5007 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005008{
5009 struct onfi_ext_param_page *ep;
5010 struct onfi_ext_section *s;
5011 struct onfi_ext_ecc_info *ecc;
5012 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005013 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005014 int len;
5015 int i;
5016
5017 len = le16_to_cpu(p->ext_param_page_length) * 16;
5018 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005019 if (!ep)
5020 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005021
5022 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005023 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5024 if (ret)
5025 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005026
5027 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005028 ret = nand_change_read_column_op(chip,
5029 sizeof(*p) * p->num_of_param_pages,
5030 ep, len, true);
5031 if (ret)
5032 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005033
Boris Brezillon97d90da2017-11-30 18:01:29 +01005034 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005035 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5036 != le16_to_cpu(ep->crc))) {
5037 pr_debug("fail in the CRC.\n");
5038 goto ext_out;
5039 }
5040
5041 /*
5042 * Check the signature.
5043 * Do not strictly follow the ONFI spec, maybe changed in future.
5044 */
5045 if (strncmp(ep->sig, "EPPS", 4)) {
5046 pr_debug("The signature is invalid.\n");
5047 goto ext_out;
5048 }
5049
5050 /* find the ECC section. */
5051 cursor = (uint8_t *)(ep + 1);
5052 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5053 s = ep->sections + i;
5054 if (s->type == ONFI_SECTION_TYPE_2)
5055 break;
5056 cursor += s->length * 16;
5057 }
5058 if (i == ONFI_EXT_SECTION_MAX) {
5059 pr_debug("We can not find the ECC section.\n");
5060 goto ext_out;
5061 }
5062
5063 /* get the info we want. */
5064 ecc = (struct onfi_ext_ecc_info *)cursor;
5065
Brian Norris4ae7d222013-09-16 18:20:21 -07005066 if (!ecc->codeword_size) {
5067 pr_debug("Invalid codeword size\n");
5068 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005069 }
5070
Brian Norris4ae7d222013-09-16 18:20:21 -07005071 chip->ecc_strength_ds = ecc->ecc_bits;
5072 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005073 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005074
5075ext_out:
5076 kfree(ep);
5077 return ret;
5078}
5079
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005080/*
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005081 * Recover data with bit-wise majority
5082 */
5083static void nand_bit_wise_majority(const void **srcbufs,
5084 unsigned int nsrcbufs,
5085 void *dstbuf,
5086 unsigned int bufsize)
5087{
5088 int i, j, k;
5089
5090 for (i = 0; i < bufsize; i++) {
5091 u8 val = 0;
5092
5093 for (j = 0; j < 8; j++) {
5094 unsigned int cnt = 0;
5095
5096 for (k = 0; k < nsrcbufs; k++) {
5097 const u8 *srcbuf = srcbufs[k];
5098
5099 if (srcbuf[i] & BIT(j))
5100 cnt++;
5101 }
5102
5103 if (cnt > nsrcbufs / 2)
5104 val |= BIT(j);
5105 }
5106
5107 ((u8 *)dstbuf)[i] = val;
5108 }
5109}
5110
5111/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005112 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005113 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005114static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005115{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005116 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005117 struct nand_onfi_params *p;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005118 char id[4];
5119 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005120
Brian Norris7854d3f2011-06-23 14:12:08 -07005121 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005122 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5123 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005124 return 0;
5125
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005126 /* ONFI chip: allocate a buffer to hold its parameter page */
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005127 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005128 if (!p)
5129 return -ENOMEM;
5130
Boris Brezillon97d90da2017-11-30 18:01:29 +01005131 ret = nand_read_param_page_op(chip, 0, NULL, 0);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005132 if (ret) {
5133 ret = 0;
5134 goto free_onfi_param_page;
5135 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005136
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005137 for (i = 0; i < 3; i++) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005138 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005139 if (ret) {
5140 ret = 0;
5141 goto free_onfi_param_page;
5142 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005143
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005144 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005145 le16_to_cpu(p->crc)) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005146 if (i)
5147 memcpy(p, &p[i], sizeof(*p));
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005148 break;
5149 }
5150 }
5151
Brian Norrisc7f23a72013-08-13 10:51:55 -07005152 if (i == 3) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005153 const void *srcbufs[3] = {p, p + 1, p + 2};
5154
5155 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5156 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5157 sizeof(*p));
5158
5159 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5160 le16_to_cpu(p->crc)) {
5161 pr_err("ONFI parameter recovery failed, aborting\n");
5162 goto free_onfi_param_page;
5163 }
Brian Norrisc7f23a72013-08-13 10:51:55 -07005164 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005165
Brian Norris8b6e50c2011-05-25 14:59:01 -07005166 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005167 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005168 if (val & (1 << 5))
Miquel Raynala97421c2018-03-19 14:47:27 +01005169 chip->parameters.onfi.version = 23;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005170 else if (val & (1 << 4))
Miquel Raynala97421c2018-03-19 14:47:27 +01005171 chip->parameters.onfi.version = 22;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005172 else if (val & (1 << 3))
Miquel Raynala97421c2018-03-19 14:47:27 +01005173 chip->parameters.onfi.version = 21;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005174 else if (val & (1 << 2))
Miquel Raynala97421c2018-03-19 14:47:27 +01005175 chip->parameters.onfi.version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005176 else if (val & (1 << 1))
Miquel Raynala97421c2018-03-19 14:47:27 +01005177 chip->parameters.onfi.version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005178
Miquel Raynala97421c2018-03-19 14:47:27 +01005179 if (!chip->parameters.onfi.version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005180 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005181 goto free_onfi_param_page;
5182 } else {
5183 ret = 1;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005184 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005185
5186 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5187 sanitize_string(p->model, sizeof(p->model));
Miquel Raynalf4531b22018-03-19 14:47:26 +01005188 strncpy(chip->parameters.model, p->model,
5189 sizeof(chip->parameters.model) - 1);
Brian Norris4355b702013-08-27 18:45:10 -07005190
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005191 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005192
5193 /*
5194 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5195 * (don't ask me who thought of this...). MTD assumes that these
5196 * dimensions will be power-of-2, so just truncate the remaining area.
5197 */
5198 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5199 mtd->erasesize *= mtd->writesize;
5200
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005201 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005202
5203 /* See erasesize comment */
5204 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005205 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005206 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005207
Zach Brown34da5f52017-01-10 13:30:21 -06005208 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5209 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5210
Miquel Raynala97421c2018-03-19 14:47:27 +01005211 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005212 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005213
Huang Shijie10c86ba2013-05-17 11:17:26 +08005214 if (p->ecc_bits != 0xff) {
5215 chip->ecc_strength_ds = p->ecc_bits;
5216 chip->ecc_step_ds = 512;
Miquel Raynala97421c2018-03-19 14:47:27 +01005217 } else if (chip->parameters.onfi.version >= 21 &&
5218 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005219
5220 /*
5221 * The nand_flash_detect_ext_param_page() uses the
5222 * Change Read Column command which maybe not supported
5223 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5224 * now. We do not replace user supplied command function.
5225 */
5226 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5227 chip->cmdfunc = nand_command_lp;
5228
5229 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005230 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005231 pr_warn("Failed to detect ONFI extended param page\n");
5232 } else {
5233 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005234 }
5235
Miquel Raynalf4531b22018-03-19 14:47:26 +01005236 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005237 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005238 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005239 bitmap_set(chip->parameters.get_feature_list,
5240 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5241 bitmap_set(chip->parameters.set_feature_list,
5242 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5243 }
Miquel Raynala97421c2018-03-19 14:47:27 +01005244 chip->parameters.onfi.tPROG = le16_to_cpu(p->t_prog);
5245 chip->parameters.onfi.tBERS = le16_to_cpu(p->t_bers);
5246 chip->parameters.onfi.tR = le16_to_cpu(p->t_r);
5247 chip->parameters.onfi.tCCS = le16_to_cpu(p->t_ccs);
5248 chip->parameters.onfi.async_timing_mode =
5249 le16_to_cpu(p->async_timing_mode);
5250 chip->parameters.onfi.vendor_revision =
5251 le16_to_cpu(p->vendor_revision);
5252 memcpy(chip->parameters.onfi.vendor, p->vendor,
5253 sizeof(p->vendor));
Miquel Raynalf4531b22018-03-19 14:47:26 +01005254
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005255free_onfi_param_page:
5256 kfree(p);
5257 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005258}
5259
5260/*
Huang Shijie91361812014-02-21 13:39:40 +08005261 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5262 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005263static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005264{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005265 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005266 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005267 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005268 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005269 char id[5];
5270 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005271
5272 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005273 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5274 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005275 return 0;
5276
Miquel Raynal480139d2018-03-19 14:47:30 +01005277 /* JEDEC chip: allocate a buffer to hold its parameter page */
5278 p = kzalloc(sizeof(*p), GFP_KERNEL);
5279 if (!p)
5280 return -ENOMEM;
5281
Boris Brezillon97d90da2017-11-30 18:01:29 +01005282 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005283 if (ret) {
5284 ret = 0;
5285 goto free_jedec_param_page;
5286 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005287
Huang Shijie91361812014-02-21 13:39:40 +08005288 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005289 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005290 if (ret) {
5291 ret = 0;
5292 goto free_jedec_param_page;
5293 }
Huang Shijie91361812014-02-21 13:39:40 +08005294
5295 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5296 le16_to_cpu(p->crc))
5297 break;
5298 }
5299
5300 if (i == 3) {
5301 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005302 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005303 }
5304
5305 /* Check version */
5306 val = le16_to_cpu(p->revision);
5307 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005308 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005309 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005310 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005311
Miquel Raynal480139d2018-03-19 14:47:30 +01005312 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005313 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005314 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005315 }
5316
5317 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5318 sanitize_string(p->model, sizeof(p->model));
Miquel Raynalf4531b22018-03-19 14:47:26 +01005319 strncpy(chip->parameters.model, p->model,
5320 sizeof(chip->parameters.model) - 1);
Huang Shijie91361812014-02-21 13:39:40 +08005321
5322 mtd->writesize = le32_to_cpu(p->byte_per_page);
5323
5324 /* Please reference to the comment for nand_flash_detect_onfi. */
5325 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5326 mtd->erasesize *= mtd->writesize;
5327
5328 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5329
5330 /* Please reference to the comment for nand_flash_detect_onfi. */
5331 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5332 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5333 chip->bits_per_cell = p->bits_per_cell;
5334
Miquel Raynal480139d2018-03-19 14:47:30 +01005335 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005336 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005337
5338 /* ECC info */
5339 ecc = &p->ecc_info[0];
5340
5341 if (ecc->codeword_size >= 9) {
5342 chip->ecc_strength_ds = ecc->ecc_bits;
5343 chip->ecc_step_ds = 1 << ecc->codeword_size;
5344 } else {
5345 pr_warn("Invalid codeword size\n");
5346 }
5347
Miquel Raynal480139d2018-03-19 14:47:30 +01005348free_jedec_param_page:
5349 kfree(p);
5350 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005351}
5352
5353/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005354 * nand_id_has_period - Check if an ID string has a given wraparound period
5355 * @id_data: the ID string
5356 * @arrlen: the length of the @id_data array
5357 * @period: the period of repitition
5358 *
5359 * Check if an ID string is repeated within a given sequence of bytes at
5360 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005361 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005362 * if the repetition has a period of @period; otherwise, returns zero.
5363 */
5364static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5365{
5366 int i, j;
5367 for (i = 0; i < period; i++)
5368 for (j = i + period; j < arrlen; j += period)
5369 if (id_data[i] != id_data[j])
5370 return 0;
5371 return 1;
5372}
5373
5374/*
5375 * nand_id_len - Get the length of an ID string returned by CMD_READID
5376 * @id_data: the ID string
5377 * @arrlen: the length of the @id_data array
5378
5379 * Returns the length of the ID string, according to known wraparound/trailing
5380 * zero patterns. If no pattern exists, returns the length of the array.
5381 */
5382static int nand_id_len(u8 *id_data, int arrlen)
5383{
5384 int last_nonzero, period;
5385
5386 /* Find last non-zero byte */
5387 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5388 if (id_data[last_nonzero])
5389 break;
5390
5391 /* All zeros */
5392 if (last_nonzero < 0)
5393 return 0;
5394
5395 /* Calculate wraparound period */
5396 for (period = 1; period < arrlen; period++)
5397 if (nand_id_has_period(id_data, arrlen, period))
5398 break;
5399
5400 /* There's a repeated pattern */
5401 if (period < arrlen)
5402 return period;
5403
5404 /* There are trailing zeros */
5405 if (last_nonzero < arrlen - 1)
5406 return last_nonzero + 1;
5407
5408 /* No pattern detected */
5409 return arrlen;
5410}
5411
Huang Shijie7db906b2013-09-25 14:58:11 +08005412/* Extract the bits of per cell from the 3rd byte of the extended ID */
5413static int nand_get_bits_per_cell(u8 cellinfo)
5414{
5415 int bits;
5416
5417 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5418 bits >>= NAND_CI_CELLTYPE_SHIFT;
5419 return bits + 1;
5420}
5421
Brian Norrise3b88bd2012-09-24 20:40:52 -07005422/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005423 * Many new NAND share similar device ID codes, which represent the size of the
5424 * chip. The rest of the parameters must be decoded according to generic or
5425 * manufacturer-specific "extended ID" decoding patterns.
5426 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005427void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005428{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005429 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005430 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005431 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005432 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005433 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005434 /* The 4th id byte is the important one */
5435 extid = id_data[3];
5436
Boris Brezillon01389b62016-06-08 10:30:18 +02005437 /* Calc pagesize */
5438 mtd->writesize = 1024 << (extid & 0x03);
5439 extid >>= 2;
5440 /* Calc oobsize */
5441 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5442 extid >>= 2;
5443 /* Calc blocksize. Blocksize is multiples of 64KiB */
5444 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5445 extid >>= 2;
5446 /* Get buswidth information */
5447 if (extid & 0x1)
5448 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005449}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005450EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005451
5452/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005453 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5454 * decodes a matching ID table entry and assigns the MTD size parameters for
5455 * the chip.
5456 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005457static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005458{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005459 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005460
5461 mtd->erasesize = type->erasesize;
5462 mtd->writesize = type->pagesize;
5463 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005464
Huang Shijie1c195e92013-09-25 14:58:12 +08005465 /* All legacy ID NAND are small-page, SLC */
5466 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005467}
5468
5469/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005470 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5471 * heuristic patterns using various detected parameters (e.g., manufacturer,
5472 * page size, cell-type information).
5473 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005474static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005475{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005476 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005477
5478 /* Set the bad block position */
5479 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5480 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5481 else
5482 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005483}
5484
Huang Shijieec6e87e2013-03-15 11:01:00 +08005485static inline bool is_full_id_nand(struct nand_flash_dev *type)
5486{
5487 return type->id_len;
5488}
5489
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005490static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005491 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005492{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005493 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005494 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005495
Huang Shijieec6e87e2013-03-15 11:01:00 +08005496 if (!strncmp(type->id, id_data, type->id_len)) {
5497 mtd->writesize = type->pagesize;
5498 mtd->erasesize = type->erasesize;
5499 mtd->oobsize = type->oobsize;
5500
Huang Shijie7db906b2013-09-25 14:58:11 +08005501 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005502 chip->chipsize = (uint64_t)type->chipsize << 20;
5503 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005504 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5505 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005506 chip->onfi_timing_mode_default =
5507 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005508
Miquel Raynalf4531b22018-03-19 14:47:26 +01005509 strncpy(chip->parameters.model, type->name,
5510 sizeof(chip->parameters.model) - 1);
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005511
Huang Shijieec6e87e2013-03-15 11:01:00 +08005512 return true;
5513 }
5514 return false;
5515}
5516
Brian Norris7e74c2d2012-09-24 20:40:49 -07005517/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005518 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5519 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5520 * table.
5521 */
5522static void nand_manufacturer_detect(struct nand_chip *chip)
5523{
5524 /*
5525 * Try manufacturer detection if available and use
5526 * nand_decode_ext_id() otherwise.
5527 */
5528 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005529 chip->manufacturer.desc->ops->detect) {
5530 /* The 3rd id byte holds MLC / multichip data */
5531 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005532 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005533 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005534 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005535 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005536}
5537
5538/*
5539 * Manufacturer initialization. This function is called for all NANDs including
5540 * ONFI and JEDEC compliant ones.
5541 * Manufacturer drivers should put all their specific initialization code in
5542 * their ->init() hook.
5543 */
5544static int nand_manufacturer_init(struct nand_chip *chip)
5545{
5546 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5547 !chip->manufacturer.desc->ops->init)
5548 return 0;
5549
5550 return chip->manufacturer.desc->ops->init(chip);
5551}
5552
5553/*
5554 * Manufacturer cleanup. This function is called for all NANDs including
5555 * ONFI and JEDEC compliant ones.
5556 * Manufacturer drivers should put all their specific cleanup code in their
5557 * ->cleanup() hook.
5558 */
5559static void nand_manufacturer_cleanup(struct nand_chip *chip)
5560{
5561 /* Release manufacturer private data */
5562 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5563 chip->manufacturer.desc->ops->cleanup)
5564 chip->manufacturer.desc->ops->cleanup(chip);
5565}
5566
5567/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005568 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005569 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005570static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005571{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005572 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005573 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005574 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005575 u8 *id_data = chip->id.data;
5576 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577
Karl Beldanef89a882008-09-15 14:37:29 +02005578 /*
5579 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005580 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005581 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005582 ret = nand_reset(chip, 0);
5583 if (ret)
5584 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005585
5586 /* Select the device */
5587 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005588
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005590 ret = nand_readid_op(chip, 0, id_data, 2);
5591 if (ret)
5592 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
5594 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005595 maf_id = id_data[0];
5596 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Brian Norris8b6e50c2011-05-25 14:59:01 -07005598 /*
5599 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005600 * interface concerns can cause random data which looks like a
5601 * possibly credible NAND flash to appear. If the two results do
5602 * not match, ignore the device completely.
5603 */
5604
Brian Norris4aef9b72012-09-24 20:40:48 -07005605 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005606 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5607 if (ret)
5608 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005609
Boris Brezillon7f501f02016-05-24 19:20:05 +02005610 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005611 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005612 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005613 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005614 }
5615
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005616 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005617
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005618 /* Try to identify manufacturer */
5619 manufacturer = nand_get_manufacturer(maf_id);
5620 chip->manufacturer.desc = manufacturer;
5621
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005622 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005623 type = nand_flash_ids;
5624
Boris Brezillon29a198a2016-05-24 20:17:48 +02005625 /*
5626 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5627 * override it.
5628 * This is required to make sure initial NAND bus width set by the
5629 * NAND controller driver is coherent with the real NAND bus width
5630 * (extracted by auto-detection code).
5631 */
5632 busw = chip->options & NAND_BUSWIDTH_16;
5633
5634 /*
5635 * The flag is only set (never cleared), reset it to its default value
5636 * before starting auto-detection.
5637 */
5638 chip->options &= ~NAND_BUSWIDTH_16;
5639
Huang Shijieec6e87e2013-03-15 11:01:00 +08005640 for (; type->name != NULL; type++) {
5641 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005642 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005643 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005644 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005645 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005646 }
5647 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005648
Miquel Raynala97421c2018-03-19 14:47:27 +01005649 chip->parameters.onfi.version = 0;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005650 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005651 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005652 ret = nand_flash_detect_onfi(chip);
5653 if (ret < 0)
5654 return ret;
5655 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005656 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005657
5658 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005659 ret = nand_flash_detect_jedec(chip);
5660 if (ret < 0)
5661 return ret;
5662 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005663 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005664 }
5665
David Woodhouse5e81e882010-02-26 18:32:56 +00005666 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005667 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005668
Miquel Raynalf4531b22018-03-19 14:47:26 +01005669 strncpy(chip->parameters.model, type->name,
5670 sizeof(chip->parameters.model) - 1);
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005671
Adrian Hunter69423d92008-12-10 13:37:21 +00005672 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005673
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005674 if (!type->pagesize)
5675 nand_manufacturer_detect(chip);
5676 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005677 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005678
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005679 /* Get chip options */
5680 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005681
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005682ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005683 if (!mtd->name)
5684 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005685
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005686 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005687 WARN_ON(busw & NAND_BUSWIDTH_16);
5688 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005689 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5690 /*
5691 * Check, if buswidth is correct. Hardware drivers should set
5692 * chip correct!
5693 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005694 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005695 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005696 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5697 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005698 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5699 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005700 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005701 }
5702
Boris Brezillon7f501f02016-05-24 19:20:05 +02005703 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005704
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005705 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005706 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005707 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005708 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005709
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005710 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005711 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005712 if (chip->chipsize & 0xffffffff)
5713 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005714 else {
5715 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5716 chip->chip_shift += 32 - 1;
5717 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005718
Masahiro Yamada14157f82017-09-13 11:05:50 +09005719 if (chip->chip_shift - chip->page_shift > 16)
5720 chip->options |= NAND_ROW_ADDR_3;
5721
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005722 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005723 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005724
Brian Norris8b6e50c2011-05-25 14:59:01 -07005725 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005726 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5727 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005728
Ezequiel Garcia20171642013-11-25 08:30:31 -03005729 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005730 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005731 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5732 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005733 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005734 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005735 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005736 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005737}
5738
Boris Brezillond48f62b2016-04-01 14:54:32 +02005739static const char * const nand_ecc_modes[] = {
5740 [NAND_ECC_NONE] = "none",
5741 [NAND_ECC_SOFT] = "soft",
5742 [NAND_ECC_HW] = "hw",
5743 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5744 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005745 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005746};
5747
5748static int of_get_nand_ecc_mode(struct device_node *np)
5749{
5750 const char *pm;
5751 int err, i;
5752
5753 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5754 if (err < 0)
5755 return err;
5756
5757 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5758 if (!strcasecmp(pm, nand_ecc_modes[i]))
5759 return i;
5760
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005761 /*
5762 * For backward compatibility we support few obsoleted values that don't
5763 * have their mappings into nand_ecc_modes_t anymore (they were merged
5764 * with other enums).
5765 */
5766 if (!strcasecmp(pm, "soft_bch"))
5767 return NAND_ECC_SOFT;
5768
Boris Brezillond48f62b2016-04-01 14:54:32 +02005769 return -ENODEV;
5770}
5771
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005772static const char * const nand_ecc_algos[] = {
5773 [NAND_ECC_HAMMING] = "hamming",
5774 [NAND_ECC_BCH] = "bch",
5775};
5776
Boris Brezillond48f62b2016-04-01 14:54:32 +02005777static int of_get_nand_ecc_algo(struct device_node *np)
5778{
5779 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005780 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005781
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005782 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5783 if (!err) {
5784 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5785 if (!strcasecmp(pm, nand_ecc_algos[i]))
5786 return i;
5787 return -ENODEV;
5788 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005789
5790 /*
5791 * For backward compatibility we also read "nand-ecc-mode" checking
5792 * for some obsoleted values that were specifying ECC algorithm.
5793 */
5794 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5795 if (err < 0)
5796 return err;
5797
5798 if (!strcasecmp(pm, "soft"))
5799 return NAND_ECC_HAMMING;
5800 else if (!strcasecmp(pm, "soft_bch"))
5801 return NAND_ECC_BCH;
5802
5803 return -ENODEV;
5804}
5805
5806static int of_get_nand_ecc_step_size(struct device_node *np)
5807{
5808 int ret;
5809 u32 val;
5810
5811 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5812 return ret ? ret : val;
5813}
5814
5815static int of_get_nand_ecc_strength(struct device_node *np)
5816{
5817 int ret;
5818 u32 val;
5819
5820 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5821 return ret ? ret : val;
5822}
5823
5824static int of_get_nand_bus_width(struct device_node *np)
5825{
5826 u32 val;
5827
5828 if (of_property_read_u32(np, "nand-bus-width", &val))
5829 return 8;
5830
5831 switch (val) {
5832 case 8:
5833 case 16:
5834 return val;
5835 default:
5836 return -EIO;
5837 }
5838}
5839
5840static bool of_get_nand_on_flash_bbt(struct device_node *np)
5841{
5842 return of_property_read_bool(np, "nand-on-flash-bbt");
5843}
5844
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005845static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005846{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005847 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005848 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005849
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005850 if (!dn)
5851 return 0;
5852
Brian Norris5844fee2015-01-23 00:22:27 -08005853 if (of_get_nand_bus_width(dn) == 16)
5854 chip->options |= NAND_BUSWIDTH_16;
5855
5856 if (of_get_nand_on_flash_bbt(dn))
5857 chip->bbt_options |= NAND_BBT_USE_FLASH;
5858
5859 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005860 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005861 ecc_strength = of_get_nand_ecc_strength(dn);
5862 ecc_step = of_get_nand_ecc_step_size(dn);
5863
Brian Norris5844fee2015-01-23 00:22:27 -08005864 if (ecc_mode >= 0)
5865 chip->ecc.mode = ecc_mode;
5866
Rafał Miłecki79082452016-03-23 11:19:02 +01005867 if (ecc_algo >= 0)
5868 chip->ecc.algo = ecc_algo;
5869
Brian Norris5844fee2015-01-23 00:22:27 -08005870 if (ecc_strength >= 0)
5871 chip->ecc.strength = ecc_strength;
5872
5873 if (ecc_step > 0)
5874 chip->ecc.size = ecc_step;
5875
Boris Brezillonba78ee02016-06-08 17:04:22 +02005876 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5877 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5878
Brian Norris5844fee2015-01-23 00:22:27 -08005879 return 0;
5880}
5881
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005882/**
David Woodhouse3b85c322006-09-25 17:06:53 +01005883 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005884 * @mtd: MTD device structure
5885 * @maxchips: number of chips to scan for
5886 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005887 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005888 * This is the first phase of the normal nand_scan() function. It reads the
5889 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005890 *
5891 */
David Woodhouse5e81e882010-02-26 18:32:56 +00005892int nand_scan_ident(struct mtd_info *mtd, int maxchips,
5893 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005894{
Cai Zhiyongbb770822013-12-25 20:11:15 +08005895 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01005896 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08005897 int ret;
5898
Miquel Raynal17fa8042017-11-30 18:01:31 +01005899 /* Enforce the right timings for reset/detection */
5900 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5901
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005902 ret = nand_dt_init(chip);
5903 if (ret)
5904 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005905
Brian Norrisf7a8e382016-01-05 10:39:45 -08005906 if (!mtd->name && mtd->dev.parent)
5907 mtd->name = dev_name(mtd->dev.parent);
5908
Miquel Raynal8878b122017-11-09 14:16:45 +01005909 /*
5910 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5911 * populated.
5912 */
5913 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005914 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005915 * Default functions assigned for ->cmdfunc() and
5916 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005917 */
Miquel Raynal8878b122017-11-09 14:16:45 +01005918 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5919 pr_err("->cmd_ctrl() should be provided\n");
5920 return -EINVAL;
5921 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005922 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005923
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005924 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005925 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005926
5927 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005928 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005929 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005930 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005931 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005932 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005933 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 }
5935
Boris Brezillon7f501f02016-05-24 19:20:05 +02005936 nand_maf_id = chip->id.data[0];
5937 nand_dev_id = chip->id.data[1];
5938
Huang Shijie07300162012-11-09 16:23:45 +08005939 chip->select_chip(mtd, -1);
5940
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005941 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005942 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005943 u8 id[2];
5944
Karl Beldanef89a882008-09-15 14:37:29 +02005945 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005946 nand_reset(chip, i);
5947
5948 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005950 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005952 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08005953 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 break;
Huang Shijie07300162012-11-09 16:23:45 +08005955 }
5956 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 }
5958 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005959 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005960
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005962 chip->numchips = i;
5963 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
David Woodhouse3b85c322006-09-25 17:06:53 +01005965 return 0;
5966}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005967EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01005968
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005969static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
5970{
5971 struct nand_chip *chip = mtd_to_nand(mtd);
5972 struct nand_ecc_ctrl *ecc = &chip->ecc;
5973
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005974 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005975 return -EINVAL;
5976
5977 switch (ecc->algo) {
5978 case NAND_ECC_HAMMING:
5979 ecc->calculate = nand_calculate_ecc;
5980 ecc->correct = nand_correct_data;
5981 ecc->read_page = nand_read_page_swecc;
5982 ecc->read_subpage = nand_read_subpage;
5983 ecc->write_page = nand_write_page_swecc;
5984 ecc->read_page_raw = nand_read_page_raw;
5985 ecc->write_page_raw = nand_write_page_raw;
5986 ecc->read_oob = nand_read_oob_std;
5987 ecc->write_oob = nand_write_oob_std;
5988 if (!ecc->size)
5989 ecc->size = 256;
5990 ecc->bytes = 3;
5991 ecc->strength = 1;
5992 return 0;
5993 case NAND_ECC_BCH:
5994 if (!mtd_nand_has_bch()) {
5995 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5996 return -EINVAL;
5997 }
5998 ecc->calculate = nand_bch_calculate_ecc;
5999 ecc->correct = nand_bch_correct_data;
6000 ecc->read_page = nand_read_page_swecc;
6001 ecc->read_subpage = nand_read_subpage;
6002 ecc->write_page = nand_write_page_swecc;
6003 ecc->read_page_raw = nand_read_page_raw;
6004 ecc->write_page_raw = nand_write_page_raw;
6005 ecc->read_oob = nand_read_oob_std;
6006 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02006007
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006008 /*
6009 * Board driver should supply ecc.size and ecc.strength
6010 * values to select how many bits are correctable.
6011 * Otherwise, default to 4 bits for large page devices.
6012 */
6013 if (!ecc->size && (mtd->oobsize >= 64)) {
6014 ecc->size = 512;
6015 ecc->strength = 4;
6016 }
6017
6018 /*
6019 * if no ecc placement scheme was provided pickup the default
6020 * large page one.
6021 */
6022 if (!mtd->ooblayout) {
6023 /* handle large page devices only */
6024 if (mtd->oobsize < 64) {
6025 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6026 return -EINVAL;
6027 }
6028
6029 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02006030
6031 }
6032
6033 /*
6034 * We can only maximize ECC config when the default layout is
6035 * used, otherwise we don't know how many bytes can really be
6036 * used.
6037 */
6038 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6039 ecc->options & NAND_ECC_MAXIMIZE) {
6040 int steps, bytes;
6041
6042 /* Always prefer 1k blocks over 512bytes ones */
6043 ecc->size = 1024;
6044 steps = mtd->writesize / ecc->size;
6045
6046 /* Reserve 2 bytes for the BBM */
6047 bytes = (mtd->oobsize - 2) / steps;
6048 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006049 }
6050
6051 /* See nand_bch_init() for details. */
6052 ecc->bytes = 0;
6053 ecc->priv = nand_bch_init(mtd);
6054 if (!ecc->priv) {
6055 WARN(1, "BCH ECC initialization failed!\n");
6056 return -EINVAL;
6057 }
6058 return 0;
6059 default:
6060 WARN(1, "Unsupported ECC algorithm!\n");
6061 return -EINVAL;
6062 }
6063}
6064
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006065/**
6066 * nand_check_ecc_caps - check the sanity of preset ECC settings
6067 * @chip: nand chip info structure
6068 * @caps: ECC caps info structure
6069 * @oobavail: OOB size that the ECC engine can use
6070 *
6071 * When ECC step size and strength are already set, check if they are supported
6072 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6073 * On success, the calculated ECC bytes is set.
6074 */
6075int nand_check_ecc_caps(struct nand_chip *chip,
6076 const struct nand_ecc_caps *caps, int oobavail)
6077{
6078 struct mtd_info *mtd = nand_to_mtd(chip);
6079 const struct nand_ecc_step_info *stepinfo;
6080 int preset_step = chip->ecc.size;
6081 int preset_strength = chip->ecc.strength;
6082 int nsteps, ecc_bytes;
6083 int i, j;
6084
6085 if (WARN_ON(oobavail < 0))
6086 return -EINVAL;
6087
6088 if (!preset_step || !preset_strength)
6089 return -ENODATA;
6090
6091 nsteps = mtd->writesize / preset_step;
6092
6093 for (i = 0; i < caps->nstepinfos; i++) {
6094 stepinfo = &caps->stepinfos[i];
6095
6096 if (stepinfo->stepsize != preset_step)
6097 continue;
6098
6099 for (j = 0; j < stepinfo->nstrengths; j++) {
6100 if (stepinfo->strengths[j] != preset_strength)
6101 continue;
6102
6103 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6104 preset_strength);
6105 if (WARN_ON_ONCE(ecc_bytes < 0))
6106 return ecc_bytes;
6107
6108 if (ecc_bytes * nsteps > oobavail) {
6109 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6110 preset_step, preset_strength);
6111 return -ENOSPC;
6112 }
6113
6114 chip->ecc.bytes = ecc_bytes;
6115
6116 return 0;
6117 }
6118 }
6119
6120 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6121 preset_step, preset_strength);
6122
6123 return -ENOTSUPP;
6124}
6125EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
6126
6127/**
6128 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6129 * @chip: nand chip info structure
6130 * @caps: ECC engine caps info structure
6131 * @oobavail: OOB size that the ECC engine can use
6132 *
6133 * If a chip's ECC requirement is provided, try to meet it with the least
6134 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6135 * On success, the chosen ECC settings are set.
6136 */
6137int nand_match_ecc_req(struct nand_chip *chip,
6138 const struct nand_ecc_caps *caps, int oobavail)
6139{
6140 struct mtd_info *mtd = nand_to_mtd(chip);
6141 const struct nand_ecc_step_info *stepinfo;
6142 int req_step = chip->ecc_step_ds;
6143 int req_strength = chip->ecc_strength_ds;
6144 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6145 int best_step, best_strength, best_ecc_bytes;
6146 int best_ecc_bytes_total = INT_MAX;
6147 int i, j;
6148
6149 if (WARN_ON(oobavail < 0))
6150 return -EINVAL;
6151
6152 /* No information provided by the NAND chip */
6153 if (!req_step || !req_strength)
6154 return -ENOTSUPP;
6155
6156 /* number of correctable bits the chip requires in a page */
6157 req_corr = mtd->writesize / req_step * req_strength;
6158
6159 for (i = 0; i < caps->nstepinfos; i++) {
6160 stepinfo = &caps->stepinfos[i];
6161 step_size = stepinfo->stepsize;
6162
6163 for (j = 0; j < stepinfo->nstrengths; j++) {
6164 strength = stepinfo->strengths[j];
6165
6166 /*
6167 * If both step size and strength are smaller than the
6168 * chip's requirement, it is not easy to compare the
6169 * resulted reliability.
6170 */
6171 if (step_size < req_step && strength < req_strength)
6172 continue;
6173
6174 if (mtd->writesize % step_size)
6175 continue;
6176
6177 nsteps = mtd->writesize / step_size;
6178
6179 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6180 if (WARN_ON_ONCE(ecc_bytes < 0))
6181 continue;
6182 ecc_bytes_total = ecc_bytes * nsteps;
6183
6184 if (ecc_bytes_total > oobavail ||
6185 strength * nsteps < req_corr)
6186 continue;
6187
6188 /*
6189 * We assume the best is to meet the chip's requrement
6190 * with the least number of ECC bytes.
6191 */
6192 if (ecc_bytes_total < best_ecc_bytes_total) {
6193 best_ecc_bytes_total = ecc_bytes_total;
6194 best_step = step_size;
6195 best_strength = strength;
6196 best_ecc_bytes = ecc_bytes;
6197 }
6198 }
6199 }
6200
6201 if (best_ecc_bytes_total == INT_MAX)
6202 return -ENOTSUPP;
6203
6204 chip->ecc.size = best_step;
6205 chip->ecc.strength = best_strength;
6206 chip->ecc.bytes = best_ecc_bytes;
6207
6208 return 0;
6209}
6210EXPORT_SYMBOL_GPL(nand_match_ecc_req);
6211
6212/**
6213 * nand_maximize_ecc - choose the max ECC strength available
6214 * @chip: nand chip info structure
6215 * @caps: ECC engine caps info structure
6216 * @oobavail: OOB size that the ECC engine can use
6217 *
6218 * Choose the max ECC strength that is supported on the controller, and can fit
6219 * within the chip's OOB. On success, the chosen ECC settings are set.
6220 */
6221int nand_maximize_ecc(struct nand_chip *chip,
6222 const struct nand_ecc_caps *caps, int oobavail)
6223{
6224 struct mtd_info *mtd = nand_to_mtd(chip);
6225 const struct nand_ecc_step_info *stepinfo;
6226 int step_size, strength, nsteps, ecc_bytes, corr;
6227 int best_corr = 0;
6228 int best_step = 0;
6229 int best_strength, best_ecc_bytes;
6230 int i, j;
6231
6232 if (WARN_ON(oobavail < 0))
6233 return -EINVAL;
6234
6235 for (i = 0; i < caps->nstepinfos; i++) {
6236 stepinfo = &caps->stepinfos[i];
6237 step_size = stepinfo->stepsize;
6238
6239 /* If chip->ecc.size is already set, respect it */
6240 if (chip->ecc.size && step_size != chip->ecc.size)
6241 continue;
6242
6243 for (j = 0; j < stepinfo->nstrengths; j++) {
6244 strength = stepinfo->strengths[j];
6245
6246 if (mtd->writesize % step_size)
6247 continue;
6248
6249 nsteps = mtd->writesize / step_size;
6250
6251 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6252 if (WARN_ON_ONCE(ecc_bytes < 0))
6253 continue;
6254
6255 if (ecc_bytes * nsteps > oobavail)
6256 continue;
6257
6258 corr = strength * nsteps;
6259
6260 /*
6261 * If the number of correctable bits is the same,
6262 * bigger step_size has more reliability.
6263 */
6264 if (corr > best_corr ||
6265 (corr == best_corr && step_size > best_step)) {
6266 best_corr = corr;
6267 best_step = step_size;
6268 best_strength = strength;
6269 best_ecc_bytes = ecc_bytes;
6270 }
6271 }
6272 }
6273
6274 if (!best_corr)
6275 return -ENOTSUPP;
6276
6277 chip->ecc.size = best_step;
6278 chip->ecc.strength = best_strength;
6279 chip->ecc.bytes = best_ecc_bytes;
6280
6281 return 0;
6282}
6283EXPORT_SYMBOL_GPL(nand_maximize_ecc);
6284
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006285/*
6286 * Check if the chip configuration meet the datasheet requirements.
6287
6288 * If our configuration corrects A bits per B bytes and the minimum
6289 * required correction level is X bits per Y bytes, then we must ensure
6290 * both of the following are true:
6291 *
6292 * (1) A / B >= X / Y
6293 * (2) A >= X
6294 *
6295 * Requirement (1) ensures we can correct for the required bitflip density.
6296 * Requirement (2) ensures we can correct even when all bitflips are clumped
6297 * in the same sector.
6298 */
6299static bool nand_ecc_strength_good(struct mtd_info *mtd)
6300{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006301 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006302 struct nand_ecc_ctrl *ecc = &chip->ecc;
6303 int corr, ds_corr;
6304
6305 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6306 /* Not enough information */
6307 return true;
6308
6309 /*
6310 * We get the number of corrected bits per page to compare
6311 * the correction density.
6312 */
6313 corr = (mtd->writesize * ecc->strength) / ecc->size;
6314 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6315
6316 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6317}
David Woodhouse3b85c322006-09-25 17:06:53 +01006318
6319/**
6320 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006321 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01006322 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006323 * This is the second phase of the normal nand_scan() function. It fills out
6324 * all the uninitialized function pointers with the defaults and scans for a
6325 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006326 */
6327int nand_scan_tail(struct mtd_info *mtd)
6328{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006329 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08006330 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006331 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006332
Brian Norrise2414f42012-02-06 13:44:00 -08006333 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006334 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006335 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006336 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006337 }
Brian Norrise2414f42012-02-06 13:44:00 -08006338
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006339 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006340 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006341 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006342
Boris Brezillonf84674b2017-06-02 12:18:24 +02006343 /*
6344 * FIXME: some NAND manufacturer drivers expect the first die to be
6345 * selected when manufacturer->init() is called. They should be fixed
6346 * to explictly select the relevant die when interacting with the NAND
6347 * chip.
6348 */
6349 chip->select_chip(mtd, 0);
6350 ret = nand_manufacturer_init(chip);
6351 chip->select_chip(mtd, -1);
6352 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006353 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006354
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006355 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006356 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006357
6358 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006359 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006360 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006361 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006362 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006363 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006366 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 break;
6368 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006369 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006370 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006373 /*
6374 * Expose the whole OOB area to users if ECC_NONE
6375 * is passed. We could do that for all kind of
6376 * ->oobsize, but we must keep the old large/small
6377 * page with ECC layout when ->oobsize <= 128 for
6378 * compatibility reasons.
6379 */
6380 if (ecc->mode == NAND_ECC_NONE) {
6381 mtd_set_ooblayout(mtd,
6382 &nand_ooblayout_lp_ops);
6383 break;
6384 }
6385
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006386 WARN(1, "No oob scheme defined for oobsize %d\n",
6387 mtd->oobsize);
6388 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006389 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 }
6391 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006392
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006393 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006394 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006395 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006396 */
David Woodhouse956e9442006-09-25 17:12:39 +01006397
Huang Shijie97de79e02013-10-18 14:20:53 +08006398 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006399 case NAND_ECC_HW_OOB_FIRST:
6400 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006401 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006402 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6403 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006404 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006405 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006406 if (!ecc->read_page)
6407 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006408
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006409 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006410 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006411 if (!ecc->read_page)
6412 ecc->read_page = nand_read_page_hwecc;
6413 if (!ecc->write_page)
6414 ecc->write_page = nand_write_page_hwecc;
6415 if (!ecc->read_page_raw)
6416 ecc->read_page_raw = nand_read_page_raw;
6417 if (!ecc->write_page_raw)
6418 ecc->write_page_raw = nand_write_page_raw;
6419 if (!ecc->read_oob)
6420 ecc->read_oob = nand_read_oob_std;
6421 if (!ecc->write_oob)
6422 ecc->write_oob = nand_write_oob_std;
6423 if (!ecc->read_subpage)
6424 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006425 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006426 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006427
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006428 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006429 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6430 (!ecc->read_page ||
6431 ecc->read_page == nand_read_page_hwecc ||
6432 !ecc->write_page ||
6433 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006434 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6435 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006436 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006437 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006438 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006439 if (!ecc->read_page)
6440 ecc->read_page = nand_read_page_syndrome;
6441 if (!ecc->write_page)
6442 ecc->write_page = nand_write_page_syndrome;
6443 if (!ecc->read_page_raw)
6444 ecc->read_page_raw = nand_read_page_raw_syndrome;
6445 if (!ecc->write_page_raw)
6446 ecc->write_page_raw = nand_write_page_raw_syndrome;
6447 if (!ecc->read_oob)
6448 ecc->read_oob = nand_read_oob_syndrome;
6449 if (!ecc->write_oob)
6450 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006451
Huang Shijie97de79e02013-10-18 14:20:53 +08006452 if (mtd->writesize >= ecc->size) {
6453 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006454 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6455 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006456 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006457 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006458 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006459 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006460 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6461 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006462 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006463 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006465 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006466 ret = nand_set_ecc_soft_ops(mtd);
6467 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006468 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006469 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006470 }
6471 break;
6472
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006473 case NAND_ECC_ON_DIE:
6474 if (!ecc->read_page || !ecc->write_page) {
6475 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6476 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006477 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006478 }
6479 if (!ecc->read_oob)
6480 ecc->read_oob = nand_read_oob_std;
6481 if (!ecc->write_oob)
6482 ecc->write_oob = nand_write_oob_std;
6483 break;
6484
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006485 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006486 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006487 ecc->read_page = nand_read_page_raw;
6488 ecc->write_page = nand_write_page_raw;
6489 ecc->read_oob = nand_read_oob_std;
6490 ecc->read_page_raw = nand_read_page_raw;
6491 ecc->write_page_raw = nand_write_page_raw;
6492 ecc->write_oob = nand_write_oob_std;
6493 ecc->size = mtd->writesize;
6494 ecc->bytes = 0;
6495 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006497
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006499 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6500 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006501 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006504 if (ecc->correct || ecc->calculate) {
6505 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6506 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6507 if (!ecc->calc_buf || !ecc->code_buf) {
6508 ret = -ENOMEM;
6509 goto err_nand_manuf_cleanup;
6510 }
6511 }
6512
Brian Norris9ce244b2011-08-30 18:45:37 -07006513 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006514 if (!ecc->read_oob_raw)
6515 ecc->read_oob_raw = ecc->read_oob;
6516 if (!ecc->write_oob_raw)
6517 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006518
Boris Brezillon846031d2016-02-03 20:11:00 +01006519 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006520 mtd->ecc_strength = ecc->strength;
6521 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006522
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006523 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006524 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006525 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006526 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006527 ecc->steps = mtd->writesize / ecc->size;
6528 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006529 WARN(1, "Invalid ECC parameters\n");
6530 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006531 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006533 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006534 if (ecc->total > mtd->oobsize) {
6535 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6536 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006537 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006538 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006539
Boris Brezillon846031d2016-02-03 20:11:00 +01006540 /*
6541 * The number of bytes available for a client to place data into
6542 * the out of band area.
6543 */
6544 ret = mtd_ooblayout_count_freebytes(mtd);
6545 if (ret < 0)
6546 ret = 0;
6547
6548 mtd->oobavail = ret;
6549
6550 /* ECC sanity check: warn if it's too weak */
6551 if (!nand_ecc_strength_good(mtd))
6552 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6553 mtd->name);
6554
Brian Norris8b6e50c2011-05-25 14:59:01 -07006555 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006556 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006557 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006558 case 2:
6559 mtd->subpage_sft = 1;
6560 break;
6561 case 4:
6562 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006563 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006564 mtd->subpage_sft = 2;
6565 break;
6566 }
6567 }
6568 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6569
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006570 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006571 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006574 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006576 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306577 switch (ecc->mode) {
6578 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306579 if (chip->page_shift > 9)
6580 chip->options |= NAND_SUBPAGE_READ;
6581 break;
6582
6583 default:
6584 break;
6585 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006586
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006588 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006589 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6590 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006591 mtd->_erase = nand_erase;
6592 mtd->_point = NULL;
6593 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006594 mtd->_panic_write = panic_nand_write;
6595 mtd->_read_oob = nand_read_oob;
6596 mtd->_write_oob = nand_write_oob;
6597 mtd->_sync = nand_sync;
6598 mtd->_lock = NULL;
6599 mtd->_unlock = NULL;
6600 mtd->_suspend = nand_suspend;
6601 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006602 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006603 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006604 mtd->_block_isbad = nand_block_isbad;
6605 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006606 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006607 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006609 /*
6610 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6611 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6612 * properly set.
6613 */
6614 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006615 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616
Boris Brezillonf84674b2017-06-02 12:18:24 +02006617 /* Initialize the ->data_interface field. */
6618 ret = nand_init_data_interface(chip);
6619 if (ret)
6620 goto err_nand_manuf_cleanup;
6621
6622 /* Enter fastest possible mode on all dies. */
6623 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006624 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006625 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006626 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006627 }
6628
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006629 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006630 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632
6633 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07006634 ret = chip->scan_bbt(mtd);
6635 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006636 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006637
Brian Norris44d41822017-05-01 17:04:50 -07006638 return 0;
6639
Boris Brezillonf84674b2017-06-02 12:18:24 +02006640
6641err_nand_manuf_cleanup:
6642 nand_manufacturer_cleanup(chip);
6643
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006644err_free_buf:
6645 kfree(chip->data_buf);
6646 kfree(ecc->code_buf);
6647 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006648
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006649 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006651EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652
Brian Norris8b6e50c2011-05-25 14:59:01 -07006653/*
6654 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006655 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07006656 * to call us from in-kernel code if the core NAND support is modular.
6657 */
David Woodhouse3b85c322006-09-25 17:06:53 +01006658#ifdef MODULE
6659#define caller_is_module() (1)
6660#else
6661#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06006662 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01006663#endif
6664
6665/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006666 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006667 * @mtd: MTD device structure
6668 * @maxchips: number of chips to scan for
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006669 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01006670 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006671 * This fills out all the uninitialized function pointers with the defaults.
6672 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006673 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006674 */
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006675int nand_scan_with_ids(struct mtd_info *mtd, int maxchips,
6676 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01006677{
6678 int ret;
6679
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006680 ret = nand_scan_ident(mtd, maxchips, ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006681 if (!ret)
6682 ret = nand_scan_tail(mtd);
6683 return ret;
6684}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006685EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006686
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006688 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6689 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006690 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006691void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006693 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006694 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006695 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6696
Jesper Juhlfa671642005-11-07 01:01:27 -08006697 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006698 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006699 kfree(chip->data_buf);
6700 kfree(chip->ecc.code_buf);
6701 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006702
6703 /* Free bad block descriptor memory */
6704 if (chip->badblock_pattern && chip->badblock_pattern->options
6705 & NAND_BBT_DYNAMICSTRUCT)
6706 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006707
6708 /* Free manufacturer priv data. */
6709 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006711EXPORT_SYMBOL_GPL(nand_cleanup);
6712
6713/**
6714 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6715 * held by the NAND device
6716 * @mtd: MTD device structure
6717 */
6718void nand_release(struct mtd_info *mtd)
6719{
6720 mtd_device_unregister(mtd);
6721 nand_cleanup(mtd_to_nand(mtd));
6722}
David Woodhousee0c7d762006-05-13 18:07:53 +01006723EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006724
David Woodhousee0c7d762006-05-13 18:07:53 +01006725MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006726MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6727MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006728MODULE_DESCRIPTION("Generic NAND flash driver code");