blob: 0f1f45526c7f183bd1ad9e5cd2df45f0c1e16211 [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));
530 einfo.mtd = mtd;
531 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300532 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800533 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800534
Brian Norrisb32843b2013-07-30 17:52:59 -0700535 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800536 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700537 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300538 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200539 }
Brian Norrise2414f42012-02-06 13:44:00 -0800540
Brian Norrisb32843b2013-07-30 17:52:59 -0700541 /* Mark block bad in BBT */
542 if (chip->bbt) {
543 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800544 if (!ret)
545 ret = res;
546 }
547
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200548 if (!ret)
549 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300550
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000554/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700556 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700558 * Check, if the device is write protected. The function expects, that the
559 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100561static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100563 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100564 u8 status;
565 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200566
Brian Norris8b6e50c2011-05-25 14:59:01 -0700567 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200568 if (chip->options & NAND_BROKEN_XD)
569 return 0;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100572 ret = nand_status_op(chip, &status);
573 if (ret)
574 return ret;
575
576 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800580 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700581 * @mtd: MTD device structure
582 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300583 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800584 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300585 */
586static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
587{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100588 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300589
590 if (!chip->bbt)
591 return 0;
592 /* Return info from the table */
593 return nand_isreserved_bbt(mtd, ofs);
594}
595
596/**
597 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
598 * @mtd: MTD device structure
599 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700600 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
602 * Check, if the block is bad. Either by reading the bad block table or
603 * calling of the scan function.
604 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530605static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100607 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000608
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200609 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530610 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100613 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200616/**
617 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700618 * @mtd: MTD device structure
619 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200620 *
621 * Helper function for nand_wait_ready used when needing to wait in interrupt
622 * context.
623 */
624static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
625{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100626 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200627 int i;
628
629 /* Wait for the device to get ready */
630 for (i = 0; i < timeo; i++) {
631 if (chip->dev_ready(mtd))
632 break;
633 touch_softlockup_watchdog();
634 mdelay(1);
635 }
636}
637
Alex Smithb70af9b2015-10-06 14:52:07 +0100638/**
639 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
640 * @mtd: MTD device structure
641 *
642 * Wait for the ready pin after a command, and warn if a timeout occurs.
643 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100644void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000645{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100646 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100647 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000648
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200649 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100650 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200651
Brian Norris7854d3f2011-06-23 14:12:08 -0700652 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100653 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000654 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300656 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100657 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000658 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100659
Brian Norris9ebfdf52016-03-04 17:19:23 -0800660 if (!chip->dev_ready(mtd))
661 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000662}
David Woodhouse4b648b02006-09-25 17:05:24 +0100663EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200666 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
667 * @mtd: MTD device structure
668 * @timeo: Timeout in ms
669 *
670 * Wait for status ready (i.e. command done) or timeout.
671 */
672static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
673{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100674 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100675 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200676
677 timeo = jiffies + msecs_to_jiffies(timeo);
678 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100679 u8 status;
680
681 ret = nand_read_data_op(chip, &status, sizeof(status), true);
682 if (ret)
683 return;
684
685 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200686 break;
687 touch_softlockup_watchdog();
688 } while (time_before(jiffies, timeo));
689};
690
691/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100692 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
693 * @chip: NAND chip structure
694 * @timeout_ms: Timeout in ms
695 *
696 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
697 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
698 * returned.
699 *
700 * This helper is intended to be used when the controller does not have access
701 * to the NAND R/B pin.
702 *
703 * Be aware that calling this helper from an ->exec_op() implementation means
704 * ->exec_op() must be re-entrant.
705 *
706 * Return 0 if the NAND chip is ready, a negative error otherwise.
707 */
708int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
709{
710 u8 status = 0;
711 int ret;
712
713 if (!chip->exec_op)
714 return -ENOTSUPP;
715
716 ret = nand_status_op(chip, NULL);
717 if (ret)
718 return ret;
719
720 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
721 do {
722 ret = nand_read_data_op(chip, &status, sizeof(status), true);
723 if (ret)
724 break;
725
726 if (status & NAND_STATUS_READY)
727 break;
728
729 /*
730 * Typical lowest execution time for a tR on most NANDs is 10us,
731 * use this as polling delay before doing something smarter (ie.
732 * deriving a delay from the timeout value, timeout_ms/ratio).
733 */
734 udelay(10);
735 } while (time_before(jiffies, timeout_ms));
736
737 /*
738 * We have to exit READ_STATUS mode in order to read real data on the
739 * bus in case the WAITRDY instruction is preceding a DATA_IN
740 * instruction.
741 */
742 nand_exit_status_op(chip);
743
744 if (ret)
745 return ret;
746
747 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
748};
749EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
750
751/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700753 * @mtd: MTD device structure
754 * @command: the command to be sent
755 * @column: the column address for this command, -1 if none
756 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700758 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200759 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200761static void nand_command(struct mtd_info *mtd, unsigned int command,
762 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100764 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200765 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Brian Norris8b6e50c2011-05-25 14:59:01 -0700767 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (command == NAND_CMD_SEQIN) {
769 int readcmd;
770
Joern Engel28318772006-05-22 23:18:05 +0200771 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200773 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 readcmd = NAND_CMD_READOOB;
775 } else if (column < 256) {
776 /* First 256 bytes --> READ0 */
777 readcmd = NAND_CMD_READ0;
778 } else {
779 column -= 256;
780 readcmd = NAND_CMD_READ1;
781 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200782 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200783 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100785 if (command != NAND_CMD_NONE)
786 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Brian Norris8b6e50c2011-05-25 14:59:01 -0700788 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200789 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
790 /* Serially input address */
791 if (column != -1) {
792 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800793 if (chip->options & NAND_BUSWIDTH_16 &&
794 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200795 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200796 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200797 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200799 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200800 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200801 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200802 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900803 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200804 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200805 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200806 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000807
808 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700809 * Program and erase have their own busy handlers status and sequential
810 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100811 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000813
Miquel Raynaldf467892017-11-08 17:00:27 +0100814 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 case NAND_CMD_PAGEPROG:
816 case NAND_CMD_ERASE1:
817 case NAND_CMD_ERASE2:
818 case NAND_CMD_SEQIN:
819 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900820 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900821 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return;
823
824 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200825 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200827 udelay(chip->chip_delay);
828 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200829 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200830 chip->cmd_ctrl(mtd,
831 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200832 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
833 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return;
835
David Woodhousee0c7d762006-05-13 18:07:53 +0100836 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200837 case NAND_CMD_READ0:
838 /*
839 * READ0 is sometimes used to exit GET STATUS mode. When this
840 * is the case no address cycles are requested, and we can use
841 * this information to detect that we should not wait for the
842 * device to be ready.
843 */
844 if (column == -1 && page_addr == -1)
845 return;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000848 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * If we don't have access to the busy pin, we apply the given
850 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100851 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200852 if (!chip->dev_ready) {
853 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700857 /*
858 * Apply this short delay always to ensure that we do wait tWB in
859 * any case on any machine.
860 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100861 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000862
863 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200866static void nand_ccs_delay(struct nand_chip *chip)
867{
868 /*
869 * The controller already takes care of waiting for tCCS when the RNDIN
870 * or RNDOUT command is sent, return directly.
871 */
872 if (!(chip->options & NAND_WAIT_TCCS))
873 return;
874
875 /*
876 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
877 * (which should be safe for all NANDs).
878 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100879 if (chip->setup_data_interface)
880 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200881 else
882 ndelay(500);
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/**
886 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700887 * @mtd: MTD device structure
888 * @command: the command to be sent
889 * @column: the column address for this command, -1 if none
890 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200892 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700893 * devices. We don't have the separate regions as we have in the small page
894 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200896static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
897 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100899 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* Emulate NAND_CMD_READOOB */
902 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200903 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 command = NAND_CMD_READ0;
905 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000906
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200907 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100908 if (command != NAND_CMD_NONE)
909 chip->cmd_ctrl(mtd, command,
910 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200913 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /* Serially input address */
916 if (column != -1) {
917 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800918 if (chip->options & NAND_BUSWIDTH_16 &&
919 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200921 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200922 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200923
Brian Norrisf5b88de2016-10-03 09:49:35 -0700924 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200925 if (!nand_opcode_8bits(command))
926 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200929 chip->cmd_ctrl(mtd, page_addr, ctrl);
930 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200931 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900932 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200933 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200934 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200937 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000938
939 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700940 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100941 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000944
Miquel Raynaldf467892017-11-08 17:00:27 +0100945 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 case NAND_CMD_CACHEDPROG:
947 case NAND_CMD_PAGEPROG:
948 case NAND_CMD_ERASE1:
949 case NAND_CMD_ERASE2:
950 case NAND_CMD_SEQIN:
951 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900952 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900953 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000954 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200956 case NAND_CMD_RNDIN:
957 nand_ccs_delay(chip);
958 return;
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200961 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200963 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200964 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
965 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
966 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
967 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200968 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
969 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return;
971
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200972 case NAND_CMD_RNDOUT:
973 /* No ready / busy check necessary */
974 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
975 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
976 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
977 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200978
979 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200980 return;
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200983 /*
984 * READ0 is sometimes used to exit GET STATUS mode. When this
985 * is the case no address cycles are requested, and we can use
986 * this information to detect that READSTART should not be
987 * issued.
988 */
989 if (column == -1 && page_addr == -1)
990 return;
991
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200992 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
993 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
994 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
995 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000996
David Woodhousee0c7d762006-05-13 18:07:53 +0100997 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000999 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -07001001 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +01001002 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001003 if (!chip->dev_ready) {
1004 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Thomas Gleixner3b887752005-02-22 21:56:49 +00001008
Brian Norris8b6e50c2011-05-25 14:59:01 -07001009 /*
1010 * Apply this short delay always to ensure that we do wait tWB in
1011 * any case on any machine.
1012 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001013 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +00001014
1015 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001019 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001020 * @chip: the nand chip descriptor
1021 * @mtd: MTD device structure
1022 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001023 *
1024 * Used when in panic, no locks are taken.
1025 */
1026static void panic_nand_get_device(struct nand_chip *chip,
1027 struct mtd_info *mtd, int new_state)
1028{
Brian Norris7854d3f2011-06-23 14:12:08 -07001029 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001030 chip->controller->active = chip;
1031 chip->state = new_state;
1032}
1033
1034/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001036 * @mtd: MTD device structure
1037 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 *
1039 * Get the device and lock it for exclusive access
1040 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001041static int
Huang Shijie6a8214a2012-11-19 14:43:30 +08001042nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001044 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001045 spinlock_t *lock = &chip->controller->lock;
1046 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +01001047 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001048retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001049 spin_lock(lock);
1050
vimal singhb8b3ee92009-07-09 20:41:22 +05301051 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001052 if (!chip->controller->active)
1053 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001054
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001055 if (chip->controller->active == chip && chip->state == FL_READY) {
1056 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001057 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01001058 return 0;
1059 }
1060 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001061 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1062 chip->state = FL_PM_SUSPENDED;
1063 spin_unlock(lock);
1064 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001065 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001066 }
1067 set_current_state(TASK_UNINTERRUPTIBLE);
1068 add_wait_queue(wq, &wait);
1069 spin_unlock(lock);
1070 schedule();
1071 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 goto retry;
1073}
1074
1075/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001076 * panic_nand_wait - [GENERIC] wait until the command is done
1077 * @mtd: MTD device structure
1078 * @chip: NAND chip structure
1079 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001080 *
1081 * Wait for command done. This is a helper function for nand_wait used when
1082 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001083 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001084 */
1085static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1086 unsigned long timeo)
1087{
1088 int i;
1089 for (i = 0; i < timeo; i++) {
1090 if (chip->dev_ready) {
1091 if (chip->dev_ready(mtd))
1092 break;
1093 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001094 int ret;
1095 u8 status;
1096
1097 ret = nand_read_data_op(chip, &status, sizeof(status),
1098 true);
1099 if (ret)
1100 return;
1101
1102 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001103 break;
1104 }
1105 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001106 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001107}
1108
1109/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001110 * nand_wait - [DEFAULT] wait until the command is done
1111 * @mtd: MTD device structure
1112 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001114 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001115 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001116static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118
Alex Smithb70af9b2015-10-06 14:52:07 +01001119 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001120 u8 status;
1121 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Brian Norris8b6e50c2011-05-25 14:59:01 -07001123 /*
1124 * Apply this short delay always to ensure that we do wait tWB in any
1125 * case on any machine.
1126 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001127 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Boris Brezillon97d90da2017-11-30 18:01:29 +01001129 ret = nand_status_op(chip, NULL);
1130 if (ret)
1131 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001133 if (in_interrupt() || oops_in_progress)
1134 panic_nand_wait(mtd, chip, timeo);
1135 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001136 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001137 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001138 if (chip->dev_ready) {
1139 if (chip->dev_ready(mtd))
1140 break;
1141 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001142 ret = nand_read_data_op(chip, &status,
1143 sizeof(status), true);
1144 if (ret)
1145 return ret;
1146
1147 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001148 break;
1149 }
1150 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001151 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001153
Boris Brezillon97d90da2017-11-30 18:01:29 +01001154 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1155 if (ret)
1156 return ret;
1157
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001158 /* This can happen if in case of timeout or buggy dev_ready */
1159 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return status;
1161}
1162
Miquel Raynal97baea12018-03-19 14:47:20 +01001163static bool nand_supports_set_get_features(struct nand_chip *chip)
1164{
Miquel Raynalf4531b22018-03-19 14:47:26 +01001165 return chip->parameters.supports_set_get_features;
Miquel Raynal97baea12018-03-19 14:47:20 +01001166}
1167
1168/**
1169 * nand_get_features - wrapper to perform a GET_FEATURE
1170 * @chip: NAND chip info structure
1171 * @addr: feature address
1172 * @subfeature_param: the subfeature parameters, a four bytes array
1173 *
1174 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1175 * operation cannot be handled.
1176 */
1177int nand_get_features(struct nand_chip *chip, int addr,
1178 u8 *subfeature_param)
1179{
1180 struct mtd_info *mtd = nand_to_mtd(chip);
1181
1182 if (!nand_supports_set_get_features(chip))
1183 return -ENOTSUPP;
1184
1185 return chip->get_features(mtd, chip, addr, subfeature_param);
1186}
1187EXPORT_SYMBOL_GPL(nand_get_features);
1188
1189/**
1190 * nand_set_features - wrapper to perform a SET_FEATURE
1191 * @chip: NAND chip info structure
1192 * @addr: feature address
1193 * @subfeature_param: the subfeature parameters, a four bytes array
1194 *
1195 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1196 * operation cannot be handled.
1197 */
1198int nand_set_features(struct nand_chip *chip, int addr,
1199 u8 *subfeature_param)
1200{
1201 struct mtd_info *mtd = nand_to_mtd(chip);
1202
1203 if (!nand_supports_set_get_features(chip))
1204 return -ENOTSUPP;
1205
1206 return chip->set_features(mtd, chip, addr, subfeature_param);
1207}
1208EXPORT_SYMBOL_GPL(nand_set_features);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001211 * nand_reset_data_interface - Reset data interface and timings
1212 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001213 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001214 *
1215 * Reset the Data interface and timings to ONFI mode 0.
1216 *
1217 * Returns 0 for success or negative error code otherwise.
1218 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001219static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001220{
1221 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001222 int ret;
1223
1224 if (!chip->setup_data_interface)
1225 return 0;
1226
1227 /*
1228 * The ONFI specification says:
1229 * "
1230 * To transition from NV-DDR or NV-DDR2 to the SDR data
1231 * interface, the host shall use the Reset (FFh) command
1232 * using SDR timing mode 0. A device in any timing mode is
1233 * required to recognize Reset (FFh) command issued in SDR
1234 * timing mode 0.
1235 * "
1236 *
1237 * Configure the data interface in SDR mode and set the
1238 * timings to timing mode 0.
1239 */
1240
Miquel Raynal17fa8042017-11-30 18:01:31 +01001241 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1242 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001243 if (ret)
1244 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1245
1246 return ret;
1247}
1248
1249/**
1250 * nand_setup_data_interface - Setup the best data interface and timings
1251 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001252 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001253 *
1254 * Find and configure the best data interface and NAND timings supported by
1255 * the chip and the driver.
1256 * First tries to retrieve supported timing modes from ONFI information,
1257 * and if the NAND chip does not support ONFI, relies on the
1258 * ->onfi_timing_mode_default specified in the nand_ids table.
1259 *
1260 * Returns 0 for success or negative error code otherwise.
1261 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001262static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001263{
1264 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal97baea12018-03-19 14:47:20 +01001265 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1266 chip->onfi_timing_mode_default,
1267 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001268 int ret;
1269
Miquel Raynal17fa8042017-11-30 18:01:31 +01001270 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001271 return 0;
1272
Miquel Raynal993447b2018-03-19 14:47:21 +01001273 /* Change the mode on the chip side (if supported by the NAND chip) */
1274 if (nand_supports_set_get_features(chip)) {
Miquel Raynal29714d62018-03-19 14:47:23 +01001275 chip->select_chip(mtd, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001276 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1277 tmode_param);
Miquel Raynal29714d62018-03-19 14:47:23 +01001278 chip->select_chip(mtd, -1);
Miquel Raynal993447b2018-03-19 14:47:21 +01001279 if (ret)
1280 return ret;
1281 }
Boris Brezillond8e725d2016-09-15 10:32:50 +02001282
Miquel Raynal97baea12018-03-19 14:47:20 +01001283 /* Change the mode on the controller side */
Miquel Raynal415ae782018-03-19 14:47:24 +01001284 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
1285 if (ret)
1286 return ret;
1287
1288 /* Check the mode has been accepted by the chip, if supported */
1289 if (!nand_supports_set_get_features(chip))
1290 return 0;
1291
1292 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
1293 chip->select_chip(mtd, chipnr);
1294 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1295 tmode_param);
1296 chip->select_chip(mtd, -1);
1297 if (ret)
1298 goto err_reset_chip;
1299
1300 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1301 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1302 chip->onfi_timing_mode_default);
1303 goto err_reset_chip;
1304 }
1305
1306 return 0;
1307
1308err_reset_chip:
1309 /*
1310 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1311 * timing mode.
1312 */
1313 nand_reset_data_interface(chip, chipnr);
1314 chip->select_chip(mtd, chipnr);
1315 nand_reset_op(chip);
1316 chip->select_chip(mtd, -1);
1317
1318 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001319}
1320
1321/**
1322 * nand_init_data_interface - find the best data interface and timings
1323 * @chip: The NAND chip
1324 *
1325 * Find the best data interface and NAND timings supported by the chip
1326 * and the driver.
1327 * First tries to retrieve supported timing modes from ONFI information,
1328 * and if the NAND chip does not support ONFI, relies on the
1329 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1330 * function nand_chip->data_interface is initialized with the best timing mode
1331 * available.
1332 *
1333 * Returns 0 for success or negative error code otherwise.
1334 */
1335static int nand_init_data_interface(struct nand_chip *chip)
1336{
1337 struct mtd_info *mtd = nand_to_mtd(chip);
1338 int modes, mode, ret;
1339
1340 if (!chip->setup_data_interface)
1341 return 0;
1342
1343 /*
1344 * First try to identify the best timings from ONFI parameters and
1345 * if the NAND does not support ONFI, fallback to the default ONFI
1346 * timing mode.
1347 */
1348 modes = onfi_get_async_timing_mode(chip);
1349 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1350 if (!chip->onfi_timing_mode_default)
1351 return 0;
1352
1353 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1354 }
1355
Boris Brezillond8e725d2016-09-15 10:32:50 +02001356
1357 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001358 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001359 if (ret)
1360 continue;
1361
Miquel Raynald787b8b2017-12-22 18:12:41 +01001362 /*
1363 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1364 * controller supports the requested timings.
1365 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001366 ret = chip->setup_data_interface(mtd,
1367 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001368 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001369 if (!ret) {
1370 chip->onfi_timing_mode_default = mode;
1371 break;
1372 }
1373 }
1374
1375 return 0;
1376}
1377
Boris Brezillond8e725d2016-09-15 10:32:50 +02001378/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001379 * nand_fill_column_cycles - fill the column cycles of an address
1380 * @chip: The NAND chip
1381 * @addrs: Array of address cycles to fill
1382 * @offset_in_page: The offset in the page
1383 *
1384 * Fills the first or the first two bytes of the @addrs field depending
1385 * on the NAND bus width and the page size.
1386 *
1387 * Returns the number of cycles needed to encode the column, or a negative
1388 * error code in case one of the arguments is invalid.
1389 */
1390static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1391 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Miquel Raynal8878b122017-11-09 14:16:45 +01001393 struct mtd_info *mtd = nand_to_mtd(chip);
1394
1395 /* Make sure the offset is less than the actual page size. */
1396 if (offset_in_page > mtd->writesize + mtd->oobsize)
1397 return -EINVAL;
1398
1399 /*
1400 * On small page NANDs, there's a dedicated command to access the OOB
1401 * area, and the column address is relative to the start of the OOB
1402 * area, not the start of the page. Asjust the address accordingly.
1403 */
1404 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1405 offset_in_page -= mtd->writesize;
1406
1407 /*
1408 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1409 * wide, then it must be divided by 2.
1410 */
1411 if (chip->options & NAND_BUSWIDTH_16) {
1412 if (WARN_ON(offset_in_page % 2))
1413 return -EINVAL;
1414
1415 offset_in_page /= 2;
1416 }
1417
1418 addrs[0] = offset_in_page;
1419
1420 /*
1421 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1422 * need 2
1423 */
1424 if (mtd->writesize <= 512)
1425 return 1;
1426
1427 addrs[1] = offset_in_page >> 8;
1428
1429 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Miquel Raynal8878b122017-11-09 14:16:45 +01001432static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1433 unsigned int offset_in_page, void *buf,
1434 unsigned int len)
1435{
1436 struct mtd_info *mtd = nand_to_mtd(chip);
1437 const struct nand_sdr_timings *sdr =
1438 nand_get_sdr_timings(&chip->data_interface);
1439 u8 addrs[4];
1440 struct nand_op_instr instrs[] = {
1441 NAND_OP_CMD(NAND_CMD_READ0, 0),
1442 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1443 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1444 PSEC_TO_NSEC(sdr->tRR_min)),
1445 NAND_OP_DATA_IN(len, buf, 0),
1446 };
1447 struct nand_operation op = NAND_OPERATION(instrs);
1448 int ret;
1449
1450 /* Drop the DATA_IN instruction if len is set to 0. */
1451 if (!len)
1452 op.ninstrs--;
1453
1454 if (offset_in_page >= mtd->writesize)
1455 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1456 else if (offset_in_page >= 256 &&
1457 !(chip->options & NAND_BUSWIDTH_16))
1458 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1459
1460 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1461 if (ret < 0)
1462 return ret;
1463
1464 addrs[1] = page;
1465 addrs[2] = page >> 8;
1466
1467 if (chip->options & NAND_ROW_ADDR_3) {
1468 addrs[3] = page >> 16;
1469 instrs[1].ctx.addr.naddrs++;
1470 }
1471
1472 return nand_exec_op(chip, &op);
1473}
1474
1475static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1476 unsigned int offset_in_page, void *buf,
1477 unsigned int len)
1478{
1479 const struct nand_sdr_timings *sdr =
1480 nand_get_sdr_timings(&chip->data_interface);
1481 u8 addrs[5];
1482 struct nand_op_instr instrs[] = {
1483 NAND_OP_CMD(NAND_CMD_READ0, 0),
1484 NAND_OP_ADDR(4, addrs, 0),
1485 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1486 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1487 PSEC_TO_NSEC(sdr->tRR_min)),
1488 NAND_OP_DATA_IN(len, buf, 0),
1489 };
1490 struct nand_operation op = NAND_OPERATION(instrs);
1491 int ret;
1492
1493 /* Drop the DATA_IN instruction if len is set to 0. */
1494 if (!len)
1495 op.ninstrs--;
1496
1497 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1498 if (ret < 0)
1499 return ret;
1500
1501 addrs[2] = page;
1502 addrs[3] = page >> 8;
1503
1504 if (chip->options & NAND_ROW_ADDR_3) {
1505 addrs[4] = page >> 16;
1506 instrs[1].ctx.addr.naddrs++;
1507 }
1508
1509 return nand_exec_op(chip, &op);
1510}
1511
1512/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001513 * nand_read_page_op - Do a READ PAGE operation
1514 * @chip: The NAND chip
1515 * @page: page to read
1516 * @offset_in_page: offset within the page
1517 * @buf: buffer used to store the data
1518 * @len: length of the buffer
1519 *
1520 * This function issues a READ PAGE operation.
1521 * This function does not select/unselect the CS line.
1522 *
1523 * Returns 0 on success, a negative error code otherwise.
1524 */
1525int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1526 unsigned int offset_in_page, void *buf, unsigned int len)
1527{
1528 struct mtd_info *mtd = nand_to_mtd(chip);
1529
1530 if (len && !buf)
1531 return -EINVAL;
1532
1533 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1534 return -EINVAL;
1535
Miquel Raynal8878b122017-11-09 14:16:45 +01001536 if (chip->exec_op) {
1537 if (mtd->writesize > 512)
1538 return nand_lp_exec_read_page_op(chip, page,
1539 offset_in_page, buf,
1540 len);
1541
1542 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1543 buf, len);
1544 }
1545
Boris Brezillon97d90da2017-11-30 18:01:29 +01001546 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1547 if (len)
1548 chip->read_buf(mtd, buf, len);
1549
1550 return 0;
1551}
1552EXPORT_SYMBOL_GPL(nand_read_page_op);
1553
1554/**
1555 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1556 * @chip: The NAND chip
1557 * @page: parameter page to read
1558 * @buf: buffer used to store the data
1559 * @len: length of the buffer
1560 *
1561 * This function issues a READ PARAMETER PAGE operation.
1562 * This function does not select/unselect the CS line.
1563 *
1564 * Returns 0 on success, a negative error code otherwise.
1565 */
1566static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1567 unsigned int len)
1568{
1569 struct mtd_info *mtd = nand_to_mtd(chip);
1570 unsigned int i;
1571 u8 *p = buf;
1572
1573 if (len && !buf)
1574 return -EINVAL;
1575
Miquel Raynal8878b122017-11-09 14:16:45 +01001576 if (chip->exec_op) {
1577 const struct nand_sdr_timings *sdr =
1578 nand_get_sdr_timings(&chip->data_interface);
1579 struct nand_op_instr instrs[] = {
1580 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1581 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1582 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1583 PSEC_TO_NSEC(sdr->tRR_min)),
1584 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1585 };
1586 struct nand_operation op = NAND_OPERATION(instrs);
1587
1588 /* Drop the DATA_IN instruction if len is set to 0. */
1589 if (!len)
1590 op.ninstrs--;
1591
1592 return nand_exec_op(chip, &op);
1593 }
1594
Boris Brezillon97d90da2017-11-30 18:01:29 +01001595 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1596 for (i = 0; i < len; i++)
1597 p[i] = chip->read_byte(mtd);
1598
1599 return 0;
1600}
1601
1602/**
1603 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1604 * @chip: The NAND chip
1605 * @offset_in_page: offset within the page
1606 * @buf: buffer used to store the data
1607 * @len: length of the buffer
1608 * @force_8bit: force 8-bit bus access
1609 *
1610 * This function issues a CHANGE READ COLUMN operation.
1611 * This function does not select/unselect the CS line.
1612 *
1613 * Returns 0 on success, a negative error code otherwise.
1614 */
1615int nand_change_read_column_op(struct nand_chip *chip,
1616 unsigned int offset_in_page, void *buf,
1617 unsigned int len, bool force_8bit)
1618{
1619 struct mtd_info *mtd = nand_to_mtd(chip);
1620
1621 if (len && !buf)
1622 return -EINVAL;
1623
1624 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1625 return -EINVAL;
1626
Miquel Raynal8878b122017-11-09 14:16:45 +01001627 /* Small page NANDs do not support column change. */
1628 if (mtd->writesize <= 512)
1629 return -ENOTSUPP;
1630
1631 if (chip->exec_op) {
1632 const struct nand_sdr_timings *sdr =
1633 nand_get_sdr_timings(&chip->data_interface);
1634 u8 addrs[2] = {};
1635 struct nand_op_instr instrs[] = {
1636 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1637 NAND_OP_ADDR(2, addrs, 0),
1638 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1639 PSEC_TO_NSEC(sdr->tCCS_min)),
1640 NAND_OP_DATA_IN(len, buf, 0),
1641 };
1642 struct nand_operation op = NAND_OPERATION(instrs);
1643 int ret;
1644
1645 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1646 if (ret < 0)
1647 return ret;
1648
1649 /* Drop the DATA_IN instruction if len is set to 0. */
1650 if (!len)
1651 op.ninstrs--;
1652
1653 instrs[3].ctx.data.force_8bit = force_8bit;
1654
1655 return nand_exec_op(chip, &op);
1656 }
1657
Boris Brezillon97d90da2017-11-30 18:01:29 +01001658 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1659 if (len)
1660 chip->read_buf(mtd, buf, len);
1661
1662 return 0;
1663}
1664EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1665
1666/**
1667 * nand_read_oob_op - Do a READ OOB operation
1668 * @chip: The NAND chip
1669 * @page: page to read
1670 * @offset_in_oob: offset within the OOB area
1671 * @buf: buffer used to store the data
1672 * @len: length of the buffer
1673 *
1674 * This function issues a READ OOB operation.
1675 * This function does not select/unselect the CS line.
1676 *
1677 * Returns 0 on success, a negative error code otherwise.
1678 */
1679int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1680 unsigned int offset_in_oob, void *buf, unsigned int len)
1681{
1682 struct mtd_info *mtd = nand_to_mtd(chip);
1683
1684 if (len && !buf)
1685 return -EINVAL;
1686
1687 if (offset_in_oob + len > mtd->oobsize)
1688 return -EINVAL;
1689
Miquel Raynal8878b122017-11-09 14:16:45 +01001690 if (chip->exec_op)
1691 return nand_read_page_op(chip, page,
1692 mtd->writesize + offset_in_oob,
1693 buf, len);
1694
Boris Brezillon97d90da2017-11-30 18:01:29 +01001695 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1696 if (len)
1697 chip->read_buf(mtd, buf, len);
1698
1699 return 0;
1700}
1701EXPORT_SYMBOL_GPL(nand_read_oob_op);
1702
Miquel Raynal8878b122017-11-09 14:16:45 +01001703static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1704 unsigned int offset_in_page, const void *buf,
1705 unsigned int len, bool prog)
1706{
1707 struct mtd_info *mtd = nand_to_mtd(chip);
1708 const struct nand_sdr_timings *sdr =
1709 nand_get_sdr_timings(&chip->data_interface);
1710 u8 addrs[5] = {};
1711 struct nand_op_instr instrs[] = {
1712 /*
1713 * The first instruction will be dropped if we're dealing
1714 * with a large page NAND and adjusted if we're dealing
1715 * with a small page NAND and the page offset is > 255.
1716 */
1717 NAND_OP_CMD(NAND_CMD_READ0, 0),
1718 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1719 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1720 NAND_OP_DATA_OUT(len, buf, 0),
1721 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1722 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1723 };
1724 struct nand_operation op = NAND_OPERATION(instrs);
1725 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1726 int ret;
1727 u8 status;
1728
1729 if (naddrs < 0)
1730 return naddrs;
1731
1732 addrs[naddrs++] = page;
1733 addrs[naddrs++] = page >> 8;
1734 if (chip->options & NAND_ROW_ADDR_3)
1735 addrs[naddrs++] = page >> 16;
1736
1737 instrs[2].ctx.addr.naddrs = naddrs;
1738
1739 /* Drop the last two instructions if we're not programming the page. */
1740 if (!prog) {
1741 op.ninstrs -= 2;
1742 /* Also drop the DATA_OUT instruction if empty. */
1743 if (!len)
1744 op.ninstrs--;
1745 }
1746
1747 if (mtd->writesize <= 512) {
1748 /*
1749 * Small pages need some more tweaking: we have to adjust the
1750 * first instruction depending on the page offset we're trying
1751 * to access.
1752 */
1753 if (offset_in_page >= mtd->writesize)
1754 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1755 else if (offset_in_page >= 256 &&
1756 !(chip->options & NAND_BUSWIDTH_16))
1757 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1758 } else {
1759 /*
1760 * Drop the first command if we're dealing with a large page
1761 * NAND.
1762 */
1763 op.instrs++;
1764 op.ninstrs--;
1765 }
1766
1767 ret = nand_exec_op(chip, &op);
1768 if (!prog || ret)
1769 return ret;
1770
1771 ret = nand_status_op(chip, &status);
1772 if (ret)
1773 return ret;
1774
1775 return status;
1776}
1777
Boris Brezillon97d90da2017-11-30 18:01:29 +01001778/**
1779 * nand_prog_page_begin_op - starts a PROG PAGE operation
1780 * @chip: The NAND chip
1781 * @page: page to write
1782 * @offset_in_page: offset within the page
1783 * @buf: buffer containing the data to write to the page
1784 * @len: length of the buffer
1785 *
1786 * This function issues the first half of a PROG PAGE operation.
1787 * This function does not select/unselect the CS line.
1788 *
1789 * Returns 0 on success, a negative error code otherwise.
1790 */
1791int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1792 unsigned int offset_in_page, const void *buf,
1793 unsigned int len)
1794{
1795 struct mtd_info *mtd = nand_to_mtd(chip);
1796
1797 if (len && !buf)
1798 return -EINVAL;
1799
1800 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1801 return -EINVAL;
1802
Miquel Raynal8878b122017-11-09 14:16:45 +01001803 if (chip->exec_op)
1804 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1805 len, false);
1806
Boris Brezillon97d90da2017-11-30 18:01:29 +01001807 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1808
1809 if (buf)
1810 chip->write_buf(mtd, buf, len);
1811
1812 return 0;
1813}
1814EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1815
1816/**
1817 * nand_prog_page_end_op - ends a PROG PAGE operation
1818 * @chip: The NAND chip
1819 *
1820 * This function issues the second half of a PROG PAGE operation.
1821 * This function does not select/unselect the CS line.
1822 *
1823 * Returns 0 on success, a negative error code otherwise.
1824 */
1825int nand_prog_page_end_op(struct nand_chip *chip)
1826{
1827 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001828 int ret;
1829 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001830
Miquel Raynal8878b122017-11-09 14:16:45 +01001831 if (chip->exec_op) {
1832 const struct nand_sdr_timings *sdr =
1833 nand_get_sdr_timings(&chip->data_interface);
1834 struct nand_op_instr instrs[] = {
1835 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1836 PSEC_TO_NSEC(sdr->tWB_max)),
1837 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1838 };
1839 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001840
Miquel Raynal8878b122017-11-09 14:16:45 +01001841 ret = nand_exec_op(chip, &op);
1842 if (ret)
1843 return ret;
1844
1845 ret = nand_status_op(chip, &status);
1846 if (ret)
1847 return ret;
1848 } else {
1849 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1850 ret = chip->waitfunc(mtd, chip);
1851 if (ret < 0)
1852 return ret;
1853
1854 status = ret;
1855 }
1856
Boris Brezillon97d90da2017-11-30 18:01:29 +01001857 if (status & NAND_STATUS_FAIL)
1858 return -EIO;
1859
1860 return 0;
1861}
1862EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1863
1864/**
1865 * nand_prog_page_op - Do a full PROG PAGE operation
1866 * @chip: The NAND chip
1867 * @page: page to write
1868 * @offset_in_page: offset within the page
1869 * @buf: buffer containing the data to write to the page
1870 * @len: length of the buffer
1871 *
1872 * This function issues a full PROG PAGE operation.
1873 * This function does not select/unselect the CS line.
1874 *
1875 * Returns 0 on success, a negative error code otherwise.
1876 */
1877int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1878 unsigned int offset_in_page, const void *buf,
1879 unsigned int len)
1880{
1881 struct mtd_info *mtd = nand_to_mtd(chip);
1882 int status;
1883
1884 if (!len || !buf)
1885 return -EINVAL;
1886
1887 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1888 return -EINVAL;
1889
Miquel Raynal8878b122017-11-09 14:16:45 +01001890 if (chip->exec_op) {
1891 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1892 len, true);
1893 } else {
1894 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1895 chip->write_buf(mtd, buf, len);
1896 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1897 status = chip->waitfunc(mtd, chip);
1898 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001899
Boris Brezillon97d90da2017-11-30 18:01:29 +01001900 if (status & NAND_STATUS_FAIL)
1901 return -EIO;
1902
1903 return 0;
1904}
1905EXPORT_SYMBOL_GPL(nand_prog_page_op);
1906
1907/**
1908 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1909 * @chip: The NAND chip
1910 * @offset_in_page: offset within the page
1911 * @buf: buffer containing the data to send to the NAND
1912 * @len: length of the buffer
1913 * @force_8bit: force 8-bit bus access
1914 *
1915 * This function issues a CHANGE WRITE COLUMN operation.
1916 * This function does not select/unselect the CS line.
1917 *
1918 * Returns 0 on success, a negative error code otherwise.
1919 */
1920int nand_change_write_column_op(struct nand_chip *chip,
1921 unsigned int offset_in_page,
1922 const void *buf, unsigned int len,
1923 bool force_8bit)
1924{
1925 struct mtd_info *mtd = nand_to_mtd(chip);
1926
1927 if (len && !buf)
1928 return -EINVAL;
1929
1930 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1931 return -EINVAL;
1932
Miquel Raynal8878b122017-11-09 14:16:45 +01001933 /* Small page NANDs do not support column change. */
1934 if (mtd->writesize <= 512)
1935 return -ENOTSUPP;
1936
1937 if (chip->exec_op) {
1938 const struct nand_sdr_timings *sdr =
1939 nand_get_sdr_timings(&chip->data_interface);
1940 u8 addrs[2];
1941 struct nand_op_instr instrs[] = {
1942 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1943 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1944 NAND_OP_DATA_OUT(len, buf, 0),
1945 };
1946 struct nand_operation op = NAND_OPERATION(instrs);
1947 int ret;
1948
1949 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1950 if (ret < 0)
1951 return ret;
1952
1953 instrs[2].ctx.data.force_8bit = force_8bit;
1954
1955 /* Drop the DATA_OUT instruction if len is set to 0. */
1956 if (!len)
1957 op.ninstrs--;
1958
1959 return nand_exec_op(chip, &op);
1960 }
1961
Boris Brezillon97d90da2017-11-30 18:01:29 +01001962 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1963 if (len)
1964 chip->write_buf(mtd, buf, len);
1965
1966 return 0;
1967}
1968EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1969
1970/**
1971 * nand_readid_op - Do a READID operation
1972 * @chip: The NAND chip
1973 * @addr: address cycle to pass after the READID command
1974 * @buf: buffer used to store the ID
1975 * @len: length of the buffer
1976 *
1977 * This function sends a READID command and reads back the ID returned by the
1978 * NAND.
1979 * This function does not select/unselect the CS line.
1980 *
1981 * Returns 0 on success, a negative error code otherwise.
1982 */
1983int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1984 unsigned int len)
1985{
1986 struct mtd_info *mtd = nand_to_mtd(chip);
1987 unsigned int i;
1988 u8 *id = buf;
1989
1990 if (len && !buf)
1991 return -EINVAL;
1992
Miquel Raynal8878b122017-11-09 14:16:45 +01001993 if (chip->exec_op) {
1994 const struct nand_sdr_timings *sdr =
1995 nand_get_sdr_timings(&chip->data_interface);
1996 struct nand_op_instr instrs[] = {
1997 NAND_OP_CMD(NAND_CMD_READID, 0),
1998 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1999 NAND_OP_8BIT_DATA_IN(len, buf, 0),
2000 };
2001 struct nand_operation op = NAND_OPERATION(instrs);
2002
2003 /* Drop the DATA_IN instruction if len is set to 0. */
2004 if (!len)
2005 op.ninstrs--;
2006
2007 return nand_exec_op(chip, &op);
2008 }
2009
Boris Brezillon97d90da2017-11-30 18:01:29 +01002010 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
2011
2012 for (i = 0; i < len; i++)
2013 id[i] = chip->read_byte(mtd);
2014
2015 return 0;
2016}
2017EXPORT_SYMBOL_GPL(nand_readid_op);
2018
2019/**
2020 * nand_status_op - Do a STATUS operation
2021 * @chip: The NAND chip
2022 * @status: out variable to store the NAND status
2023 *
2024 * This function sends a STATUS command and reads back the status returned by
2025 * the NAND.
2026 * This function does not select/unselect the CS line.
2027 *
2028 * Returns 0 on success, a negative error code otherwise.
2029 */
2030int nand_status_op(struct nand_chip *chip, u8 *status)
2031{
2032 struct mtd_info *mtd = nand_to_mtd(chip);
2033
Miquel Raynal8878b122017-11-09 14:16:45 +01002034 if (chip->exec_op) {
2035 const struct nand_sdr_timings *sdr =
2036 nand_get_sdr_timings(&chip->data_interface);
2037 struct nand_op_instr instrs[] = {
2038 NAND_OP_CMD(NAND_CMD_STATUS,
2039 PSEC_TO_NSEC(sdr->tADL_min)),
2040 NAND_OP_8BIT_DATA_IN(1, status, 0),
2041 };
2042 struct nand_operation op = NAND_OPERATION(instrs);
2043
2044 if (!status)
2045 op.ninstrs--;
2046
2047 return nand_exec_op(chip, &op);
2048 }
2049
Boris Brezillon97d90da2017-11-30 18:01:29 +01002050 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
2051 if (status)
2052 *status = chip->read_byte(mtd);
2053
2054 return 0;
2055}
2056EXPORT_SYMBOL_GPL(nand_status_op);
2057
2058/**
2059 * nand_exit_status_op - Exit a STATUS operation
2060 * @chip: The NAND chip
2061 *
2062 * This function sends a READ0 command to cancel the effect of the STATUS
2063 * command to avoid reading only the status until a new read command is sent.
2064 *
2065 * This function does not select/unselect the CS line.
2066 *
2067 * Returns 0 on success, a negative error code otherwise.
2068 */
2069int nand_exit_status_op(struct nand_chip *chip)
2070{
2071 struct mtd_info *mtd = nand_to_mtd(chip);
2072
Miquel Raynal8878b122017-11-09 14:16:45 +01002073 if (chip->exec_op) {
2074 struct nand_op_instr instrs[] = {
2075 NAND_OP_CMD(NAND_CMD_READ0, 0),
2076 };
2077 struct nand_operation op = NAND_OPERATION(instrs);
2078
2079 return nand_exec_op(chip, &op);
2080 }
2081
Boris Brezillon97d90da2017-11-30 18:01:29 +01002082 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2083
2084 return 0;
2085}
2086EXPORT_SYMBOL_GPL(nand_exit_status_op);
2087
2088/**
2089 * nand_erase_op - Do an erase operation
2090 * @chip: The NAND chip
2091 * @eraseblock: block to erase
2092 *
2093 * This function sends an ERASE command and waits for the NAND to be ready
2094 * before returning.
2095 * This function does not select/unselect the CS line.
2096 *
2097 * Returns 0 on success, a negative error code otherwise.
2098 */
2099int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2100{
2101 struct mtd_info *mtd = nand_to_mtd(chip);
2102 unsigned int page = eraseblock <<
2103 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002104 int ret;
2105 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002106
Miquel Raynal8878b122017-11-09 14:16:45 +01002107 if (chip->exec_op) {
2108 const struct nand_sdr_timings *sdr =
2109 nand_get_sdr_timings(&chip->data_interface);
2110 u8 addrs[3] = { page, page >> 8, page >> 16 };
2111 struct nand_op_instr instrs[] = {
2112 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2113 NAND_OP_ADDR(2, addrs, 0),
2114 NAND_OP_CMD(NAND_CMD_ERASE2,
2115 PSEC_TO_MSEC(sdr->tWB_max)),
2116 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2117 };
2118 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002119
Miquel Raynal8878b122017-11-09 14:16:45 +01002120 if (chip->options & NAND_ROW_ADDR_3)
2121 instrs[1].ctx.addr.naddrs++;
2122
2123 ret = nand_exec_op(chip, &op);
2124 if (ret)
2125 return ret;
2126
2127 ret = nand_status_op(chip, &status);
2128 if (ret)
2129 return ret;
2130 } else {
2131 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2132 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2133
2134 ret = chip->waitfunc(mtd, chip);
2135 if (ret < 0)
2136 return ret;
2137
2138 status = ret;
2139 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002140
2141 if (status & NAND_STATUS_FAIL)
2142 return -EIO;
2143
2144 return 0;
2145}
2146EXPORT_SYMBOL_GPL(nand_erase_op);
2147
2148/**
2149 * nand_set_features_op - Do a SET FEATURES operation
2150 * @chip: The NAND chip
2151 * @feature: feature id
2152 * @data: 4 bytes of data
2153 *
2154 * This function sends a SET FEATURES command and waits for the NAND to be
2155 * ready before returning.
2156 * This function does not select/unselect the CS line.
2157 *
2158 * Returns 0 on success, a negative error code otherwise.
2159 */
2160static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2161 const void *data)
2162{
2163 struct mtd_info *mtd = nand_to_mtd(chip);
2164 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002165 int i, ret;
2166 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002167
Miquel Raynal8878b122017-11-09 14:16:45 +01002168 if (chip->exec_op) {
2169 const struct nand_sdr_timings *sdr =
2170 nand_get_sdr_timings(&chip->data_interface);
2171 struct nand_op_instr instrs[] = {
2172 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2173 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2174 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2175 PSEC_TO_NSEC(sdr->tWB_max)),
2176 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2177 };
2178 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002179
Miquel Raynal8878b122017-11-09 14:16:45 +01002180 ret = nand_exec_op(chip, &op);
2181 if (ret)
2182 return ret;
2183
2184 ret = nand_status_op(chip, &status);
2185 if (ret)
2186 return ret;
2187 } else {
2188 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 status = ret;
2197 }
2198
Boris Brezillon97d90da2017-11-30 18:01:29 +01002199 if (status & NAND_STATUS_FAIL)
2200 return -EIO;
2201
2202 return 0;
2203}
2204
2205/**
2206 * nand_get_features_op - Do a GET FEATURES operation
2207 * @chip: The NAND chip
2208 * @feature: feature id
2209 * @data: 4 bytes of data
2210 *
2211 * This function sends a GET FEATURES command and waits for the NAND to be
2212 * ready before returning.
2213 * This function does not select/unselect the CS line.
2214 *
2215 * Returns 0 on success, a negative error code otherwise.
2216 */
2217static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2218 void *data)
2219{
2220 struct mtd_info *mtd = nand_to_mtd(chip);
2221 u8 *params = data;
2222 int i;
2223
Miquel Raynal8878b122017-11-09 14:16:45 +01002224 if (chip->exec_op) {
2225 const struct nand_sdr_timings *sdr =
2226 nand_get_sdr_timings(&chip->data_interface);
2227 struct nand_op_instr instrs[] = {
2228 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2229 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2230 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2231 PSEC_TO_NSEC(sdr->tRR_min)),
2232 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2233 data, 0),
2234 };
2235 struct nand_operation op = NAND_OPERATION(instrs);
2236
2237 return nand_exec_op(chip, &op);
2238 }
2239
Boris Brezillon97d90da2017-11-30 18:01:29 +01002240 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2241 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2242 params[i] = chip->read_byte(mtd);
2243
2244 return 0;
2245}
2246
2247/**
2248 * nand_reset_op - Do a reset operation
2249 * @chip: The NAND chip
2250 *
2251 * This function sends a RESET command and waits for the NAND to be ready
2252 * before returning.
2253 * This function does not select/unselect the CS line.
2254 *
2255 * Returns 0 on success, a negative error code otherwise.
2256 */
2257int nand_reset_op(struct nand_chip *chip)
2258{
2259 struct mtd_info *mtd = nand_to_mtd(chip);
2260
Miquel Raynal8878b122017-11-09 14:16:45 +01002261 if (chip->exec_op) {
2262 const struct nand_sdr_timings *sdr =
2263 nand_get_sdr_timings(&chip->data_interface);
2264 struct nand_op_instr instrs[] = {
2265 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2266 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2267 };
2268 struct nand_operation op = NAND_OPERATION(instrs);
2269
2270 return nand_exec_op(chip, &op);
2271 }
2272
Boris Brezillon97d90da2017-11-30 18:01:29 +01002273 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2274
2275 return 0;
2276}
2277EXPORT_SYMBOL_GPL(nand_reset_op);
2278
2279/**
2280 * nand_read_data_op - Read data from the NAND
2281 * @chip: The NAND chip
2282 * @buf: buffer used to store the data
2283 * @len: length of the buffer
2284 * @force_8bit: force 8-bit bus access
2285 *
2286 * This function does a raw data read on the bus. Usually used after launching
2287 * another NAND operation like nand_read_page_op().
2288 * This function does not select/unselect the CS line.
2289 *
2290 * Returns 0 on success, a negative error code otherwise.
2291 */
2292int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2293 bool force_8bit)
2294{
2295 struct mtd_info *mtd = nand_to_mtd(chip);
2296
2297 if (!len || !buf)
2298 return -EINVAL;
2299
Miquel Raynal8878b122017-11-09 14:16:45 +01002300 if (chip->exec_op) {
2301 struct nand_op_instr instrs[] = {
2302 NAND_OP_DATA_IN(len, buf, 0),
2303 };
2304 struct nand_operation op = NAND_OPERATION(instrs);
2305
2306 instrs[0].ctx.data.force_8bit = force_8bit;
2307
2308 return nand_exec_op(chip, &op);
2309 }
2310
Boris Brezillon97d90da2017-11-30 18:01:29 +01002311 if (force_8bit) {
2312 u8 *p = buf;
2313 unsigned int i;
2314
2315 for (i = 0; i < len; i++)
2316 p[i] = chip->read_byte(mtd);
2317 } else {
2318 chip->read_buf(mtd, buf, len);
2319 }
2320
2321 return 0;
2322}
2323EXPORT_SYMBOL_GPL(nand_read_data_op);
2324
2325/**
2326 * nand_write_data_op - Write data from the NAND
2327 * @chip: The NAND chip
2328 * @buf: buffer containing the data to send on the bus
2329 * @len: length of the buffer
2330 * @force_8bit: force 8-bit bus access
2331 *
2332 * This function does a raw data write on the bus. Usually used after launching
2333 * another NAND operation like nand_write_page_begin_op().
2334 * This function does not select/unselect the CS line.
2335 *
2336 * Returns 0 on success, a negative error code otherwise.
2337 */
2338int nand_write_data_op(struct nand_chip *chip, const void *buf,
2339 unsigned int len, bool force_8bit)
2340{
2341 struct mtd_info *mtd = nand_to_mtd(chip);
2342
2343 if (!len || !buf)
2344 return -EINVAL;
2345
Miquel Raynal8878b122017-11-09 14:16:45 +01002346 if (chip->exec_op) {
2347 struct nand_op_instr instrs[] = {
2348 NAND_OP_DATA_OUT(len, buf, 0),
2349 };
2350 struct nand_operation op = NAND_OPERATION(instrs);
2351
2352 instrs[0].ctx.data.force_8bit = force_8bit;
2353
2354 return nand_exec_op(chip, &op);
2355 }
2356
Boris Brezillon97d90da2017-11-30 18:01:29 +01002357 if (force_8bit) {
2358 const u8 *p = buf;
2359 unsigned int i;
2360
2361 for (i = 0; i < len; i++)
2362 chip->write_byte(mtd, p[i]);
2363 } else {
2364 chip->write_buf(mtd, buf, len);
2365 }
2366
2367 return 0;
2368}
2369EXPORT_SYMBOL_GPL(nand_write_data_op);
2370
2371/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002372 * struct nand_op_parser_ctx - Context used by the parser
2373 * @instrs: array of all the instructions that must be addressed
2374 * @ninstrs: length of the @instrs array
2375 * @subop: Sub-operation to be passed to the NAND controller
2376 *
2377 * This structure is used by the core to split NAND operations into
2378 * sub-operations that can be handled by the NAND controller.
2379 */
2380struct nand_op_parser_ctx {
2381 const struct nand_op_instr *instrs;
2382 unsigned int ninstrs;
2383 struct nand_subop subop;
2384};
2385
2386/**
2387 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2388 * @pat: the parser pattern element that matches @instr
2389 * @instr: pointer to the instruction to check
2390 * @start_offset: this is an in/out parameter. If @instr has already been
2391 * split, then @start_offset is the offset from which to start
2392 * (either an address cycle or an offset in the data buffer).
2393 * Conversely, if the function returns true (ie. instr must be
2394 * split), this parameter is updated to point to the first
2395 * data/address cycle that has not been taken care of.
2396 *
2397 * Some NAND controllers are limited and cannot send X address cycles with a
2398 * unique operation, or cannot read/write more than Y bytes at the same time.
2399 * In this case, split the instruction that does not fit in a single
2400 * controller-operation into two or more chunks.
2401 *
2402 * Returns true if the instruction must be split, false otherwise.
2403 * The @start_offset parameter is also updated to the offset at which the next
2404 * bundle of instruction must start (if an address or a data instruction).
2405 */
2406static bool
2407nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2408 const struct nand_op_instr *instr,
2409 unsigned int *start_offset)
2410{
2411 switch (pat->type) {
2412 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002413 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002414 break;
2415
2416 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002417 pat->ctx.addr.maxcycles) {
2418 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002419 return true;
2420 }
2421 break;
2422
2423 case NAND_OP_DATA_IN_INSTR:
2424 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002425 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002426 break;
2427
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002428 if (instr->ctx.data.len - *start_offset >
2429 pat->ctx.data.maxlen) {
2430 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002431 return true;
2432 }
2433 break;
2434
2435 default:
2436 break;
2437 }
2438
2439 return false;
2440}
2441
2442/**
2443 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2444 * remaining in the parser context
2445 * @pat: the pattern to test
2446 * @ctx: the parser context structure to match with the pattern @pat
2447 *
2448 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2449 * Returns true if this is the case, false ortherwise. When true is returned,
2450 * @ctx->subop is updated with the set of instructions to be passed to the
2451 * controller driver.
2452 */
2453static bool
2454nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2455 struct nand_op_parser_ctx *ctx)
2456{
2457 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2458 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2459 const struct nand_op_instr *instr = ctx->subop.instrs;
2460 unsigned int i, ninstrs;
2461
2462 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2463 /*
2464 * The pattern instruction does not match the operation
2465 * instruction. If the instruction is marked optional in the
2466 * pattern definition, we skip the pattern element and continue
2467 * to the next one. If the element is mandatory, there's no
2468 * match and we can return false directly.
2469 */
2470 if (instr->type != pat->elems[i].type) {
2471 if (!pat->elems[i].optional)
2472 return false;
2473
2474 continue;
2475 }
2476
2477 /*
2478 * Now check the pattern element constraints. If the pattern is
2479 * not able to handle the whole instruction in a single step,
2480 * we have to split it.
2481 * The last_instr_end_off value comes back updated to point to
2482 * the position where we have to split the instruction (the
2483 * start of the next subop chunk).
2484 */
2485 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2486 &instr_offset)) {
2487 ninstrs++;
2488 i++;
2489 break;
2490 }
2491
2492 instr++;
2493 ninstrs++;
2494 instr_offset = 0;
2495 }
2496
2497 /*
2498 * This can happen if all instructions of a pattern are optional.
2499 * Still, if there's not at least one instruction handled by this
2500 * pattern, this is not a match, and we should try the next one (if
2501 * any).
2502 */
2503 if (!ninstrs)
2504 return false;
2505
2506 /*
2507 * We had a match on the pattern head, but the pattern may be longer
2508 * than the instructions we're asked to execute. We need to make sure
2509 * there's no mandatory elements in the pattern tail.
2510 */
2511 for (; i < pat->nelems; i++) {
2512 if (!pat->elems[i].optional)
2513 return false;
2514 }
2515
2516 /*
2517 * We have a match: update the subop structure accordingly and return
2518 * true.
2519 */
2520 ctx->subop.ninstrs = ninstrs;
2521 ctx->subop.last_instr_end_off = instr_offset;
2522
2523 return true;
2524}
2525
2526#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2527static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2528{
2529 const struct nand_op_instr *instr;
2530 char *prefix = " ";
2531 unsigned int i;
2532
2533 pr_debug("executing subop:\n");
2534
2535 for (i = 0; i < ctx->ninstrs; i++) {
2536 instr = &ctx->instrs[i];
2537
2538 if (instr == &ctx->subop.instrs[0])
2539 prefix = " ->";
2540
2541 switch (instr->type) {
2542 case NAND_OP_CMD_INSTR:
2543 pr_debug("%sCMD [0x%02x]\n", prefix,
2544 instr->ctx.cmd.opcode);
2545 break;
2546 case NAND_OP_ADDR_INSTR:
2547 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2548 instr->ctx.addr.naddrs,
2549 instr->ctx.addr.naddrs < 64 ?
2550 instr->ctx.addr.naddrs : 64,
2551 instr->ctx.addr.addrs);
2552 break;
2553 case NAND_OP_DATA_IN_INSTR:
2554 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2555 instr->ctx.data.len,
2556 instr->ctx.data.force_8bit ?
2557 ", force 8-bit" : "");
2558 break;
2559 case NAND_OP_DATA_OUT_INSTR:
2560 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2561 instr->ctx.data.len,
2562 instr->ctx.data.force_8bit ?
2563 ", force 8-bit" : "");
2564 break;
2565 case NAND_OP_WAITRDY_INSTR:
2566 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2567 instr->ctx.waitrdy.timeout_ms);
2568 break;
2569 }
2570
2571 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2572 prefix = " ";
2573 }
2574}
2575#else
2576static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2577{
2578 /* NOP */
2579}
2580#endif
2581
2582/**
2583 * nand_op_parser_exec_op - exec_op parser
2584 * @chip: the NAND chip
2585 * @parser: patterns description provided by the controller driver
2586 * @op: the NAND operation to address
2587 * @check_only: when true, the function only checks if @op can be handled but
2588 * does not execute the operation
2589 *
2590 * Helper function designed to ease integration of NAND controller drivers that
2591 * only support a limited set of instruction sequences. The supported sequences
2592 * are described in @parser, and the framework takes care of splitting @op into
2593 * multiple sub-operations (if required) and pass them back to the ->exec()
2594 * callback of the matching pattern if @check_only is set to false.
2595 *
2596 * NAND controller drivers should call this function from their own ->exec_op()
2597 * implementation.
2598 *
2599 * Returns 0 on success, a negative error code otherwise. A failure can be
2600 * caused by an unsupported operation (none of the supported patterns is able
2601 * to handle the requested operation), or an error returned by one of the
2602 * matching pattern->exec() hook.
2603 */
2604int nand_op_parser_exec_op(struct nand_chip *chip,
2605 const struct nand_op_parser *parser,
2606 const struct nand_operation *op, bool check_only)
2607{
2608 struct nand_op_parser_ctx ctx = {
2609 .subop.instrs = op->instrs,
2610 .instrs = op->instrs,
2611 .ninstrs = op->ninstrs,
2612 };
2613 unsigned int i;
2614
2615 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2616 int ret;
2617
2618 for (i = 0; i < parser->npatterns; i++) {
2619 const struct nand_op_parser_pattern *pattern;
2620
2621 pattern = &parser->patterns[i];
2622 if (!nand_op_parser_match_pat(pattern, &ctx))
2623 continue;
2624
2625 nand_op_parser_trace(&ctx);
2626
2627 if (check_only)
2628 break;
2629
2630 ret = pattern->exec(chip, &ctx.subop);
2631 if (ret)
2632 return ret;
2633
2634 break;
2635 }
2636
2637 if (i == parser->npatterns) {
2638 pr_debug("->exec_op() parser: pattern not found!\n");
2639 return -ENOTSUPP;
2640 }
2641
2642 /*
2643 * Update the context structure by pointing to the start of the
2644 * next subop.
2645 */
2646 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2647 if (ctx.subop.last_instr_end_off)
2648 ctx.subop.instrs -= 1;
2649
2650 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2651 }
2652
2653 return 0;
2654}
2655EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2656
2657static bool nand_instr_is_data(const struct nand_op_instr *instr)
2658{
2659 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2660 instr->type == NAND_OP_DATA_OUT_INSTR);
2661}
2662
2663static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2664 unsigned int instr_idx)
2665{
2666 return subop && instr_idx < subop->ninstrs;
2667}
2668
2669static int nand_subop_get_start_off(const struct nand_subop *subop,
2670 unsigned int instr_idx)
2671{
2672 if (instr_idx)
2673 return 0;
2674
2675 return subop->first_instr_start_off;
2676}
2677
2678/**
2679 * nand_subop_get_addr_start_off - Get the start offset in an address array
2680 * @subop: The entire sub-operation
2681 * @instr_idx: Index of the instruction inside the sub-operation
2682 *
2683 * During driver development, one could be tempted to directly use the
2684 * ->addr.addrs field of address instructions. This is wrong as address
2685 * instructions might be split.
2686 *
2687 * Given an address instruction, returns the offset of the first cycle to issue.
2688 */
2689int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2690 unsigned int instr_idx)
2691{
2692 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2693 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2694 return -EINVAL;
2695
2696 return nand_subop_get_start_off(subop, instr_idx);
2697}
2698EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2699
2700/**
2701 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2702 * @subop: The entire sub-operation
2703 * @instr_idx: Index of the instruction inside the sub-operation
2704 *
2705 * During driver development, one could be tempted to directly use the
2706 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2707 * might be split.
2708 *
2709 * Given an address instruction, returns the number of address cycle to issue.
2710 */
2711int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2712 unsigned int instr_idx)
2713{
2714 int start_off, end_off;
2715
2716 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2717 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2718 return -EINVAL;
2719
2720 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2721
2722 if (instr_idx == subop->ninstrs - 1 &&
2723 subop->last_instr_end_off)
2724 end_off = subop->last_instr_end_off;
2725 else
2726 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2727
2728 return end_off - start_off;
2729}
2730EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2731
2732/**
2733 * nand_subop_get_data_start_off - Get the start offset in a data array
2734 * @subop: The entire sub-operation
2735 * @instr_idx: Index of the instruction inside the sub-operation
2736 *
2737 * During driver development, one could be tempted to directly use the
2738 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2739 * instructions might be split.
2740 *
2741 * Given a data instruction, returns the offset to start from.
2742 */
2743int nand_subop_get_data_start_off(const struct nand_subop *subop,
2744 unsigned int instr_idx)
2745{
2746 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2747 !nand_instr_is_data(&subop->instrs[instr_idx]))
2748 return -EINVAL;
2749
2750 return nand_subop_get_start_off(subop, instr_idx);
2751}
2752EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2753
2754/**
2755 * nand_subop_get_data_len - Get the number of bytes to retrieve
2756 * @subop: The entire sub-operation
2757 * @instr_idx: Index of the instruction inside the sub-operation
2758 *
2759 * During driver development, one could be tempted to directly use the
2760 * ->data->len field of a data instruction. This is wrong as data instructions
2761 * might be split.
2762 *
2763 * Returns the length of the chunk of data to send/receive.
2764 */
2765int nand_subop_get_data_len(const struct nand_subop *subop,
2766 unsigned int instr_idx)
2767{
2768 int start_off = 0, end_off;
2769
2770 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2771 !nand_instr_is_data(&subop->instrs[instr_idx]))
2772 return -EINVAL;
2773
2774 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2775
2776 if (instr_idx == subop->ninstrs - 1 &&
2777 subop->last_instr_end_off)
2778 end_off = subop->last_instr_end_off;
2779 else
2780 end_off = subop->instrs[instr_idx].ctx.data.len;
2781
2782 return end_off - start_off;
2783}
2784EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002787 * nand_reset - Reset and initialize a NAND device
2788 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002789 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002790 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002791 * Save the timings data structure, then apply SDR timings mode 0 (see
2792 * nand_reset_data_interface for details), do the reset operation, and
2793 * apply back the previous timings.
2794 *
2795 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002796 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002797int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002798{
2799 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal17fa8042017-11-30 18:01:31 +01002800 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002801 int ret;
2802
Boris Brezillon104e4422017-03-16 09:35:58 +01002803 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002804 if (ret)
2805 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002806
Boris Brezillon73f907f2016-10-24 16:46:20 +02002807 /*
2808 * The CS line has to be released before we can apply the new NAND
2809 * interface settings, hence this weird ->select_chip() dance.
2810 */
2811 chip->select_chip(mtd, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002812 ret = nand_reset_op(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02002813 chip->select_chip(mtd, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002814 if (ret)
2815 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002816
Miquel Raynal107b7d62018-03-19 14:47:25 +01002817 /*
2818 * A nand_reset_data_interface() put both the NAND chip and the NAND
2819 * controller in timings mode 0. If the default mode for this chip is
2820 * also 0, no need to proceed to the change again. Plus, at probe time,
2821 * nand_setup_data_interface() uses ->set/get_features() which would
2822 * fail anyway as the parameter page is not available yet.
2823 */
2824 if (!chip->onfi_timing_mode_default)
2825 return 0;
2826
Miquel Raynal17fa8042017-11-30 18:01:31 +01002827 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002828 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002829 if (ret)
2830 return ret;
2831
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002832 return 0;
2833}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002834EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002835
2836/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002837 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2838 * @buf: buffer to test
2839 * @len: buffer length
2840 * @bitflips_threshold: maximum number of bitflips
2841 *
2842 * Check if a buffer contains only 0xff, which means the underlying region
2843 * has been erased and is ready to be programmed.
2844 * The bitflips_threshold specify the maximum number of bitflips before
2845 * considering the region is not erased.
2846 * Note: The logic of this function has been extracted from the memweight
2847 * implementation, except that nand_check_erased_buf function exit before
2848 * testing the whole buffer if the number of bitflips exceed the
2849 * bitflips_threshold value.
2850 *
2851 * Returns a positive number of bitflips less than or equal to
2852 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2853 * threshold.
2854 */
2855static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2856{
2857 const unsigned char *bitmap = buf;
2858 int bitflips = 0;
2859 int weight;
2860
2861 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2862 len--, bitmap++) {
2863 weight = hweight8(*bitmap);
2864 bitflips += BITS_PER_BYTE - weight;
2865 if (unlikely(bitflips > bitflips_threshold))
2866 return -EBADMSG;
2867 }
2868
2869 for (; len >= sizeof(long);
2870 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002871 unsigned long d = *((unsigned long *)bitmap);
2872 if (d == ~0UL)
2873 continue;
2874 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002875 bitflips += BITS_PER_LONG - weight;
2876 if (unlikely(bitflips > bitflips_threshold))
2877 return -EBADMSG;
2878 }
2879
2880 for (; len > 0; len--, bitmap++) {
2881 weight = hweight8(*bitmap);
2882 bitflips += BITS_PER_BYTE - weight;
2883 if (unlikely(bitflips > bitflips_threshold))
2884 return -EBADMSG;
2885 }
2886
2887 return bitflips;
2888}
2889
2890/**
2891 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2892 * 0xff data
2893 * @data: data buffer to test
2894 * @datalen: data length
2895 * @ecc: ECC buffer
2896 * @ecclen: ECC length
2897 * @extraoob: extra OOB buffer
2898 * @extraooblen: extra OOB length
2899 * @bitflips_threshold: maximum number of bitflips
2900 *
2901 * Check if a data buffer and its associated ECC and OOB data contains only
2902 * 0xff pattern, which means the underlying region has been erased and is
2903 * ready to be programmed.
2904 * The bitflips_threshold specify the maximum number of bitflips before
2905 * considering the region as not erased.
2906 *
2907 * Note:
2908 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2909 * different from the NAND page size. When fixing bitflips, ECC engines will
2910 * report the number of errors per chunk, and the NAND core infrastructure
2911 * expect you to return the maximum number of bitflips for the whole page.
2912 * This is why you should always use this function on a single chunk and
2913 * not on the whole page. After checking each chunk you should update your
2914 * max_bitflips value accordingly.
2915 * 2/ When checking for bitflips in erased pages you should not only check
2916 * the payload data but also their associated ECC data, because a user might
2917 * have programmed almost all bits to 1 but a few. In this case, we
2918 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2919 * this case.
2920 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2921 * data are protected by the ECC engine.
2922 * It could also be used if you support subpages and want to attach some
2923 * extra OOB data to an ECC chunk.
2924 *
2925 * Returns a positive number of bitflips less than or equal to
2926 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2927 * threshold. In case of success, the passed buffers are filled with 0xff.
2928 */
2929int nand_check_erased_ecc_chunk(void *data, int datalen,
2930 void *ecc, int ecclen,
2931 void *extraoob, int extraooblen,
2932 int bitflips_threshold)
2933{
2934 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2935
2936 data_bitflips = nand_check_erased_buf(data, datalen,
2937 bitflips_threshold);
2938 if (data_bitflips < 0)
2939 return data_bitflips;
2940
2941 bitflips_threshold -= data_bitflips;
2942
2943 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2944 if (ecc_bitflips < 0)
2945 return ecc_bitflips;
2946
2947 bitflips_threshold -= ecc_bitflips;
2948
2949 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2950 bitflips_threshold);
2951 if (extraoob_bitflips < 0)
2952 return extraoob_bitflips;
2953
2954 if (data_bitflips)
2955 memset(data, 0xff, datalen);
2956
2957 if (ecc_bitflips)
2958 memset(ecc, 0xff, ecclen);
2959
2960 if (extraoob_bitflips)
2961 memset(extraoob, 0xff, extraooblen);
2962
2963 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2964}
2965EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2966
2967/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002968 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002969 * @mtd: mtd info structure
2970 * @chip: nand chip info structure
2971 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002972 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002973 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002974 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002975 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002976 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002977int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2978 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002979{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002980 int ret;
2981
Boris Brezillon25f815f2017-11-30 18:01:30 +01002982 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002983 if (ret)
2984 return ret;
2985
2986 if (oob_required) {
2987 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2988 false);
2989 if (ret)
2990 return ret;
2991 }
2992
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002993 return 0;
2994}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002995EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002996
2997/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002998 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002999 * @mtd: mtd info structure
3000 * @chip: nand chip info structure
3001 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003002 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003003 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003004 *
3005 * We need a special oob layout and handling even when OOB isn't used.
3006 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003007static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003008 struct nand_chip *chip, uint8_t *buf,
3009 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003010{
3011 int eccsize = chip->ecc.size;
3012 int eccbytes = chip->ecc.bytes;
3013 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003014 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003015
Boris Brezillon25f815f2017-11-30 18:01:30 +01003016 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3017 if (ret)
3018 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003019
3020 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003021 ret = nand_read_data_op(chip, buf, eccsize, false);
3022 if (ret)
3023 return ret;
3024
David Brownell52ff49d2009-03-04 12:01:36 -08003025 buf += eccsize;
3026
3027 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003028 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3029 false);
3030 if (ret)
3031 return ret;
3032
David Brownell52ff49d2009-03-04 12:01:36 -08003033 oob += chip->ecc.prepad;
3034 }
3035
Boris Brezillon97d90da2017-11-30 18:01:29 +01003036 ret = nand_read_data_op(chip, oob, eccbytes, false);
3037 if (ret)
3038 return ret;
3039
David Brownell52ff49d2009-03-04 12:01:36 -08003040 oob += eccbytes;
3041
3042 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003043 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3044 false);
3045 if (ret)
3046 return ret;
3047
David Brownell52ff49d2009-03-04 12:01:36 -08003048 oob += chip->ecc.postpad;
3049 }
3050 }
3051
3052 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003053 if (size) {
3054 ret = nand_read_data_op(chip, oob, size, false);
3055 if (ret)
3056 return ret;
3057 }
David Brownell52ff49d2009-03-04 12:01:36 -08003058
3059 return 0;
3060}
3061
3062/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003063 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003064 * @mtd: mtd info structure
3065 * @chip: nand chip info structure
3066 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003067 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003068 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003069 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003070static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003071 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Boris Brezillon846031d2016-02-03 20:11:00 +01003073 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003074 int eccbytes = chip->ecc.bytes;
3075 int eccsteps = chip->ecc.steps;
3076 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003077 uint8_t *ecc_calc = chip->ecc.calc_buf;
3078 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003079 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003080
Brian Norris1fbb9382012-05-02 10:14:55 -07003081 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003082
3083 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3084 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3085
Boris Brezillon846031d2016-02-03 20:11:00 +01003086 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3087 chip->ecc.total);
3088 if (ret)
3089 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003090
3091 eccsteps = chip->ecc.steps;
3092 p = buf;
3093
3094 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3095 int stat;
3096
3097 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003098 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003099 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003100 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003101 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003102 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3103 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003104 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003105 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303109 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003110 * @mtd: mtd info structure
3111 * @chip: nand chip info structure
3112 * @data_offs: offset of requested data within the page
3113 * @readlen: data length
3114 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003115 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003116 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003117static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003118 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3119 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003120{
Boris Brezillon846031d2016-02-03 20:11:00 +01003121 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003122 uint8_t *p;
3123 int data_col_addr, i, gaps = 0;
3124 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3125 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003126 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003127 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003128 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003129
Brian Norris7854d3f2011-06-23 14:12:08 -07003130 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003131 start_step = data_offs / chip->ecc.size;
3132 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3133 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303134 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003135
Brian Norris8b6e50c2011-05-25 14:59:01 -07003136 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003137 datafrag_len = num_steps * chip->ecc.size;
3138 eccfrag_len = num_steps * chip->ecc.bytes;
3139
3140 data_col_addr = start_step * chip->ecc.size;
3141 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003142 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003143 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003144 if (ret)
3145 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003146
Brian Norris8b6e50c2011-05-25 14:59:01 -07003147 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003148 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003149 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003150
Brian Norris8b6e50c2011-05-25 14:59:01 -07003151 /*
3152 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003153 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003154 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003155 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3156 if (ret)
3157 return ret;
3158
3159 if (oobregion.length < eccfrag_len)
3160 gaps = 1;
3161
Alexey Korolev3d459552008-05-15 17:23:18 +01003162 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003163 ret = nand_change_read_column_op(chip, mtd->writesize,
3164 chip->oob_poi, mtd->oobsize,
3165 false);
3166 if (ret)
3167 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003168 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003169 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003170 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003171 * about buswidth alignment in read_buf.
3172 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003173 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003174 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003175 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003176 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003177 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3178 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003179 aligned_len++;
3180
Boris Brezillon97d90da2017-11-30 18:01:29 +01003181 ret = nand_change_read_column_op(chip,
3182 mtd->writesize + aligned_pos,
3183 &chip->oob_poi[aligned_pos],
3184 aligned_len, false);
3185 if (ret)
3186 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003187 }
3188
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003189 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003190 chip->oob_poi, index, eccfrag_len);
3191 if (ret)
3192 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003193
3194 p = bufpoi + data_col_addr;
3195 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3196 int stat;
3197
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003198 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3199 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003200 if (stat == -EBADMSG &&
3201 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3202 /* check for empty pages with bitflips */
3203 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003204 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003205 chip->ecc.bytes,
3206 NULL, 0,
3207 chip->ecc.strength);
3208 }
3209
Mike Dunn3f91e942012-04-25 12:06:09 -07003210 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003211 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003212 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003213 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003214 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3215 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003216 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003217 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003218}
3219
3220/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003221 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003222 * @mtd: mtd info structure
3223 * @chip: nand chip info structure
3224 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003225 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003226 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003227 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003228 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003229 */
3230static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003231 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003232{
Boris Brezillon846031d2016-02-03 20:11:00 +01003233 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003234 int eccbytes = chip->ecc.bytes;
3235 int eccsteps = chip->ecc.steps;
3236 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003237 uint8_t *ecc_calc = chip->ecc.calc_buf;
3238 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003239 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003240
Boris Brezillon25f815f2017-11-30 18:01:30 +01003241 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3242 if (ret)
3243 return ret;
3244
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003245 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3246 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003247
3248 ret = nand_read_data_op(chip, p, eccsize, false);
3249 if (ret)
3250 return ret;
3251
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003252 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3253 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003254
3255 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3256 if (ret)
3257 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003258
Boris Brezillon846031d2016-02-03 20:11:00 +01003259 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3260 chip->ecc.total);
3261 if (ret)
3262 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003263
3264 eccsteps = chip->ecc.steps;
3265 p = buf;
3266
3267 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3268 int stat;
3269
3270 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003271 if (stat == -EBADMSG &&
3272 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3273 /* check for empty pages with bitflips */
3274 stat = nand_check_erased_ecc_chunk(p, eccsize,
3275 &ecc_code[i], eccbytes,
3276 NULL, 0,
3277 chip->ecc.strength);
3278 }
3279
Mike Dunn3f91e942012-04-25 12:06:09 -07003280 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003281 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003282 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003283 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003284 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3285 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003286 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003287 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003288}
3289
3290/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003291 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003292 * @mtd: mtd info structure
3293 * @chip: nand chip info structure
3294 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003295 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003296 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003297 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003298 * Hardware ECC for large page chips, require OOB to be read first. For this
3299 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3300 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3301 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3302 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003303 */
3304static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003305 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003306{
Boris Brezillon846031d2016-02-03 20:11:00 +01003307 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003308 int eccbytes = chip->ecc.bytes;
3309 int eccsteps = chip->ecc.steps;
3310 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003311 uint8_t *ecc_code = chip->ecc.code_buf;
3312 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003313 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003314
3315 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003316 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3317 if (ret)
3318 return ret;
3319
3320 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3321 if (ret)
3322 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003323
Boris Brezillon846031d2016-02-03 20:11:00 +01003324 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3325 chip->ecc.total);
3326 if (ret)
3327 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003328
3329 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3330 int stat;
3331
3332 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003333
3334 ret = nand_read_data_op(chip, p, eccsize, false);
3335 if (ret)
3336 return ret;
3337
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003338 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3339
3340 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003341 if (stat == -EBADMSG &&
3342 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3343 /* check for empty pages with bitflips */
3344 stat = nand_check_erased_ecc_chunk(p, eccsize,
3345 &ecc_code[i], eccbytes,
3346 NULL, 0,
3347 chip->ecc.strength);
3348 }
3349
Mike Dunn3f91e942012-04-25 12:06:09 -07003350 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003351 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003352 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003353 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003354 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3355 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003356 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003357 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003358}
3359
3360/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003361 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003362 * @mtd: mtd info structure
3363 * @chip: nand chip info structure
3364 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003365 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003366 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003367 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003368 * The hw generator calculates the error syndrome automatically. Therefore we
3369 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003370 */
3371static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003372 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003373{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003374 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003375 int eccbytes = chip->ecc.bytes;
3376 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003377 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003378 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003379 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003380 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003381
Boris Brezillon25f815f2017-11-30 18:01:30 +01003382 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3383 if (ret)
3384 return ret;
3385
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003386 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3387 int stat;
3388
3389 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003390
3391 ret = nand_read_data_op(chip, p, eccsize, false);
3392 if (ret)
3393 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003394
3395 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003396 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3397 false);
3398 if (ret)
3399 return ret;
3400
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003401 oob += chip->ecc.prepad;
3402 }
3403
3404 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003405
3406 ret = nand_read_data_op(chip, oob, eccbytes, false);
3407 if (ret)
3408 return ret;
3409
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003410 stat = chip->ecc.correct(mtd, p, oob, NULL);
3411
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003412 oob += eccbytes;
3413
3414 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003415 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3416 false);
3417 if (ret)
3418 return ret;
3419
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003420 oob += chip->ecc.postpad;
3421 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003422
3423 if (stat == -EBADMSG &&
3424 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3425 /* check for empty pages with bitflips */
3426 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3427 oob - eccpadbytes,
3428 eccpadbytes,
3429 NULL, 0,
3430 chip->ecc.strength);
3431 }
3432
3433 if (stat < 0) {
3434 mtd->ecc_stats.failed++;
3435 } else {
3436 mtd->ecc_stats.corrected += stat;
3437 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3438 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003439 }
3440
3441 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003442 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003443 if (i) {
3444 ret = nand_read_data_op(chip, oob, i, false);
3445 if (ret)
3446 return ret;
3447 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003448
Mike Dunn3f91e942012-04-25 12:06:09 -07003449 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003450}
3451
3452/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003453 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003454 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003455 * @oob: oob destination address
3456 * @ops: oob ops structure
3457 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003458 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003459static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003460 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003461{
Boris Brezillon846031d2016-02-03 20:11:00 +01003462 struct nand_chip *chip = mtd_to_nand(mtd);
3463 int ret;
3464
Florian Fainellif8ac0412010-09-07 13:23:43 +02003465 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003466
Brian Norris0612b9d2011-08-30 18:45:40 -07003467 case MTD_OPS_PLACE_OOB:
3468 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003469 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3470 return oob + len;
3471
Boris Brezillon846031d2016-02-03 20:11:00 +01003472 case MTD_OPS_AUTO_OOB:
3473 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3474 ops->ooboffs, len);
3475 BUG_ON(ret);
3476 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003477
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003478 default:
3479 BUG();
3480 }
3481 return NULL;
3482}
3483
3484/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003485 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3486 * @mtd: MTD device structure
3487 * @retry_mode: the retry mode to use
3488 *
3489 * Some vendors supply a special command to shift the Vt threshold, to be used
3490 * when there are too many bitflips in a page (i.e., ECC error). After setting
3491 * a new threshold, the host should retry reading the page.
3492 */
3493static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3494{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003495 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08003496
3497 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3498
3499 if (retry_mode >= chip->read_retries)
3500 return -EINVAL;
3501
3502 if (!chip->setup_read_retry)
3503 return -EOPNOTSUPP;
3504
3505 return chip->setup_read_retry(mtd, retry_mode);
3506}
3507
3508/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003509 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003510 * @mtd: MTD device structure
3511 * @from: offset to read from
3512 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003513 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003514 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003515 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003516static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3517 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003518{
Brian Norrise47f3db2012-05-02 10:14:56 -07003519 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003520 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003521 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003522 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003523 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003524 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003525
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003526 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003527 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003528 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003529 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003530 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003532 chipnr = (int)(from >> chip->chip_shift);
3533 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003535 realpage = (int)(from >> chip->page_shift);
3536 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003538 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003540 buf = ops->datbuf;
3541 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003542 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003543
Florian Fainellif8ac0412010-09-07 13:23:43 +02003544 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003545 unsigned int ecc_failures = mtd->ecc_stats.failed;
3546
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003547 bytes = min(mtd->writesize - col, readlen);
3548 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003549
Kamal Dasu66507c72014-05-01 20:51:19 -04003550 if (!aligned)
3551 use_bufpoi = 1;
3552 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003553 use_bufpoi = !virt_addr_valid(buf) ||
3554 !IS_ALIGNED((unsigned long)buf,
3555 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003556 else
3557 use_bufpoi = 0;
3558
Brian Norris8b6e50c2011-05-25 14:59:01 -07003559 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003560 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003561 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003562
3563 if (use_bufpoi && aligned)
3564 pr_debug("%s: using read bounce buffer for buf@%p\n",
3565 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Brian Norrisba84fb52014-01-03 15:13:33 -08003567read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003568 /*
3569 * Now read the page into the buffer. Absent an error,
3570 * the read methods return max bitflips per ecc step.
3571 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003572 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07003573 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003574 oob_required,
3575 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003576 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3577 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003578 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003579 col, bytes, bufpoi,
3580 page);
David Woodhouse956e9442006-09-25 17:12:39 +01003581 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07003582 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003583 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003584 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003585 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003586 /* Invalidate page cache */
3587 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003588 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003589 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003590
3591 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003592 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003593 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003594 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003595 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003596 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003597 chip->pagebuf_bitflips = ret;
3598 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003599 /* Invalidate page cache */
3600 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003601 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003602 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003604
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003605 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003606 int toread = min(oobreadlen, max_oobsize);
3607
3608 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003609 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003610 oob, ops, toread);
3611 oobreadlen -= toread;
3612 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003613 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003614
3615 if (chip->options & NAND_NEED_READRDY) {
3616 /* Apply delay or wait for ready/busy pin */
3617 if (!chip->dev_ready)
3618 udelay(chip->chip_delay);
3619 else
3620 nand_wait_ready(mtd);
3621 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08003622
Brian Norrisba84fb52014-01-03 15:13:33 -08003623 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003624 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003625 retry_mode++;
3626 ret = nand_setup_read_retry(mtd,
3627 retry_mode);
3628 if (ret < 0)
3629 break;
3630
3631 /* Reset failures; retry */
3632 mtd->ecc_stats.failed = ecc_failures;
3633 goto read_retry;
3634 } else {
3635 /* No more retry modes; real failure */
3636 ecc_fail = true;
3637 }
3638 }
3639
3640 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003641 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003642 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003643 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003644 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003645 max_bitflips = max_t(unsigned int, max_bitflips,
3646 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003649 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003650
Brian Norrisba84fb52014-01-03 15:13:33 -08003651 /* Reset to retry mode 0 */
3652 if (retry_mode) {
3653 ret = nand_setup_read_retry(mtd, 0);
3654 if (ret < 0)
3655 break;
3656 retry_mode = 0;
3657 }
3658
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003659 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003660 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Brian Norris8b6e50c2011-05-25 14:59:01 -07003662 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 col = 0;
3664 /* Increment page address */
3665 realpage++;
3666
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003667 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 /* Check, if we cross a chip boundary */
3669 if (!page) {
3670 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003671 chip->select_chip(mtd, -1);
3672 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003675 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003677 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003678 if (oob)
3679 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Mike Dunn3f91e942012-04-25 12:06:09 -07003681 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003682 return ret;
3683
Brian Norrisb72f3df2013-12-03 11:04:14 -08003684 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003685 return -EBADMSG;
3686
Mike Dunnedbc45402012-04-25 12:06:11 -07003687 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003688}
3689
3690/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003691 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003692 * @mtd: mtd info structure
3693 * @chip: nand chip info structure
3694 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003695 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003696int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003697{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003698 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003699}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003700EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003701
3702/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003703 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003704 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003705 * @mtd: mtd info structure
3706 * @chip: nand chip info structure
3707 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003708 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003709int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3710 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003711{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003712 int length = mtd->oobsize;
3713 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3714 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003715 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003716 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003717
Boris Brezillon97d90da2017-11-30 18:01:29 +01003718 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3719 if (ret)
3720 return ret;
3721
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003722 for (i = 0; i < chip->ecc.steps; i++) {
3723 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003724 int ret;
3725
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003726 pos = eccsize + i * (eccsize + chunk);
3727 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003728 ret = nand_change_read_column_op(chip, pos,
3729 NULL, 0,
3730 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003731 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003732 ret = nand_read_page_op(chip, page, pos, NULL,
3733 0);
3734
3735 if (ret)
3736 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003737 } else
3738 sndrnd = 1;
3739 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003740
3741 ret = nand_read_data_op(chip, bufpoi, toread, false);
3742 if (ret)
3743 return ret;
3744
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003745 bufpoi += toread;
3746 length -= toread;
3747 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003748 if (length > 0) {
3749 ret = nand_read_data_op(chip, bufpoi, length, false);
3750 if (ret)
3751 return ret;
3752 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003753
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003754 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003755}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003756EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003757
3758/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003759 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003760 * @mtd: mtd info structure
3761 * @chip: nand chip info structure
3762 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003763 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003764int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003765{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003766 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3767 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003768}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003769EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003770
3771/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003772 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003773 * with syndrome - only for large page flash
3774 * @mtd: mtd info structure
3775 * @chip: nand chip info structure
3776 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003777 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003778int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3779 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003780{
3781 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3782 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003783 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003784 const uint8_t *bufpoi = chip->oob_poi;
3785
3786 /*
3787 * data-ecc-data-ecc ... ecc-oob
3788 * or
3789 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3790 */
3791 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3792 pos = steps * (eccsize + chunk);
3793 steps = 0;
3794 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003795 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003796
Boris Brezillon97d90da2017-11-30 18:01:29 +01003797 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3798 if (ret)
3799 return ret;
3800
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003801 for (i = 0; i < steps; i++) {
3802 if (sndcmd) {
3803 if (mtd->writesize <= 512) {
3804 uint32_t fill = 0xFFFFFFFF;
3805
3806 len = eccsize;
3807 while (len > 0) {
3808 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003809
3810 ret = nand_write_data_op(chip, &fill,
3811 num, false);
3812 if (ret)
3813 return ret;
3814
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003815 len -= num;
3816 }
3817 } else {
3818 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003819 ret = nand_change_write_column_op(chip, pos,
3820 NULL, 0,
3821 false);
3822 if (ret)
3823 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003824 }
3825 } else
3826 sndcmd = 1;
3827 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003828
3829 ret = nand_write_data_op(chip, bufpoi, len, false);
3830 if (ret)
3831 return ret;
3832
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003833 bufpoi += len;
3834 length -= len;
3835 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003836 if (length > 0) {
3837 ret = nand_write_data_op(chip, bufpoi, length, false);
3838 if (ret)
3839 return ret;
3840 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003841
Boris Brezillon97d90da2017-11-30 18:01:29 +01003842 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003843}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003844EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003845
3846/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003847 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003848 * @mtd: MTD device structure
3849 * @from: offset to read from
3850 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003852 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003854static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3855 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003857 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003858 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003859 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003860 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003861 int readlen = ops->ooblen;
3862 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003863 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003864 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Brian Norris289c0522011-07-19 10:06:09 -07003866 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303867 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Brian Norris041e4572011-06-23 16:45:24 -07003869 stats = mtd->ecc_stats;
3870
Boris BREZILLON29f10582016-03-07 10:46:52 +01003871 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003872
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003873 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003874 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003876 /* Shift to get page */
3877 realpage = (int)(from >> chip->page_shift);
3878 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Florian Fainellif8ac0412010-09-07 13:23:43 +02003880 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003881 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003882 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003883 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003884 ret = chip->ecc.read_oob(mtd, chip, page);
3885
3886 if (ret < 0)
3887 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003888
3889 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003890 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003891
Brian Norris5bc7c332013-03-13 09:51:31 -07003892 if (chip->options & NAND_NEED_READRDY) {
3893 /* Apply delay or wait for ready/busy pin */
3894 if (!chip->dev_ready)
3895 udelay(chip->chip_delay);
3896 else
3897 nand_wait_ready(mtd);
3898 }
3899
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003900 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3901
Vitaly Wool70145682006-11-03 18:20:38 +03003902 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003903 if (!readlen)
3904 break;
3905
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003906 /* Increment page address */
3907 realpage++;
3908
3909 page = realpage & chip->pagemask;
3910 /* Check, if we cross a chip boundary */
3911 if (!page) {
3912 chipnr++;
3913 chip->select_chip(mtd, -1);
3914 chip->select_chip(mtd, chipnr);
3915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003917 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003919 ops->oobretlen = ops->ooblen - readlen;
3920
3921 if (ret < 0)
3922 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003923
3924 if (mtd->ecc_stats.failed - stats.failed)
3925 return -EBADMSG;
3926
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003927 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928}
3929
3930/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003931 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003932 * @mtd: MTD device structure
3933 * @from: offset to read from
3934 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003936 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003938static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3939 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003941 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003942
3943 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003945 if (ops->mode != MTD_OPS_PLACE_OOB &&
3946 ops->mode != MTD_OPS_AUTO_OOB &&
3947 ops->mode != MTD_OPS_RAW)
3948 return -ENOTSUPP;
3949
Huang Shijie6a8214a2012-11-19 14:43:30 +08003950 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003952 if (!ops->datbuf)
3953 ret = nand_do_read_oob(mtd, from, ops);
3954 else
3955 ret = nand_do_read_ops(mtd, from, ops);
3956
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003958 return ret;
3959}
3960
3961
3962/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003963 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003964 * @mtd: mtd info structure
3965 * @chip: nand chip info structure
3966 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003967 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003968 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003969 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003970 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003971 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003972int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
3973 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003974{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003975 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003976
Boris Brezillon25f815f2017-11-30 18:01:30 +01003977 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003978 if (ret)
3979 return ret;
3980
3981 if (oob_required) {
3982 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3983 false);
3984 if (ret)
3985 return ret;
3986 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003987
Boris Brezillon25f815f2017-11-30 18:01:30 +01003988 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003990EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003992/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003993 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003994 * @mtd: mtd info structure
3995 * @chip: nand chip info structure
3996 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003997 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003998 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003999 *
4000 * We need a special oob layout and handling even when ECC isn't checked.
4001 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004002static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004003 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004004 const uint8_t *buf, int oob_required,
4005 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004006{
4007 int eccsize = chip->ecc.size;
4008 int eccbytes = chip->ecc.bytes;
4009 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004010 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004011
Boris Brezillon25f815f2017-11-30 18:01:30 +01004012 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4013 if (ret)
4014 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004015
4016 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004017 ret = nand_write_data_op(chip, buf, eccsize, false);
4018 if (ret)
4019 return ret;
4020
David Brownell52ff49d2009-03-04 12:01:36 -08004021 buf += eccsize;
4022
4023 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004024 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4025 false);
4026 if (ret)
4027 return ret;
4028
David Brownell52ff49d2009-03-04 12:01:36 -08004029 oob += chip->ecc.prepad;
4030 }
4031
Boris Brezillon97d90da2017-11-30 18:01:29 +01004032 ret = nand_write_data_op(chip, oob, eccbytes, false);
4033 if (ret)
4034 return ret;
4035
David Brownell52ff49d2009-03-04 12:01:36 -08004036 oob += eccbytes;
4037
4038 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004039 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4040 false);
4041 if (ret)
4042 return ret;
4043
David Brownell52ff49d2009-03-04 12:01:36 -08004044 oob += chip->ecc.postpad;
4045 }
4046 }
4047
4048 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004049 if (size) {
4050 ret = nand_write_data_op(chip, oob, size, false);
4051 if (ret)
4052 return ret;
4053 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004054
Boris Brezillon25f815f2017-11-30 18:01:30 +01004055 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004056}
4057/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004058 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004059 * @mtd: mtd info structure
4060 * @chip: nand chip info structure
4061 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004062 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004063 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004064 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004065static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004066 const uint8_t *buf, int oob_required,
4067 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004068{
Boris Brezillon846031d2016-02-03 20:11:00 +01004069 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004070 int eccbytes = chip->ecc.bytes;
4071 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004072 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004073 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004074
Brian Norris7854d3f2011-06-23 14:12:08 -07004075 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004076 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
4077 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004078
Boris Brezillon846031d2016-02-03 20:11:00 +01004079 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4080 chip->ecc.total);
4081 if (ret)
4082 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004083
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004084 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004085}
4086
4087/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004088 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004089 * @mtd: mtd info structure
4090 * @chip: nand chip info structure
4091 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004092 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004093 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004094 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004095static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004096 const uint8_t *buf, int oob_required,
4097 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004098{
Boris Brezillon846031d2016-02-03 20:11:00 +01004099 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004100 int eccbytes = chip->ecc.bytes;
4101 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004102 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004103 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004104
Boris Brezillon25f815f2017-11-30 18:01:30 +01004105 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4106 if (ret)
4107 return ret;
4108
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004109 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4110 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004111
4112 ret = nand_write_data_op(chip, p, eccsize, false);
4113 if (ret)
4114 return ret;
4115
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004116 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
4117 }
4118
Boris Brezillon846031d2016-02-03 20:11:00 +01004119 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4120 chip->ecc.total);
4121 if (ret)
4122 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004123
Boris Brezillon97d90da2017-11-30 18:01:29 +01004124 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4125 if (ret)
4126 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004127
Boris Brezillon25f815f2017-11-30 18:01:30 +01004128 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004129}
4130
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304131
4132/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004133 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304134 * @mtd: mtd info structure
4135 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004136 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304137 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004138 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304139 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004140 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304141 */
4142static int nand_write_subpage_hwecc(struct mtd_info *mtd,
4143 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07004144 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004145 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304146{
4147 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004148 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304149 int ecc_size = chip->ecc.size;
4150 int ecc_bytes = chip->ecc.bytes;
4151 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304152 uint32_t start_step = offset / ecc_size;
4153 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4154 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004155 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304156
Boris Brezillon25f815f2017-11-30 18:01:30 +01004157 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4158 if (ret)
4159 return ret;
4160
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304161 for (step = 0; step < ecc_steps; step++) {
4162 /* configure controller for WRITE access */
4163 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
4164
4165 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004166 ret = nand_write_data_op(chip, buf, ecc_size, false);
4167 if (ret)
4168 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304169
4170 /* mask ECC of un-touched subpages by padding 0xFF */
4171 if ((step < start_step) || (step > end_step))
4172 memset(ecc_calc, 0xff, ecc_bytes);
4173 else
Brian Norrisd6a950802013-08-08 17:16:36 -07004174 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304175
4176 /* mask OOB of un-touched subpages by padding 0xFF */
4177 /* if oob_required, preserve OOB metadata of written subpage */
4178 if (!oob_required || (step < start_step) || (step > end_step))
4179 memset(oob_buf, 0xff, oob_bytes);
4180
Brian Norrisd6a950802013-08-08 17:16:36 -07004181 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304182 ecc_calc += ecc_bytes;
4183 oob_buf += oob_bytes;
4184 }
4185
4186 /* copy calculated ECC for whole page to chip->buffer->oob */
4187 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004188 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004189 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4190 chip->ecc.total);
4191 if (ret)
4192 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304193
4194 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004195 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4196 if (ret)
4197 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304198
Boris Brezillon25f815f2017-11-30 18:01:30 +01004199 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304200}
4201
4202
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004203/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004204 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004205 * @mtd: mtd info structure
4206 * @chip: nand chip info structure
4207 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004208 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004209 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004210 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004211 * The hw generator calculates the error syndrome automatically. Therefore we
4212 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004213 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004214static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07004215 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004216 const uint8_t *buf, int oob_required,
4217 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004218{
4219 int i, eccsize = chip->ecc.size;
4220 int eccbytes = chip->ecc.bytes;
4221 int eccsteps = chip->ecc.steps;
4222 const uint8_t *p = buf;
4223 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004224 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004225
Boris Brezillon25f815f2017-11-30 18:01:30 +01004226 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4227 if (ret)
4228 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004229
4230 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004231 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004232
4233 ret = nand_write_data_op(chip, p, eccsize, false);
4234 if (ret)
4235 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004236
4237 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004238 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4239 false);
4240 if (ret)
4241 return ret;
4242
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004243 oob += chip->ecc.prepad;
4244 }
4245
4246 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004247
4248 ret = nand_write_data_op(chip, oob, eccbytes, false);
4249 if (ret)
4250 return ret;
4251
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004252 oob += eccbytes;
4253
4254 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004255 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4256 false);
4257 if (ret)
4258 return ret;
4259
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004260 oob += chip->ecc.postpad;
4261 }
4262 }
4263
4264 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004265 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004266 if (i) {
4267 ret = nand_write_data_op(chip, oob, i, false);
4268 if (ret)
4269 return ret;
4270 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004271
Boris Brezillon25f815f2017-11-30 18:01:30 +01004272 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004273}
4274
4275/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004276 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004277 * @mtd: MTD device structure
4278 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304279 * @offset: address offset within the page
4280 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004281 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004282 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004283 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004284 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004285 */
4286static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304287 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004288 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004289{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304290 int status, subpage;
4291
4292 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4293 chip->ecc.write_subpage)
4294 subpage = offset || (data_len < mtd->writesize);
4295 else
4296 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004297
David Woodhouse956e9442006-09-25 17:12:39 +01004298 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304299 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004300 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304301 else if (subpage)
4302 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004303 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004304 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004305 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
4306 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004307
4308 if (status < 0)
4309 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004310
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004311 return 0;
4312}
4313
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004314/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004315 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004316 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004317 * @oob: oob data buffer
4318 * @len: oob data write length
4319 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004320 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004321static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4322 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004323{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004324 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004325 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004326
4327 /*
4328 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4329 * data from a previous OOB read.
4330 */
4331 memset(chip->oob_poi, 0xff, mtd->oobsize);
4332
Florian Fainellif8ac0412010-09-07 13:23:43 +02004333 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004334
Brian Norris0612b9d2011-08-30 18:45:40 -07004335 case MTD_OPS_PLACE_OOB:
4336 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004337 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4338 return oob + len;
4339
Boris Brezillon846031d2016-02-03 20:11:00 +01004340 case MTD_OPS_AUTO_OOB:
4341 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4342 ops->ooboffs, len);
4343 BUG_ON(ret);
4344 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004345
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004346 default:
4347 BUG();
4348 }
4349 return NULL;
4350}
4351
Florian Fainellif8ac0412010-09-07 13:23:43 +02004352#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004353
4354/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004355 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004356 * @mtd: MTD device structure
4357 * @to: offset to write to
4358 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004359 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004360 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004361 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004362static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4363 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004364{
Corentin Labbe73600b62017-09-02 10:49:38 +02004365 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004366 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004367 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004368
4369 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004370 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004371
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004372 uint8_t *oob = ops->oobbuf;
4373 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304374 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004375 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004376
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004377 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004378 if (!writelen)
4379 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004380
Brian Norris8b6e50c2011-05-25 14:59:01 -07004381 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004382 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004383 pr_notice("%s: attempt to write non page aligned data\n",
4384 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004385 return -EINVAL;
4386 }
4387
Thomas Gleixner29072b92006-09-28 15:38:36 +02004388 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004389
Thomas Gleixner6a930962006-06-28 00:11:45 +02004390 chipnr = (int)(to >> chip->chip_shift);
4391 chip->select_chip(mtd, chipnr);
4392
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004393 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004394 if (nand_check_wp(mtd)) {
4395 ret = -EIO;
4396 goto err_out;
4397 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004398
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004399 realpage = (int)(to >> chip->page_shift);
4400 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004401
4402 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004403 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4404 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004405 chip->pagebuf = -1;
4406
Maxim Levitsky782ce792010-02-22 20:39:36 +02004407 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004408 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4409 ret = -EINVAL;
4410 goto err_out;
4411 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004412
Florian Fainellif8ac0412010-09-07 13:23:43 +02004413 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004414 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004415 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004416 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004417 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004418
Kamal Dasu66507c72014-05-01 20:51:19 -04004419 if (part_pagewr)
4420 use_bufpoi = 1;
4421 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004422 use_bufpoi = !virt_addr_valid(buf) ||
4423 !IS_ALIGNED((unsigned long)buf,
4424 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004425 else
4426 use_bufpoi = 0;
4427
4428 /* Partial page write?, or need to use bounce buffer */
4429 if (use_bufpoi) {
4430 pr_debug("%s: using write bounce buffer for buf@%p\n",
4431 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004432 if (part_pagewr)
4433 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004434 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004435 memset(chip->data_buf, 0xff, mtd->writesize);
4436 memcpy(&chip->data_buf[column], buf, bytes);
4437 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004438 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004439
Maxim Levitsky782ce792010-02-22 20:39:36 +02004440 if (unlikely(oob)) {
4441 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004442 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004443 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004444 } else {
4445 /* We still need to erase leftover OOB data */
4446 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004447 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004448
4449 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004450 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004451 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004452 if (ret)
4453 break;
4454
4455 writelen -= bytes;
4456 if (!writelen)
4457 break;
4458
Thomas Gleixner29072b92006-09-28 15:38:36 +02004459 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004460 buf += bytes;
4461 realpage++;
4462
4463 page = realpage & chip->pagemask;
4464 /* Check, if we cross a chip boundary */
4465 if (!page) {
4466 chipnr++;
4467 chip->select_chip(mtd, -1);
4468 chip->select_chip(mtd, chipnr);
4469 }
4470 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004471
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004472 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004473 if (unlikely(oob))
4474 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004475
4476err_out:
4477 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004478 return ret;
4479}
4480
4481/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004482 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004483 * @mtd: MTD device structure
4484 * @to: offset to write to
4485 * @len: number of bytes to write
4486 * @retlen: pointer to variable to store the number of written bytes
4487 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004488 *
4489 * NAND write with ECC. Used when performing writes in interrupt context, this
4490 * may for example be called by mtdoops when writing an oops while in panic.
4491 */
4492static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4493 size_t *retlen, const uint8_t *buf)
4494{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004495 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004496 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004497 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004498 int ret;
4499
Brian Norris8b6e50c2011-05-25 14:59:01 -07004500 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004501 panic_nand_get_device(chip, mtd, FL_WRITING);
4502
Brent Taylor30863e382017-10-30 22:32:45 -05004503 chip->select_chip(mtd, chipnr);
4504
4505 /* Wait for the device to get ready */
4506 panic_nand_wait(mtd, chip, 400);
4507
Brian Norris0ec56dc2015-02-28 02:02:30 -08004508 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004509 ops.len = len;
4510 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004511 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004512
Brian Norris4a89ff82011-08-30 18:45:45 -07004513 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004514
Brian Norris4a89ff82011-08-30 18:45:45 -07004515 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004516 return ret;
4517}
4518
4519/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004520 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004521 * @mtd: MTD device structure
4522 * @to: offset to write to
4523 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004524 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004525 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004526 */
4527static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4528 struct mtd_oob_ops *ops)
4529{
Adrian Hunter03736152007-01-31 17:58:29 +02004530 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004531 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Brian Norris289c0522011-07-19 10:06:09 -07004533 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304534 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Boris BREZILLON29f10582016-03-07 10:46:52 +01004536 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004537
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004539 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004540 pr_debug("%s: attempt to write past end of page\n",
4541 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 return -EINVAL;
4543 }
4544
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004545 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004546
4547 /*
4548 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4549 * of my DiskOnChip 2000 test units) will clear the whole data page too
4550 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4551 * it in the doc2000 driver in August 1999. dwmw2.
4552 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004553 nand_reset(chip, chipnr);
4554
4555 chip->select_chip(mtd, chipnr);
4556
4557 /* Shift to get page */
4558 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004561 if (nand_check_wp(mtd)) {
4562 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004563 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004564 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004567 if (page == chip->pagebuf)
4568 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004570 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004571
Brian Norris0612b9d2011-08-30 18:45:40 -07004572 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07004573 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
4574 else
4575 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004576
Huang Shijieb0bb6902012-11-19 14:43:29 +08004577 chip->select_chip(mtd, -1);
4578
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004579 if (status)
4580 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Vitaly Wool70145682006-11-03 18:20:38 +03004582 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004584 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004587/**
4588 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004589 * @mtd: MTD device structure
4590 * @to: offset to write to
4591 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004592 */
4593static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4594 struct mtd_oob_ops *ops)
4595{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004596 int ret = -ENOTSUPP;
4597
4598 ops->retlen = 0;
4599
Huang Shijie6a8214a2012-11-19 14:43:30 +08004600 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004601
Florian Fainellif8ac0412010-09-07 13:23:43 +02004602 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004603 case MTD_OPS_PLACE_OOB:
4604 case MTD_OPS_AUTO_OOB:
4605 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004606 break;
4607
4608 default:
4609 goto out;
4610 }
4611
4612 if (!ops->datbuf)
4613 ret = nand_do_write_oob(mtd, to, ops);
4614 else
4615 ret = nand_do_write_ops(mtd, to, ops);
4616
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004617out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004618 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 return ret;
4620}
4621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622/**
Brian Norris49c50b92014-05-06 16:02:19 -07004623 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004624 * @mtd: MTD device structure
4625 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 *
Brian Norris49c50b92014-05-06 16:02:19 -07004627 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 */
Brian Norris49c50b92014-05-06 16:02:19 -07004629static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004631 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004632 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004633
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004635 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004636
Boris Brezillon97d90da2017-11-30 18:01:29 +01004637 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638}
4639
4640/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004642 * @mtd: MTD device structure
4643 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004645 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004647static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
David Woodhousee0c7d762006-05-13 18:07:53 +01004649 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004651
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004653 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004654 * @mtd: MTD device structure
4655 * @instr: erase instruction
4656 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004658 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004660int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4661 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
Adrian Hunter69423d92008-12-10 13:37:21 +00004663 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004664 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00004665 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Brian Norris289c0522011-07-19 10:06:09 -07004667 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4668 __func__, (unsigned long long)instr->addr,
4669 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304671 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004675 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
4677 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004678 page = (int)(instr->addr >> chip->page_shift);
4679 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004682 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
4684 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004685 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 /* Check, if it is write protected */
4688 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004689 pr_debug("%s: device is write protected!\n",
4690 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 instr->state = MTD_ERASE_FAILED;
4692 goto erase_exit;
4693 }
4694
4695 /* Loop through the pages */
4696 len = instr->len;
4697
4698 instr->state = MTD_ERASING;
4699
4700 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004701 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004702 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304703 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004704 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4705 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 instr->state = MTD_ERASE_FAILED;
4707 goto erase_exit;
4708 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004709
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004710 /*
4711 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004712 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004713 */
4714 if (page <= chip->pagebuf && chip->pagebuf <
4715 (page + pages_per_block))
4716 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Brian Norris49c50b92014-05-06 16:02:19 -07004718 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
4720 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004721 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004722 pr_debug("%s: failed erase, page 0x%08x\n",
4723 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00004725 instr->fail_addr =
4726 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 goto erase_exit;
4728 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004729
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004731 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 page += pages_per_block;
4733
4734 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004735 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004737 chip->select_chip(mtd, -1);
4738 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 }
4740 }
4741 instr->state = MTD_ERASE_DONE;
4742
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004743erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
4745 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
4747 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004748 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 nand_release_device(mtd);
4750
David Woodhouse49defc02007-10-06 15:01:59 -04004751 /* Do call back function */
4752 if (!ret)
4753 mtd_erase_callback(instr);
4754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 /* Return more or less happy */
4756 return ret;
4757}
4758
4759/**
4760 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004761 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004763 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004765static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
Brian Norris289c0522011-07-19 10:06:09 -07004767 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
4769 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004770 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004772 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773}
4774
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004776 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004777 * @mtd: MTD device structure
4778 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004780static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304782 struct nand_chip *chip = mtd_to_nand(mtd);
4783 int chipnr = (int)(offs >> chip->chip_shift);
4784 int ret;
4785
4786 /* Select the NAND device */
4787 nand_get_device(mtd, FL_READING);
4788 chip->select_chip(mtd, chipnr);
4789
4790 ret = nand_block_checkbad(mtd, offs, 0);
4791
4792 chip->select_chip(mtd, -1);
4793 nand_release_device(mtd);
4794
4795 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796}
4797
4798/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004799 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004800 * @mtd: MTD device structure
4801 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004803static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 int ret;
4806
Florian Fainellif8ac0412010-09-07 13:23:43 +02004807 ret = nand_block_isbad(mtd, ofs);
4808 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004809 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 if (ret > 0)
4811 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004812 return ret;
4813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Brian Norris5a0edb22013-07-30 17:52:58 -07004815 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
4818/**
Zach Brown56718422017-01-10 13:30:20 -06004819 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4820 * @mtd: MTD device structure
4821 * @ofs: offset relative to mtd start
4822 * @len: length of mtd
4823 */
4824static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4825{
4826 struct nand_chip *chip = mtd_to_nand(mtd);
4827 u32 part_start_block;
4828 u32 part_end_block;
4829 u32 part_start_die;
4830 u32 part_end_die;
4831
4832 /*
4833 * max_bb_per_die and blocks_per_die used to determine
4834 * the maximum bad block count.
4835 */
4836 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4837 return -ENOTSUPP;
4838
4839 /* Get the start and end of the partition in erase blocks. */
4840 part_start_block = mtd_div_by_eb(ofs, mtd);
4841 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4842
4843 /* Get the start and end LUNs of the partition. */
4844 part_start_die = part_start_block / chip->blocks_per_die;
4845 part_end_die = part_end_block / chip->blocks_per_die;
4846
4847 /*
4848 * Look up the bad blocks per unit and multiply by the number of units
4849 * that the partition spans.
4850 */
4851 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4852}
4853
4854/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004855 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004856 * @mtd: MTD device structure
4857 * @chip: nand chip info structure
4858 * @addr: feature address.
4859 * @subfeature_param: the subfeature parameters, a four bytes array.
4860 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004861static int nand_default_set_features(struct mtd_info *mtd,
4862 struct nand_chip *chip, int addr,
4863 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004864{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004865 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004866}
4867
4868/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004869 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004870 * @mtd: MTD device structure
4871 * @chip: nand chip info structure
4872 * @addr: feature address.
4873 * @subfeature_param: the subfeature parameters, a four bytes array.
4874 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004875static int nand_default_get_features(struct mtd_info *mtd,
4876 struct nand_chip *chip, int addr,
4877 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004878{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004879 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004880}
4881
4882/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004883 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004884 * @mtd: MTD device structure
4885 * @chip: nand chip info structure
4886 * @addr: feature address.
4887 * @subfeature_param: the subfeature parameters, a four bytes array.
4888 *
4889 * Should be used by NAND controller drivers that do not support the SET/GET
4890 * FEATURES operations.
4891 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004892int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4893 int addr, u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004894{
4895 return -ENOTSUPP;
4896}
Miquel Raynalb9587582018-03-19 14:47:19 +01004897EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004898
4899/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004900 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004901 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004902 */
4903static int nand_suspend(struct mtd_info *mtd)
4904{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004905 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004906}
4907
4908/**
4909 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004910 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004911 */
4912static void nand_resume(struct mtd_info *mtd)
4913{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004914 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004915
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004916 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004917 nand_release_device(mtd);
4918 else
Brian Norrisd0370212011-07-19 10:06:08 -07004919 pr_err("%s called for a chip which is not in suspended state\n",
4920 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004921}
4922
Scott Branden72ea4032014-11-20 11:18:05 -08004923/**
4924 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4925 * prevent further operations
4926 * @mtd: MTD device structure
4927 */
4928static void nand_shutdown(struct mtd_info *mtd)
4929{
Brian Norris9ca641b2015-11-09 16:37:28 -08004930 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004931}
4932
Brian Norris8b6e50c2011-05-25 14:59:01 -07004933/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004934static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004935{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004936 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004939 if (!chip->chip_delay)
4940 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
4942 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004943 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004944 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
4946 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004947 if (chip->waitfunc == NULL)
4948 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004950 if (!chip->select_chip)
4951 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004952
Huang Shijie4204ccc2013-08-16 10:10:07 +08004953 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004954 if (!chip->set_features)
4955 chip->set_features = nand_default_set_features;
4956 if (!chip->get_features)
4957 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004958
Brian Norris68e80782013-07-18 01:17:02 -07004959 /* If called twice, pointers that depend on busw may need to be reset */
4960 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004961 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4962 if (!chip->read_word)
4963 chip->read_word = nand_read_word;
4964 if (!chip->block_bad)
4965 chip->block_bad = nand_block_bad;
4966 if (!chip->block_markbad)
4967 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004968 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004969 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004970 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4971 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004972 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004973 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004974 if (!chip->scan_bbt)
4975 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004976
4977 if (!chip->controller) {
4978 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02004979 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004980 }
4981
Masahiro Yamada477544c2017-03-30 17:15:05 +09004982 if (!chip->buf_align)
4983 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004984}
4985
Brian Norris8b6e50c2011-05-25 14:59:01 -07004986/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004987static void sanitize_string(uint8_t *s, size_t len)
4988{
4989 ssize_t i;
4990
Brian Norris8b6e50c2011-05-25 14:59:01 -07004991 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004992 s[len - 1] = 0;
4993
Brian Norris8b6e50c2011-05-25 14:59:01 -07004994 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004995 for (i = 0; i < len - 1; i++) {
4996 if (s[i] < ' ' || s[i] > 127)
4997 s[i] = '?';
4998 }
4999
Brian Norris8b6e50c2011-05-25 14:59:01 -07005000 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005001 strim(s);
5002}
5003
5004static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
5005{
5006 int i;
5007 while (len--) {
5008 crc ^= *p++ << 8;
5009 for (i = 0; i < 8; i++)
5010 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
5011 }
5012
5013 return crc;
5014}
5015
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005016/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005017static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
5018 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005019{
5020 struct onfi_ext_param_page *ep;
5021 struct onfi_ext_section *s;
5022 struct onfi_ext_ecc_info *ecc;
5023 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005024 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005025 int len;
5026 int i;
5027
5028 len = le16_to_cpu(p->ext_param_page_length) * 16;
5029 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005030 if (!ep)
5031 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005032
5033 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005034 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5035 if (ret)
5036 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005037
5038 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005039 ret = nand_change_read_column_op(chip,
5040 sizeof(*p) * p->num_of_param_pages,
5041 ep, len, true);
5042 if (ret)
5043 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005044
Boris Brezillon97d90da2017-11-30 18:01:29 +01005045 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005046 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5047 != le16_to_cpu(ep->crc))) {
5048 pr_debug("fail in the CRC.\n");
5049 goto ext_out;
5050 }
5051
5052 /*
5053 * Check the signature.
5054 * Do not strictly follow the ONFI spec, maybe changed in future.
5055 */
5056 if (strncmp(ep->sig, "EPPS", 4)) {
5057 pr_debug("The signature is invalid.\n");
5058 goto ext_out;
5059 }
5060
5061 /* find the ECC section. */
5062 cursor = (uint8_t *)(ep + 1);
5063 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5064 s = ep->sections + i;
5065 if (s->type == ONFI_SECTION_TYPE_2)
5066 break;
5067 cursor += s->length * 16;
5068 }
5069 if (i == ONFI_EXT_SECTION_MAX) {
5070 pr_debug("We can not find the ECC section.\n");
5071 goto ext_out;
5072 }
5073
5074 /* get the info we want. */
5075 ecc = (struct onfi_ext_ecc_info *)cursor;
5076
Brian Norris4ae7d222013-09-16 18:20:21 -07005077 if (!ecc->codeword_size) {
5078 pr_debug("Invalid codeword size\n");
5079 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005080 }
5081
Brian Norris4ae7d222013-09-16 18:20:21 -07005082 chip->ecc_strength_ds = ecc->ecc_bits;
5083 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005084 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005085
5086ext_out:
5087 kfree(ep);
5088 return ret;
5089}
5090
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005091/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005092 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005093 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005094static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005095{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005096 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005097 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005098 char id[4];
5099 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005100
Brian Norris7854d3f2011-06-23 14:12:08 -07005101 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005102 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5103 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005104 return 0;
5105
Boris Brezillon97d90da2017-11-30 18:01:29 +01005106 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5107 if (ret)
5108 return 0;
5109
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005110 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005111 ret = nand_read_data_op(chip, p, sizeof(*p), true);
5112 if (ret)
5113 return 0;
5114
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005115 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
5116 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005117 break;
5118 }
5119 }
5120
Brian Norrisc7f23a72013-08-13 10:51:55 -07005121 if (i == 3) {
5122 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005123 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07005124 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005125
Brian Norris8b6e50c2011-05-25 14:59:01 -07005126 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005127 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005128 if (val & (1 << 5))
5129 chip->onfi_version = 23;
5130 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005131 chip->onfi_version = 22;
5132 else if (val & (1 << 3))
5133 chip->onfi_version = 21;
5134 else if (val & (1 << 2))
5135 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005136 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005137 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005138
5139 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005140 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005141 return 0;
5142 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005143
5144 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5145 sanitize_string(p->model, sizeof(p->model));
Miquel Raynalf4531b22018-03-19 14:47:26 +01005146 strncpy(chip->parameters.model, p->model,
5147 sizeof(chip->parameters.model) - 1);
Brian Norris4355b702013-08-27 18:45:10 -07005148
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005149 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005150
5151 /*
5152 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5153 * (don't ask me who thought of this...). MTD assumes that these
5154 * dimensions will be power-of-2, so just truncate the remaining area.
5155 */
5156 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5157 mtd->erasesize *= mtd->writesize;
5158
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005159 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005160
5161 /* See erasesize comment */
5162 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005163 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005164 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005165
Zach Brown34da5f52017-01-10 13:30:21 -06005166 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5167 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5168
Huang Shijiee2985fc2013-05-17 11:17:30 +08005169 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005170 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005171
Huang Shijie10c86ba2013-05-17 11:17:26 +08005172 if (p->ecc_bits != 0xff) {
5173 chip->ecc_strength_ds = p->ecc_bits;
5174 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005175 } else if (chip->onfi_version >= 21 &&
5176 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
5177
5178 /*
5179 * The nand_flash_detect_ext_param_page() uses the
5180 * Change Read Column command which maybe not supported
5181 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5182 * now. We do not replace user supplied command function.
5183 */
5184 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5185 chip->cmdfunc = nand_command_lp;
5186
5187 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005188 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005189 pr_warn("Failed to detect ONFI extended param page\n");
5190 } else {
5191 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005192 }
5193
Miquel Raynalf4531b22018-03-19 14:47:26 +01005194 /* Save some parameters from the parameter page for future use */
5195 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES)
5196 chip->parameters.supports_set_get_features = true;
5197
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005198 return 1;
5199}
5200
5201/*
Huang Shijie91361812014-02-21 13:39:40 +08005202 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5203 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005204static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005205{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005206 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08005207 struct nand_jedec_params *p = &chip->jedec_params;
5208 struct jedec_ecc_info *ecc;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005209 char id[5];
5210 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005211
5212 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005213 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5214 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005215 return 0;
5216
Boris Brezillon97d90da2017-11-30 18:01:29 +01005217 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
5218 if (ret)
5219 return 0;
5220
Huang Shijie91361812014-02-21 13:39:40 +08005221 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005222 ret = nand_read_data_op(chip, p, sizeof(*p), true);
5223 if (ret)
5224 return 0;
Huang Shijie91361812014-02-21 13:39:40 +08005225
5226 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5227 le16_to_cpu(p->crc))
5228 break;
5229 }
5230
5231 if (i == 3) {
5232 pr_err("Could not find valid JEDEC parameter page; aborting\n");
5233 return 0;
5234 }
5235
5236 /* Check version */
5237 val = le16_to_cpu(p->revision);
5238 if (val & (1 << 2))
5239 chip->jedec_version = 10;
5240 else if (val & (1 << 1))
5241 chip->jedec_version = 1; /* vendor specific version */
5242
5243 if (!chip->jedec_version) {
5244 pr_info("unsupported JEDEC version: %d\n", val);
5245 return 0;
5246 }
5247
5248 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5249 sanitize_string(p->model, sizeof(p->model));
Miquel Raynalf4531b22018-03-19 14:47:26 +01005250 strncpy(chip->parameters.model, p->model,
5251 sizeof(chip->parameters.model) - 1);
Huang Shijie91361812014-02-21 13:39:40 +08005252
5253 mtd->writesize = le32_to_cpu(p->byte_per_page);
5254
5255 /* Please reference to the comment for nand_flash_detect_onfi. */
5256 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5257 mtd->erasesize *= mtd->writesize;
5258
5259 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5260
5261 /* Please reference to the comment for nand_flash_detect_onfi. */
5262 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5263 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5264 chip->bits_per_cell = p->bits_per_cell;
5265
5266 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005267 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005268
5269 /* ECC info */
5270 ecc = &p->ecc_info[0];
5271
5272 if (ecc->codeword_size >= 9) {
5273 chip->ecc_strength_ds = ecc->ecc_bits;
5274 chip->ecc_step_ds = 1 << ecc->codeword_size;
5275 } else {
5276 pr_warn("Invalid codeword size\n");
5277 }
5278
5279 return 1;
5280}
5281
5282/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005283 * nand_id_has_period - Check if an ID string has a given wraparound period
5284 * @id_data: the ID string
5285 * @arrlen: the length of the @id_data array
5286 * @period: the period of repitition
5287 *
5288 * Check if an ID string is repeated within a given sequence of bytes at
5289 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005290 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005291 * if the repetition has a period of @period; otherwise, returns zero.
5292 */
5293static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5294{
5295 int i, j;
5296 for (i = 0; i < period; i++)
5297 for (j = i + period; j < arrlen; j += period)
5298 if (id_data[i] != id_data[j])
5299 return 0;
5300 return 1;
5301}
5302
5303/*
5304 * nand_id_len - Get the length of an ID string returned by CMD_READID
5305 * @id_data: the ID string
5306 * @arrlen: the length of the @id_data array
5307
5308 * Returns the length of the ID string, according to known wraparound/trailing
5309 * zero patterns. If no pattern exists, returns the length of the array.
5310 */
5311static int nand_id_len(u8 *id_data, int arrlen)
5312{
5313 int last_nonzero, period;
5314
5315 /* Find last non-zero byte */
5316 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5317 if (id_data[last_nonzero])
5318 break;
5319
5320 /* All zeros */
5321 if (last_nonzero < 0)
5322 return 0;
5323
5324 /* Calculate wraparound period */
5325 for (period = 1; period < arrlen; period++)
5326 if (nand_id_has_period(id_data, arrlen, period))
5327 break;
5328
5329 /* There's a repeated pattern */
5330 if (period < arrlen)
5331 return period;
5332
5333 /* There are trailing zeros */
5334 if (last_nonzero < arrlen - 1)
5335 return last_nonzero + 1;
5336
5337 /* No pattern detected */
5338 return arrlen;
5339}
5340
Huang Shijie7db906b2013-09-25 14:58:11 +08005341/* Extract the bits of per cell from the 3rd byte of the extended ID */
5342static int nand_get_bits_per_cell(u8 cellinfo)
5343{
5344 int bits;
5345
5346 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5347 bits >>= NAND_CI_CELLTYPE_SHIFT;
5348 return bits + 1;
5349}
5350
Brian Norrise3b88bd2012-09-24 20:40:52 -07005351/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005352 * Many new NAND share similar device ID codes, which represent the size of the
5353 * chip. The rest of the parameters must be decoded according to generic or
5354 * manufacturer-specific "extended ID" decoding patterns.
5355 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005356void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005357{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005358 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005359 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005360 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005361 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005362 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005363 /* The 4th id byte is the important one */
5364 extid = id_data[3];
5365
Boris Brezillon01389b62016-06-08 10:30:18 +02005366 /* Calc pagesize */
5367 mtd->writesize = 1024 << (extid & 0x03);
5368 extid >>= 2;
5369 /* Calc oobsize */
5370 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5371 extid >>= 2;
5372 /* Calc blocksize. Blocksize is multiples of 64KiB */
5373 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5374 extid >>= 2;
5375 /* Get buswidth information */
5376 if (extid & 0x1)
5377 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005378}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005379EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005380
5381/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005382 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5383 * decodes a matching ID table entry and assigns the MTD size parameters for
5384 * the chip.
5385 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005386static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005387{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005388 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005389
5390 mtd->erasesize = type->erasesize;
5391 mtd->writesize = type->pagesize;
5392 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005393
Huang Shijie1c195e92013-09-25 14:58:12 +08005394 /* All legacy ID NAND are small-page, SLC */
5395 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005396}
5397
5398/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005399 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5400 * heuristic patterns using various detected parameters (e.g., manufacturer,
5401 * page size, cell-type information).
5402 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005403static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005404{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005405 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005406
5407 /* Set the bad block position */
5408 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5409 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5410 else
5411 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005412}
5413
Huang Shijieec6e87e2013-03-15 11:01:00 +08005414static inline bool is_full_id_nand(struct nand_flash_dev *type)
5415{
5416 return type->id_len;
5417}
5418
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005419static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005420 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005421{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005422 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005423 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005424
Huang Shijieec6e87e2013-03-15 11:01:00 +08005425 if (!strncmp(type->id, id_data, type->id_len)) {
5426 mtd->writesize = type->pagesize;
5427 mtd->erasesize = type->erasesize;
5428 mtd->oobsize = type->oobsize;
5429
Huang Shijie7db906b2013-09-25 14:58:11 +08005430 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005431 chip->chipsize = (uint64_t)type->chipsize << 20;
5432 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005433 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5434 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005435 chip->onfi_timing_mode_default =
5436 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005437
Miquel Raynalf4531b22018-03-19 14:47:26 +01005438 strncpy(chip->parameters.model, type->name,
5439 sizeof(chip->parameters.model) - 1);
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005440
Huang Shijieec6e87e2013-03-15 11:01:00 +08005441 return true;
5442 }
5443 return false;
5444}
5445
Brian Norris7e74c2d2012-09-24 20:40:49 -07005446/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005447 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5448 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5449 * table.
5450 */
5451static void nand_manufacturer_detect(struct nand_chip *chip)
5452{
5453 /*
5454 * Try manufacturer detection if available and use
5455 * nand_decode_ext_id() otherwise.
5456 */
5457 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005458 chip->manufacturer.desc->ops->detect) {
5459 /* The 3rd id byte holds MLC / multichip data */
5460 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005461 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005462 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005463 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005464 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005465}
5466
5467/*
5468 * Manufacturer initialization. This function is called for all NANDs including
5469 * ONFI and JEDEC compliant ones.
5470 * Manufacturer drivers should put all their specific initialization code in
5471 * their ->init() hook.
5472 */
5473static int nand_manufacturer_init(struct nand_chip *chip)
5474{
5475 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5476 !chip->manufacturer.desc->ops->init)
5477 return 0;
5478
5479 return chip->manufacturer.desc->ops->init(chip);
5480}
5481
5482/*
5483 * Manufacturer cleanup. This function is called for all NANDs including
5484 * ONFI and JEDEC compliant ones.
5485 * Manufacturer drivers should put all their specific cleanup code in their
5486 * ->cleanup() hook.
5487 */
5488static void nand_manufacturer_cleanup(struct nand_chip *chip)
5489{
5490 /* Release manufacturer private data */
5491 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5492 chip->manufacturer.desc->ops->cleanup)
5493 chip->manufacturer.desc->ops->cleanup(chip);
5494}
5495
5496/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005497 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005498 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005499static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005500{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005501 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005502 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005503 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005504 u8 *id_data = chip->id.data;
5505 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
Karl Beldanef89a882008-09-15 14:37:29 +02005507 /*
5508 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005509 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005510 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005511 ret = nand_reset(chip, 0);
5512 if (ret)
5513 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005514
5515 /* Select the device */
5516 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005517
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005519 ret = nand_readid_op(chip, 0, id_data, 2);
5520 if (ret)
5521 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522
5523 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005524 maf_id = id_data[0];
5525 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526
Brian Norris8b6e50c2011-05-25 14:59:01 -07005527 /*
5528 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005529 * interface concerns can cause random data which looks like a
5530 * possibly credible NAND flash to appear. If the two results do
5531 * not match, ignore the device completely.
5532 */
5533
Brian Norris4aef9b72012-09-24 20:40:48 -07005534 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005535 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5536 if (ret)
5537 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005538
Boris Brezillon7f501f02016-05-24 19:20:05 +02005539 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005540 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005541 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005542 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005543 }
5544
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005545 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005546
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005547 /* Try to identify manufacturer */
5548 manufacturer = nand_get_manufacturer(maf_id);
5549 chip->manufacturer.desc = manufacturer;
5550
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005551 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005552 type = nand_flash_ids;
5553
Boris Brezillon29a198a2016-05-24 20:17:48 +02005554 /*
5555 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5556 * override it.
5557 * This is required to make sure initial NAND bus width set by the
5558 * NAND controller driver is coherent with the real NAND bus width
5559 * (extracted by auto-detection code).
5560 */
5561 busw = chip->options & NAND_BUSWIDTH_16;
5562
5563 /*
5564 * The flag is only set (never cleared), reset it to its default value
5565 * before starting auto-detection.
5566 */
5567 chip->options &= ~NAND_BUSWIDTH_16;
5568
Huang Shijieec6e87e2013-03-15 11:01:00 +08005569 for (; type->name != NULL; type++) {
5570 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005571 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005572 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005573 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005574 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005575 }
5576 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005577
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005578 chip->onfi_version = 0;
5579 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005580 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005581 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005582 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005583
5584 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005585 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08005586 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005587 }
5588
David Woodhouse5e81e882010-02-26 18:32:56 +00005589 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005590 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005591
Miquel Raynalf4531b22018-03-19 14:47:26 +01005592 strncpy(chip->parameters.model, type->name,
5593 sizeof(chip->parameters.model) - 1);
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005594
Adrian Hunter69423d92008-12-10 13:37:21 +00005595 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005596
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005597 if (!type->pagesize)
5598 nand_manufacturer_detect(chip);
5599 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005600 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005601
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005602 /* Get chip options */
5603 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005604
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005605ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005606 if (!mtd->name)
5607 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005608
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005609 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005610 WARN_ON(busw & NAND_BUSWIDTH_16);
5611 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005612 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5613 /*
5614 * Check, if buswidth is correct. Hardware drivers should set
5615 * chip correct!
5616 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005617 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005618 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005619 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5620 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005621 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5622 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005623 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005624 }
5625
Boris Brezillon7f501f02016-05-24 19:20:05 +02005626 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005627
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005628 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005629 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005630 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005631 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005632
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005633 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005634 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005635 if (chip->chipsize & 0xffffffff)
5636 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005637 else {
5638 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5639 chip->chip_shift += 32 - 1;
5640 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005641
Masahiro Yamada14157f82017-09-13 11:05:50 +09005642 if (chip->chip_shift - chip->page_shift > 16)
5643 chip->options |= NAND_ROW_ADDR_3;
5644
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005645 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005646 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005647
Brian Norris8b6e50c2011-05-25 14:59:01 -07005648 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005649 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5650 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005651
Ezequiel Garcia20171642013-11-25 08:30:31 -03005652 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005653 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005654 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5655 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005656 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005657 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005658 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005659 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005660}
5661
Boris Brezillond48f62b2016-04-01 14:54:32 +02005662static const char * const nand_ecc_modes[] = {
5663 [NAND_ECC_NONE] = "none",
5664 [NAND_ECC_SOFT] = "soft",
5665 [NAND_ECC_HW] = "hw",
5666 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5667 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005668 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005669};
5670
5671static int of_get_nand_ecc_mode(struct device_node *np)
5672{
5673 const char *pm;
5674 int err, i;
5675
5676 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5677 if (err < 0)
5678 return err;
5679
5680 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5681 if (!strcasecmp(pm, nand_ecc_modes[i]))
5682 return i;
5683
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005684 /*
5685 * For backward compatibility we support few obsoleted values that don't
5686 * have their mappings into nand_ecc_modes_t anymore (they were merged
5687 * with other enums).
5688 */
5689 if (!strcasecmp(pm, "soft_bch"))
5690 return NAND_ECC_SOFT;
5691
Boris Brezillond48f62b2016-04-01 14:54:32 +02005692 return -ENODEV;
5693}
5694
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005695static const char * const nand_ecc_algos[] = {
5696 [NAND_ECC_HAMMING] = "hamming",
5697 [NAND_ECC_BCH] = "bch",
5698};
5699
Boris Brezillond48f62b2016-04-01 14:54:32 +02005700static int of_get_nand_ecc_algo(struct device_node *np)
5701{
5702 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005703 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005704
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005705 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5706 if (!err) {
5707 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5708 if (!strcasecmp(pm, nand_ecc_algos[i]))
5709 return i;
5710 return -ENODEV;
5711 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005712
5713 /*
5714 * For backward compatibility we also read "nand-ecc-mode" checking
5715 * for some obsoleted values that were specifying ECC algorithm.
5716 */
5717 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5718 if (err < 0)
5719 return err;
5720
5721 if (!strcasecmp(pm, "soft"))
5722 return NAND_ECC_HAMMING;
5723 else if (!strcasecmp(pm, "soft_bch"))
5724 return NAND_ECC_BCH;
5725
5726 return -ENODEV;
5727}
5728
5729static int of_get_nand_ecc_step_size(struct device_node *np)
5730{
5731 int ret;
5732 u32 val;
5733
5734 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5735 return ret ? ret : val;
5736}
5737
5738static int of_get_nand_ecc_strength(struct device_node *np)
5739{
5740 int ret;
5741 u32 val;
5742
5743 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5744 return ret ? ret : val;
5745}
5746
5747static int of_get_nand_bus_width(struct device_node *np)
5748{
5749 u32 val;
5750
5751 if (of_property_read_u32(np, "nand-bus-width", &val))
5752 return 8;
5753
5754 switch (val) {
5755 case 8:
5756 case 16:
5757 return val;
5758 default:
5759 return -EIO;
5760 }
5761}
5762
5763static bool of_get_nand_on_flash_bbt(struct device_node *np)
5764{
5765 return of_property_read_bool(np, "nand-on-flash-bbt");
5766}
5767
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005768static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005769{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005770 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005771 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005772
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005773 if (!dn)
5774 return 0;
5775
Brian Norris5844fee2015-01-23 00:22:27 -08005776 if (of_get_nand_bus_width(dn) == 16)
5777 chip->options |= NAND_BUSWIDTH_16;
5778
5779 if (of_get_nand_on_flash_bbt(dn))
5780 chip->bbt_options |= NAND_BBT_USE_FLASH;
5781
5782 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005783 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005784 ecc_strength = of_get_nand_ecc_strength(dn);
5785 ecc_step = of_get_nand_ecc_step_size(dn);
5786
Brian Norris5844fee2015-01-23 00:22:27 -08005787 if (ecc_mode >= 0)
5788 chip->ecc.mode = ecc_mode;
5789
Rafał Miłecki79082452016-03-23 11:19:02 +01005790 if (ecc_algo >= 0)
5791 chip->ecc.algo = ecc_algo;
5792
Brian Norris5844fee2015-01-23 00:22:27 -08005793 if (ecc_strength >= 0)
5794 chip->ecc.strength = ecc_strength;
5795
5796 if (ecc_step > 0)
5797 chip->ecc.size = ecc_step;
5798
Boris Brezillonba78ee02016-06-08 17:04:22 +02005799 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5800 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5801
Brian Norris5844fee2015-01-23 00:22:27 -08005802 return 0;
5803}
5804
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005805/**
David Woodhouse3b85c322006-09-25 17:06:53 +01005806 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005807 * @mtd: MTD device structure
5808 * @maxchips: number of chips to scan for
5809 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005810 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005811 * This is the first phase of the normal nand_scan() function. It reads the
5812 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005813 *
5814 */
David Woodhouse5e81e882010-02-26 18:32:56 +00005815int nand_scan_ident(struct mtd_info *mtd, int maxchips,
5816 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005817{
Cai Zhiyongbb770822013-12-25 20:11:15 +08005818 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01005819 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08005820 int ret;
5821
Miquel Raynal17fa8042017-11-30 18:01:31 +01005822 /* Enforce the right timings for reset/detection */
5823 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5824
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005825 ret = nand_dt_init(chip);
5826 if (ret)
5827 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005828
Brian Norrisf7a8e382016-01-05 10:39:45 -08005829 if (!mtd->name && mtd->dev.parent)
5830 mtd->name = dev_name(mtd->dev.parent);
5831
Miquel Raynal8878b122017-11-09 14:16:45 +01005832 /*
5833 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5834 * populated.
5835 */
5836 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005837 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005838 * Default functions assigned for ->cmdfunc() and
5839 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005840 */
Miquel Raynal8878b122017-11-09 14:16:45 +01005841 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5842 pr_err("->cmd_ctrl() should be provided\n");
5843 return -EINVAL;
5844 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005845 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005846
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005847 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005848 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005849
5850 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005851 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005852 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005853 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005854 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005855 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005856 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 }
5858
Boris Brezillon7f501f02016-05-24 19:20:05 +02005859 nand_maf_id = chip->id.data[0];
5860 nand_dev_id = chip->id.data[1];
5861
Huang Shijie07300162012-11-09 16:23:45 +08005862 chip->select_chip(mtd, -1);
5863
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005864 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005865 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005866 u8 id[2];
5867
Karl Beldanef89a882008-09-15 14:37:29 +02005868 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005869 nand_reset(chip, i);
5870
5871 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005873 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005875 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08005876 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 break;
Huang Shijie07300162012-11-09 16:23:45 +08005878 }
5879 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 }
5881 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005882 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005883
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005885 chip->numchips = i;
5886 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
David Woodhouse3b85c322006-09-25 17:06:53 +01005888 return 0;
5889}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005890EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01005891
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005892static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
5893{
5894 struct nand_chip *chip = mtd_to_nand(mtd);
5895 struct nand_ecc_ctrl *ecc = &chip->ecc;
5896
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005897 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005898 return -EINVAL;
5899
5900 switch (ecc->algo) {
5901 case NAND_ECC_HAMMING:
5902 ecc->calculate = nand_calculate_ecc;
5903 ecc->correct = nand_correct_data;
5904 ecc->read_page = nand_read_page_swecc;
5905 ecc->read_subpage = nand_read_subpage;
5906 ecc->write_page = nand_write_page_swecc;
5907 ecc->read_page_raw = nand_read_page_raw;
5908 ecc->write_page_raw = nand_write_page_raw;
5909 ecc->read_oob = nand_read_oob_std;
5910 ecc->write_oob = nand_write_oob_std;
5911 if (!ecc->size)
5912 ecc->size = 256;
5913 ecc->bytes = 3;
5914 ecc->strength = 1;
5915 return 0;
5916 case NAND_ECC_BCH:
5917 if (!mtd_nand_has_bch()) {
5918 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5919 return -EINVAL;
5920 }
5921 ecc->calculate = nand_bch_calculate_ecc;
5922 ecc->correct = nand_bch_correct_data;
5923 ecc->read_page = nand_read_page_swecc;
5924 ecc->read_subpage = nand_read_subpage;
5925 ecc->write_page = nand_write_page_swecc;
5926 ecc->read_page_raw = nand_read_page_raw;
5927 ecc->write_page_raw = nand_write_page_raw;
5928 ecc->read_oob = nand_read_oob_std;
5929 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005930
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005931 /*
5932 * Board driver should supply ecc.size and ecc.strength
5933 * values to select how many bits are correctable.
5934 * Otherwise, default to 4 bits for large page devices.
5935 */
5936 if (!ecc->size && (mtd->oobsize >= 64)) {
5937 ecc->size = 512;
5938 ecc->strength = 4;
5939 }
5940
5941 /*
5942 * if no ecc placement scheme was provided pickup the default
5943 * large page one.
5944 */
5945 if (!mtd->ooblayout) {
5946 /* handle large page devices only */
5947 if (mtd->oobsize < 64) {
5948 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5949 return -EINVAL;
5950 }
5951
5952 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005953
5954 }
5955
5956 /*
5957 * We can only maximize ECC config when the default layout is
5958 * used, otherwise we don't know how many bytes can really be
5959 * used.
5960 */
5961 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5962 ecc->options & NAND_ECC_MAXIMIZE) {
5963 int steps, bytes;
5964
5965 /* Always prefer 1k blocks over 512bytes ones */
5966 ecc->size = 1024;
5967 steps = mtd->writesize / ecc->size;
5968
5969 /* Reserve 2 bytes for the BBM */
5970 bytes = (mtd->oobsize - 2) / steps;
5971 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005972 }
5973
5974 /* See nand_bch_init() for details. */
5975 ecc->bytes = 0;
5976 ecc->priv = nand_bch_init(mtd);
5977 if (!ecc->priv) {
5978 WARN(1, "BCH ECC initialization failed!\n");
5979 return -EINVAL;
5980 }
5981 return 0;
5982 default:
5983 WARN(1, "Unsupported ECC algorithm!\n");
5984 return -EINVAL;
5985 }
5986}
5987
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005988/**
5989 * nand_check_ecc_caps - check the sanity of preset ECC settings
5990 * @chip: nand chip info structure
5991 * @caps: ECC caps info structure
5992 * @oobavail: OOB size that the ECC engine can use
5993 *
5994 * When ECC step size and strength are already set, check if they are supported
5995 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5996 * On success, the calculated ECC bytes is set.
5997 */
5998int nand_check_ecc_caps(struct nand_chip *chip,
5999 const struct nand_ecc_caps *caps, int oobavail)
6000{
6001 struct mtd_info *mtd = nand_to_mtd(chip);
6002 const struct nand_ecc_step_info *stepinfo;
6003 int preset_step = chip->ecc.size;
6004 int preset_strength = chip->ecc.strength;
6005 int nsteps, ecc_bytes;
6006 int i, j;
6007
6008 if (WARN_ON(oobavail < 0))
6009 return -EINVAL;
6010
6011 if (!preset_step || !preset_strength)
6012 return -ENODATA;
6013
6014 nsteps = mtd->writesize / preset_step;
6015
6016 for (i = 0; i < caps->nstepinfos; i++) {
6017 stepinfo = &caps->stepinfos[i];
6018
6019 if (stepinfo->stepsize != preset_step)
6020 continue;
6021
6022 for (j = 0; j < stepinfo->nstrengths; j++) {
6023 if (stepinfo->strengths[j] != preset_strength)
6024 continue;
6025
6026 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6027 preset_strength);
6028 if (WARN_ON_ONCE(ecc_bytes < 0))
6029 return ecc_bytes;
6030
6031 if (ecc_bytes * nsteps > oobavail) {
6032 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6033 preset_step, preset_strength);
6034 return -ENOSPC;
6035 }
6036
6037 chip->ecc.bytes = ecc_bytes;
6038
6039 return 0;
6040 }
6041 }
6042
6043 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6044 preset_step, preset_strength);
6045
6046 return -ENOTSUPP;
6047}
6048EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
6049
6050/**
6051 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6052 * @chip: nand chip info structure
6053 * @caps: ECC engine caps info structure
6054 * @oobavail: OOB size that the ECC engine can use
6055 *
6056 * If a chip's ECC requirement is provided, try to meet it with the least
6057 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6058 * On success, the chosen ECC settings are set.
6059 */
6060int nand_match_ecc_req(struct nand_chip *chip,
6061 const struct nand_ecc_caps *caps, int oobavail)
6062{
6063 struct mtd_info *mtd = nand_to_mtd(chip);
6064 const struct nand_ecc_step_info *stepinfo;
6065 int req_step = chip->ecc_step_ds;
6066 int req_strength = chip->ecc_strength_ds;
6067 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6068 int best_step, best_strength, best_ecc_bytes;
6069 int best_ecc_bytes_total = INT_MAX;
6070 int i, j;
6071
6072 if (WARN_ON(oobavail < 0))
6073 return -EINVAL;
6074
6075 /* No information provided by the NAND chip */
6076 if (!req_step || !req_strength)
6077 return -ENOTSUPP;
6078
6079 /* number of correctable bits the chip requires in a page */
6080 req_corr = mtd->writesize / req_step * req_strength;
6081
6082 for (i = 0; i < caps->nstepinfos; i++) {
6083 stepinfo = &caps->stepinfos[i];
6084 step_size = stepinfo->stepsize;
6085
6086 for (j = 0; j < stepinfo->nstrengths; j++) {
6087 strength = stepinfo->strengths[j];
6088
6089 /*
6090 * If both step size and strength are smaller than the
6091 * chip's requirement, it is not easy to compare the
6092 * resulted reliability.
6093 */
6094 if (step_size < req_step && strength < req_strength)
6095 continue;
6096
6097 if (mtd->writesize % step_size)
6098 continue;
6099
6100 nsteps = mtd->writesize / step_size;
6101
6102 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6103 if (WARN_ON_ONCE(ecc_bytes < 0))
6104 continue;
6105 ecc_bytes_total = ecc_bytes * nsteps;
6106
6107 if (ecc_bytes_total > oobavail ||
6108 strength * nsteps < req_corr)
6109 continue;
6110
6111 /*
6112 * We assume the best is to meet the chip's requrement
6113 * with the least number of ECC bytes.
6114 */
6115 if (ecc_bytes_total < best_ecc_bytes_total) {
6116 best_ecc_bytes_total = ecc_bytes_total;
6117 best_step = step_size;
6118 best_strength = strength;
6119 best_ecc_bytes = ecc_bytes;
6120 }
6121 }
6122 }
6123
6124 if (best_ecc_bytes_total == INT_MAX)
6125 return -ENOTSUPP;
6126
6127 chip->ecc.size = best_step;
6128 chip->ecc.strength = best_strength;
6129 chip->ecc.bytes = best_ecc_bytes;
6130
6131 return 0;
6132}
6133EXPORT_SYMBOL_GPL(nand_match_ecc_req);
6134
6135/**
6136 * nand_maximize_ecc - choose the max ECC strength available
6137 * @chip: nand chip info structure
6138 * @caps: ECC engine caps info structure
6139 * @oobavail: OOB size that the ECC engine can use
6140 *
6141 * Choose the max ECC strength that is supported on the controller, and can fit
6142 * within the chip's OOB. On success, the chosen ECC settings are set.
6143 */
6144int nand_maximize_ecc(struct nand_chip *chip,
6145 const struct nand_ecc_caps *caps, int oobavail)
6146{
6147 struct mtd_info *mtd = nand_to_mtd(chip);
6148 const struct nand_ecc_step_info *stepinfo;
6149 int step_size, strength, nsteps, ecc_bytes, corr;
6150 int best_corr = 0;
6151 int best_step = 0;
6152 int best_strength, best_ecc_bytes;
6153 int i, j;
6154
6155 if (WARN_ON(oobavail < 0))
6156 return -EINVAL;
6157
6158 for (i = 0; i < caps->nstepinfos; i++) {
6159 stepinfo = &caps->stepinfos[i];
6160 step_size = stepinfo->stepsize;
6161
6162 /* If chip->ecc.size is already set, respect it */
6163 if (chip->ecc.size && step_size != chip->ecc.size)
6164 continue;
6165
6166 for (j = 0; j < stepinfo->nstrengths; j++) {
6167 strength = stepinfo->strengths[j];
6168
6169 if (mtd->writesize % step_size)
6170 continue;
6171
6172 nsteps = mtd->writesize / step_size;
6173
6174 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6175 if (WARN_ON_ONCE(ecc_bytes < 0))
6176 continue;
6177
6178 if (ecc_bytes * nsteps > oobavail)
6179 continue;
6180
6181 corr = strength * nsteps;
6182
6183 /*
6184 * If the number of correctable bits is the same,
6185 * bigger step_size has more reliability.
6186 */
6187 if (corr > best_corr ||
6188 (corr == best_corr && step_size > best_step)) {
6189 best_corr = corr;
6190 best_step = step_size;
6191 best_strength = strength;
6192 best_ecc_bytes = ecc_bytes;
6193 }
6194 }
6195 }
6196
6197 if (!best_corr)
6198 return -ENOTSUPP;
6199
6200 chip->ecc.size = best_step;
6201 chip->ecc.strength = best_strength;
6202 chip->ecc.bytes = best_ecc_bytes;
6203
6204 return 0;
6205}
6206EXPORT_SYMBOL_GPL(nand_maximize_ecc);
6207
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006208/*
6209 * Check if the chip configuration meet the datasheet requirements.
6210
6211 * If our configuration corrects A bits per B bytes and the minimum
6212 * required correction level is X bits per Y bytes, then we must ensure
6213 * both of the following are true:
6214 *
6215 * (1) A / B >= X / Y
6216 * (2) A >= X
6217 *
6218 * Requirement (1) ensures we can correct for the required bitflip density.
6219 * Requirement (2) ensures we can correct even when all bitflips are clumped
6220 * in the same sector.
6221 */
6222static bool nand_ecc_strength_good(struct mtd_info *mtd)
6223{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006224 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006225 struct nand_ecc_ctrl *ecc = &chip->ecc;
6226 int corr, ds_corr;
6227
6228 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6229 /* Not enough information */
6230 return true;
6231
6232 /*
6233 * We get the number of corrected bits per page to compare
6234 * the correction density.
6235 */
6236 corr = (mtd->writesize * ecc->strength) / ecc->size;
6237 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6238
6239 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6240}
David Woodhouse3b85c322006-09-25 17:06:53 +01006241
6242/**
6243 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006244 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01006245 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006246 * This is the second phase of the normal nand_scan() function. It fills out
6247 * all the uninitialized function pointers with the defaults and scans for a
6248 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006249 */
6250int nand_scan_tail(struct mtd_info *mtd)
6251{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006252 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08006253 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006254 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006255
Brian Norrise2414f42012-02-06 13:44:00 -08006256 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006257 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006258 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006259 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006260 }
Brian Norrise2414f42012-02-06 13:44:00 -08006261
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006262 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006263 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006264 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006265
Boris Brezillonf84674b2017-06-02 12:18:24 +02006266 /*
6267 * FIXME: some NAND manufacturer drivers expect the first die to be
6268 * selected when manufacturer->init() is called. They should be fixed
6269 * to explictly select the relevant die when interacting with the NAND
6270 * chip.
6271 */
6272 chip->select_chip(mtd, 0);
6273 ret = nand_manufacturer_init(chip);
6274 chip->select_chip(mtd, -1);
6275 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006276 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006277
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006278 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006279 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006280
6281 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006282 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006283 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006284 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006285 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006286 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006289 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 break;
6291 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006292 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006293 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006296 /*
6297 * Expose the whole OOB area to users if ECC_NONE
6298 * is passed. We could do that for all kind of
6299 * ->oobsize, but we must keep the old large/small
6300 * page with ECC layout when ->oobsize <= 128 for
6301 * compatibility reasons.
6302 */
6303 if (ecc->mode == NAND_ECC_NONE) {
6304 mtd_set_ooblayout(mtd,
6305 &nand_ooblayout_lp_ops);
6306 break;
6307 }
6308
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006309 WARN(1, "No oob scheme defined for oobsize %d\n",
6310 mtd->oobsize);
6311 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006312 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 }
6314 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006315
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006316 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006317 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006318 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006319 */
David Woodhouse956e9442006-09-25 17:12:39 +01006320
Huang Shijie97de79e02013-10-18 14:20:53 +08006321 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006322 case NAND_ECC_HW_OOB_FIRST:
6323 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006324 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006325 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6326 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006327 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006328 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006329 if (!ecc->read_page)
6330 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006331
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006332 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006333 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006334 if (!ecc->read_page)
6335 ecc->read_page = nand_read_page_hwecc;
6336 if (!ecc->write_page)
6337 ecc->write_page = nand_write_page_hwecc;
6338 if (!ecc->read_page_raw)
6339 ecc->read_page_raw = nand_read_page_raw;
6340 if (!ecc->write_page_raw)
6341 ecc->write_page_raw = nand_write_page_raw;
6342 if (!ecc->read_oob)
6343 ecc->read_oob = nand_read_oob_std;
6344 if (!ecc->write_oob)
6345 ecc->write_oob = nand_write_oob_std;
6346 if (!ecc->read_subpage)
6347 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006348 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006349 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006350
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006351 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006352 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6353 (!ecc->read_page ||
6354 ecc->read_page == nand_read_page_hwecc ||
6355 !ecc->write_page ||
6356 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006357 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6358 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006359 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006360 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006361 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006362 if (!ecc->read_page)
6363 ecc->read_page = nand_read_page_syndrome;
6364 if (!ecc->write_page)
6365 ecc->write_page = nand_write_page_syndrome;
6366 if (!ecc->read_page_raw)
6367 ecc->read_page_raw = nand_read_page_raw_syndrome;
6368 if (!ecc->write_page_raw)
6369 ecc->write_page_raw = nand_write_page_raw_syndrome;
6370 if (!ecc->read_oob)
6371 ecc->read_oob = nand_read_oob_syndrome;
6372 if (!ecc->write_oob)
6373 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006374
Huang Shijie97de79e02013-10-18 14:20:53 +08006375 if (mtd->writesize >= ecc->size) {
6376 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006377 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6378 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006379 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006380 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006381 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006382 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006383 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6384 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006385 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006386 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006388 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006389 ret = nand_set_ecc_soft_ops(mtd);
6390 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006391 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006392 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006393 }
6394 break;
6395
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006396 case NAND_ECC_ON_DIE:
6397 if (!ecc->read_page || !ecc->write_page) {
6398 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6399 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006400 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006401 }
6402 if (!ecc->read_oob)
6403 ecc->read_oob = nand_read_oob_std;
6404 if (!ecc->write_oob)
6405 ecc->write_oob = nand_write_oob_std;
6406 break;
6407
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006408 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006409 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006410 ecc->read_page = nand_read_page_raw;
6411 ecc->write_page = nand_write_page_raw;
6412 ecc->read_oob = nand_read_oob_std;
6413 ecc->read_page_raw = nand_read_page_raw;
6414 ecc->write_page_raw = nand_write_page_raw;
6415 ecc->write_oob = nand_write_oob_std;
6416 ecc->size = mtd->writesize;
6417 ecc->bytes = 0;
6418 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006420
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006422 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6423 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006424 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006427 if (ecc->correct || ecc->calculate) {
6428 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6429 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6430 if (!ecc->calc_buf || !ecc->code_buf) {
6431 ret = -ENOMEM;
6432 goto err_nand_manuf_cleanup;
6433 }
6434 }
6435
Brian Norris9ce244b2011-08-30 18:45:37 -07006436 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006437 if (!ecc->read_oob_raw)
6438 ecc->read_oob_raw = ecc->read_oob;
6439 if (!ecc->write_oob_raw)
6440 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006441
Boris Brezillon846031d2016-02-03 20:11:00 +01006442 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006443 mtd->ecc_strength = ecc->strength;
6444 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006445
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006446 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006447 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006448 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006449 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006450 ecc->steps = mtd->writesize / ecc->size;
6451 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006452 WARN(1, "Invalid ECC parameters\n");
6453 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006454 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006456 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006457 if (ecc->total > mtd->oobsize) {
6458 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6459 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006460 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006461 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006462
Boris Brezillon846031d2016-02-03 20:11:00 +01006463 /*
6464 * The number of bytes available for a client to place data into
6465 * the out of band area.
6466 */
6467 ret = mtd_ooblayout_count_freebytes(mtd);
6468 if (ret < 0)
6469 ret = 0;
6470
6471 mtd->oobavail = ret;
6472
6473 /* ECC sanity check: warn if it's too weak */
6474 if (!nand_ecc_strength_good(mtd))
6475 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6476 mtd->name);
6477
Brian Norris8b6e50c2011-05-25 14:59:01 -07006478 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006479 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006480 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006481 case 2:
6482 mtd->subpage_sft = 1;
6483 break;
6484 case 4:
6485 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006486 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006487 mtd->subpage_sft = 2;
6488 break;
6489 }
6490 }
6491 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6492
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006493 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006494 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006497 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006499 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306500 switch (ecc->mode) {
6501 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306502 if (chip->page_shift > 9)
6503 chip->options |= NAND_SUBPAGE_READ;
6504 break;
6505
6506 default:
6507 break;
6508 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006509
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006511 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006512 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6513 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006514 mtd->_erase = nand_erase;
6515 mtd->_point = NULL;
6516 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006517 mtd->_panic_write = panic_nand_write;
6518 mtd->_read_oob = nand_read_oob;
6519 mtd->_write_oob = nand_write_oob;
6520 mtd->_sync = nand_sync;
6521 mtd->_lock = NULL;
6522 mtd->_unlock = NULL;
6523 mtd->_suspend = nand_suspend;
6524 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006525 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006526 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006527 mtd->_block_isbad = nand_block_isbad;
6528 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006529 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006530 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006532 /*
6533 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6534 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6535 * properly set.
6536 */
6537 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006538 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539
Boris Brezillonf84674b2017-06-02 12:18:24 +02006540 /* Initialize the ->data_interface field. */
6541 ret = nand_init_data_interface(chip);
6542 if (ret)
6543 goto err_nand_manuf_cleanup;
6544
6545 /* Enter fastest possible mode on all dies. */
6546 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006547 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006548 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006549 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006550 }
6551
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006552 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006553 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555
6556 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07006557 ret = chip->scan_bbt(mtd);
6558 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006559 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006560
Brian Norris44d41822017-05-01 17:04:50 -07006561 return 0;
6562
Boris Brezillonf84674b2017-06-02 12:18:24 +02006563
6564err_nand_manuf_cleanup:
6565 nand_manufacturer_cleanup(chip);
6566
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006567err_free_buf:
6568 kfree(chip->data_buf);
6569 kfree(ecc->code_buf);
6570 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006571
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006572 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006574EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575
Brian Norris8b6e50c2011-05-25 14:59:01 -07006576/*
6577 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006578 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07006579 * to call us from in-kernel code if the core NAND support is modular.
6580 */
David Woodhouse3b85c322006-09-25 17:06:53 +01006581#ifdef MODULE
6582#define caller_is_module() (1)
6583#else
6584#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06006585 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01006586#endif
6587
6588/**
6589 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006590 * @mtd: MTD device structure
6591 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01006592 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006593 * This fills out all the uninitialized function pointers with the defaults.
6594 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006595 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006596 */
6597int nand_scan(struct mtd_info *mtd, int maxchips)
6598{
6599 int ret;
6600
David Woodhouse5e81e882010-02-26 18:32:56 +00006601 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01006602 if (!ret)
6603 ret = nand_scan_tail(mtd);
6604 return ret;
6605}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006606EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01006607
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006609 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6610 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006611 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006612void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006614 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006615 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006616 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6617
Jesper Juhlfa671642005-11-07 01:01:27 -08006618 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006619 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006620 kfree(chip->data_buf);
6621 kfree(chip->ecc.code_buf);
6622 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006623
6624 /* Free bad block descriptor memory */
6625 if (chip->badblock_pattern && chip->badblock_pattern->options
6626 & NAND_BBT_DYNAMICSTRUCT)
6627 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006628
6629 /* Free manufacturer priv data. */
6630 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006632EXPORT_SYMBOL_GPL(nand_cleanup);
6633
6634/**
6635 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6636 * held by the NAND device
6637 * @mtd: MTD device structure
6638 */
6639void nand_release(struct mtd_info *mtd)
6640{
6641 mtd_device_unregister(mtd);
6642 nand_cleanup(mtd_to_nand(mtd));
6643}
David Woodhousee0c7d762006-05-13 18:07:53 +01006644EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006645
David Woodhousee0c7d762006-05-13 18:07:53 +01006646MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006647MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6648MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006649MODULE_DESCRIPTION("Generic NAND flash driver code");