blob: 837ea698af083cee6d57db0238e0c9d620d1e6b0 [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{
1165 return (chip->onfi_version &&
1166 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1167 ONFI_OPT_CMD_SET_GET_FEATURES));
1168}
1169
1170/**
1171 * nand_get_features - wrapper to perform a GET_FEATURE
1172 * @chip: NAND chip info structure
1173 * @addr: feature address
1174 * @subfeature_param: the subfeature parameters, a four bytes array
1175 *
1176 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1177 * operation cannot be handled.
1178 */
1179int nand_get_features(struct nand_chip *chip, int addr,
1180 u8 *subfeature_param)
1181{
1182 struct mtd_info *mtd = nand_to_mtd(chip);
1183
1184 if (!nand_supports_set_get_features(chip))
1185 return -ENOTSUPP;
1186
1187 return chip->get_features(mtd, chip, addr, subfeature_param);
1188}
1189EXPORT_SYMBOL_GPL(nand_get_features);
1190
1191/**
1192 * nand_set_features - wrapper to perform a SET_FEATURE
1193 * @chip: NAND chip info structure
1194 * @addr: feature address
1195 * @subfeature_param: the subfeature parameters, a four bytes array
1196 *
1197 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1198 * operation cannot be handled.
1199 */
1200int nand_set_features(struct nand_chip *chip, int addr,
1201 u8 *subfeature_param)
1202{
1203 struct mtd_info *mtd = nand_to_mtd(chip);
1204
1205 if (!nand_supports_set_get_features(chip))
1206 return -ENOTSUPP;
1207
1208 return chip->set_features(mtd, chip, addr, subfeature_param);
1209}
1210EXPORT_SYMBOL_GPL(nand_set_features);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001213 * nand_reset_data_interface - Reset data interface and timings
1214 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001215 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001216 *
1217 * Reset the Data interface and timings to ONFI mode 0.
1218 *
1219 * Returns 0 for success or negative error code otherwise.
1220 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001221static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001222{
1223 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001224 int ret;
1225
1226 if (!chip->setup_data_interface)
1227 return 0;
1228
1229 /*
1230 * The ONFI specification says:
1231 * "
1232 * To transition from NV-DDR or NV-DDR2 to the SDR data
1233 * interface, the host shall use the Reset (FFh) command
1234 * using SDR timing mode 0. A device in any timing mode is
1235 * required to recognize Reset (FFh) command issued in SDR
1236 * timing mode 0.
1237 * "
1238 *
1239 * Configure the data interface in SDR mode and set the
1240 * timings to timing mode 0.
1241 */
1242
Miquel Raynal17fa8042017-11-30 18:01:31 +01001243 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1244 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001245 if (ret)
1246 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1247
1248 return ret;
1249}
1250
1251/**
1252 * nand_setup_data_interface - Setup the best data interface and timings
1253 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001254 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001255 *
1256 * Find and configure the best data interface and NAND timings supported by
1257 * the chip and the driver.
1258 * First tries to retrieve supported timing modes from ONFI information,
1259 * and if the NAND chip does not support ONFI, relies on the
1260 * ->onfi_timing_mode_default specified in the nand_ids table.
1261 *
1262 * Returns 0 for success or negative error code otherwise.
1263 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001264static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001265{
1266 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal97baea12018-03-19 14:47:20 +01001267 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1268 chip->onfi_timing_mode_default,
1269 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001270 int ret;
1271
Miquel Raynal17fa8042017-11-30 18:01:31 +01001272 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001273 return 0;
1274
Miquel Raynal993447b2018-03-19 14:47:21 +01001275 /* Change the mode on the chip side (if supported by the NAND chip) */
1276 if (nand_supports_set_get_features(chip)) {
Miquel Raynal29714d62018-03-19 14:47:23 +01001277 chip->select_chip(mtd, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001278 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1279 tmode_param);
Miquel Raynal29714d62018-03-19 14:47:23 +01001280 chip->select_chip(mtd, -1);
Miquel Raynal993447b2018-03-19 14:47:21 +01001281 if (ret)
1282 return ret;
1283 }
Boris Brezillond8e725d2016-09-15 10:32:50 +02001284
Miquel Raynal97baea12018-03-19 14:47:20 +01001285 /* Change the mode on the controller side */
Miquel Raynal415ae782018-03-19 14:47:24 +01001286 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
1287 if (ret)
1288 return ret;
1289
1290 /* Check the mode has been accepted by the chip, if supported */
1291 if (!nand_supports_set_get_features(chip))
1292 return 0;
1293
1294 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
1295 chip->select_chip(mtd, chipnr);
1296 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1297 tmode_param);
1298 chip->select_chip(mtd, -1);
1299 if (ret)
1300 goto err_reset_chip;
1301
1302 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1303 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1304 chip->onfi_timing_mode_default);
1305 goto err_reset_chip;
1306 }
1307
1308 return 0;
1309
1310err_reset_chip:
1311 /*
1312 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1313 * timing mode.
1314 */
1315 nand_reset_data_interface(chip, chipnr);
1316 chip->select_chip(mtd, chipnr);
1317 nand_reset_op(chip);
1318 chip->select_chip(mtd, -1);
1319
1320 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001321}
1322
1323/**
1324 * nand_init_data_interface - find the best data interface and timings
1325 * @chip: The NAND chip
1326 *
1327 * Find the best data interface and NAND timings supported by the chip
1328 * and the driver.
1329 * First tries to retrieve supported timing modes from ONFI information,
1330 * and if the NAND chip does not support ONFI, relies on the
1331 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1332 * function nand_chip->data_interface is initialized with the best timing mode
1333 * available.
1334 *
1335 * Returns 0 for success or negative error code otherwise.
1336 */
1337static int nand_init_data_interface(struct nand_chip *chip)
1338{
1339 struct mtd_info *mtd = nand_to_mtd(chip);
1340 int modes, mode, ret;
1341
1342 if (!chip->setup_data_interface)
1343 return 0;
1344
1345 /*
1346 * First try to identify the best timings from ONFI parameters and
1347 * if the NAND does not support ONFI, fallback to the default ONFI
1348 * timing mode.
1349 */
1350 modes = onfi_get_async_timing_mode(chip);
1351 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1352 if (!chip->onfi_timing_mode_default)
1353 return 0;
1354
1355 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1356 }
1357
Boris Brezillond8e725d2016-09-15 10:32:50 +02001358
1359 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001360 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001361 if (ret)
1362 continue;
1363
Miquel Raynald787b8b2017-12-22 18:12:41 +01001364 /*
1365 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1366 * controller supports the requested timings.
1367 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001368 ret = chip->setup_data_interface(mtd,
1369 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001370 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001371 if (!ret) {
1372 chip->onfi_timing_mode_default = mode;
1373 break;
1374 }
1375 }
1376
1377 return 0;
1378}
1379
Boris Brezillond8e725d2016-09-15 10:32:50 +02001380/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001381 * nand_fill_column_cycles - fill the column cycles of an address
1382 * @chip: The NAND chip
1383 * @addrs: Array of address cycles to fill
1384 * @offset_in_page: The offset in the page
1385 *
1386 * Fills the first or the first two bytes of the @addrs field depending
1387 * on the NAND bus width and the page size.
1388 *
1389 * Returns the number of cycles needed to encode the column, or a negative
1390 * error code in case one of the arguments is invalid.
1391 */
1392static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1393 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Miquel Raynal8878b122017-11-09 14:16:45 +01001395 struct mtd_info *mtd = nand_to_mtd(chip);
1396
1397 /* Make sure the offset is less than the actual page size. */
1398 if (offset_in_page > mtd->writesize + mtd->oobsize)
1399 return -EINVAL;
1400
1401 /*
1402 * On small page NANDs, there's a dedicated command to access the OOB
1403 * area, and the column address is relative to the start of the OOB
1404 * area, not the start of the page. Asjust the address accordingly.
1405 */
1406 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1407 offset_in_page -= mtd->writesize;
1408
1409 /*
1410 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1411 * wide, then it must be divided by 2.
1412 */
1413 if (chip->options & NAND_BUSWIDTH_16) {
1414 if (WARN_ON(offset_in_page % 2))
1415 return -EINVAL;
1416
1417 offset_in_page /= 2;
1418 }
1419
1420 addrs[0] = offset_in_page;
1421
1422 /*
1423 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1424 * need 2
1425 */
1426 if (mtd->writesize <= 512)
1427 return 1;
1428
1429 addrs[1] = offset_in_page >> 8;
1430
1431 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Miquel Raynal8878b122017-11-09 14:16:45 +01001434static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1435 unsigned int offset_in_page, void *buf,
1436 unsigned int len)
1437{
1438 struct mtd_info *mtd = nand_to_mtd(chip);
1439 const struct nand_sdr_timings *sdr =
1440 nand_get_sdr_timings(&chip->data_interface);
1441 u8 addrs[4];
1442 struct nand_op_instr instrs[] = {
1443 NAND_OP_CMD(NAND_CMD_READ0, 0),
1444 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1445 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1446 PSEC_TO_NSEC(sdr->tRR_min)),
1447 NAND_OP_DATA_IN(len, buf, 0),
1448 };
1449 struct nand_operation op = NAND_OPERATION(instrs);
1450 int ret;
1451
1452 /* Drop the DATA_IN instruction if len is set to 0. */
1453 if (!len)
1454 op.ninstrs--;
1455
1456 if (offset_in_page >= mtd->writesize)
1457 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1458 else if (offset_in_page >= 256 &&
1459 !(chip->options & NAND_BUSWIDTH_16))
1460 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1461
1462 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1463 if (ret < 0)
1464 return ret;
1465
1466 addrs[1] = page;
1467 addrs[2] = page >> 8;
1468
1469 if (chip->options & NAND_ROW_ADDR_3) {
1470 addrs[3] = page >> 16;
1471 instrs[1].ctx.addr.naddrs++;
1472 }
1473
1474 return nand_exec_op(chip, &op);
1475}
1476
1477static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1478 unsigned int offset_in_page, void *buf,
1479 unsigned int len)
1480{
1481 const struct nand_sdr_timings *sdr =
1482 nand_get_sdr_timings(&chip->data_interface);
1483 u8 addrs[5];
1484 struct nand_op_instr instrs[] = {
1485 NAND_OP_CMD(NAND_CMD_READ0, 0),
1486 NAND_OP_ADDR(4, addrs, 0),
1487 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1488 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1489 PSEC_TO_NSEC(sdr->tRR_min)),
1490 NAND_OP_DATA_IN(len, buf, 0),
1491 };
1492 struct nand_operation op = NAND_OPERATION(instrs);
1493 int ret;
1494
1495 /* Drop the DATA_IN instruction if len is set to 0. */
1496 if (!len)
1497 op.ninstrs--;
1498
1499 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1500 if (ret < 0)
1501 return ret;
1502
1503 addrs[2] = page;
1504 addrs[3] = page >> 8;
1505
1506 if (chip->options & NAND_ROW_ADDR_3) {
1507 addrs[4] = page >> 16;
1508 instrs[1].ctx.addr.naddrs++;
1509 }
1510
1511 return nand_exec_op(chip, &op);
1512}
1513
1514/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001515 * nand_read_page_op - Do a READ PAGE operation
1516 * @chip: The NAND chip
1517 * @page: page to read
1518 * @offset_in_page: offset within the page
1519 * @buf: buffer used to store the data
1520 * @len: length of the buffer
1521 *
1522 * This function issues a READ PAGE operation.
1523 * This function does not select/unselect the CS line.
1524 *
1525 * Returns 0 on success, a negative error code otherwise.
1526 */
1527int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1528 unsigned int offset_in_page, void *buf, unsigned int len)
1529{
1530 struct mtd_info *mtd = nand_to_mtd(chip);
1531
1532 if (len && !buf)
1533 return -EINVAL;
1534
1535 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1536 return -EINVAL;
1537
Miquel Raynal8878b122017-11-09 14:16:45 +01001538 if (chip->exec_op) {
1539 if (mtd->writesize > 512)
1540 return nand_lp_exec_read_page_op(chip, page,
1541 offset_in_page, buf,
1542 len);
1543
1544 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1545 buf, len);
1546 }
1547
Boris Brezillon97d90da2017-11-30 18:01:29 +01001548 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1549 if (len)
1550 chip->read_buf(mtd, buf, len);
1551
1552 return 0;
1553}
1554EXPORT_SYMBOL_GPL(nand_read_page_op);
1555
1556/**
1557 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1558 * @chip: The NAND chip
1559 * @page: parameter page to read
1560 * @buf: buffer used to store the data
1561 * @len: length of the buffer
1562 *
1563 * This function issues a READ PARAMETER PAGE operation.
1564 * This function does not select/unselect the CS line.
1565 *
1566 * Returns 0 on success, a negative error code otherwise.
1567 */
1568static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1569 unsigned int len)
1570{
1571 struct mtd_info *mtd = nand_to_mtd(chip);
1572 unsigned int i;
1573 u8 *p = buf;
1574
1575 if (len && !buf)
1576 return -EINVAL;
1577
Miquel Raynal8878b122017-11-09 14:16:45 +01001578 if (chip->exec_op) {
1579 const struct nand_sdr_timings *sdr =
1580 nand_get_sdr_timings(&chip->data_interface);
1581 struct nand_op_instr instrs[] = {
1582 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1583 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1584 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1585 PSEC_TO_NSEC(sdr->tRR_min)),
1586 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1587 };
1588 struct nand_operation op = NAND_OPERATION(instrs);
1589
1590 /* Drop the DATA_IN instruction if len is set to 0. */
1591 if (!len)
1592 op.ninstrs--;
1593
1594 return nand_exec_op(chip, &op);
1595 }
1596
Boris Brezillon97d90da2017-11-30 18:01:29 +01001597 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1598 for (i = 0; i < len; i++)
1599 p[i] = chip->read_byte(mtd);
1600
1601 return 0;
1602}
1603
1604/**
1605 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1606 * @chip: The NAND chip
1607 * @offset_in_page: offset within the page
1608 * @buf: buffer used to store the data
1609 * @len: length of the buffer
1610 * @force_8bit: force 8-bit bus access
1611 *
1612 * This function issues a CHANGE READ COLUMN operation.
1613 * This function does not select/unselect the CS line.
1614 *
1615 * Returns 0 on success, a negative error code otherwise.
1616 */
1617int nand_change_read_column_op(struct nand_chip *chip,
1618 unsigned int offset_in_page, void *buf,
1619 unsigned int len, bool force_8bit)
1620{
1621 struct mtd_info *mtd = nand_to_mtd(chip);
1622
1623 if (len && !buf)
1624 return -EINVAL;
1625
1626 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1627 return -EINVAL;
1628
Miquel Raynal8878b122017-11-09 14:16:45 +01001629 /* Small page NANDs do not support column change. */
1630 if (mtd->writesize <= 512)
1631 return -ENOTSUPP;
1632
1633 if (chip->exec_op) {
1634 const struct nand_sdr_timings *sdr =
1635 nand_get_sdr_timings(&chip->data_interface);
1636 u8 addrs[2] = {};
1637 struct nand_op_instr instrs[] = {
1638 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1639 NAND_OP_ADDR(2, addrs, 0),
1640 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1641 PSEC_TO_NSEC(sdr->tCCS_min)),
1642 NAND_OP_DATA_IN(len, buf, 0),
1643 };
1644 struct nand_operation op = NAND_OPERATION(instrs);
1645 int ret;
1646
1647 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1648 if (ret < 0)
1649 return ret;
1650
1651 /* Drop the DATA_IN instruction if len is set to 0. */
1652 if (!len)
1653 op.ninstrs--;
1654
1655 instrs[3].ctx.data.force_8bit = force_8bit;
1656
1657 return nand_exec_op(chip, &op);
1658 }
1659
Boris Brezillon97d90da2017-11-30 18:01:29 +01001660 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1661 if (len)
1662 chip->read_buf(mtd, buf, len);
1663
1664 return 0;
1665}
1666EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1667
1668/**
1669 * nand_read_oob_op - Do a READ OOB operation
1670 * @chip: The NAND chip
1671 * @page: page to read
1672 * @offset_in_oob: offset within the OOB area
1673 * @buf: buffer used to store the data
1674 * @len: length of the buffer
1675 *
1676 * This function issues a READ OOB operation.
1677 * This function does not select/unselect the CS line.
1678 *
1679 * Returns 0 on success, a negative error code otherwise.
1680 */
1681int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1682 unsigned int offset_in_oob, void *buf, unsigned int len)
1683{
1684 struct mtd_info *mtd = nand_to_mtd(chip);
1685
1686 if (len && !buf)
1687 return -EINVAL;
1688
1689 if (offset_in_oob + len > mtd->oobsize)
1690 return -EINVAL;
1691
Miquel Raynal8878b122017-11-09 14:16:45 +01001692 if (chip->exec_op)
1693 return nand_read_page_op(chip, page,
1694 mtd->writesize + offset_in_oob,
1695 buf, len);
1696
Boris Brezillon97d90da2017-11-30 18:01:29 +01001697 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1698 if (len)
1699 chip->read_buf(mtd, buf, len);
1700
1701 return 0;
1702}
1703EXPORT_SYMBOL_GPL(nand_read_oob_op);
1704
Miquel Raynal8878b122017-11-09 14:16:45 +01001705static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1706 unsigned int offset_in_page, const void *buf,
1707 unsigned int len, bool prog)
1708{
1709 struct mtd_info *mtd = nand_to_mtd(chip);
1710 const struct nand_sdr_timings *sdr =
1711 nand_get_sdr_timings(&chip->data_interface);
1712 u8 addrs[5] = {};
1713 struct nand_op_instr instrs[] = {
1714 /*
1715 * The first instruction will be dropped if we're dealing
1716 * with a large page NAND and adjusted if we're dealing
1717 * with a small page NAND and the page offset is > 255.
1718 */
1719 NAND_OP_CMD(NAND_CMD_READ0, 0),
1720 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1721 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1722 NAND_OP_DATA_OUT(len, buf, 0),
1723 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1724 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1725 };
1726 struct nand_operation op = NAND_OPERATION(instrs);
1727 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1728 int ret;
1729 u8 status;
1730
1731 if (naddrs < 0)
1732 return naddrs;
1733
1734 addrs[naddrs++] = page;
1735 addrs[naddrs++] = page >> 8;
1736 if (chip->options & NAND_ROW_ADDR_3)
1737 addrs[naddrs++] = page >> 16;
1738
1739 instrs[2].ctx.addr.naddrs = naddrs;
1740
1741 /* Drop the last two instructions if we're not programming the page. */
1742 if (!prog) {
1743 op.ninstrs -= 2;
1744 /* Also drop the DATA_OUT instruction if empty. */
1745 if (!len)
1746 op.ninstrs--;
1747 }
1748
1749 if (mtd->writesize <= 512) {
1750 /*
1751 * Small pages need some more tweaking: we have to adjust the
1752 * first instruction depending on the page offset we're trying
1753 * to access.
1754 */
1755 if (offset_in_page >= mtd->writesize)
1756 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1757 else if (offset_in_page >= 256 &&
1758 !(chip->options & NAND_BUSWIDTH_16))
1759 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1760 } else {
1761 /*
1762 * Drop the first command if we're dealing with a large page
1763 * NAND.
1764 */
1765 op.instrs++;
1766 op.ninstrs--;
1767 }
1768
1769 ret = nand_exec_op(chip, &op);
1770 if (!prog || ret)
1771 return ret;
1772
1773 ret = nand_status_op(chip, &status);
1774 if (ret)
1775 return ret;
1776
1777 return status;
1778}
1779
Boris Brezillon97d90da2017-11-30 18:01:29 +01001780/**
1781 * nand_prog_page_begin_op - starts a PROG PAGE operation
1782 * @chip: The NAND chip
1783 * @page: page to write
1784 * @offset_in_page: offset within the page
1785 * @buf: buffer containing the data to write to the page
1786 * @len: length of the buffer
1787 *
1788 * This function issues the first half of a PROG PAGE operation.
1789 * This function does not select/unselect the CS line.
1790 *
1791 * Returns 0 on success, a negative error code otherwise.
1792 */
1793int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1794 unsigned int offset_in_page, const void *buf,
1795 unsigned int len)
1796{
1797 struct mtd_info *mtd = nand_to_mtd(chip);
1798
1799 if (len && !buf)
1800 return -EINVAL;
1801
1802 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1803 return -EINVAL;
1804
Miquel Raynal8878b122017-11-09 14:16:45 +01001805 if (chip->exec_op)
1806 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1807 len, false);
1808
Boris Brezillon97d90da2017-11-30 18:01:29 +01001809 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1810
1811 if (buf)
1812 chip->write_buf(mtd, buf, len);
1813
1814 return 0;
1815}
1816EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1817
1818/**
1819 * nand_prog_page_end_op - ends a PROG PAGE operation
1820 * @chip: The NAND chip
1821 *
1822 * This function issues the second half of a PROG PAGE operation.
1823 * This function does not select/unselect the CS line.
1824 *
1825 * Returns 0 on success, a negative error code otherwise.
1826 */
1827int nand_prog_page_end_op(struct nand_chip *chip)
1828{
1829 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001830 int ret;
1831 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001832
Miquel Raynal8878b122017-11-09 14:16:45 +01001833 if (chip->exec_op) {
1834 const struct nand_sdr_timings *sdr =
1835 nand_get_sdr_timings(&chip->data_interface);
1836 struct nand_op_instr instrs[] = {
1837 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1838 PSEC_TO_NSEC(sdr->tWB_max)),
1839 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1840 };
1841 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001842
Miquel Raynal8878b122017-11-09 14:16:45 +01001843 ret = nand_exec_op(chip, &op);
1844 if (ret)
1845 return ret;
1846
1847 ret = nand_status_op(chip, &status);
1848 if (ret)
1849 return ret;
1850 } else {
1851 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1852 ret = chip->waitfunc(mtd, chip);
1853 if (ret < 0)
1854 return ret;
1855
1856 status = ret;
1857 }
1858
Boris Brezillon97d90da2017-11-30 18:01:29 +01001859 if (status & NAND_STATUS_FAIL)
1860 return -EIO;
1861
1862 return 0;
1863}
1864EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1865
1866/**
1867 * nand_prog_page_op - Do a full PROG PAGE operation
1868 * @chip: The NAND chip
1869 * @page: page to write
1870 * @offset_in_page: offset within the page
1871 * @buf: buffer containing the data to write to the page
1872 * @len: length of the buffer
1873 *
1874 * This function issues a full PROG PAGE operation.
1875 * This function does not select/unselect the CS line.
1876 *
1877 * Returns 0 on success, a negative error code otherwise.
1878 */
1879int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1880 unsigned int offset_in_page, const void *buf,
1881 unsigned int len)
1882{
1883 struct mtd_info *mtd = nand_to_mtd(chip);
1884 int status;
1885
1886 if (!len || !buf)
1887 return -EINVAL;
1888
1889 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1890 return -EINVAL;
1891
Miquel Raynal8878b122017-11-09 14:16:45 +01001892 if (chip->exec_op) {
1893 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1894 len, true);
1895 } else {
1896 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1897 chip->write_buf(mtd, buf, len);
1898 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1899 status = chip->waitfunc(mtd, chip);
1900 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001901
Boris Brezillon97d90da2017-11-30 18:01:29 +01001902 if (status & NAND_STATUS_FAIL)
1903 return -EIO;
1904
1905 return 0;
1906}
1907EXPORT_SYMBOL_GPL(nand_prog_page_op);
1908
1909/**
1910 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1911 * @chip: The NAND chip
1912 * @offset_in_page: offset within the page
1913 * @buf: buffer containing the data to send to the NAND
1914 * @len: length of the buffer
1915 * @force_8bit: force 8-bit bus access
1916 *
1917 * This function issues a CHANGE WRITE COLUMN operation.
1918 * This function does not select/unselect the CS line.
1919 *
1920 * Returns 0 on success, a negative error code otherwise.
1921 */
1922int nand_change_write_column_op(struct nand_chip *chip,
1923 unsigned int offset_in_page,
1924 const void *buf, unsigned int len,
1925 bool force_8bit)
1926{
1927 struct mtd_info *mtd = nand_to_mtd(chip);
1928
1929 if (len && !buf)
1930 return -EINVAL;
1931
1932 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1933 return -EINVAL;
1934
Miquel Raynal8878b122017-11-09 14:16:45 +01001935 /* Small page NANDs do not support column change. */
1936 if (mtd->writesize <= 512)
1937 return -ENOTSUPP;
1938
1939 if (chip->exec_op) {
1940 const struct nand_sdr_timings *sdr =
1941 nand_get_sdr_timings(&chip->data_interface);
1942 u8 addrs[2];
1943 struct nand_op_instr instrs[] = {
1944 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1945 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1946 NAND_OP_DATA_OUT(len, buf, 0),
1947 };
1948 struct nand_operation op = NAND_OPERATION(instrs);
1949 int ret;
1950
1951 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1952 if (ret < 0)
1953 return ret;
1954
1955 instrs[2].ctx.data.force_8bit = force_8bit;
1956
1957 /* Drop the DATA_OUT instruction if len is set to 0. */
1958 if (!len)
1959 op.ninstrs--;
1960
1961 return nand_exec_op(chip, &op);
1962 }
1963
Boris Brezillon97d90da2017-11-30 18:01:29 +01001964 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1965 if (len)
1966 chip->write_buf(mtd, buf, len);
1967
1968 return 0;
1969}
1970EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1971
1972/**
1973 * nand_readid_op - Do a READID operation
1974 * @chip: The NAND chip
1975 * @addr: address cycle to pass after the READID command
1976 * @buf: buffer used to store the ID
1977 * @len: length of the buffer
1978 *
1979 * This function sends a READID command and reads back the ID returned by the
1980 * NAND.
1981 * This function does not select/unselect the CS line.
1982 *
1983 * Returns 0 on success, a negative error code otherwise.
1984 */
1985int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1986 unsigned int len)
1987{
1988 struct mtd_info *mtd = nand_to_mtd(chip);
1989 unsigned int i;
1990 u8 *id = buf;
1991
1992 if (len && !buf)
1993 return -EINVAL;
1994
Miquel Raynal8878b122017-11-09 14:16:45 +01001995 if (chip->exec_op) {
1996 const struct nand_sdr_timings *sdr =
1997 nand_get_sdr_timings(&chip->data_interface);
1998 struct nand_op_instr instrs[] = {
1999 NAND_OP_CMD(NAND_CMD_READID, 0),
2000 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
2001 NAND_OP_8BIT_DATA_IN(len, buf, 0),
2002 };
2003 struct nand_operation op = NAND_OPERATION(instrs);
2004
2005 /* Drop the DATA_IN instruction if len is set to 0. */
2006 if (!len)
2007 op.ninstrs--;
2008
2009 return nand_exec_op(chip, &op);
2010 }
2011
Boris Brezillon97d90da2017-11-30 18:01:29 +01002012 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
2013
2014 for (i = 0; i < len; i++)
2015 id[i] = chip->read_byte(mtd);
2016
2017 return 0;
2018}
2019EXPORT_SYMBOL_GPL(nand_readid_op);
2020
2021/**
2022 * nand_status_op - Do a STATUS operation
2023 * @chip: The NAND chip
2024 * @status: out variable to store the NAND status
2025 *
2026 * This function sends a STATUS command and reads back the status returned by
2027 * the NAND.
2028 * This function does not select/unselect the CS line.
2029 *
2030 * Returns 0 on success, a negative error code otherwise.
2031 */
2032int nand_status_op(struct nand_chip *chip, u8 *status)
2033{
2034 struct mtd_info *mtd = nand_to_mtd(chip);
2035
Miquel Raynal8878b122017-11-09 14:16:45 +01002036 if (chip->exec_op) {
2037 const struct nand_sdr_timings *sdr =
2038 nand_get_sdr_timings(&chip->data_interface);
2039 struct nand_op_instr instrs[] = {
2040 NAND_OP_CMD(NAND_CMD_STATUS,
2041 PSEC_TO_NSEC(sdr->tADL_min)),
2042 NAND_OP_8BIT_DATA_IN(1, status, 0),
2043 };
2044 struct nand_operation op = NAND_OPERATION(instrs);
2045
2046 if (!status)
2047 op.ninstrs--;
2048
2049 return nand_exec_op(chip, &op);
2050 }
2051
Boris Brezillon97d90da2017-11-30 18:01:29 +01002052 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
2053 if (status)
2054 *status = chip->read_byte(mtd);
2055
2056 return 0;
2057}
2058EXPORT_SYMBOL_GPL(nand_status_op);
2059
2060/**
2061 * nand_exit_status_op - Exit a STATUS operation
2062 * @chip: The NAND chip
2063 *
2064 * This function sends a READ0 command to cancel the effect of the STATUS
2065 * command to avoid reading only the status until a new read command is sent.
2066 *
2067 * This function does not select/unselect the CS line.
2068 *
2069 * Returns 0 on success, a negative error code otherwise.
2070 */
2071int nand_exit_status_op(struct nand_chip *chip)
2072{
2073 struct mtd_info *mtd = nand_to_mtd(chip);
2074
Miquel Raynal8878b122017-11-09 14:16:45 +01002075 if (chip->exec_op) {
2076 struct nand_op_instr instrs[] = {
2077 NAND_OP_CMD(NAND_CMD_READ0, 0),
2078 };
2079 struct nand_operation op = NAND_OPERATION(instrs);
2080
2081 return nand_exec_op(chip, &op);
2082 }
2083
Boris Brezillon97d90da2017-11-30 18:01:29 +01002084 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2085
2086 return 0;
2087}
2088EXPORT_SYMBOL_GPL(nand_exit_status_op);
2089
2090/**
2091 * nand_erase_op - Do an erase operation
2092 * @chip: The NAND chip
2093 * @eraseblock: block to erase
2094 *
2095 * This function sends an ERASE command and waits for the NAND to be ready
2096 * before returning.
2097 * This function does not select/unselect the CS line.
2098 *
2099 * Returns 0 on success, a negative error code otherwise.
2100 */
2101int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2102{
2103 struct mtd_info *mtd = nand_to_mtd(chip);
2104 unsigned int page = eraseblock <<
2105 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002106 int ret;
2107 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002108
Miquel Raynal8878b122017-11-09 14:16:45 +01002109 if (chip->exec_op) {
2110 const struct nand_sdr_timings *sdr =
2111 nand_get_sdr_timings(&chip->data_interface);
2112 u8 addrs[3] = { page, page >> 8, page >> 16 };
2113 struct nand_op_instr instrs[] = {
2114 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2115 NAND_OP_ADDR(2, addrs, 0),
2116 NAND_OP_CMD(NAND_CMD_ERASE2,
2117 PSEC_TO_MSEC(sdr->tWB_max)),
2118 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2119 };
2120 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002121
Miquel Raynal8878b122017-11-09 14:16:45 +01002122 if (chip->options & NAND_ROW_ADDR_3)
2123 instrs[1].ctx.addr.naddrs++;
2124
2125 ret = nand_exec_op(chip, &op);
2126 if (ret)
2127 return ret;
2128
2129 ret = nand_status_op(chip, &status);
2130 if (ret)
2131 return ret;
2132 } else {
2133 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2134 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2135
2136 ret = chip->waitfunc(mtd, chip);
2137 if (ret < 0)
2138 return ret;
2139
2140 status = ret;
2141 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002142
2143 if (status & NAND_STATUS_FAIL)
2144 return -EIO;
2145
2146 return 0;
2147}
2148EXPORT_SYMBOL_GPL(nand_erase_op);
2149
2150/**
2151 * nand_set_features_op - Do a SET FEATURES operation
2152 * @chip: The NAND chip
2153 * @feature: feature id
2154 * @data: 4 bytes of data
2155 *
2156 * This function sends a SET FEATURES command and waits for the NAND to be
2157 * ready before returning.
2158 * This function does not select/unselect the CS line.
2159 *
2160 * Returns 0 on success, a negative error code otherwise.
2161 */
2162static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2163 const void *data)
2164{
2165 struct mtd_info *mtd = nand_to_mtd(chip);
2166 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002167 int i, ret;
2168 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002169
Miquel Raynal8878b122017-11-09 14:16:45 +01002170 if (chip->exec_op) {
2171 const struct nand_sdr_timings *sdr =
2172 nand_get_sdr_timings(&chip->data_interface);
2173 struct nand_op_instr instrs[] = {
2174 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2175 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2176 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2177 PSEC_TO_NSEC(sdr->tWB_max)),
2178 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2179 };
2180 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002181
Miquel Raynal8878b122017-11-09 14:16:45 +01002182 ret = nand_exec_op(chip, &op);
2183 if (ret)
2184 return ret;
2185
2186 ret = nand_status_op(chip, &status);
2187 if (ret)
2188 return ret;
2189 } else {
2190 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
2191 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2192 chip->write_byte(mtd, params[i]);
2193
2194 ret = chip->waitfunc(mtd, chip);
2195 if (ret < 0)
2196 return ret;
2197
2198 status = ret;
2199 }
2200
Boris Brezillon97d90da2017-11-30 18:01:29 +01002201 if (status & NAND_STATUS_FAIL)
2202 return -EIO;
2203
2204 return 0;
2205}
2206
2207/**
2208 * nand_get_features_op - Do a GET FEATURES operation
2209 * @chip: The NAND chip
2210 * @feature: feature id
2211 * @data: 4 bytes of data
2212 *
2213 * This function sends a GET FEATURES command and waits for the NAND to be
2214 * ready before returning.
2215 * This function does not select/unselect the CS line.
2216 *
2217 * Returns 0 on success, a negative error code otherwise.
2218 */
2219static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2220 void *data)
2221{
2222 struct mtd_info *mtd = nand_to_mtd(chip);
2223 u8 *params = data;
2224 int i;
2225
Miquel Raynal8878b122017-11-09 14:16:45 +01002226 if (chip->exec_op) {
2227 const struct nand_sdr_timings *sdr =
2228 nand_get_sdr_timings(&chip->data_interface);
2229 struct nand_op_instr instrs[] = {
2230 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2231 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2232 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2233 PSEC_TO_NSEC(sdr->tRR_min)),
2234 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2235 data, 0),
2236 };
2237 struct nand_operation op = NAND_OPERATION(instrs);
2238
2239 return nand_exec_op(chip, &op);
2240 }
2241
Boris Brezillon97d90da2017-11-30 18:01:29 +01002242 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2243 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2244 params[i] = chip->read_byte(mtd);
2245
2246 return 0;
2247}
2248
2249/**
2250 * nand_reset_op - Do a reset operation
2251 * @chip: The NAND chip
2252 *
2253 * This function sends a RESET command and waits for the NAND to be ready
2254 * before returning.
2255 * This function does not select/unselect the CS line.
2256 *
2257 * Returns 0 on success, a negative error code otherwise.
2258 */
2259int nand_reset_op(struct nand_chip *chip)
2260{
2261 struct mtd_info *mtd = nand_to_mtd(chip);
2262
Miquel Raynal8878b122017-11-09 14:16:45 +01002263 if (chip->exec_op) {
2264 const struct nand_sdr_timings *sdr =
2265 nand_get_sdr_timings(&chip->data_interface);
2266 struct nand_op_instr instrs[] = {
2267 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2268 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2269 };
2270 struct nand_operation op = NAND_OPERATION(instrs);
2271
2272 return nand_exec_op(chip, &op);
2273 }
2274
Boris Brezillon97d90da2017-11-30 18:01:29 +01002275 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2276
2277 return 0;
2278}
2279EXPORT_SYMBOL_GPL(nand_reset_op);
2280
2281/**
2282 * nand_read_data_op - Read data from the NAND
2283 * @chip: The NAND chip
2284 * @buf: buffer used to store the data
2285 * @len: length of the buffer
2286 * @force_8bit: force 8-bit bus access
2287 *
2288 * This function does a raw data read on the bus. Usually used after launching
2289 * another NAND operation like nand_read_page_op().
2290 * This function does not select/unselect the CS line.
2291 *
2292 * Returns 0 on success, a negative error code otherwise.
2293 */
2294int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2295 bool force_8bit)
2296{
2297 struct mtd_info *mtd = nand_to_mtd(chip);
2298
2299 if (!len || !buf)
2300 return -EINVAL;
2301
Miquel Raynal8878b122017-11-09 14:16:45 +01002302 if (chip->exec_op) {
2303 struct nand_op_instr instrs[] = {
2304 NAND_OP_DATA_IN(len, buf, 0),
2305 };
2306 struct nand_operation op = NAND_OPERATION(instrs);
2307
2308 instrs[0].ctx.data.force_8bit = force_8bit;
2309
2310 return nand_exec_op(chip, &op);
2311 }
2312
Boris Brezillon97d90da2017-11-30 18:01:29 +01002313 if (force_8bit) {
2314 u8 *p = buf;
2315 unsigned int i;
2316
2317 for (i = 0; i < len; i++)
2318 p[i] = chip->read_byte(mtd);
2319 } else {
2320 chip->read_buf(mtd, buf, len);
2321 }
2322
2323 return 0;
2324}
2325EXPORT_SYMBOL_GPL(nand_read_data_op);
2326
2327/**
2328 * nand_write_data_op - Write data from the NAND
2329 * @chip: The NAND chip
2330 * @buf: buffer containing the data to send on the bus
2331 * @len: length of the buffer
2332 * @force_8bit: force 8-bit bus access
2333 *
2334 * This function does a raw data write on the bus. Usually used after launching
2335 * another NAND operation like nand_write_page_begin_op().
2336 * This function does not select/unselect the CS line.
2337 *
2338 * Returns 0 on success, a negative error code otherwise.
2339 */
2340int nand_write_data_op(struct nand_chip *chip, const void *buf,
2341 unsigned int len, bool force_8bit)
2342{
2343 struct mtd_info *mtd = nand_to_mtd(chip);
2344
2345 if (!len || !buf)
2346 return -EINVAL;
2347
Miquel Raynal8878b122017-11-09 14:16:45 +01002348 if (chip->exec_op) {
2349 struct nand_op_instr instrs[] = {
2350 NAND_OP_DATA_OUT(len, buf, 0),
2351 };
2352 struct nand_operation op = NAND_OPERATION(instrs);
2353
2354 instrs[0].ctx.data.force_8bit = force_8bit;
2355
2356 return nand_exec_op(chip, &op);
2357 }
2358
Boris Brezillon97d90da2017-11-30 18:01:29 +01002359 if (force_8bit) {
2360 const u8 *p = buf;
2361 unsigned int i;
2362
2363 for (i = 0; i < len; i++)
2364 chip->write_byte(mtd, p[i]);
2365 } else {
2366 chip->write_buf(mtd, buf, len);
2367 }
2368
2369 return 0;
2370}
2371EXPORT_SYMBOL_GPL(nand_write_data_op);
2372
2373/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002374 * struct nand_op_parser_ctx - Context used by the parser
2375 * @instrs: array of all the instructions that must be addressed
2376 * @ninstrs: length of the @instrs array
2377 * @subop: Sub-operation to be passed to the NAND controller
2378 *
2379 * This structure is used by the core to split NAND operations into
2380 * sub-operations that can be handled by the NAND controller.
2381 */
2382struct nand_op_parser_ctx {
2383 const struct nand_op_instr *instrs;
2384 unsigned int ninstrs;
2385 struct nand_subop subop;
2386};
2387
2388/**
2389 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2390 * @pat: the parser pattern element that matches @instr
2391 * @instr: pointer to the instruction to check
2392 * @start_offset: this is an in/out parameter. If @instr has already been
2393 * split, then @start_offset is the offset from which to start
2394 * (either an address cycle or an offset in the data buffer).
2395 * Conversely, if the function returns true (ie. instr must be
2396 * split), this parameter is updated to point to the first
2397 * data/address cycle that has not been taken care of.
2398 *
2399 * Some NAND controllers are limited and cannot send X address cycles with a
2400 * unique operation, or cannot read/write more than Y bytes at the same time.
2401 * In this case, split the instruction that does not fit in a single
2402 * controller-operation into two or more chunks.
2403 *
2404 * Returns true if the instruction must be split, false otherwise.
2405 * The @start_offset parameter is also updated to the offset at which the next
2406 * bundle of instruction must start (if an address or a data instruction).
2407 */
2408static bool
2409nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2410 const struct nand_op_instr *instr,
2411 unsigned int *start_offset)
2412{
2413 switch (pat->type) {
2414 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002415 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002416 break;
2417
2418 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002419 pat->ctx.addr.maxcycles) {
2420 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002421 return true;
2422 }
2423 break;
2424
2425 case NAND_OP_DATA_IN_INSTR:
2426 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002427 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002428 break;
2429
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002430 if (instr->ctx.data.len - *start_offset >
2431 pat->ctx.data.maxlen) {
2432 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002433 return true;
2434 }
2435 break;
2436
2437 default:
2438 break;
2439 }
2440
2441 return false;
2442}
2443
2444/**
2445 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2446 * remaining in the parser context
2447 * @pat: the pattern to test
2448 * @ctx: the parser context structure to match with the pattern @pat
2449 *
2450 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2451 * Returns true if this is the case, false ortherwise. When true is returned,
2452 * @ctx->subop is updated with the set of instructions to be passed to the
2453 * controller driver.
2454 */
2455static bool
2456nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2457 struct nand_op_parser_ctx *ctx)
2458{
2459 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2460 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2461 const struct nand_op_instr *instr = ctx->subop.instrs;
2462 unsigned int i, ninstrs;
2463
2464 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2465 /*
2466 * The pattern instruction does not match the operation
2467 * instruction. If the instruction is marked optional in the
2468 * pattern definition, we skip the pattern element and continue
2469 * to the next one. If the element is mandatory, there's no
2470 * match and we can return false directly.
2471 */
2472 if (instr->type != pat->elems[i].type) {
2473 if (!pat->elems[i].optional)
2474 return false;
2475
2476 continue;
2477 }
2478
2479 /*
2480 * Now check the pattern element constraints. If the pattern is
2481 * not able to handle the whole instruction in a single step,
2482 * we have to split it.
2483 * The last_instr_end_off value comes back updated to point to
2484 * the position where we have to split the instruction (the
2485 * start of the next subop chunk).
2486 */
2487 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2488 &instr_offset)) {
2489 ninstrs++;
2490 i++;
2491 break;
2492 }
2493
2494 instr++;
2495 ninstrs++;
2496 instr_offset = 0;
2497 }
2498
2499 /*
2500 * This can happen if all instructions of a pattern are optional.
2501 * Still, if there's not at least one instruction handled by this
2502 * pattern, this is not a match, and we should try the next one (if
2503 * any).
2504 */
2505 if (!ninstrs)
2506 return false;
2507
2508 /*
2509 * We had a match on the pattern head, but the pattern may be longer
2510 * than the instructions we're asked to execute. We need to make sure
2511 * there's no mandatory elements in the pattern tail.
2512 */
2513 for (; i < pat->nelems; i++) {
2514 if (!pat->elems[i].optional)
2515 return false;
2516 }
2517
2518 /*
2519 * We have a match: update the subop structure accordingly and return
2520 * true.
2521 */
2522 ctx->subop.ninstrs = ninstrs;
2523 ctx->subop.last_instr_end_off = instr_offset;
2524
2525 return true;
2526}
2527
2528#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2529static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2530{
2531 const struct nand_op_instr *instr;
2532 char *prefix = " ";
2533 unsigned int i;
2534
2535 pr_debug("executing subop:\n");
2536
2537 for (i = 0; i < ctx->ninstrs; i++) {
2538 instr = &ctx->instrs[i];
2539
2540 if (instr == &ctx->subop.instrs[0])
2541 prefix = " ->";
2542
2543 switch (instr->type) {
2544 case NAND_OP_CMD_INSTR:
2545 pr_debug("%sCMD [0x%02x]\n", prefix,
2546 instr->ctx.cmd.opcode);
2547 break;
2548 case NAND_OP_ADDR_INSTR:
2549 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2550 instr->ctx.addr.naddrs,
2551 instr->ctx.addr.naddrs < 64 ?
2552 instr->ctx.addr.naddrs : 64,
2553 instr->ctx.addr.addrs);
2554 break;
2555 case NAND_OP_DATA_IN_INSTR:
2556 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2557 instr->ctx.data.len,
2558 instr->ctx.data.force_8bit ?
2559 ", force 8-bit" : "");
2560 break;
2561 case NAND_OP_DATA_OUT_INSTR:
2562 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2563 instr->ctx.data.len,
2564 instr->ctx.data.force_8bit ?
2565 ", force 8-bit" : "");
2566 break;
2567 case NAND_OP_WAITRDY_INSTR:
2568 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2569 instr->ctx.waitrdy.timeout_ms);
2570 break;
2571 }
2572
2573 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2574 prefix = " ";
2575 }
2576}
2577#else
2578static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2579{
2580 /* NOP */
2581}
2582#endif
2583
2584/**
2585 * nand_op_parser_exec_op - exec_op parser
2586 * @chip: the NAND chip
2587 * @parser: patterns description provided by the controller driver
2588 * @op: the NAND operation to address
2589 * @check_only: when true, the function only checks if @op can be handled but
2590 * does not execute the operation
2591 *
2592 * Helper function designed to ease integration of NAND controller drivers that
2593 * only support a limited set of instruction sequences. The supported sequences
2594 * are described in @parser, and the framework takes care of splitting @op into
2595 * multiple sub-operations (if required) and pass them back to the ->exec()
2596 * callback of the matching pattern if @check_only is set to false.
2597 *
2598 * NAND controller drivers should call this function from their own ->exec_op()
2599 * implementation.
2600 *
2601 * Returns 0 on success, a negative error code otherwise. A failure can be
2602 * caused by an unsupported operation (none of the supported patterns is able
2603 * to handle the requested operation), or an error returned by one of the
2604 * matching pattern->exec() hook.
2605 */
2606int nand_op_parser_exec_op(struct nand_chip *chip,
2607 const struct nand_op_parser *parser,
2608 const struct nand_operation *op, bool check_only)
2609{
2610 struct nand_op_parser_ctx ctx = {
2611 .subop.instrs = op->instrs,
2612 .instrs = op->instrs,
2613 .ninstrs = op->ninstrs,
2614 };
2615 unsigned int i;
2616
2617 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2618 int ret;
2619
2620 for (i = 0; i < parser->npatterns; i++) {
2621 const struct nand_op_parser_pattern *pattern;
2622
2623 pattern = &parser->patterns[i];
2624 if (!nand_op_parser_match_pat(pattern, &ctx))
2625 continue;
2626
2627 nand_op_parser_trace(&ctx);
2628
2629 if (check_only)
2630 break;
2631
2632 ret = pattern->exec(chip, &ctx.subop);
2633 if (ret)
2634 return ret;
2635
2636 break;
2637 }
2638
2639 if (i == parser->npatterns) {
2640 pr_debug("->exec_op() parser: pattern not found!\n");
2641 return -ENOTSUPP;
2642 }
2643
2644 /*
2645 * Update the context structure by pointing to the start of the
2646 * next subop.
2647 */
2648 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2649 if (ctx.subop.last_instr_end_off)
2650 ctx.subop.instrs -= 1;
2651
2652 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2653 }
2654
2655 return 0;
2656}
2657EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2658
2659static bool nand_instr_is_data(const struct nand_op_instr *instr)
2660{
2661 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2662 instr->type == NAND_OP_DATA_OUT_INSTR);
2663}
2664
2665static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2666 unsigned int instr_idx)
2667{
2668 return subop && instr_idx < subop->ninstrs;
2669}
2670
2671static int nand_subop_get_start_off(const struct nand_subop *subop,
2672 unsigned int instr_idx)
2673{
2674 if (instr_idx)
2675 return 0;
2676
2677 return subop->first_instr_start_off;
2678}
2679
2680/**
2681 * nand_subop_get_addr_start_off - Get the start offset in an address array
2682 * @subop: The entire sub-operation
2683 * @instr_idx: Index of the instruction inside the sub-operation
2684 *
2685 * During driver development, one could be tempted to directly use the
2686 * ->addr.addrs field of address instructions. This is wrong as address
2687 * instructions might be split.
2688 *
2689 * Given an address instruction, returns the offset of the first cycle to issue.
2690 */
2691int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2692 unsigned int instr_idx)
2693{
2694 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2695 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2696 return -EINVAL;
2697
2698 return nand_subop_get_start_off(subop, instr_idx);
2699}
2700EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2701
2702/**
2703 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2704 * @subop: The entire sub-operation
2705 * @instr_idx: Index of the instruction inside the sub-operation
2706 *
2707 * During driver development, one could be tempted to directly use the
2708 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2709 * might be split.
2710 *
2711 * Given an address instruction, returns the number of address cycle to issue.
2712 */
2713int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2714 unsigned int instr_idx)
2715{
2716 int start_off, end_off;
2717
2718 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2719 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2720 return -EINVAL;
2721
2722 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2723
2724 if (instr_idx == subop->ninstrs - 1 &&
2725 subop->last_instr_end_off)
2726 end_off = subop->last_instr_end_off;
2727 else
2728 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2729
2730 return end_off - start_off;
2731}
2732EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2733
2734/**
2735 * nand_subop_get_data_start_off - Get the start offset in a data array
2736 * @subop: The entire sub-operation
2737 * @instr_idx: Index of the instruction inside the sub-operation
2738 *
2739 * During driver development, one could be tempted to directly use the
2740 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2741 * instructions might be split.
2742 *
2743 * Given a data instruction, returns the offset to start from.
2744 */
2745int nand_subop_get_data_start_off(const struct nand_subop *subop,
2746 unsigned int instr_idx)
2747{
2748 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2749 !nand_instr_is_data(&subop->instrs[instr_idx]))
2750 return -EINVAL;
2751
2752 return nand_subop_get_start_off(subop, instr_idx);
2753}
2754EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2755
2756/**
2757 * nand_subop_get_data_len - Get the number of bytes to retrieve
2758 * @subop: The entire sub-operation
2759 * @instr_idx: Index of the instruction inside the sub-operation
2760 *
2761 * During driver development, one could be tempted to directly use the
2762 * ->data->len field of a data instruction. This is wrong as data instructions
2763 * might be split.
2764 *
2765 * Returns the length of the chunk of data to send/receive.
2766 */
2767int nand_subop_get_data_len(const struct nand_subop *subop,
2768 unsigned int instr_idx)
2769{
2770 int start_off = 0, end_off;
2771
2772 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2773 !nand_instr_is_data(&subop->instrs[instr_idx]))
2774 return -EINVAL;
2775
2776 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2777
2778 if (instr_idx == subop->ninstrs - 1 &&
2779 subop->last_instr_end_off)
2780 end_off = subop->last_instr_end_off;
2781 else
2782 end_off = subop->instrs[instr_idx].ctx.data.len;
2783
2784 return end_off - start_off;
2785}
2786EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002789 * nand_reset - Reset and initialize a NAND device
2790 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002791 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002792 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002793 * Save the timings data structure, then apply SDR timings mode 0 (see
2794 * nand_reset_data_interface for details), do the reset operation, and
2795 * apply back the previous timings.
2796 *
2797 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002798 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002799int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002800{
2801 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal17fa8042017-11-30 18:01:31 +01002802 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002803 int ret;
2804
Boris Brezillon104e4422017-03-16 09:35:58 +01002805 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002806 if (ret)
2807 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002808
Boris Brezillon73f907f2016-10-24 16:46:20 +02002809 /*
2810 * The CS line has to be released before we can apply the new NAND
2811 * interface settings, hence this weird ->select_chip() dance.
2812 */
2813 chip->select_chip(mtd, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002814 ret = nand_reset_op(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02002815 chip->select_chip(mtd, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002816 if (ret)
2817 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002818
Miquel Raynal17fa8042017-11-30 18:01:31 +01002819 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002820 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002821 if (ret)
2822 return ret;
2823
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002824 return 0;
2825}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002826EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002827
2828/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002829 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2830 * @buf: buffer to test
2831 * @len: buffer length
2832 * @bitflips_threshold: maximum number of bitflips
2833 *
2834 * Check if a buffer contains only 0xff, which means the underlying region
2835 * has been erased and is ready to be programmed.
2836 * The bitflips_threshold specify the maximum number of bitflips before
2837 * considering the region is not erased.
2838 * Note: The logic of this function has been extracted from the memweight
2839 * implementation, except that nand_check_erased_buf function exit before
2840 * testing the whole buffer if the number of bitflips exceed the
2841 * bitflips_threshold value.
2842 *
2843 * Returns a positive number of bitflips less than or equal to
2844 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2845 * threshold.
2846 */
2847static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2848{
2849 const unsigned char *bitmap = buf;
2850 int bitflips = 0;
2851 int weight;
2852
2853 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2854 len--, bitmap++) {
2855 weight = hweight8(*bitmap);
2856 bitflips += BITS_PER_BYTE - weight;
2857 if (unlikely(bitflips > bitflips_threshold))
2858 return -EBADMSG;
2859 }
2860
2861 for (; len >= sizeof(long);
2862 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002863 unsigned long d = *((unsigned long *)bitmap);
2864 if (d == ~0UL)
2865 continue;
2866 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002867 bitflips += BITS_PER_LONG - weight;
2868 if (unlikely(bitflips > bitflips_threshold))
2869 return -EBADMSG;
2870 }
2871
2872 for (; len > 0; len--, bitmap++) {
2873 weight = hweight8(*bitmap);
2874 bitflips += BITS_PER_BYTE - weight;
2875 if (unlikely(bitflips > bitflips_threshold))
2876 return -EBADMSG;
2877 }
2878
2879 return bitflips;
2880}
2881
2882/**
2883 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2884 * 0xff data
2885 * @data: data buffer to test
2886 * @datalen: data length
2887 * @ecc: ECC buffer
2888 * @ecclen: ECC length
2889 * @extraoob: extra OOB buffer
2890 * @extraooblen: extra OOB length
2891 * @bitflips_threshold: maximum number of bitflips
2892 *
2893 * Check if a data buffer and its associated ECC and OOB data contains only
2894 * 0xff pattern, which means the underlying region has been erased and is
2895 * ready to be programmed.
2896 * The bitflips_threshold specify the maximum number of bitflips before
2897 * considering the region as not erased.
2898 *
2899 * Note:
2900 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2901 * different from the NAND page size. When fixing bitflips, ECC engines will
2902 * report the number of errors per chunk, and the NAND core infrastructure
2903 * expect you to return the maximum number of bitflips for the whole page.
2904 * This is why you should always use this function on a single chunk and
2905 * not on the whole page. After checking each chunk you should update your
2906 * max_bitflips value accordingly.
2907 * 2/ When checking for bitflips in erased pages you should not only check
2908 * the payload data but also their associated ECC data, because a user might
2909 * have programmed almost all bits to 1 but a few. In this case, we
2910 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2911 * this case.
2912 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2913 * data are protected by the ECC engine.
2914 * It could also be used if you support subpages and want to attach some
2915 * extra OOB data to an ECC chunk.
2916 *
2917 * Returns a positive number of bitflips less than or equal to
2918 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2919 * threshold. In case of success, the passed buffers are filled with 0xff.
2920 */
2921int nand_check_erased_ecc_chunk(void *data, int datalen,
2922 void *ecc, int ecclen,
2923 void *extraoob, int extraooblen,
2924 int bitflips_threshold)
2925{
2926 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2927
2928 data_bitflips = nand_check_erased_buf(data, datalen,
2929 bitflips_threshold);
2930 if (data_bitflips < 0)
2931 return data_bitflips;
2932
2933 bitflips_threshold -= data_bitflips;
2934
2935 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2936 if (ecc_bitflips < 0)
2937 return ecc_bitflips;
2938
2939 bitflips_threshold -= ecc_bitflips;
2940
2941 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2942 bitflips_threshold);
2943 if (extraoob_bitflips < 0)
2944 return extraoob_bitflips;
2945
2946 if (data_bitflips)
2947 memset(data, 0xff, datalen);
2948
2949 if (ecc_bitflips)
2950 memset(ecc, 0xff, ecclen);
2951
2952 if (extraoob_bitflips)
2953 memset(extraoob, 0xff, extraooblen);
2954
2955 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2956}
2957EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2958
2959/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002960 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002961 * @mtd: mtd info structure
2962 * @chip: nand chip info structure
2963 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002964 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002965 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002966 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002967 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002968 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002969int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2970 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002971{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002972 int ret;
2973
Boris Brezillon25f815f2017-11-30 18:01:30 +01002974 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002975 if (ret)
2976 return ret;
2977
2978 if (oob_required) {
2979 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2980 false);
2981 if (ret)
2982 return ret;
2983 }
2984
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002985 return 0;
2986}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002987EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002988
2989/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002990 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002991 * @mtd: mtd info structure
2992 * @chip: nand chip info structure
2993 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002994 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002995 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002996 *
2997 * We need a special oob layout and handling even when OOB isn't used.
2998 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002999static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003000 struct nand_chip *chip, uint8_t *buf,
3001 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003002{
3003 int eccsize = chip->ecc.size;
3004 int eccbytes = chip->ecc.bytes;
3005 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003006 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003007
Boris Brezillon25f815f2017-11-30 18:01:30 +01003008 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3009 if (ret)
3010 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003011
3012 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003013 ret = nand_read_data_op(chip, buf, eccsize, false);
3014 if (ret)
3015 return ret;
3016
David Brownell52ff49d2009-03-04 12:01:36 -08003017 buf += eccsize;
3018
3019 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003020 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3021 false);
3022 if (ret)
3023 return ret;
3024
David Brownell52ff49d2009-03-04 12:01:36 -08003025 oob += chip->ecc.prepad;
3026 }
3027
Boris Brezillon97d90da2017-11-30 18:01:29 +01003028 ret = nand_read_data_op(chip, oob, eccbytes, false);
3029 if (ret)
3030 return ret;
3031
David Brownell52ff49d2009-03-04 12:01:36 -08003032 oob += eccbytes;
3033
3034 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003035 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3036 false);
3037 if (ret)
3038 return ret;
3039
David Brownell52ff49d2009-03-04 12:01:36 -08003040 oob += chip->ecc.postpad;
3041 }
3042 }
3043
3044 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003045 if (size) {
3046 ret = nand_read_data_op(chip, oob, size, false);
3047 if (ret)
3048 return ret;
3049 }
David Brownell52ff49d2009-03-04 12:01:36 -08003050
3051 return 0;
3052}
3053
3054/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003055 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003056 * @mtd: mtd info structure
3057 * @chip: nand chip info structure
3058 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003059 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003060 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003061 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003062static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003063 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Boris Brezillon846031d2016-02-03 20:11:00 +01003065 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003066 int eccbytes = chip->ecc.bytes;
3067 int eccsteps = chip->ecc.steps;
3068 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003069 uint8_t *ecc_calc = chip->ecc.calc_buf;
3070 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003071 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003072
Brian Norris1fbb9382012-05-02 10:14:55 -07003073 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003074
3075 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3076 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3077
Boris Brezillon846031d2016-02-03 20:11:00 +01003078 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3079 chip->ecc.total);
3080 if (ret)
3081 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003082
3083 eccsteps = chip->ecc.steps;
3084 p = buf;
3085
3086 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3087 int stat;
3088
3089 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003090 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003091 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003092 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003093 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003094 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3095 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003096 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003097 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303101 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003102 * @mtd: mtd info structure
3103 * @chip: nand chip info structure
3104 * @data_offs: offset of requested data within the page
3105 * @readlen: data length
3106 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003107 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003108 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003109static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003110 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3111 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003112{
Boris Brezillon846031d2016-02-03 20:11:00 +01003113 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003114 uint8_t *p;
3115 int data_col_addr, i, gaps = 0;
3116 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3117 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003118 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003119 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003120 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003121
Brian Norris7854d3f2011-06-23 14:12:08 -07003122 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003123 start_step = data_offs / chip->ecc.size;
3124 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3125 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303126 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003127
Brian Norris8b6e50c2011-05-25 14:59:01 -07003128 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003129 datafrag_len = num_steps * chip->ecc.size;
3130 eccfrag_len = num_steps * chip->ecc.bytes;
3131
3132 data_col_addr = start_step * chip->ecc.size;
3133 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003134 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003135 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003136 if (ret)
3137 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003138
Brian Norris8b6e50c2011-05-25 14:59:01 -07003139 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003140 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003141 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003142
Brian Norris8b6e50c2011-05-25 14:59:01 -07003143 /*
3144 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003145 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003146 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003147 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3148 if (ret)
3149 return ret;
3150
3151 if (oobregion.length < eccfrag_len)
3152 gaps = 1;
3153
Alexey Korolev3d459552008-05-15 17:23:18 +01003154 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003155 ret = nand_change_read_column_op(chip, mtd->writesize,
3156 chip->oob_poi, mtd->oobsize,
3157 false);
3158 if (ret)
3159 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003160 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003161 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003162 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003163 * about buswidth alignment in read_buf.
3164 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003165 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003166 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003167 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003168 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003169 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3170 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003171 aligned_len++;
3172
Boris Brezillon97d90da2017-11-30 18:01:29 +01003173 ret = nand_change_read_column_op(chip,
3174 mtd->writesize + aligned_pos,
3175 &chip->oob_poi[aligned_pos],
3176 aligned_len, false);
3177 if (ret)
3178 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003179 }
3180
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003181 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003182 chip->oob_poi, index, eccfrag_len);
3183 if (ret)
3184 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003185
3186 p = bufpoi + data_col_addr;
3187 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3188 int stat;
3189
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003190 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3191 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003192 if (stat == -EBADMSG &&
3193 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3194 /* check for empty pages with bitflips */
3195 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003196 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003197 chip->ecc.bytes,
3198 NULL, 0,
3199 chip->ecc.strength);
3200 }
3201
Mike Dunn3f91e942012-04-25 12:06:09 -07003202 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003203 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003204 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003205 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003206 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3207 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003208 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003209 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003210}
3211
3212/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003213 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003214 * @mtd: mtd info structure
3215 * @chip: nand chip info structure
3216 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003217 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003218 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003219 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003220 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003221 */
3222static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003223 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003224{
Boris Brezillon846031d2016-02-03 20:11:00 +01003225 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003226 int eccbytes = chip->ecc.bytes;
3227 int eccsteps = chip->ecc.steps;
3228 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003229 uint8_t *ecc_calc = chip->ecc.calc_buf;
3230 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003231 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003232
Boris Brezillon25f815f2017-11-30 18:01:30 +01003233 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3234 if (ret)
3235 return ret;
3236
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003237 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3238 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003239
3240 ret = nand_read_data_op(chip, p, eccsize, false);
3241 if (ret)
3242 return ret;
3243
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003244 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3245 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003246
3247 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3248 if (ret)
3249 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003250
Boris Brezillon846031d2016-02-03 20:11:00 +01003251 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3252 chip->ecc.total);
3253 if (ret)
3254 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003255
3256 eccsteps = chip->ecc.steps;
3257 p = buf;
3258
3259 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3260 int stat;
3261
3262 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003263 if (stat == -EBADMSG &&
3264 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3265 /* check for empty pages with bitflips */
3266 stat = nand_check_erased_ecc_chunk(p, eccsize,
3267 &ecc_code[i], eccbytes,
3268 NULL, 0,
3269 chip->ecc.strength);
3270 }
3271
Mike Dunn3f91e942012-04-25 12:06:09 -07003272 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003273 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003274 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003275 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003276 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3277 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003278 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003279 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003280}
3281
3282/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003283 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003284 * @mtd: mtd info structure
3285 * @chip: nand chip info structure
3286 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003287 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003288 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003289 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003290 * Hardware ECC for large page chips, require OOB to be read first. For this
3291 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3292 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3293 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3294 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003295 */
3296static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003297 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003298{
Boris Brezillon846031d2016-02-03 20:11:00 +01003299 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003300 int eccbytes = chip->ecc.bytes;
3301 int eccsteps = chip->ecc.steps;
3302 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003303 uint8_t *ecc_code = chip->ecc.code_buf;
3304 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003305 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003306
3307 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003308 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3309 if (ret)
3310 return ret;
3311
3312 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3313 if (ret)
3314 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003315
Boris Brezillon846031d2016-02-03 20:11:00 +01003316 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3317 chip->ecc.total);
3318 if (ret)
3319 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003320
3321 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3322 int stat;
3323
3324 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003325
3326 ret = nand_read_data_op(chip, p, eccsize, false);
3327 if (ret)
3328 return ret;
3329
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003330 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3331
3332 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003333 if (stat == -EBADMSG &&
3334 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3335 /* check for empty pages with bitflips */
3336 stat = nand_check_erased_ecc_chunk(p, eccsize,
3337 &ecc_code[i], eccbytes,
3338 NULL, 0,
3339 chip->ecc.strength);
3340 }
3341
Mike Dunn3f91e942012-04-25 12:06:09 -07003342 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003343 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003344 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003345 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003346 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3347 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003348 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003349 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003350}
3351
3352/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003353 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003354 * @mtd: mtd info structure
3355 * @chip: nand chip info structure
3356 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003357 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003358 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003359 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003360 * The hw generator calculates the error syndrome automatically. Therefore we
3361 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003362 */
3363static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003364 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003365{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003366 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003367 int eccbytes = chip->ecc.bytes;
3368 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003369 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003370 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003371 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003372 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003373
Boris Brezillon25f815f2017-11-30 18:01:30 +01003374 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3375 if (ret)
3376 return ret;
3377
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003378 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3379 int stat;
3380
3381 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003382
3383 ret = nand_read_data_op(chip, p, eccsize, false);
3384 if (ret)
3385 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003386
3387 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003388 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3389 false);
3390 if (ret)
3391 return ret;
3392
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003393 oob += chip->ecc.prepad;
3394 }
3395
3396 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003397
3398 ret = nand_read_data_op(chip, oob, eccbytes, false);
3399 if (ret)
3400 return ret;
3401
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003402 stat = chip->ecc.correct(mtd, p, oob, NULL);
3403
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003404 oob += eccbytes;
3405
3406 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003407 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3408 false);
3409 if (ret)
3410 return ret;
3411
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003412 oob += chip->ecc.postpad;
3413 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003414
3415 if (stat == -EBADMSG &&
3416 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3417 /* check for empty pages with bitflips */
3418 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3419 oob - eccpadbytes,
3420 eccpadbytes,
3421 NULL, 0,
3422 chip->ecc.strength);
3423 }
3424
3425 if (stat < 0) {
3426 mtd->ecc_stats.failed++;
3427 } else {
3428 mtd->ecc_stats.corrected += stat;
3429 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3430 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003431 }
3432
3433 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003434 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003435 if (i) {
3436 ret = nand_read_data_op(chip, oob, i, false);
3437 if (ret)
3438 return ret;
3439 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003440
Mike Dunn3f91e942012-04-25 12:06:09 -07003441 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003442}
3443
3444/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003445 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003446 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003447 * @oob: oob destination address
3448 * @ops: oob ops structure
3449 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003450 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003451static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003452 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003453{
Boris Brezillon846031d2016-02-03 20:11:00 +01003454 struct nand_chip *chip = mtd_to_nand(mtd);
3455 int ret;
3456
Florian Fainellif8ac0412010-09-07 13:23:43 +02003457 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003458
Brian Norris0612b9d2011-08-30 18:45:40 -07003459 case MTD_OPS_PLACE_OOB:
3460 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003461 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3462 return oob + len;
3463
Boris Brezillon846031d2016-02-03 20:11:00 +01003464 case MTD_OPS_AUTO_OOB:
3465 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3466 ops->ooboffs, len);
3467 BUG_ON(ret);
3468 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003469
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003470 default:
3471 BUG();
3472 }
3473 return NULL;
3474}
3475
3476/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003477 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3478 * @mtd: MTD device structure
3479 * @retry_mode: the retry mode to use
3480 *
3481 * Some vendors supply a special command to shift the Vt threshold, to be used
3482 * when there are too many bitflips in a page (i.e., ECC error). After setting
3483 * a new threshold, the host should retry reading the page.
3484 */
3485static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3486{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003487 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08003488
3489 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3490
3491 if (retry_mode >= chip->read_retries)
3492 return -EINVAL;
3493
3494 if (!chip->setup_read_retry)
3495 return -EOPNOTSUPP;
3496
3497 return chip->setup_read_retry(mtd, retry_mode);
3498}
3499
3500/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003501 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003502 * @mtd: MTD device structure
3503 * @from: offset to read from
3504 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003505 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003506 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003507 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003508static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3509 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003510{
Brian Norrise47f3db2012-05-02 10:14:56 -07003511 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003512 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003513 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003514 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003515 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003516 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003517
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003518 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003519 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003520 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003521 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003522 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003524 chipnr = (int)(from >> chip->chip_shift);
3525 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003527 realpage = (int)(from >> chip->page_shift);
3528 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003530 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003532 buf = ops->datbuf;
3533 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003534 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003535
Florian Fainellif8ac0412010-09-07 13:23:43 +02003536 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003537 unsigned int ecc_failures = mtd->ecc_stats.failed;
3538
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003539 bytes = min(mtd->writesize - col, readlen);
3540 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003541
Kamal Dasu66507c72014-05-01 20:51:19 -04003542 if (!aligned)
3543 use_bufpoi = 1;
3544 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003545 use_bufpoi = !virt_addr_valid(buf) ||
3546 !IS_ALIGNED((unsigned long)buf,
3547 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003548 else
3549 use_bufpoi = 0;
3550
Brian Norris8b6e50c2011-05-25 14:59:01 -07003551 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003552 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003553 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003554
3555 if (use_bufpoi && aligned)
3556 pr_debug("%s: using read bounce buffer for buf@%p\n",
3557 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Brian Norrisba84fb52014-01-03 15:13:33 -08003559read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003560 /*
3561 * Now read the page into the buffer. Absent an error,
3562 * the read methods return max bitflips per ecc step.
3563 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003564 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07003565 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003566 oob_required,
3567 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003568 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3569 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003570 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003571 col, bytes, bufpoi,
3572 page);
David Woodhouse956e9442006-09-25 17:12:39 +01003573 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07003574 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003575 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003576 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003577 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003578 /* Invalidate page cache */
3579 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003580 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003581 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003582
3583 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003584 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003585 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003586 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003587 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003588 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003589 chip->pagebuf_bitflips = ret;
3590 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003591 /* Invalidate page cache */
3592 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003593 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003594 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003596
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003597 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003598 int toread = min(oobreadlen, max_oobsize);
3599
3600 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003601 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003602 oob, ops, toread);
3603 oobreadlen -= toread;
3604 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003605 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003606
3607 if (chip->options & NAND_NEED_READRDY) {
3608 /* Apply delay or wait for ready/busy pin */
3609 if (!chip->dev_ready)
3610 udelay(chip->chip_delay);
3611 else
3612 nand_wait_ready(mtd);
3613 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08003614
Brian Norrisba84fb52014-01-03 15:13:33 -08003615 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003616 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003617 retry_mode++;
3618 ret = nand_setup_read_retry(mtd,
3619 retry_mode);
3620 if (ret < 0)
3621 break;
3622
3623 /* Reset failures; retry */
3624 mtd->ecc_stats.failed = ecc_failures;
3625 goto read_retry;
3626 } else {
3627 /* No more retry modes; real failure */
3628 ecc_fail = true;
3629 }
3630 }
3631
3632 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003633 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003634 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003635 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003636 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003637 max_bitflips = max_t(unsigned int, max_bitflips,
3638 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003641 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003642
Brian Norrisba84fb52014-01-03 15:13:33 -08003643 /* Reset to retry mode 0 */
3644 if (retry_mode) {
3645 ret = nand_setup_read_retry(mtd, 0);
3646 if (ret < 0)
3647 break;
3648 retry_mode = 0;
3649 }
3650
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003651 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Brian Norris8b6e50c2011-05-25 14:59:01 -07003654 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 col = 0;
3656 /* Increment page address */
3657 realpage++;
3658
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003659 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 /* Check, if we cross a chip boundary */
3661 if (!page) {
3662 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003663 chip->select_chip(mtd, -1);
3664 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003667 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003669 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003670 if (oob)
3671 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Mike Dunn3f91e942012-04-25 12:06:09 -07003673 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003674 return ret;
3675
Brian Norrisb72f3df2013-12-03 11:04:14 -08003676 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003677 return -EBADMSG;
3678
Mike Dunnedbc45402012-04-25 12:06:11 -07003679 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003680}
3681
3682/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003683 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003684 * @mtd: mtd info structure
3685 * @chip: nand chip info structure
3686 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003687 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003688int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003689{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003690 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003691}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003692EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003693
3694/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003695 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003696 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003697 * @mtd: mtd info structure
3698 * @chip: nand chip info structure
3699 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003700 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003701int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3702 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003703{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003704 int length = mtd->oobsize;
3705 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3706 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003707 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003708 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003709
Boris Brezillon97d90da2017-11-30 18:01:29 +01003710 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3711 if (ret)
3712 return ret;
3713
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003714 for (i = 0; i < chip->ecc.steps; i++) {
3715 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003716 int ret;
3717
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003718 pos = eccsize + i * (eccsize + chunk);
3719 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003720 ret = nand_change_read_column_op(chip, pos,
3721 NULL, 0,
3722 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003723 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003724 ret = nand_read_page_op(chip, page, pos, NULL,
3725 0);
3726
3727 if (ret)
3728 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003729 } else
3730 sndrnd = 1;
3731 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003732
3733 ret = nand_read_data_op(chip, bufpoi, toread, false);
3734 if (ret)
3735 return ret;
3736
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003737 bufpoi += toread;
3738 length -= toread;
3739 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003740 if (length > 0) {
3741 ret = nand_read_data_op(chip, bufpoi, length, false);
3742 if (ret)
3743 return ret;
3744 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003745
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003746 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003747}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003748EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003749
3750/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003751 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003752 * @mtd: mtd info structure
3753 * @chip: nand chip info structure
3754 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003755 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003756int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003757{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003758 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3759 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003760}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003761EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003762
3763/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003764 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003765 * with syndrome - only for large page flash
3766 * @mtd: mtd info structure
3767 * @chip: nand chip info structure
3768 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003769 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003770int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3771 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003772{
3773 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3774 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003775 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003776 const uint8_t *bufpoi = chip->oob_poi;
3777
3778 /*
3779 * data-ecc-data-ecc ... ecc-oob
3780 * or
3781 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3782 */
3783 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3784 pos = steps * (eccsize + chunk);
3785 steps = 0;
3786 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003787 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003788
Boris Brezillon97d90da2017-11-30 18:01:29 +01003789 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3790 if (ret)
3791 return ret;
3792
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003793 for (i = 0; i < steps; i++) {
3794 if (sndcmd) {
3795 if (mtd->writesize <= 512) {
3796 uint32_t fill = 0xFFFFFFFF;
3797
3798 len = eccsize;
3799 while (len > 0) {
3800 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003801
3802 ret = nand_write_data_op(chip, &fill,
3803 num, false);
3804 if (ret)
3805 return ret;
3806
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003807 len -= num;
3808 }
3809 } else {
3810 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003811 ret = nand_change_write_column_op(chip, pos,
3812 NULL, 0,
3813 false);
3814 if (ret)
3815 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003816 }
3817 } else
3818 sndcmd = 1;
3819 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003820
3821 ret = nand_write_data_op(chip, bufpoi, len, false);
3822 if (ret)
3823 return ret;
3824
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003825 bufpoi += len;
3826 length -= len;
3827 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003828 if (length > 0) {
3829 ret = nand_write_data_op(chip, bufpoi, length, false);
3830 if (ret)
3831 return ret;
3832 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003833
Boris Brezillon97d90da2017-11-30 18:01:29 +01003834 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003835}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003836EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003837
3838/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003839 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003840 * @mtd: MTD device structure
3841 * @from: offset to read from
3842 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003844 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003846static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3847 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003849 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003850 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003851 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003852 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003853 int readlen = ops->ooblen;
3854 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003855 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003856 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
Brian Norris289c0522011-07-19 10:06:09 -07003858 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303859 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
Brian Norris041e4572011-06-23 16:45:24 -07003861 stats = mtd->ecc_stats;
3862
Boris BREZILLON29f10582016-03-07 10:46:52 +01003863 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003864
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003865 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003866 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003868 /* Shift to get page */
3869 realpage = (int)(from >> chip->page_shift);
3870 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
Florian Fainellif8ac0412010-09-07 13:23:43 +02003872 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003873 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003874 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003875 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003876 ret = chip->ecc.read_oob(mtd, chip, page);
3877
3878 if (ret < 0)
3879 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003880
3881 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003882 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003883
Brian Norris5bc7c332013-03-13 09:51:31 -07003884 if (chip->options & NAND_NEED_READRDY) {
3885 /* Apply delay or wait for ready/busy pin */
3886 if (!chip->dev_ready)
3887 udelay(chip->chip_delay);
3888 else
3889 nand_wait_ready(mtd);
3890 }
3891
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003892 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3893
Vitaly Wool70145682006-11-03 18:20:38 +03003894 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003895 if (!readlen)
3896 break;
3897
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003898 /* Increment page address */
3899 realpage++;
3900
3901 page = realpage & chip->pagemask;
3902 /* Check, if we cross a chip boundary */
3903 if (!page) {
3904 chipnr++;
3905 chip->select_chip(mtd, -1);
3906 chip->select_chip(mtd, chipnr);
3907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003909 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003911 ops->oobretlen = ops->ooblen - readlen;
3912
3913 if (ret < 0)
3914 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003915
3916 if (mtd->ecc_stats.failed - stats.failed)
3917 return -EBADMSG;
3918
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003919 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920}
3921
3922/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003923 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003924 * @mtd: MTD device structure
3925 * @from: offset to read from
3926 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003928 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003930static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3931 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003933 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003934
3935 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003937 if (ops->mode != MTD_OPS_PLACE_OOB &&
3938 ops->mode != MTD_OPS_AUTO_OOB &&
3939 ops->mode != MTD_OPS_RAW)
3940 return -ENOTSUPP;
3941
Huang Shijie6a8214a2012-11-19 14:43:30 +08003942 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003944 if (!ops->datbuf)
3945 ret = nand_do_read_oob(mtd, from, ops);
3946 else
3947 ret = nand_do_read_ops(mtd, from, ops);
3948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003950 return ret;
3951}
3952
3953
3954/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003955 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003956 * @mtd: mtd info structure
3957 * @chip: nand chip info structure
3958 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003959 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003960 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003961 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003962 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003963 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003964int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
3965 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003966{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003967 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003968
Boris Brezillon25f815f2017-11-30 18:01:30 +01003969 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003970 if (ret)
3971 return ret;
3972
3973 if (oob_required) {
3974 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3975 false);
3976 if (ret)
3977 return ret;
3978 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003979
Boris Brezillon25f815f2017-11-30 18:01:30 +01003980 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003982EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003984/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003985 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003986 * @mtd: mtd info structure
3987 * @chip: nand chip info structure
3988 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003989 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003990 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003991 *
3992 * We need a special oob layout and handling even when ECC isn't checked.
3993 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003994static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003995 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003996 const uint8_t *buf, int oob_required,
3997 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003998{
3999 int eccsize = chip->ecc.size;
4000 int eccbytes = chip->ecc.bytes;
4001 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004002 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004003
Boris Brezillon25f815f2017-11-30 18:01:30 +01004004 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4005 if (ret)
4006 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004007
4008 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004009 ret = nand_write_data_op(chip, buf, eccsize, false);
4010 if (ret)
4011 return ret;
4012
David Brownell52ff49d2009-03-04 12:01:36 -08004013 buf += eccsize;
4014
4015 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004016 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4017 false);
4018 if (ret)
4019 return ret;
4020
David Brownell52ff49d2009-03-04 12:01:36 -08004021 oob += chip->ecc.prepad;
4022 }
4023
Boris Brezillon97d90da2017-11-30 18:01:29 +01004024 ret = nand_write_data_op(chip, oob, eccbytes, false);
4025 if (ret)
4026 return ret;
4027
David Brownell52ff49d2009-03-04 12:01:36 -08004028 oob += eccbytes;
4029
4030 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004031 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4032 false);
4033 if (ret)
4034 return ret;
4035
David Brownell52ff49d2009-03-04 12:01:36 -08004036 oob += chip->ecc.postpad;
4037 }
4038 }
4039
4040 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004041 if (size) {
4042 ret = nand_write_data_op(chip, oob, size, false);
4043 if (ret)
4044 return ret;
4045 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004046
Boris Brezillon25f815f2017-11-30 18:01:30 +01004047 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004048}
4049/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004050 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004051 * @mtd: mtd info structure
4052 * @chip: nand chip info structure
4053 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004054 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004055 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004056 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004057static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004058 const uint8_t *buf, int oob_required,
4059 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004060{
Boris Brezillon846031d2016-02-03 20:11:00 +01004061 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004062 int eccbytes = chip->ecc.bytes;
4063 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004064 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004065 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004066
Brian Norris7854d3f2011-06-23 14:12:08 -07004067 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004068 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
4069 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004070
Boris Brezillon846031d2016-02-03 20:11:00 +01004071 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4072 chip->ecc.total);
4073 if (ret)
4074 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004075
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004076 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004077}
4078
4079/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004080 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004081 * @mtd: mtd info structure
4082 * @chip: nand chip info structure
4083 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004084 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004085 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004086 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004087static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004088 const uint8_t *buf, int oob_required,
4089 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004090{
Boris Brezillon846031d2016-02-03 20:11:00 +01004091 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004092 int eccbytes = chip->ecc.bytes;
4093 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004094 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004095 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004096
Boris Brezillon25f815f2017-11-30 18:01:30 +01004097 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4098 if (ret)
4099 return ret;
4100
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004101 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4102 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004103
4104 ret = nand_write_data_op(chip, p, eccsize, false);
4105 if (ret)
4106 return ret;
4107
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004108 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
4109 }
4110
Boris Brezillon846031d2016-02-03 20:11:00 +01004111 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4112 chip->ecc.total);
4113 if (ret)
4114 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004115
Boris Brezillon97d90da2017-11-30 18:01:29 +01004116 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4117 if (ret)
4118 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004119
Boris Brezillon25f815f2017-11-30 18:01:30 +01004120 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004121}
4122
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304123
4124/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004125 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304126 * @mtd: mtd info structure
4127 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004128 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304129 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004130 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304131 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004132 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304133 */
4134static int nand_write_subpage_hwecc(struct mtd_info *mtd,
4135 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07004136 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004137 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304138{
4139 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004140 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304141 int ecc_size = chip->ecc.size;
4142 int ecc_bytes = chip->ecc.bytes;
4143 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304144 uint32_t start_step = offset / ecc_size;
4145 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4146 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004147 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304148
Boris Brezillon25f815f2017-11-30 18:01:30 +01004149 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4150 if (ret)
4151 return ret;
4152
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304153 for (step = 0; step < ecc_steps; step++) {
4154 /* configure controller for WRITE access */
4155 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
4156
4157 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004158 ret = nand_write_data_op(chip, buf, ecc_size, false);
4159 if (ret)
4160 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304161
4162 /* mask ECC of un-touched subpages by padding 0xFF */
4163 if ((step < start_step) || (step > end_step))
4164 memset(ecc_calc, 0xff, ecc_bytes);
4165 else
Brian Norrisd6a950802013-08-08 17:16:36 -07004166 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304167
4168 /* mask OOB of un-touched subpages by padding 0xFF */
4169 /* if oob_required, preserve OOB metadata of written subpage */
4170 if (!oob_required || (step < start_step) || (step > end_step))
4171 memset(oob_buf, 0xff, oob_bytes);
4172
Brian Norrisd6a950802013-08-08 17:16:36 -07004173 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304174 ecc_calc += ecc_bytes;
4175 oob_buf += oob_bytes;
4176 }
4177
4178 /* copy calculated ECC for whole page to chip->buffer->oob */
4179 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004180 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004181 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4182 chip->ecc.total);
4183 if (ret)
4184 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304185
4186 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004187 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4188 if (ret)
4189 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304190
Boris Brezillon25f815f2017-11-30 18:01:30 +01004191 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304192}
4193
4194
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004195/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004196 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004197 * @mtd: mtd info structure
4198 * @chip: nand chip info structure
4199 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004200 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004201 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004202 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004203 * The hw generator calculates the error syndrome automatically. Therefore we
4204 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004205 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004206static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07004207 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004208 const uint8_t *buf, int oob_required,
4209 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004210{
4211 int i, eccsize = chip->ecc.size;
4212 int eccbytes = chip->ecc.bytes;
4213 int eccsteps = chip->ecc.steps;
4214 const uint8_t *p = buf;
4215 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004216 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004217
Boris Brezillon25f815f2017-11-30 18:01:30 +01004218 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4219 if (ret)
4220 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004221
4222 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004223 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004224
4225 ret = nand_write_data_op(chip, p, eccsize, false);
4226 if (ret)
4227 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004228
4229 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004230 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4231 false);
4232 if (ret)
4233 return ret;
4234
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004235 oob += chip->ecc.prepad;
4236 }
4237
4238 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004239
4240 ret = nand_write_data_op(chip, oob, eccbytes, false);
4241 if (ret)
4242 return ret;
4243
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004244 oob += eccbytes;
4245
4246 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004247 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4248 false);
4249 if (ret)
4250 return ret;
4251
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004252 oob += chip->ecc.postpad;
4253 }
4254 }
4255
4256 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004257 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004258 if (i) {
4259 ret = nand_write_data_op(chip, oob, i, false);
4260 if (ret)
4261 return ret;
4262 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004263
Boris Brezillon25f815f2017-11-30 18:01:30 +01004264 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004265}
4266
4267/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004268 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004269 * @mtd: MTD device structure
4270 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304271 * @offset: address offset within the page
4272 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004273 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004274 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004275 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004276 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004277 */
4278static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304279 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004280 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004281{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304282 int status, subpage;
4283
4284 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4285 chip->ecc.write_subpage)
4286 subpage = offset || (data_len < mtd->writesize);
4287 else
4288 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004289
David Woodhouse956e9442006-09-25 17:12:39 +01004290 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304291 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004292 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304293 else if (subpage)
4294 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004295 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004296 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004297 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
4298 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004299
4300 if (status < 0)
4301 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004302
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004303 return 0;
4304}
4305
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004306/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004307 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004308 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004309 * @oob: oob data buffer
4310 * @len: oob data write length
4311 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004312 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004313static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4314 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004315{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004316 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004317 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004318
4319 /*
4320 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4321 * data from a previous OOB read.
4322 */
4323 memset(chip->oob_poi, 0xff, mtd->oobsize);
4324
Florian Fainellif8ac0412010-09-07 13:23:43 +02004325 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004326
Brian Norris0612b9d2011-08-30 18:45:40 -07004327 case MTD_OPS_PLACE_OOB:
4328 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004329 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4330 return oob + len;
4331
Boris Brezillon846031d2016-02-03 20:11:00 +01004332 case MTD_OPS_AUTO_OOB:
4333 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4334 ops->ooboffs, len);
4335 BUG_ON(ret);
4336 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004337
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004338 default:
4339 BUG();
4340 }
4341 return NULL;
4342}
4343
Florian Fainellif8ac0412010-09-07 13:23:43 +02004344#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004345
4346/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004347 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004348 * @mtd: MTD device structure
4349 * @to: offset to write to
4350 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004351 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004352 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004353 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004354static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4355 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004356{
Corentin Labbe73600b62017-09-02 10:49:38 +02004357 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004358 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004359 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004360
4361 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004362 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004363
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004364 uint8_t *oob = ops->oobbuf;
4365 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304366 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004367 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004368
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004369 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004370 if (!writelen)
4371 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004372
Brian Norris8b6e50c2011-05-25 14:59:01 -07004373 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004374 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004375 pr_notice("%s: attempt to write non page aligned data\n",
4376 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004377 return -EINVAL;
4378 }
4379
Thomas Gleixner29072b92006-09-28 15:38:36 +02004380 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004381
Thomas Gleixner6a930962006-06-28 00:11:45 +02004382 chipnr = (int)(to >> chip->chip_shift);
4383 chip->select_chip(mtd, chipnr);
4384
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004385 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004386 if (nand_check_wp(mtd)) {
4387 ret = -EIO;
4388 goto err_out;
4389 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004390
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004391 realpage = (int)(to >> chip->page_shift);
4392 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004393
4394 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004395 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4396 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004397 chip->pagebuf = -1;
4398
Maxim Levitsky782ce792010-02-22 20:39:36 +02004399 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004400 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4401 ret = -EINVAL;
4402 goto err_out;
4403 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004404
Florian Fainellif8ac0412010-09-07 13:23:43 +02004405 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004406 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004407 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004408 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004409 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004410
Kamal Dasu66507c72014-05-01 20:51:19 -04004411 if (part_pagewr)
4412 use_bufpoi = 1;
4413 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004414 use_bufpoi = !virt_addr_valid(buf) ||
4415 !IS_ALIGNED((unsigned long)buf,
4416 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004417 else
4418 use_bufpoi = 0;
4419
4420 /* Partial page write?, or need to use bounce buffer */
4421 if (use_bufpoi) {
4422 pr_debug("%s: using write bounce buffer for buf@%p\n",
4423 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004424 if (part_pagewr)
4425 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004426 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004427 memset(chip->data_buf, 0xff, mtd->writesize);
4428 memcpy(&chip->data_buf[column], buf, bytes);
4429 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004430 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004431
Maxim Levitsky782ce792010-02-22 20:39:36 +02004432 if (unlikely(oob)) {
4433 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004434 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004435 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004436 } else {
4437 /* We still need to erase leftover OOB data */
4438 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004439 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004440
4441 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004442 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004443 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004444 if (ret)
4445 break;
4446
4447 writelen -= bytes;
4448 if (!writelen)
4449 break;
4450
Thomas Gleixner29072b92006-09-28 15:38:36 +02004451 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004452 buf += bytes;
4453 realpage++;
4454
4455 page = realpage & chip->pagemask;
4456 /* Check, if we cross a chip boundary */
4457 if (!page) {
4458 chipnr++;
4459 chip->select_chip(mtd, -1);
4460 chip->select_chip(mtd, chipnr);
4461 }
4462 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004463
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004464 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004465 if (unlikely(oob))
4466 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004467
4468err_out:
4469 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004470 return ret;
4471}
4472
4473/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004474 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004475 * @mtd: MTD device structure
4476 * @to: offset to write to
4477 * @len: number of bytes to write
4478 * @retlen: pointer to variable to store the number of written bytes
4479 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004480 *
4481 * NAND write with ECC. Used when performing writes in interrupt context, this
4482 * may for example be called by mtdoops when writing an oops while in panic.
4483 */
4484static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4485 size_t *retlen, const uint8_t *buf)
4486{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004487 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004488 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004489 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004490 int ret;
4491
Brian Norris8b6e50c2011-05-25 14:59:01 -07004492 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004493 panic_nand_get_device(chip, mtd, FL_WRITING);
4494
Brent Taylor30863e382017-10-30 22:32:45 -05004495 chip->select_chip(mtd, chipnr);
4496
4497 /* Wait for the device to get ready */
4498 panic_nand_wait(mtd, chip, 400);
4499
Brian Norris0ec56dc2015-02-28 02:02:30 -08004500 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004501 ops.len = len;
4502 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004503 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004504
Brian Norris4a89ff82011-08-30 18:45:45 -07004505 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004506
Brian Norris4a89ff82011-08-30 18:45:45 -07004507 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004508 return ret;
4509}
4510
4511/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004512 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004513 * @mtd: MTD device structure
4514 * @to: offset to write to
4515 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004516 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004517 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004518 */
4519static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4520 struct mtd_oob_ops *ops)
4521{
Adrian Hunter03736152007-01-31 17:58:29 +02004522 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004523 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Brian Norris289c0522011-07-19 10:06:09 -07004525 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304526 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Boris BREZILLON29f10582016-03-07 10:46:52 +01004528 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004531 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004532 pr_debug("%s: attempt to write past end of page\n",
4533 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 return -EINVAL;
4535 }
4536
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004537 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004538
4539 /*
4540 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4541 * of my DiskOnChip 2000 test units) will clear the whole data page too
4542 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4543 * it in the doc2000 driver in August 1999. dwmw2.
4544 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004545 nand_reset(chip, chipnr);
4546
4547 chip->select_chip(mtd, chipnr);
4548
4549 /* Shift to get page */
4550 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
4552 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004553 if (nand_check_wp(mtd)) {
4554 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004555 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004556 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004559 if (page == chip->pagebuf)
4560 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004562 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004563
Brian Norris0612b9d2011-08-30 18:45:40 -07004564 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07004565 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
4566 else
4567 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004568
Huang Shijieb0bb6902012-11-19 14:43:29 +08004569 chip->select_chip(mtd, -1);
4570
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004571 if (status)
4572 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
Vitaly Wool70145682006-11-03 18:20:38 +03004574 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004576 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004577}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004579/**
4580 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004581 * @mtd: MTD device structure
4582 * @to: offset to write to
4583 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004584 */
4585static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4586 struct mtd_oob_ops *ops)
4587{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004588 int ret = -ENOTSUPP;
4589
4590 ops->retlen = 0;
4591
Huang Shijie6a8214a2012-11-19 14:43:30 +08004592 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004593
Florian Fainellif8ac0412010-09-07 13:23:43 +02004594 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004595 case MTD_OPS_PLACE_OOB:
4596 case MTD_OPS_AUTO_OOB:
4597 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004598 break;
4599
4600 default:
4601 goto out;
4602 }
4603
4604 if (!ops->datbuf)
4605 ret = nand_do_write_oob(mtd, to, ops);
4606 else
4607 ret = nand_do_write_ops(mtd, to, ops);
4608
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004609out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004610 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 return ret;
4612}
4613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614/**
Brian Norris49c50b92014-05-06 16:02:19 -07004615 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004616 * @mtd: MTD device structure
4617 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 *
Brian Norris49c50b92014-05-06 16:02:19 -07004619 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 */
Brian Norris49c50b92014-05-06 16:02:19 -07004621static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004623 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004624 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004627 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004628
Boris Brezillon97d90da2017-11-30 18:01:29 +01004629 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630}
4631
4632/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004634 * @mtd: MTD device structure
4635 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004637 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004639static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640{
David Woodhousee0c7d762006-05-13 18:07:53 +01004641 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004643
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004645 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004646 * @mtd: MTD device structure
4647 * @instr: erase instruction
4648 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004650 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004652int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4653 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654{
Adrian Hunter69423d92008-12-10 13:37:21 +00004655 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004656 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00004657 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
Brian Norris289c0522011-07-19 10:06:09 -07004659 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4660 __func__, (unsigned long long)instr->addr,
4661 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304663 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004667 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004670 page = (int)(instr->addr >> chip->page_shift);
4671 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004674 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
4676 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004677 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 /* Check, if it is write protected */
4680 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004681 pr_debug("%s: device is write protected!\n",
4682 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 instr->state = MTD_ERASE_FAILED;
4684 goto erase_exit;
4685 }
4686
4687 /* Loop through the pages */
4688 len = instr->len;
4689
4690 instr->state = MTD_ERASING;
4691
4692 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004693 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004694 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304695 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004696 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4697 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 instr->state = MTD_ERASE_FAILED;
4699 goto erase_exit;
4700 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004701
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004702 /*
4703 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004704 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004705 */
4706 if (page <= chip->pagebuf && chip->pagebuf <
4707 (page + pages_per_block))
4708 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
Brian Norris49c50b92014-05-06 16:02:19 -07004710 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
4712 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004713 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004714 pr_debug("%s: failed erase, page 0x%08x\n",
4715 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00004717 instr->fail_addr =
4718 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 goto erase_exit;
4720 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004721
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004723 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 page += pages_per_block;
4725
4726 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004727 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004729 chip->select_chip(mtd, -1);
4730 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 }
4732 }
4733 instr->state = MTD_ERASE_DONE;
4734
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004735erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
4739 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004740 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 nand_release_device(mtd);
4742
David Woodhouse49defc02007-10-06 15:01:59 -04004743 /* Do call back function */
4744 if (!ret)
4745 mtd_erase_callback(instr);
4746
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 /* Return more or less happy */
4748 return ret;
4749}
4750
4751/**
4752 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004753 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004755 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004757static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758{
Brian Norris289c0522011-07-19 10:06:09 -07004759 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
4761 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004762 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004764 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765}
4766
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004768 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004769 * @mtd: MTD device structure
4770 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004772static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304774 struct nand_chip *chip = mtd_to_nand(mtd);
4775 int chipnr = (int)(offs >> chip->chip_shift);
4776 int ret;
4777
4778 /* Select the NAND device */
4779 nand_get_device(mtd, FL_READING);
4780 chip->select_chip(mtd, chipnr);
4781
4782 ret = nand_block_checkbad(mtd, offs, 0);
4783
4784 chip->select_chip(mtd, -1);
4785 nand_release_device(mtd);
4786
4787 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788}
4789
4790/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004791 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004792 * @mtd: MTD device structure
4793 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004795static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 int ret;
4798
Florian Fainellif8ac0412010-09-07 13:23:43 +02004799 ret = nand_block_isbad(mtd, ofs);
4800 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004801 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 if (ret > 0)
4803 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004804 return ret;
4805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
Brian Norris5a0edb22013-07-30 17:52:58 -07004807 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808}
4809
4810/**
Zach Brown56718422017-01-10 13:30:20 -06004811 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4812 * @mtd: MTD device structure
4813 * @ofs: offset relative to mtd start
4814 * @len: length of mtd
4815 */
4816static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4817{
4818 struct nand_chip *chip = mtd_to_nand(mtd);
4819 u32 part_start_block;
4820 u32 part_end_block;
4821 u32 part_start_die;
4822 u32 part_end_die;
4823
4824 /*
4825 * max_bb_per_die and blocks_per_die used to determine
4826 * the maximum bad block count.
4827 */
4828 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4829 return -ENOTSUPP;
4830
4831 /* Get the start and end of the partition in erase blocks. */
4832 part_start_block = mtd_div_by_eb(ofs, mtd);
4833 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4834
4835 /* Get the start and end LUNs of the partition. */
4836 part_start_die = part_start_block / chip->blocks_per_die;
4837 part_end_die = part_end_block / chip->blocks_per_die;
4838
4839 /*
4840 * Look up the bad blocks per unit and multiply by the number of units
4841 * that the partition spans.
4842 */
4843 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4844}
4845
4846/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004847 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004848 * @mtd: MTD device structure
4849 * @chip: nand chip info structure
4850 * @addr: feature address.
4851 * @subfeature_param: the subfeature parameters, a four bytes array.
4852 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004853static int nand_default_set_features(struct mtd_info *mtd,
4854 struct nand_chip *chip, int addr,
4855 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004856{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004857 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004858}
4859
4860/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004861 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004862 * @mtd: MTD device structure
4863 * @chip: nand chip info structure
4864 * @addr: feature address.
4865 * @subfeature_param: the subfeature parameters, a four bytes array.
4866 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004867static int nand_default_get_features(struct mtd_info *mtd,
4868 struct nand_chip *chip, int addr,
4869 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004870{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004871 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004872}
4873
4874/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004875 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004876 * @mtd: MTD device structure
4877 * @chip: nand chip info structure
4878 * @addr: feature address.
4879 * @subfeature_param: the subfeature parameters, a four bytes array.
4880 *
4881 * Should be used by NAND controller drivers that do not support the SET/GET
4882 * FEATURES operations.
4883 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004884int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4885 int addr, u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004886{
4887 return -ENOTSUPP;
4888}
Miquel Raynalb9587582018-03-19 14:47:19 +01004889EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004890
4891/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004892 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004893 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004894 */
4895static int nand_suspend(struct mtd_info *mtd)
4896{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004897 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004898}
4899
4900/**
4901 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004902 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004903 */
4904static void nand_resume(struct mtd_info *mtd)
4905{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004906 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004907
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004908 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004909 nand_release_device(mtd);
4910 else
Brian Norrisd0370212011-07-19 10:06:08 -07004911 pr_err("%s called for a chip which is not in suspended state\n",
4912 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004913}
4914
Scott Branden72ea4032014-11-20 11:18:05 -08004915/**
4916 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4917 * prevent further operations
4918 * @mtd: MTD device structure
4919 */
4920static void nand_shutdown(struct mtd_info *mtd)
4921{
Brian Norris9ca641b2015-11-09 16:37:28 -08004922 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004923}
4924
Brian Norris8b6e50c2011-05-25 14:59:01 -07004925/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004926static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004927{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004928 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004931 if (!chip->chip_delay)
4932 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
4934 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004935 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004936 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
4938 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004939 if (chip->waitfunc == NULL)
4940 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004942 if (!chip->select_chip)
4943 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004944
Huang Shijie4204ccc2013-08-16 10:10:07 +08004945 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004946 if (!chip->set_features)
4947 chip->set_features = nand_default_set_features;
4948 if (!chip->get_features)
4949 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004950
Brian Norris68e80782013-07-18 01:17:02 -07004951 /* If called twice, pointers that depend on busw may need to be reset */
4952 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004953 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4954 if (!chip->read_word)
4955 chip->read_word = nand_read_word;
4956 if (!chip->block_bad)
4957 chip->block_bad = nand_block_bad;
4958 if (!chip->block_markbad)
4959 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004960 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004961 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004962 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4963 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004964 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004965 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004966 if (!chip->scan_bbt)
4967 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004968
4969 if (!chip->controller) {
4970 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02004971 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004972 }
4973
Masahiro Yamada477544c2017-03-30 17:15:05 +09004974 if (!chip->buf_align)
4975 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004976}
4977
Brian Norris8b6e50c2011-05-25 14:59:01 -07004978/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004979static void sanitize_string(uint8_t *s, size_t len)
4980{
4981 ssize_t i;
4982
Brian Norris8b6e50c2011-05-25 14:59:01 -07004983 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004984 s[len - 1] = 0;
4985
Brian Norris8b6e50c2011-05-25 14:59:01 -07004986 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004987 for (i = 0; i < len - 1; i++) {
4988 if (s[i] < ' ' || s[i] > 127)
4989 s[i] = '?';
4990 }
4991
Brian Norris8b6e50c2011-05-25 14:59:01 -07004992 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004993 strim(s);
4994}
4995
4996static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4997{
4998 int i;
4999 while (len--) {
5000 crc ^= *p++ << 8;
5001 for (i = 0; i < 8; i++)
5002 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
5003 }
5004
5005 return crc;
5006}
5007
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005008/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005009static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
5010 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005011{
5012 struct onfi_ext_param_page *ep;
5013 struct onfi_ext_section *s;
5014 struct onfi_ext_ecc_info *ecc;
5015 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005016 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005017 int len;
5018 int i;
5019
5020 len = le16_to_cpu(p->ext_param_page_length) * 16;
5021 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005022 if (!ep)
5023 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005024
5025 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005026 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5027 if (ret)
5028 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005029
5030 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005031 ret = nand_change_read_column_op(chip,
5032 sizeof(*p) * p->num_of_param_pages,
5033 ep, len, true);
5034 if (ret)
5035 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005036
Boris Brezillon97d90da2017-11-30 18:01:29 +01005037 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005038 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5039 != le16_to_cpu(ep->crc))) {
5040 pr_debug("fail in the CRC.\n");
5041 goto ext_out;
5042 }
5043
5044 /*
5045 * Check the signature.
5046 * Do not strictly follow the ONFI spec, maybe changed in future.
5047 */
5048 if (strncmp(ep->sig, "EPPS", 4)) {
5049 pr_debug("The signature is invalid.\n");
5050 goto ext_out;
5051 }
5052
5053 /* find the ECC section. */
5054 cursor = (uint8_t *)(ep + 1);
5055 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5056 s = ep->sections + i;
5057 if (s->type == ONFI_SECTION_TYPE_2)
5058 break;
5059 cursor += s->length * 16;
5060 }
5061 if (i == ONFI_EXT_SECTION_MAX) {
5062 pr_debug("We can not find the ECC section.\n");
5063 goto ext_out;
5064 }
5065
5066 /* get the info we want. */
5067 ecc = (struct onfi_ext_ecc_info *)cursor;
5068
Brian Norris4ae7d222013-09-16 18:20:21 -07005069 if (!ecc->codeword_size) {
5070 pr_debug("Invalid codeword size\n");
5071 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005072 }
5073
Brian Norris4ae7d222013-09-16 18:20:21 -07005074 chip->ecc_strength_ds = ecc->ecc_bits;
5075 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005076 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005077
5078ext_out:
5079 kfree(ep);
5080 return ret;
5081}
5082
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005083/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005084 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005085 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005086static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005087{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005088 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005089 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005090 char id[4];
5091 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005092
Brian Norris7854d3f2011-06-23 14:12:08 -07005093 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005094 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5095 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005096 return 0;
5097
Boris Brezillon97d90da2017-11-30 18:01:29 +01005098 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5099 if (ret)
5100 return 0;
5101
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005102 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005103 ret = nand_read_data_op(chip, p, sizeof(*p), true);
5104 if (ret)
5105 return 0;
5106
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005107 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
5108 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005109 break;
5110 }
5111 }
5112
Brian Norrisc7f23a72013-08-13 10:51:55 -07005113 if (i == 3) {
5114 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005115 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07005116 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005117
Brian Norris8b6e50c2011-05-25 14:59:01 -07005118 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005119 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005120 if (val & (1 << 5))
5121 chip->onfi_version = 23;
5122 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005123 chip->onfi_version = 22;
5124 else if (val & (1 << 3))
5125 chip->onfi_version = 21;
5126 else if (val & (1 << 2))
5127 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005128 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005129 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005130
5131 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005132 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005133 return 0;
5134 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005135
5136 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5137 sanitize_string(p->model, sizeof(p->model));
5138 if (!mtd->name)
5139 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07005140
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005141 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005142
5143 /*
5144 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5145 * (don't ask me who thought of this...). MTD assumes that these
5146 * dimensions will be power-of-2, so just truncate the remaining area.
5147 */
5148 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5149 mtd->erasesize *= mtd->writesize;
5150
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005151 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005152
5153 /* See erasesize comment */
5154 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005155 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005156 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005157
Zach Brown34da5f52017-01-10 13:30:21 -06005158 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5159 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5160
Huang Shijiee2985fc2013-05-17 11:17:30 +08005161 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005162 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005163
Huang Shijie10c86ba2013-05-17 11:17:26 +08005164 if (p->ecc_bits != 0xff) {
5165 chip->ecc_strength_ds = p->ecc_bits;
5166 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005167 } else if (chip->onfi_version >= 21 &&
5168 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
5169
5170 /*
5171 * The nand_flash_detect_ext_param_page() uses the
5172 * Change Read Column command which maybe not supported
5173 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5174 * now. We do not replace user supplied command function.
5175 */
5176 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5177 chip->cmdfunc = nand_command_lp;
5178
5179 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005180 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005181 pr_warn("Failed to detect ONFI extended param page\n");
5182 } else {
5183 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005184 }
5185
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005186 return 1;
5187}
5188
5189/*
Huang Shijie91361812014-02-21 13:39:40 +08005190 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5191 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005192static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005193{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005194 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08005195 struct nand_jedec_params *p = &chip->jedec_params;
5196 struct jedec_ecc_info *ecc;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005197 char id[5];
5198 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005199
5200 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005201 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5202 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005203 return 0;
5204
Boris Brezillon97d90da2017-11-30 18:01:29 +01005205 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
5206 if (ret)
5207 return 0;
5208
Huang Shijie91361812014-02-21 13:39:40 +08005209 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005210 ret = nand_read_data_op(chip, p, sizeof(*p), true);
5211 if (ret)
5212 return 0;
Huang Shijie91361812014-02-21 13:39:40 +08005213
5214 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5215 le16_to_cpu(p->crc))
5216 break;
5217 }
5218
5219 if (i == 3) {
5220 pr_err("Could not find valid JEDEC parameter page; aborting\n");
5221 return 0;
5222 }
5223
5224 /* Check version */
5225 val = le16_to_cpu(p->revision);
5226 if (val & (1 << 2))
5227 chip->jedec_version = 10;
5228 else if (val & (1 << 1))
5229 chip->jedec_version = 1; /* vendor specific version */
5230
5231 if (!chip->jedec_version) {
5232 pr_info("unsupported JEDEC version: %d\n", val);
5233 return 0;
5234 }
5235
5236 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5237 sanitize_string(p->model, sizeof(p->model));
5238 if (!mtd->name)
5239 mtd->name = p->model;
5240
5241 mtd->writesize = le32_to_cpu(p->byte_per_page);
5242
5243 /* Please reference to the comment for nand_flash_detect_onfi. */
5244 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5245 mtd->erasesize *= mtd->writesize;
5246
5247 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5248
5249 /* Please reference to the comment for nand_flash_detect_onfi. */
5250 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5251 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5252 chip->bits_per_cell = p->bits_per_cell;
5253
5254 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005255 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005256
5257 /* ECC info */
5258 ecc = &p->ecc_info[0];
5259
5260 if (ecc->codeword_size >= 9) {
5261 chip->ecc_strength_ds = ecc->ecc_bits;
5262 chip->ecc_step_ds = 1 << ecc->codeword_size;
5263 } else {
5264 pr_warn("Invalid codeword size\n");
5265 }
5266
5267 return 1;
5268}
5269
5270/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005271 * nand_id_has_period - Check if an ID string has a given wraparound period
5272 * @id_data: the ID string
5273 * @arrlen: the length of the @id_data array
5274 * @period: the period of repitition
5275 *
5276 * Check if an ID string is repeated within a given sequence of bytes at
5277 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005278 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005279 * if the repetition has a period of @period; otherwise, returns zero.
5280 */
5281static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5282{
5283 int i, j;
5284 for (i = 0; i < period; i++)
5285 for (j = i + period; j < arrlen; j += period)
5286 if (id_data[i] != id_data[j])
5287 return 0;
5288 return 1;
5289}
5290
5291/*
5292 * nand_id_len - Get the length of an ID string returned by CMD_READID
5293 * @id_data: the ID string
5294 * @arrlen: the length of the @id_data array
5295
5296 * Returns the length of the ID string, according to known wraparound/trailing
5297 * zero patterns. If no pattern exists, returns the length of the array.
5298 */
5299static int nand_id_len(u8 *id_data, int arrlen)
5300{
5301 int last_nonzero, period;
5302
5303 /* Find last non-zero byte */
5304 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5305 if (id_data[last_nonzero])
5306 break;
5307
5308 /* All zeros */
5309 if (last_nonzero < 0)
5310 return 0;
5311
5312 /* Calculate wraparound period */
5313 for (period = 1; period < arrlen; period++)
5314 if (nand_id_has_period(id_data, arrlen, period))
5315 break;
5316
5317 /* There's a repeated pattern */
5318 if (period < arrlen)
5319 return period;
5320
5321 /* There are trailing zeros */
5322 if (last_nonzero < arrlen - 1)
5323 return last_nonzero + 1;
5324
5325 /* No pattern detected */
5326 return arrlen;
5327}
5328
Huang Shijie7db906b2013-09-25 14:58:11 +08005329/* Extract the bits of per cell from the 3rd byte of the extended ID */
5330static int nand_get_bits_per_cell(u8 cellinfo)
5331{
5332 int bits;
5333
5334 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5335 bits >>= NAND_CI_CELLTYPE_SHIFT;
5336 return bits + 1;
5337}
5338
Brian Norrise3b88bd2012-09-24 20:40:52 -07005339/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005340 * Many new NAND share similar device ID codes, which represent the size of the
5341 * chip. The rest of the parameters must be decoded according to generic or
5342 * manufacturer-specific "extended ID" decoding patterns.
5343 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005344void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005345{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005346 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005347 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005348 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005349 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005350 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005351 /* The 4th id byte is the important one */
5352 extid = id_data[3];
5353
Boris Brezillon01389b62016-06-08 10:30:18 +02005354 /* Calc pagesize */
5355 mtd->writesize = 1024 << (extid & 0x03);
5356 extid >>= 2;
5357 /* Calc oobsize */
5358 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5359 extid >>= 2;
5360 /* Calc blocksize. Blocksize is multiples of 64KiB */
5361 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5362 extid >>= 2;
5363 /* Get buswidth information */
5364 if (extid & 0x1)
5365 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005366}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005367EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005368
5369/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005370 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5371 * decodes a matching ID table entry and assigns the MTD size parameters for
5372 * the chip.
5373 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005374static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005375{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005376 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005377
5378 mtd->erasesize = type->erasesize;
5379 mtd->writesize = type->pagesize;
5380 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005381
Huang Shijie1c195e92013-09-25 14:58:12 +08005382 /* All legacy ID NAND are small-page, SLC */
5383 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005384}
5385
5386/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005387 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5388 * heuristic patterns using various detected parameters (e.g., manufacturer,
5389 * page size, cell-type information).
5390 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005391static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005392{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005393 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005394
5395 /* Set the bad block position */
5396 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5397 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5398 else
5399 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005400}
5401
Huang Shijieec6e87e2013-03-15 11:01:00 +08005402static inline bool is_full_id_nand(struct nand_flash_dev *type)
5403{
5404 return type->id_len;
5405}
5406
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005407static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005408 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005409{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005410 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005411 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005412
Huang Shijieec6e87e2013-03-15 11:01:00 +08005413 if (!strncmp(type->id, id_data, type->id_len)) {
5414 mtd->writesize = type->pagesize;
5415 mtd->erasesize = type->erasesize;
5416 mtd->oobsize = type->oobsize;
5417
Huang Shijie7db906b2013-09-25 14:58:11 +08005418 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005419 chip->chipsize = (uint64_t)type->chipsize << 20;
5420 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005421 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5422 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005423 chip->onfi_timing_mode_default =
5424 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005425
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005426 if (!mtd->name)
5427 mtd->name = type->name;
5428
Huang Shijieec6e87e2013-03-15 11:01:00 +08005429 return true;
5430 }
5431 return false;
5432}
5433
Brian Norris7e74c2d2012-09-24 20:40:49 -07005434/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005435 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5436 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5437 * table.
5438 */
5439static void nand_manufacturer_detect(struct nand_chip *chip)
5440{
5441 /*
5442 * Try manufacturer detection if available and use
5443 * nand_decode_ext_id() otherwise.
5444 */
5445 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005446 chip->manufacturer.desc->ops->detect) {
5447 /* The 3rd id byte holds MLC / multichip data */
5448 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005449 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005450 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005451 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005452 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005453}
5454
5455/*
5456 * Manufacturer initialization. This function is called for all NANDs including
5457 * ONFI and JEDEC compliant ones.
5458 * Manufacturer drivers should put all their specific initialization code in
5459 * their ->init() hook.
5460 */
5461static int nand_manufacturer_init(struct nand_chip *chip)
5462{
5463 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5464 !chip->manufacturer.desc->ops->init)
5465 return 0;
5466
5467 return chip->manufacturer.desc->ops->init(chip);
5468}
5469
5470/*
5471 * Manufacturer cleanup. This function is called for all NANDs including
5472 * ONFI and JEDEC compliant ones.
5473 * Manufacturer drivers should put all their specific cleanup code in their
5474 * ->cleanup() hook.
5475 */
5476static void nand_manufacturer_cleanup(struct nand_chip *chip)
5477{
5478 /* Release manufacturer private data */
5479 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5480 chip->manufacturer.desc->ops->cleanup)
5481 chip->manufacturer.desc->ops->cleanup(chip);
5482}
5483
5484/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005485 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005486 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005487static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005488{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005489 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005490 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005491 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005492 u8 *id_data = chip->id.data;
5493 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
Karl Beldanef89a882008-09-15 14:37:29 +02005495 /*
5496 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005497 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005498 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005499 ret = nand_reset(chip, 0);
5500 if (ret)
5501 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005502
5503 /* Select the device */
5504 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005505
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005507 ret = nand_readid_op(chip, 0, id_data, 2);
5508 if (ret)
5509 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
5511 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005512 maf_id = id_data[0];
5513 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
Brian Norris8b6e50c2011-05-25 14:59:01 -07005515 /*
5516 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005517 * interface concerns can cause random data which looks like a
5518 * possibly credible NAND flash to appear. If the two results do
5519 * not match, ignore the device completely.
5520 */
5521
Brian Norris4aef9b72012-09-24 20:40:48 -07005522 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005523 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5524 if (ret)
5525 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005526
Boris Brezillon7f501f02016-05-24 19:20:05 +02005527 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005528 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005529 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005530 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005531 }
5532
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005533 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005534
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005535 /* Try to identify manufacturer */
5536 manufacturer = nand_get_manufacturer(maf_id);
5537 chip->manufacturer.desc = manufacturer;
5538
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005539 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005540 type = nand_flash_ids;
5541
Boris Brezillon29a198a2016-05-24 20:17:48 +02005542 /*
5543 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5544 * override it.
5545 * This is required to make sure initial NAND bus width set by the
5546 * NAND controller driver is coherent with the real NAND bus width
5547 * (extracted by auto-detection code).
5548 */
5549 busw = chip->options & NAND_BUSWIDTH_16;
5550
5551 /*
5552 * The flag is only set (never cleared), reset it to its default value
5553 * before starting auto-detection.
5554 */
5555 chip->options &= ~NAND_BUSWIDTH_16;
5556
Huang Shijieec6e87e2013-03-15 11:01:00 +08005557 for (; type->name != NULL; type++) {
5558 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005559 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005560 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005561 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005562 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005563 }
5564 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005565
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005566 chip->onfi_version = 0;
5567 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005568 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005569 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005570 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005571
5572 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005573 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08005574 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005575 }
5576
David Woodhouse5e81e882010-02-26 18:32:56 +00005577 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005578 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005579
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005580 if (!mtd->name)
5581 mtd->name = type->name;
5582
Adrian Hunter69423d92008-12-10 13:37:21 +00005583 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005584
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005585 if (!type->pagesize)
5586 nand_manufacturer_detect(chip);
5587 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005588 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005589
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005590 /* Get chip options */
5591 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005592
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005593ident_done:
5594
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005595 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005596 WARN_ON(busw & NAND_BUSWIDTH_16);
5597 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005598 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5599 /*
5600 * Check, if buswidth is correct. Hardware drivers should set
5601 * chip correct!
5602 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005603 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005604 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005605 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5606 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005607 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5608 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005609 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005610 }
5611
Boris Brezillon7f501f02016-05-24 19:20:05 +02005612 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005613
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005614 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005615 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005616 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005617 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005618
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005619 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005620 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005621 if (chip->chipsize & 0xffffffff)
5622 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005623 else {
5624 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5625 chip->chip_shift += 32 - 1;
5626 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005627
Masahiro Yamada14157f82017-09-13 11:05:50 +09005628 if (chip->chip_shift - chip->page_shift > 16)
5629 chip->options |= NAND_ROW_ADDR_3;
5630
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005631 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005632 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005633
Brian Norris8b6e50c2011-05-25 14:59:01 -07005634 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005635 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5636 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005637
Ezequiel Garcia20171642013-11-25 08:30:31 -03005638 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005639 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005640
5641 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005642 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5643 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005644 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005645 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5646 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005647 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005648 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5649 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005650
Rafał Miłecki3755a992014-10-21 00:01:04 +02005651 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005652 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005653 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005654 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005655}
5656
Boris Brezillond48f62b2016-04-01 14:54:32 +02005657static const char * const nand_ecc_modes[] = {
5658 [NAND_ECC_NONE] = "none",
5659 [NAND_ECC_SOFT] = "soft",
5660 [NAND_ECC_HW] = "hw",
5661 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5662 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005663 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005664};
5665
5666static int of_get_nand_ecc_mode(struct device_node *np)
5667{
5668 const char *pm;
5669 int err, i;
5670
5671 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5672 if (err < 0)
5673 return err;
5674
5675 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5676 if (!strcasecmp(pm, nand_ecc_modes[i]))
5677 return i;
5678
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005679 /*
5680 * For backward compatibility we support few obsoleted values that don't
5681 * have their mappings into nand_ecc_modes_t anymore (they were merged
5682 * with other enums).
5683 */
5684 if (!strcasecmp(pm, "soft_bch"))
5685 return NAND_ECC_SOFT;
5686
Boris Brezillond48f62b2016-04-01 14:54:32 +02005687 return -ENODEV;
5688}
5689
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005690static const char * const nand_ecc_algos[] = {
5691 [NAND_ECC_HAMMING] = "hamming",
5692 [NAND_ECC_BCH] = "bch",
5693};
5694
Boris Brezillond48f62b2016-04-01 14:54:32 +02005695static int of_get_nand_ecc_algo(struct device_node *np)
5696{
5697 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005698 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005699
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005700 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5701 if (!err) {
5702 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5703 if (!strcasecmp(pm, nand_ecc_algos[i]))
5704 return i;
5705 return -ENODEV;
5706 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005707
5708 /*
5709 * For backward compatibility we also read "nand-ecc-mode" checking
5710 * for some obsoleted values that were specifying ECC algorithm.
5711 */
5712 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5713 if (err < 0)
5714 return err;
5715
5716 if (!strcasecmp(pm, "soft"))
5717 return NAND_ECC_HAMMING;
5718 else if (!strcasecmp(pm, "soft_bch"))
5719 return NAND_ECC_BCH;
5720
5721 return -ENODEV;
5722}
5723
5724static int of_get_nand_ecc_step_size(struct device_node *np)
5725{
5726 int ret;
5727 u32 val;
5728
5729 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5730 return ret ? ret : val;
5731}
5732
5733static int of_get_nand_ecc_strength(struct device_node *np)
5734{
5735 int ret;
5736 u32 val;
5737
5738 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5739 return ret ? ret : val;
5740}
5741
5742static int of_get_nand_bus_width(struct device_node *np)
5743{
5744 u32 val;
5745
5746 if (of_property_read_u32(np, "nand-bus-width", &val))
5747 return 8;
5748
5749 switch (val) {
5750 case 8:
5751 case 16:
5752 return val;
5753 default:
5754 return -EIO;
5755 }
5756}
5757
5758static bool of_get_nand_on_flash_bbt(struct device_node *np)
5759{
5760 return of_property_read_bool(np, "nand-on-flash-bbt");
5761}
5762
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005763static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005764{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005765 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005766 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005767
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005768 if (!dn)
5769 return 0;
5770
Brian Norris5844fee2015-01-23 00:22:27 -08005771 if (of_get_nand_bus_width(dn) == 16)
5772 chip->options |= NAND_BUSWIDTH_16;
5773
5774 if (of_get_nand_on_flash_bbt(dn))
5775 chip->bbt_options |= NAND_BBT_USE_FLASH;
5776
5777 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005778 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005779 ecc_strength = of_get_nand_ecc_strength(dn);
5780 ecc_step = of_get_nand_ecc_step_size(dn);
5781
Brian Norris5844fee2015-01-23 00:22:27 -08005782 if (ecc_mode >= 0)
5783 chip->ecc.mode = ecc_mode;
5784
Rafał Miłecki79082452016-03-23 11:19:02 +01005785 if (ecc_algo >= 0)
5786 chip->ecc.algo = ecc_algo;
5787
Brian Norris5844fee2015-01-23 00:22:27 -08005788 if (ecc_strength >= 0)
5789 chip->ecc.strength = ecc_strength;
5790
5791 if (ecc_step > 0)
5792 chip->ecc.size = ecc_step;
5793
Boris Brezillonba78ee02016-06-08 17:04:22 +02005794 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5795 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5796
Brian Norris5844fee2015-01-23 00:22:27 -08005797 return 0;
5798}
5799
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005800/**
David Woodhouse3b85c322006-09-25 17:06:53 +01005801 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005802 * @mtd: MTD device structure
5803 * @maxchips: number of chips to scan for
5804 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005805 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005806 * This is the first phase of the normal nand_scan() function. It reads the
5807 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005808 *
5809 */
David Woodhouse5e81e882010-02-26 18:32:56 +00005810int nand_scan_ident(struct mtd_info *mtd, int maxchips,
5811 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005812{
Cai Zhiyongbb770822013-12-25 20:11:15 +08005813 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01005814 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08005815 int ret;
5816
Miquel Raynal17fa8042017-11-30 18:01:31 +01005817 /* Enforce the right timings for reset/detection */
5818 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5819
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005820 ret = nand_dt_init(chip);
5821 if (ret)
5822 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005823
Brian Norrisf7a8e382016-01-05 10:39:45 -08005824 if (!mtd->name && mtd->dev.parent)
5825 mtd->name = dev_name(mtd->dev.parent);
5826
Miquel Raynal8878b122017-11-09 14:16:45 +01005827 /*
5828 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5829 * populated.
5830 */
5831 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005832 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005833 * Default functions assigned for ->cmdfunc() and
5834 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005835 */
Miquel Raynal8878b122017-11-09 14:16:45 +01005836 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5837 pr_err("->cmd_ctrl() should be provided\n");
5838 return -EINVAL;
5839 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005840 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005841
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005842 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005843 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005844
5845 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005846 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005847 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005848 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005849 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005850 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005851 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 }
5853
Boris Brezillon7f501f02016-05-24 19:20:05 +02005854 nand_maf_id = chip->id.data[0];
5855 nand_dev_id = chip->id.data[1];
5856
Huang Shijie07300162012-11-09 16:23:45 +08005857 chip->select_chip(mtd, -1);
5858
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005859 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005860 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005861 u8 id[2];
5862
Karl Beldanef89a882008-09-15 14:37:29 +02005863 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005864 nand_reset(chip, i);
5865
5866 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005868 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005870 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08005871 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 break;
Huang Shijie07300162012-11-09 16:23:45 +08005873 }
5874 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 }
5876 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005877 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005878
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005880 chip->numchips = i;
5881 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
David Woodhouse3b85c322006-09-25 17:06:53 +01005883 return 0;
5884}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005885EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01005886
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005887static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
5888{
5889 struct nand_chip *chip = mtd_to_nand(mtd);
5890 struct nand_ecc_ctrl *ecc = &chip->ecc;
5891
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005892 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005893 return -EINVAL;
5894
5895 switch (ecc->algo) {
5896 case NAND_ECC_HAMMING:
5897 ecc->calculate = nand_calculate_ecc;
5898 ecc->correct = nand_correct_data;
5899 ecc->read_page = nand_read_page_swecc;
5900 ecc->read_subpage = nand_read_subpage;
5901 ecc->write_page = nand_write_page_swecc;
5902 ecc->read_page_raw = nand_read_page_raw;
5903 ecc->write_page_raw = nand_write_page_raw;
5904 ecc->read_oob = nand_read_oob_std;
5905 ecc->write_oob = nand_write_oob_std;
5906 if (!ecc->size)
5907 ecc->size = 256;
5908 ecc->bytes = 3;
5909 ecc->strength = 1;
5910 return 0;
5911 case NAND_ECC_BCH:
5912 if (!mtd_nand_has_bch()) {
5913 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5914 return -EINVAL;
5915 }
5916 ecc->calculate = nand_bch_calculate_ecc;
5917 ecc->correct = nand_bch_correct_data;
5918 ecc->read_page = nand_read_page_swecc;
5919 ecc->read_subpage = nand_read_subpage;
5920 ecc->write_page = nand_write_page_swecc;
5921 ecc->read_page_raw = nand_read_page_raw;
5922 ecc->write_page_raw = nand_write_page_raw;
5923 ecc->read_oob = nand_read_oob_std;
5924 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005925
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005926 /*
5927 * Board driver should supply ecc.size and ecc.strength
5928 * values to select how many bits are correctable.
5929 * Otherwise, default to 4 bits for large page devices.
5930 */
5931 if (!ecc->size && (mtd->oobsize >= 64)) {
5932 ecc->size = 512;
5933 ecc->strength = 4;
5934 }
5935
5936 /*
5937 * if no ecc placement scheme was provided pickup the default
5938 * large page one.
5939 */
5940 if (!mtd->ooblayout) {
5941 /* handle large page devices only */
5942 if (mtd->oobsize < 64) {
5943 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5944 return -EINVAL;
5945 }
5946
5947 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005948
5949 }
5950
5951 /*
5952 * We can only maximize ECC config when the default layout is
5953 * used, otherwise we don't know how many bytes can really be
5954 * used.
5955 */
5956 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5957 ecc->options & NAND_ECC_MAXIMIZE) {
5958 int steps, bytes;
5959
5960 /* Always prefer 1k blocks over 512bytes ones */
5961 ecc->size = 1024;
5962 steps = mtd->writesize / ecc->size;
5963
5964 /* Reserve 2 bytes for the BBM */
5965 bytes = (mtd->oobsize - 2) / steps;
5966 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005967 }
5968
5969 /* See nand_bch_init() for details. */
5970 ecc->bytes = 0;
5971 ecc->priv = nand_bch_init(mtd);
5972 if (!ecc->priv) {
5973 WARN(1, "BCH ECC initialization failed!\n");
5974 return -EINVAL;
5975 }
5976 return 0;
5977 default:
5978 WARN(1, "Unsupported ECC algorithm!\n");
5979 return -EINVAL;
5980 }
5981}
5982
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005983/**
5984 * nand_check_ecc_caps - check the sanity of preset ECC settings
5985 * @chip: nand chip info structure
5986 * @caps: ECC caps info structure
5987 * @oobavail: OOB size that the ECC engine can use
5988 *
5989 * When ECC step size and strength are already set, check if they are supported
5990 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5991 * On success, the calculated ECC bytes is set.
5992 */
5993int nand_check_ecc_caps(struct nand_chip *chip,
5994 const struct nand_ecc_caps *caps, int oobavail)
5995{
5996 struct mtd_info *mtd = nand_to_mtd(chip);
5997 const struct nand_ecc_step_info *stepinfo;
5998 int preset_step = chip->ecc.size;
5999 int preset_strength = chip->ecc.strength;
6000 int nsteps, ecc_bytes;
6001 int i, j;
6002
6003 if (WARN_ON(oobavail < 0))
6004 return -EINVAL;
6005
6006 if (!preset_step || !preset_strength)
6007 return -ENODATA;
6008
6009 nsteps = mtd->writesize / preset_step;
6010
6011 for (i = 0; i < caps->nstepinfos; i++) {
6012 stepinfo = &caps->stepinfos[i];
6013
6014 if (stepinfo->stepsize != preset_step)
6015 continue;
6016
6017 for (j = 0; j < stepinfo->nstrengths; j++) {
6018 if (stepinfo->strengths[j] != preset_strength)
6019 continue;
6020
6021 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6022 preset_strength);
6023 if (WARN_ON_ONCE(ecc_bytes < 0))
6024 return ecc_bytes;
6025
6026 if (ecc_bytes * nsteps > oobavail) {
6027 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6028 preset_step, preset_strength);
6029 return -ENOSPC;
6030 }
6031
6032 chip->ecc.bytes = ecc_bytes;
6033
6034 return 0;
6035 }
6036 }
6037
6038 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6039 preset_step, preset_strength);
6040
6041 return -ENOTSUPP;
6042}
6043EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
6044
6045/**
6046 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6047 * @chip: nand chip info structure
6048 * @caps: ECC engine caps info structure
6049 * @oobavail: OOB size that the ECC engine can use
6050 *
6051 * If a chip's ECC requirement is provided, try to meet it with the least
6052 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6053 * On success, the chosen ECC settings are set.
6054 */
6055int nand_match_ecc_req(struct nand_chip *chip,
6056 const struct nand_ecc_caps *caps, int oobavail)
6057{
6058 struct mtd_info *mtd = nand_to_mtd(chip);
6059 const struct nand_ecc_step_info *stepinfo;
6060 int req_step = chip->ecc_step_ds;
6061 int req_strength = chip->ecc_strength_ds;
6062 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6063 int best_step, best_strength, best_ecc_bytes;
6064 int best_ecc_bytes_total = INT_MAX;
6065 int i, j;
6066
6067 if (WARN_ON(oobavail < 0))
6068 return -EINVAL;
6069
6070 /* No information provided by the NAND chip */
6071 if (!req_step || !req_strength)
6072 return -ENOTSUPP;
6073
6074 /* number of correctable bits the chip requires in a page */
6075 req_corr = mtd->writesize / req_step * req_strength;
6076
6077 for (i = 0; i < caps->nstepinfos; i++) {
6078 stepinfo = &caps->stepinfos[i];
6079 step_size = stepinfo->stepsize;
6080
6081 for (j = 0; j < stepinfo->nstrengths; j++) {
6082 strength = stepinfo->strengths[j];
6083
6084 /*
6085 * If both step size and strength are smaller than the
6086 * chip's requirement, it is not easy to compare the
6087 * resulted reliability.
6088 */
6089 if (step_size < req_step && strength < req_strength)
6090 continue;
6091
6092 if (mtd->writesize % step_size)
6093 continue;
6094
6095 nsteps = mtd->writesize / step_size;
6096
6097 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6098 if (WARN_ON_ONCE(ecc_bytes < 0))
6099 continue;
6100 ecc_bytes_total = ecc_bytes * nsteps;
6101
6102 if (ecc_bytes_total > oobavail ||
6103 strength * nsteps < req_corr)
6104 continue;
6105
6106 /*
6107 * We assume the best is to meet the chip's requrement
6108 * with the least number of ECC bytes.
6109 */
6110 if (ecc_bytes_total < best_ecc_bytes_total) {
6111 best_ecc_bytes_total = ecc_bytes_total;
6112 best_step = step_size;
6113 best_strength = strength;
6114 best_ecc_bytes = ecc_bytes;
6115 }
6116 }
6117 }
6118
6119 if (best_ecc_bytes_total == INT_MAX)
6120 return -ENOTSUPP;
6121
6122 chip->ecc.size = best_step;
6123 chip->ecc.strength = best_strength;
6124 chip->ecc.bytes = best_ecc_bytes;
6125
6126 return 0;
6127}
6128EXPORT_SYMBOL_GPL(nand_match_ecc_req);
6129
6130/**
6131 * nand_maximize_ecc - choose the max ECC strength available
6132 * @chip: nand chip info structure
6133 * @caps: ECC engine caps info structure
6134 * @oobavail: OOB size that the ECC engine can use
6135 *
6136 * Choose the max ECC strength that is supported on the controller, and can fit
6137 * within the chip's OOB. On success, the chosen ECC settings are set.
6138 */
6139int nand_maximize_ecc(struct nand_chip *chip,
6140 const struct nand_ecc_caps *caps, int oobavail)
6141{
6142 struct mtd_info *mtd = nand_to_mtd(chip);
6143 const struct nand_ecc_step_info *stepinfo;
6144 int step_size, strength, nsteps, ecc_bytes, corr;
6145 int best_corr = 0;
6146 int best_step = 0;
6147 int best_strength, best_ecc_bytes;
6148 int i, j;
6149
6150 if (WARN_ON(oobavail < 0))
6151 return -EINVAL;
6152
6153 for (i = 0; i < caps->nstepinfos; i++) {
6154 stepinfo = &caps->stepinfos[i];
6155 step_size = stepinfo->stepsize;
6156
6157 /* If chip->ecc.size is already set, respect it */
6158 if (chip->ecc.size && step_size != chip->ecc.size)
6159 continue;
6160
6161 for (j = 0; j < stepinfo->nstrengths; j++) {
6162 strength = stepinfo->strengths[j];
6163
6164 if (mtd->writesize % step_size)
6165 continue;
6166
6167 nsteps = mtd->writesize / step_size;
6168
6169 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6170 if (WARN_ON_ONCE(ecc_bytes < 0))
6171 continue;
6172
6173 if (ecc_bytes * nsteps > oobavail)
6174 continue;
6175
6176 corr = strength * nsteps;
6177
6178 /*
6179 * If the number of correctable bits is the same,
6180 * bigger step_size has more reliability.
6181 */
6182 if (corr > best_corr ||
6183 (corr == best_corr && step_size > best_step)) {
6184 best_corr = corr;
6185 best_step = step_size;
6186 best_strength = strength;
6187 best_ecc_bytes = ecc_bytes;
6188 }
6189 }
6190 }
6191
6192 if (!best_corr)
6193 return -ENOTSUPP;
6194
6195 chip->ecc.size = best_step;
6196 chip->ecc.strength = best_strength;
6197 chip->ecc.bytes = best_ecc_bytes;
6198
6199 return 0;
6200}
6201EXPORT_SYMBOL_GPL(nand_maximize_ecc);
6202
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006203/*
6204 * Check if the chip configuration meet the datasheet requirements.
6205
6206 * If our configuration corrects A bits per B bytes and the minimum
6207 * required correction level is X bits per Y bytes, then we must ensure
6208 * both of the following are true:
6209 *
6210 * (1) A / B >= X / Y
6211 * (2) A >= X
6212 *
6213 * Requirement (1) ensures we can correct for the required bitflip density.
6214 * Requirement (2) ensures we can correct even when all bitflips are clumped
6215 * in the same sector.
6216 */
6217static bool nand_ecc_strength_good(struct mtd_info *mtd)
6218{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006219 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006220 struct nand_ecc_ctrl *ecc = &chip->ecc;
6221 int corr, ds_corr;
6222
6223 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6224 /* Not enough information */
6225 return true;
6226
6227 /*
6228 * We get the number of corrected bits per page to compare
6229 * the correction density.
6230 */
6231 corr = (mtd->writesize * ecc->strength) / ecc->size;
6232 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6233
6234 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6235}
David Woodhouse3b85c322006-09-25 17:06:53 +01006236
6237/**
6238 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006239 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01006240 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006241 * This is the second phase of the normal nand_scan() function. It fills out
6242 * all the uninitialized function pointers with the defaults and scans for a
6243 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006244 */
6245int nand_scan_tail(struct mtd_info *mtd)
6246{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006247 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08006248 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006249 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006250
Brian Norrise2414f42012-02-06 13:44:00 -08006251 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006252 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006253 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006254 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006255 }
Brian Norrise2414f42012-02-06 13:44:00 -08006256
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006257 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006258 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006259 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006260
Boris Brezillonf84674b2017-06-02 12:18:24 +02006261 /*
6262 * FIXME: some NAND manufacturer drivers expect the first die to be
6263 * selected when manufacturer->init() is called. They should be fixed
6264 * to explictly select the relevant die when interacting with the NAND
6265 * chip.
6266 */
6267 chip->select_chip(mtd, 0);
6268 ret = nand_manufacturer_init(chip);
6269 chip->select_chip(mtd, -1);
6270 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006271 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006272
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006273 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006274 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006275
6276 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006277 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006278 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006279 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006280 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006281 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006284 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 break;
6286 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006287 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006288 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006289 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006291 /*
6292 * Expose the whole OOB area to users if ECC_NONE
6293 * is passed. We could do that for all kind of
6294 * ->oobsize, but we must keep the old large/small
6295 * page with ECC layout when ->oobsize <= 128 for
6296 * compatibility reasons.
6297 */
6298 if (ecc->mode == NAND_ECC_NONE) {
6299 mtd_set_ooblayout(mtd,
6300 &nand_ooblayout_lp_ops);
6301 break;
6302 }
6303
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006304 WARN(1, "No oob scheme defined for oobsize %d\n",
6305 mtd->oobsize);
6306 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006307 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 }
6309 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006310
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006311 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006312 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006313 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006314 */
David Woodhouse956e9442006-09-25 17:12:39 +01006315
Huang Shijie97de79e02013-10-18 14:20:53 +08006316 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006317 case NAND_ECC_HW_OOB_FIRST:
6318 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006319 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006320 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6321 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006322 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006323 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006324 if (!ecc->read_page)
6325 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006326
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006327 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006328 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006329 if (!ecc->read_page)
6330 ecc->read_page = nand_read_page_hwecc;
6331 if (!ecc->write_page)
6332 ecc->write_page = nand_write_page_hwecc;
6333 if (!ecc->read_page_raw)
6334 ecc->read_page_raw = nand_read_page_raw;
6335 if (!ecc->write_page_raw)
6336 ecc->write_page_raw = nand_write_page_raw;
6337 if (!ecc->read_oob)
6338 ecc->read_oob = nand_read_oob_std;
6339 if (!ecc->write_oob)
6340 ecc->write_oob = nand_write_oob_std;
6341 if (!ecc->read_subpage)
6342 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006343 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006344 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006345
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006346 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006347 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6348 (!ecc->read_page ||
6349 ecc->read_page == nand_read_page_hwecc ||
6350 !ecc->write_page ||
6351 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006352 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6353 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006354 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006355 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006356 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006357 if (!ecc->read_page)
6358 ecc->read_page = nand_read_page_syndrome;
6359 if (!ecc->write_page)
6360 ecc->write_page = nand_write_page_syndrome;
6361 if (!ecc->read_page_raw)
6362 ecc->read_page_raw = nand_read_page_raw_syndrome;
6363 if (!ecc->write_page_raw)
6364 ecc->write_page_raw = nand_write_page_raw_syndrome;
6365 if (!ecc->read_oob)
6366 ecc->read_oob = nand_read_oob_syndrome;
6367 if (!ecc->write_oob)
6368 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006369
Huang Shijie97de79e02013-10-18 14:20:53 +08006370 if (mtd->writesize >= ecc->size) {
6371 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006372 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6373 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006374 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006375 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006376 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006377 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006378 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6379 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006380 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006381 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006383 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006384 ret = nand_set_ecc_soft_ops(mtd);
6385 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006386 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006387 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006388 }
6389 break;
6390
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006391 case NAND_ECC_ON_DIE:
6392 if (!ecc->read_page || !ecc->write_page) {
6393 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6394 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006395 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006396 }
6397 if (!ecc->read_oob)
6398 ecc->read_oob = nand_read_oob_std;
6399 if (!ecc->write_oob)
6400 ecc->write_oob = nand_write_oob_std;
6401 break;
6402
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006403 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006404 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006405 ecc->read_page = nand_read_page_raw;
6406 ecc->write_page = nand_write_page_raw;
6407 ecc->read_oob = nand_read_oob_std;
6408 ecc->read_page_raw = nand_read_page_raw;
6409 ecc->write_page_raw = nand_write_page_raw;
6410 ecc->write_oob = nand_write_oob_std;
6411 ecc->size = mtd->writesize;
6412 ecc->bytes = 0;
6413 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006415
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006417 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6418 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006419 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006422 if (ecc->correct || ecc->calculate) {
6423 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6424 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6425 if (!ecc->calc_buf || !ecc->code_buf) {
6426 ret = -ENOMEM;
6427 goto err_nand_manuf_cleanup;
6428 }
6429 }
6430
Brian Norris9ce244b2011-08-30 18:45:37 -07006431 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006432 if (!ecc->read_oob_raw)
6433 ecc->read_oob_raw = ecc->read_oob;
6434 if (!ecc->write_oob_raw)
6435 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006436
Boris Brezillon846031d2016-02-03 20:11:00 +01006437 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006438 mtd->ecc_strength = ecc->strength;
6439 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006440
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006441 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006442 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006443 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006444 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006445 ecc->steps = mtd->writesize / ecc->size;
6446 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006447 WARN(1, "Invalid ECC parameters\n");
6448 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006449 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006451 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006452 if (ecc->total > mtd->oobsize) {
6453 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6454 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006455 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006456 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006457
Boris Brezillon846031d2016-02-03 20:11:00 +01006458 /*
6459 * The number of bytes available for a client to place data into
6460 * the out of band area.
6461 */
6462 ret = mtd_ooblayout_count_freebytes(mtd);
6463 if (ret < 0)
6464 ret = 0;
6465
6466 mtd->oobavail = ret;
6467
6468 /* ECC sanity check: warn if it's too weak */
6469 if (!nand_ecc_strength_good(mtd))
6470 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6471 mtd->name);
6472
Brian Norris8b6e50c2011-05-25 14:59:01 -07006473 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006474 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006475 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006476 case 2:
6477 mtd->subpage_sft = 1;
6478 break;
6479 case 4:
6480 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006481 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006482 mtd->subpage_sft = 2;
6483 break;
6484 }
6485 }
6486 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6487
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006488 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006489 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006492 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006494 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306495 switch (ecc->mode) {
6496 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306497 if (chip->page_shift > 9)
6498 chip->options |= NAND_SUBPAGE_READ;
6499 break;
6500
6501 default:
6502 break;
6503 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006504
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006506 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006507 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6508 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006509 mtd->_erase = nand_erase;
6510 mtd->_point = NULL;
6511 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006512 mtd->_panic_write = panic_nand_write;
6513 mtd->_read_oob = nand_read_oob;
6514 mtd->_write_oob = nand_write_oob;
6515 mtd->_sync = nand_sync;
6516 mtd->_lock = NULL;
6517 mtd->_unlock = NULL;
6518 mtd->_suspend = nand_suspend;
6519 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006520 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006521 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006522 mtd->_block_isbad = nand_block_isbad;
6523 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006524 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006525 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006527 /*
6528 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6529 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6530 * properly set.
6531 */
6532 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006533 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534
Boris Brezillonf84674b2017-06-02 12:18:24 +02006535 /* Initialize the ->data_interface field. */
6536 ret = nand_init_data_interface(chip);
6537 if (ret)
6538 goto err_nand_manuf_cleanup;
6539
6540 /* Enter fastest possible mode on all dies. */
6541 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006542 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006543 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006544 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006545 }
6546
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006547 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006548 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550
6551 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07006552 ret = chip->scan_bbt(mtd);
6553 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006554 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006555
Brian Norris44d41822017-05-01 17:04:50 -07006556 return 0;
6557
Boris Brezillonf84674b2017-06-02 12:18:24 +02006558
6559err_nand_manuf_cleanup:
6560 nand_manufacturer_cleanup(chip);
6561
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006562err_free_buf:
6563 kfree(chip->data_buf);
6564 kfree(ecc->code_buf);
6565 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006566
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006567 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006569EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570
Brian Norris8b6e50c2011-05-25 14:59:01 -07006571/*
6572 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006573 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07006574 * to call us from in-kernel code if the core NAND support is modular.
6575 */
David Woodhouse3b85c322006-09-25 17:06:53 +01006576#ifdef MODULE
6577#define caller_is_module() (1)
6578#else
6579#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06006580 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01006581#endif
6582
6583/**
6584 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006585 * @mtd: MTD device structure
6586 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01006587 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006588 * This fills out all the uninitialized function pointers with the defaults.
6589 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006590 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006591 */
6592int nand_scan(struct mtd_info *mtd, int maxchips)
6593{
6594 int ret;
6595
David Woodhouse5e81e882010-02-26 18:32:56 +00006596 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01006597 if (!ret)
6598 ret = nand_scan_tail(mtd);
6599 return ret;
6600}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006601EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01006602
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006604 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6605 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006606 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006607void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006609 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006610 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006611 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6612
Jesper Juhlfa671642005-11-07 01:01:27 -08006613 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006614 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006615 kfree(chip->data_buf);
6616 kfree(chip->ecc.code_buf);
6617 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006618
6619 /* Free bad block descriptor memory */
6620 if (chip->badblock_pattern && chip->badblock_pattern->options
6621 & NAND_BBT_DYNAMICSTRUCT)
6622 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006623
6624 /* Free manufacturer priv data. */
6625 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006627EXPORT_SYMBOL_GPL(nand_cleanup);
6628
6629/**
6630 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6631 * held by the NAND device
6632 * @mtd: MTD device structure
6633 */
6634void nand_release(struct mtd_info *mtd)
6635{
6636 mtd_device_unregister(mtd);
6637 nand_cleanup(mtd_to_nand(mtd));
6638}
David Woodhousee0c7d762006-05-13 18:07:53 +01006639EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006640
David Woodhousee0c7d762006-05-13 18:07:53 +01006641MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006642MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6643MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006644MODULE_DESCRIPTION("Generic NAND flash driver code");