blob: d344dcb1262023ecb5686daf013b6bd9195e1e5f [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 Raynal97baea12018-03-19 14:47:20 +01001275 /* Change the mode on the chip side */
1276 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1277 tmode_param);
1278 if (ret)
1279 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001280
Miquel Raynal97baea12018-03-19 14:47:20 +01001281 /* Change the mode on the controller side */
1282 return chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001283}
1284
1285/**
1286 * nand_init_data_interface - find the best data interface and timings
1287 * @chip: The NAND chip
1288 *
1289 * Find the best data interface and NAND timings supported by the chip
1290 * and the driver.
1291 * First tries to retrieve supported timing modes from ONFI information,
1292 * and if the NAND chip does not support ONFI, relies on the
1293 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1294 * function nand_chip->data_interface is initialized with the best timing mode
1295 * available.
1296 *
1297 * Returns 0 for success or negative error code otherwise.
1298 */
1299static int nand_init_data_interface(struct nand_chip *chip)
1300{
1301 struct mtd_info *mtd = nand_to_mtd(chip);
1302 int modes, mode, ret;
1303
1304 if (!chip->setup_data_interface)
1305 return 0;
1306
1307 /*
1308 * First try to identify the best timings from ONFI parameters and
1309 * if the NAND does not support ONFI, fallback to the default ONFI
1310 * timing mode.
1311 */
1312 modes = onfi_get_async_timing_mode(chip);
1313 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1314 if (!chip->onfi_timing_mode_default)
1315 return 0;
1316
1317 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1318 }
1319
Boris Brezillond8e725d2016-09-15 10:32:50 +02001320
1321 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001322 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001323 if (ret)
1324 continue;
1325
Miquel Raynald787b8b2017-12-22 18:12:41 +01001326 /*
1327 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1328 * controller supports the requested timings.
1329 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001330 ret = chip->setup_data_interface(mtd,
1331 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001332 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001333 if (!ret) {
1334 chip->onfi_timing_mode_default = mode;
1335 break;
1336 }
1337 }
1338
1339 return 0;
1340}
1341
Boris Brezillond8e725d2016-09-15 10:32:50 +02001342/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001343 * nand_fill_column_cycles - fill the column cycles of an address
1344 * @chip: The NAND chip
1345 * @addrs: Array of address cycles to fill
1346 * @offset_in_page: The offset in the page
1347 *
1348 * Fills the first or the first two bytes of the @addrs field depending
1349 * on the NAND bus width and the page size.
1350 *
1351 * Returns the number of cycles needed to encode the column, or a negative
1352 * error code in case one of the arguments is invalid.
1353 */
1354static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1355 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Miquel Raynal8878b122017-11-09 14:16:45 +01001357 struct mtd_info *mtd = nand_to_mtd(chip);
1358
1359 /* Make sure the offset is less than the actual page size. */
1360 if (offset_in_page > mtd->writesize + mtd->oobsize)
1361 return -EINVAL;
1362
1363 /*
1364 * On small page NANDs, there's a dedicated command to access the OOB
1365 * area, and the column address is relative to the start of the OOB
1366 * area, not the start of the page. Asjust the address accordingly.
1367 */
1368 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1369 offset_in_page -= mtd->writesize;
1370
1371 /*
1372 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1373 * wide, then it must be divided by 2.
1374 */
1375 if (chip->options & NAND_BUSWIDTH_16) {
1376 if (WARN_ON(offset_in_page % 2))
1377 return -EINVAL;
1378
1379 offset_in_page /= 2;
1380 }
1381
1382 addrs[0] = offset_in_page;
1383
1384 /*
1385 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1386 * need 2
1387 */
1388 if (mtd->writesize <= 512)
1389 return 1;
1390
1391 addrs[1] = offset_in_page >> 8;
1392
1393 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Miquel Raynal8878b122017-11-09 14:16:45 +01001396static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1397 unsigned int offset_in_page, void *buf,
1398 unsigned int len)
1399{
1400 struct mtd_info *mtd = nand_to_mtd(chip);
1401 const struct nand_sdr_timings *sdr =
1402 nand_get_sdr_timings(&chip->data_interface);
1403 u8 addrs[4];
1404 struct nand_op_instr instrs[] = {
1405 NAND_OP_CMD(NAND_CMD_READ0, 0),
1406 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1407 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1408 PSEC_TO_NSEC(sdr->tRR_min)),
1409 NAND_OP_DATA_IN(len, buf, 0),
1410 };
1411 struct nand_operation op = NAND_OPERATION(instrs);
1412 int ret;
1413
1414 /* Drop the DATA_IN instruction if len is set to 0. */
1415 if (!len)
1416 op.ninstrs--;
1417
1418 if (offset_in_page >= mtd->writesize)
1419 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1420 else if (offset_in_page >= 256 &&
1421 !(chip->options & NAND_BUSWIDTH_16))
1422 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1423
1424 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1425 if (ret < 0)
1426 return ret;
1427
1428 addrs[1] = page;
1429 addrs[2] = page >> 8;
1430
1431 if (chip->options & NAND_ROW_ADDR_3) {
1432 addrs[3] = page >> 16;
1433 instrs[1].ctx.addr.naddrs++;
1434 }
1435
1436 return nand_exec_op(chip, &op);
1437}
1438
1439static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1440 unsigned int offset_in_page, void *buf,
1441 unsigned int len)
1442{
1443 const struct nand_sdr_timings *sdr =
1444 nand_get_sdr_timings(&chip->data_interface);
1445 u8 addrs[5];
1446 struct nand_op_instr instrs[] = {
1447 NAND_OP_CMD(NAND_CMD_READ0, 0),
1448 NAND_OP_ADDR(4, addrs, 0),
1449 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1450 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1451 PSEC_TO_NSEC(sdr->tRR_min)),
1452 NAND_OP_DATA_IN(len, buf, 0),
1453 };
1454 struct nand_operation op = NAND_OPERATION(instrs);
1455 int ret;
1456
1457 /* Drop the DATA_IN instruction if len is set to 0. */
1458 if (!len)
1459 op.ninstrs--;
1460
1461 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1462 if (ret < 0)
1463 return ret;
1464
1465 addrs[2] = page;
1466 addrs[3] = page >> 8;
1467
1468 if (chip->options & NAND_ROW_ADDR_3) {
1469 addrs[4] = page >> 16;
1470 instrs[1].ctx.addr.naddrs++;
1471 }
1472
1473 return nand_exec_op(chip, &op);
1474}
1475
1476/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001477 * nand_read_page_op - Do a READ PAGE operation
1478 * @chip: The NAND chip
1479 * @page: page to read
1480 * @offset_in_page: offset within the page
1481 * @buf: buffer used to store the data
1482 * @len: length of the buffer
1483 *
1484 * This function issues a READ PAGE operation.
1485 * This function does not select/unselect the CS line.
1486 *
1487 * Returns 0 on success, a negative error code otherwise.
1488 */
1489int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1490 unsigned int offset_in_page, void *buf, unsigned int len)
1491{
1492 struct mtd_info *mtd = nand_to_mtd(chip);
1493
1494 if (len && !buf)
1495 return -EINVAL;
1496
1497 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1498 return -EINVAL;
1499
Miquel Raynal8878b122017-11-09 14:16:45 +01001500 if (chip->exec_op) {
1501 if (mtd->writesize > 512)
1502 return nand_lp_exec_read_page_op(chip, page,
1503 offset_in_page, buf,
1504 len);
1505
1506 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1507 buf, len);
1508 }
1509
Boris Brezillon97d90da2017-11-30 18:01:29 +01001510 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1511 if (len)
1512 chip->read_buf(mtd, buf, len);
1513
1514 return 0;
1515}
1516EXPORT_SYMBOL_GPL(nand_read_page_op);
1517
1518/**
1519 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1520 * @chip: The NAND chip
1521 * @page: parameter page to read
1522 * @buf: buffer used to store the data
1523 * @len: length of the buffer
1524 *
1525 * This function issues a READ PARAMETER PAGE operation.
1526 * This function does not select/unselect the CS line.
1527 *
1528 * Returns 0 on success, a negative error code otherwise.
1529 */
1530static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1531 unsigned int len)
1532{
1533 struct mtd_info *mtd = nand_to_mtd(chip);
1534 unsigned int i;
1535 u8 *p = buf;
1536
1537 if (len && !buf)
1538 return -EINVAL;
1539
Miquel Raynal8878b122017-11-09 14:16:45 +01001540 if (chip->exec_op) {
1541 const struct nand_sdr_timings *sdr =
1542 nand_get_sdr_timings(&chip->data_interface);
1543 struct nand_op_instr instrs[] = {
1544 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1545 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1546 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1547 PSEC_TO_NSEC(sdr->tRR_min)),
1548 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1549 };
1550 struct nand_operation op = NAND_OPERATION(instrs);
1551
1552 /* Drop the DATA_IN instruction if len is set to 0. */
1553 if (!len)
1554 op.ninstrs--;
1555
1556 return nand_exec_op(chip, &op);
1557 }
1558
Boris Brezillon97d90da2017-11-30 18:01:29 +01001559 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1560 for (i = 0; i < len; i++)
1561 p[i] = chip->read_byte(mtd);
1562
1563 return 0;
1564}
1565
1566/**
1567 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1568 * @chip: The NAND chip
1569 * @offset_in_page: offset within the page
1570 * @buf: buffer used to store the data
1571 * @len: length of the buffer
1572 * @force_8bit: force 8-bit bus access
1573 *
1574 * This function issues a CHANGE READ COLUMN operation.
1575 * This function does not select/unselect the CS line.
1576 *
1577 * Returns 0 on success, a negative error code otherwise.
1578 */
1579int nand_change_read_column_op(struct nand_chip *chip,
1580 unsigned int offset_in_page, void *buf,
1581 unsigned int len, bool force_8bit)
1582{
1583 struct mtd_info *mtd = nand_to_mtd(chip);
1584
1585 if (len && !buf)
1586 return -EINVAL;
1587
1588 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1589 return -EINVAL;
1590
Miquel Raynal8878b122017-11-09 14:16:45 +01001591 /* Small page NANDs do not support column change. */
1592 if (mtd->writesize <= 512)
1593 return -ENOTSUPP;
1594
1595 if (chip->exec_op) {
1596 const struct nand_sdr_timings *sdr =
1597 nand_get_sdr_timings(&chip->data_interface);
1598 u8 addrs[2] = {};
1599 struct nand_op_instr instrs[] = {
1600 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1601 NAND_OP_ADDR(2, addrs, 0),
1602 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1603 PSEC_TO_NSEC(sdr->tCCS_min)),
1604 NAND_OP_DATA_IN(len, buf, 0),
1605 };
1606 struct nand_operation op = NAND_OPERATION(instrs);
1607 int ret;
1608
1609 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1610 if (ret < 0)
1611 return ret;
1612
1613 /* Drop the DATA_IN instruction if len is set to 0. */
1614 if (!len)
1615 op.ninstrs--;
1616
1617 instrs[3].ctx.data.force_8bit = force_8bit;
1618
1619 return nand_exec_op(chip, &op);
1620 }
1621
Boris Brezillon97d90da2017-11-30 18:01:29 +01001622 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1623 if (len)
1624 chip->read_buf(mtd, buf, len);
1625
1626 return 0;
1627}
1628EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1629
1630/**
1631 * nand_read_oob_op - Do a READ OOB operation
1632 * @chip: The NAND chip
1633 * @page: page to read
1634 * @offset_in_oob: offset within the OOB area
1635 * @buf: buffer used to store the data
1636 * @len: length of the buffer
1637 *
1638 * This function issues a READ OOB operation.
1639 * This function does not select/unselect the CS line.
1640 *
1641 * Returns 0 on success, a negative error code otherwise.
1642 */
1643int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1644 unsigned int offset_in_oob, void *buf, unsigned int len)
1645{
1646 struct mtd_info *mtd = nand_to_mtd(chip);
1647
1648 if (len && !buf)
1649 return -EINVAL;
1650
1651 if (offset_in_oob + len > mtd->oobsize)
1652 return -EINVAL;
1653
Miquel Raynal8878b122017-11-09 14:16:45 +01001654 if (chip->exec_op)
1655 return nand_read_page_op(chip, page,
1656 mtd->writesize + offset_in_oob,
1657 buf, len);
1658
Boris Brezillon97d90da2017-11-30 18:01:29 +01001659 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1660 if (len)
1661 chip->read_buf(mtd, buf, len);
1662
1663 return 0;
1664}
1665EXPORT_SYMBOL_GPL(nand_read_oob_op);
1666
Miquel Raynal8878b122017-11-09 14:16:45 +01001667static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1668 unsigned int offset_in_page, const void *buf,
1669 unsigned int len, bool prog)
1670{
1671 struct mtd_info *mtd = nand_to_mtd(chip);
1672 const struct nand_sdr_timings *sdr =
1673 nand_get_sdr_timings(&chip->data_interface);
1674 u8 addrs[5] = {};
1675 struct nand_op_instr instrs[] = {
1676 /*
1677 * The first instruction will be dropped if we're dealing
1678 * with a large page NAND and adjusted if we're dealing
1679 * with a small page NAND and the page offset is > 255.
1680 */
1681 NAND_OP_CMD(NAND_CMD_READ0, 0),
1682 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1683 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1684 NAND_OP_DATA_OUT(len, buf, 0),
1685 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1686 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1687 };
1688 struct nand_operation op = NAND_OPERATION(instrs);
1689 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1690 int ret;
1691 u8 status;
1692
1693 if (naddrs < 0)
1694 return naddrs;
1695
1696 addrs[naddrs++] = page;
1697 addrs[naddrs++] = page >> 8;
1698 if (chip->options & NAND_ROW_ADDR_3)
1699 addrs[naddrs++] = page >> 16;
1700
1701 instrs[2].ctx.addr.naddrs = naddrs;
1702
1703 /* Drop the last two instructions if we're not programming the page. */
1704 if (!prog) {
1705 op.ninstrs -= 2;
1706 /* Also drop the DATA_OUT instruction if empty. */
1707 if (!len)
1708 op.ninstrs--;
1709 }
1710
1711 if (mtd->writesize <= 512) {
1712 /*
1713 * Small pages need some more tweaking: we have to adjust the
1714 * first instruction depending on the page offset we're trying
1715 * to access.
1716 */
1717 if (offset_in_page >= mtd->writesize)
1718 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1719 else if (offset_in_page >= 256 &&
1720 !(chip->options & NAND_BUSWIDTH_16))
1721 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1722 } else {
1723 /*
1724 * Drop the first command if we're dealing with a large page
1725 * NAND.
1726 */
1727 op.instrs++;
1728 op.ninstrs--;
1729 }
1730
1731 ret = nand_exec_op(chip, &op);
1732 if (!prog || ret)
1733 return ret;
1734
1735 ret = nand_status_op(chip, &status);
1736 if (ret)
1737 return ret;
1738
1739 return status;
1740}
1741
Boris Brezillon97d90da2017-11-30 18:01:29 +01001742/**
1743 * nand_prog_page_begin_op - starts a PROG PAGE operation
1744 * @chip: The NAND chip
1745 * @page: page to write
1746 * @offset_in_page: offset within the page
1747 * @buf: buffer containing the data to write to the page
1748 * @len: length of the buffer
1749 *
1750 * This function issues the first half of a PROG PAGE operation.
1751 * This function does not select/unselect the CS line.
1752 *
1753 * Returns 0 on success, a negative error code otherwise.
1754 */
1755int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1756 unsigned int offset_in_page, const void *buf,
1757 unsigned int len)
1758{
1759 struct mtd_info *mtd = nand_to_mtd(chip);
1760
1761 if (len && !buf)
1762 return -EINVAL;
1763
1764 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1765 return -EINVAL;
1766
Miquel Raynal8878b122017-11-09 14:16:45 +01001767 if (chip->exec_op)
1768 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1769 len, false);
1770
Boris Brezillon97d90da2017-11-30 18:01:29 +01001771 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1772
1773 if (buf)
1774 chip->write_buf(mtd, buf, len);
1775
1776 return 0;
1777}
1778EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1779
1780/**
1781 * nand_prog_page_end_op - ends a PROG PAGE operation
1782 * @chip: The NAND chip
1783 *
1784 * This function issues the second half of a PROG PAGE operation.
1785 * This function does not select/unselect the CS line.
1786 *
1787 * Returns 0 on success, a negative error code otherwise.
1788 */
1789int nand_prog_page_end_op(struct nand_chip *chip)
1790{
1791 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001792 int ret;
1793 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001794
Miquel Raynal8878b122017-11-09 14:16:45 +01001795 if (chip->exec_op) {
1796 const struct nand_sdr_timings *sdr =
1797 nand_get_sdr_timings(&chip->data_interface);
1798 struct nand_op_instr instrs[] = {
1799 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1800 PSEC_TO_NSEC(sdr->tWB_max)),
1801 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1802 };
1803 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001804
Miquel Raynal8878b122017-11-09 14:16:45 +01001805 ret = nand_exec_op(chip, &op);
1806 if (ret)
1807 return ret;
1808
1809 ret = nand_status_op(chip, &status);
1810 if (ret)
1811 return ret;
1812 } else {
1813 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1814 ret = chip->waitfunc(mtd, chip);
1815 if (ret < 0)
1816 return ret;
1817
1818 status = ret;
1819 }
1820
Boris Brezillon97d90da2017-11-30 18:01:29 +01001821 if (status & NAND_STATUS_FAIL)
1822 return -EIO;
1823
1824 return 0;
1825}
1826EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1827
1828/**
1829 * nand_prog_page_op - Do a full PROG PAGE operation
1830 * @chip: The NAND chip
1831 * @page: page to write
1832 * @offset_in_page: offset within the page
1833 * @buf: buffer containing the data to write to the page
1834 * @len: length of the buffer
1835 *
1836 * This function issues a full PROG PAGE operation.
1837 * This function does not select/unselect the CS line.
1838 *
1839 * Returns 0 on success, a negative error code otherwise.
1840 */
1841int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1842 unsigned int offset_in_page, const void *buf,
1843 unsigned int len)
1844{
1845 struct mtd_info *mtd = nand_to_mtd(chip);
1846 int status;
1847
1848 if (!len || !buf)
1849 return -EINVAL;
1850
1851 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1852 return -EINVAL;
1853
Miquel Raynal8878b122017-11-09 14:16:45 +01001854 if (chip->exec_op) {
1855 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1856 len, true);
1857 } else {
1858 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1859 chip->write_buf(mtd, buf, len);
1860 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1861 status = chip->waitfunc(mtd, chip);
1862 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001863
Boris Brezillon97d90da2017-11-30 18:01:29 +01001864 if (status & NAND_STATUS_FAIL)
1865 return -EIO;
1866
1867 return 0;
1868}
1869EXPORT_SYMBOL_GPL(nand_prog_page_op);
1870
1871/**
1872 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1873 * @chip: The NAND chip
1874 * @offset_in_page: offset within the page
1875 * @buf: buffer containing the data to send to the NAND
1876 * @len: length of the buffer
1877 * @force_8bit: force 8-bit bus access
1878 *
1879 * This function issues a CHANGE WRITE COLUMN operation.
1880 * This function does not select/unselect the CS line.
1881 *
1882 * Returns 0 on success, a negative error code otherwise.
1883 */
1884int nand_change_write_column_op(struct nand_chip *chip,
1885 unsigned int offset_in_page,
1886 const void *buf, unsigned int len,
1887 bool force_8bit)
1888{
1889 struct mtd_info *mtd = nand_to_mtd(chip);
1890
1891 if (len && !buf)
1892 return -EINVAL;
1893
1894 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1895 return -EINVAL;
1896
Miquel Raynal8878b122017-11-09 14:16:45 +01001897 /* Small page NANDs do not support column change. */
1898 if (mtd->writesize <= 512)
1899 return -ENOTSUPP;
1900
1901 if (chip->exec_op) {
1902 const struct nand_sdr_timings *sdr =
1903 nand_get_sdr_timings(&chip->data_interface);
1904 u8 addrs[2];
1905 struct nand_op_instr instrs[] = {
1906 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1907 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1908 NAND_OP_DATA_OUT(len, buf, 0),
1909 };
1910 struct nand_operation op = NAND_OPERATION(instrs);
1911 int ret;
1912
1913 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1914 if (ret < 0)
1915 return ret;
1916
1917 instrs[2].ctx.data.force_8bit = force_8bit;
1918
1919 /* Drop the DATA_OUT instruction if len is set to 0. */
1920 if (!len)
1921 op.ninstrs--;
1922
1923 return nand_exec_op(chip, &op);
1924 }
1925
Boris Brezillon97d90da2017-11-30 18:01:29 +01001926 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1927 if (len)
1928 chip->write_buf(mtd, buf, len);
1929
1930 return 0;
1931}
1932EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1933
1934/**
1935 * nand_readid_op - Do a READID operation
1936 * @chip: The NAND chip
1937 * @addr: address cycle to pass after the READID command
1938 * @buf: buffer used to store the ID
1939 * @len: length of the buffer
1940 *
1941 * This function sends a READID command and reads back the ID returned by the
1942 * NAND.
1943 * This function does not select/unselect the CS line.
1944 *
1945 * Returns 0 on success, a negative error code otherwise.
1946 */
1947int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1948 unsigned int len)
1949{
1950 struct mtd_info *mtd = nand_to_mtd(chip);
1951 unsigned int i;
1952 u8 *id = buf;
1953
1954 if (len && !buf)
1955 return -EINVAL;
1956
Miquel Raynal8878b122017-11-09 14:16:45 +01001957 if (chip->exec_op) {
1958 const struct nand_sdr_timings *sdr =
1959 nand_get_sdr_timings(&chip->data_interface);
1960 struct nand_op_instr instrs[] = {
1961 NAND_OP_CMD(NAND_CMD_READID, 0),
1962 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1963 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1964 };
1965 struct nand_operation op = NAND_OPERATION(instrs);
1966
1967 /* Drop the DATA_IN instruction if len is set to 0. */
1968 if (!len)
1969 op.ninstrs--;
1970
1971 return nand_exec_op(chip, &op);
1972 }
1973
Boris Brezillon97d90da2017-11-30 18:01:29 +01001974 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1975
1976 for (i = 0; i < len; i++)
1977 id[i] = chip->read_byte(mtd);
1978
1979 return 0;
1980}
1981EXPORT_SYMBOL_GPL(nand_readid_op);
1982
1983/**
1984 * nand_status_op - Do a STATUS operation
1985 * @chip: The NAND chip
1986 * @status: out variable to store the NAND status
1987 *
1988 * This function sends a STATUS command and reads back the status returned by
1989 * the NAND.
1990 * This function does not select/unselect the CS line.
1991 *
1992 * Returns 0 on success, a negative error code otherwise.
1993 */
1994int nand_status_op(struct nand_chip *chip, u8 *status)
1995{
1996 struct mtd_info *mtd = nand_to_mtd(chip);
1997
Miquel Raynal8878b122017-11-09 14:16:45 +01001998 if (chip->exec_op) {
1999 const struct nand_sdr_timings *sdr =
2000 nand_get_sdr_timings(&chip->data_interface);
2001 struct nand_op_instr instrs[] = {
2002 NAND_OP_CMD(NAND_CMD_STATUS,
2003 PSEC_TO_NSEC(sdr->tADL_min)),
2004 NAND_OP_8BIT_DATA_IN(1, status, 0),
2005 };
2006 struct nand_operation op = NAND_OPERATION(instrs);
2007
2008 if (!status)
2009 op.ninstrs--;
2010
2011 return nand_exec_op(chip, &op);
2012 }
2013
Boris Brezillon97d90da2017-11-30 18:01:29 +01002014 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
2015 if (status)
2016 *status = chip->read_byte(mtd);
2017
2018 return 0;
2019}
2020EXPORT_SYMBOL_GPL(nand_status_op);
2021
2022/**
2023 * nand_exit_status_op - Exit a STATUS operation
2024 * @chip: The NAND chip
2025 *
2026 * This function sends a READ0 command to cancel the effect of the STATUS
2027 * command to avoid reading only the status until a new read command is sent.
2028 *
2029 * This function does not select/unselect the CS line.
2030 *
2031 * Returns 0 on success, a negative error code otherwise.
2032 */
2033int nand_exit_status_op(struct nand_chip *chip)
2034{
2035 struct mtd_info *mtd = nand_to_mtd(chip);
2036
Miquel Raynal8878b122017-11-09 14:16:45 +01002037 if (chip->exec_op) {
2038 struct nand_op_instr instrs[] = {
2039 NAND_OP_CMD(NAND_CMD_READ0, 0),
2040 };
2041 struct nand_operation op = NAND_OPERATION(instrs);
2042
2043 return nand_exec_op(chip, &op);
2044 }
2045
Boris Brezillon97d90da2017-11-30 18:01:29 +01002046 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2047
2048 return 0;
2049}
2050EXPORT_SYMBOL_GPL(nand_exit_status_op);
2051
2052/**
2053 * nand_erase_op - Do an erase operation
2054 * @chip: The NAND chip
2055 * @eraseblock: block to erase
2056 *
2057 * This function sends an ERASE command and waits for the NAND to be ready
2058 * before returning.
2059 * This function does not select/unselect the CS line.
2060 *
2061 * Returns 0 on success, a negative error code otherwise.
2062 */
2063int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2064{
2065 struct mtd_info *mtd = nand_to_mtd(chip);
2066 unsigned int page = eraseblock <<
2067 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002068 int ret;
2069 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002070
Miquel Raynal8878b122017-11-09 14:16:45 +01002071 if (chip->exec_op) {
2072 const struct nand_sdr_timings *sdr =
2073 nand_get_sdr_timings(&chip->data_interface);
2074 u8 addrs[3] = { page, page >> 8, page >> 16 };
2075 struct nand_op_instr instrs[] = {
2076 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2077 NAND_OP_ADDR(2, addrs, 0),
2078 NAND_OP_CMD(NAND_CMD_ERASE2,
2079 PSEC_TO_MSEC(sdr->tWB_max)),
2080 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2081 };
2082 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002083
Miquel Raynal8878b122017-11-09 14:16:45 +01002084 if (chip->options & NAND_ROW_ADDR_3)
2085 instrs[1].ctx.addr.naddrs++;
2086
2087 ret = nand_exec_op(chip, &op);
2088 if (ret)
2089 return ret;
2090
2091 ret = nand_status_op(chip, &status);
2092 if (ret)
2093 return ret;
2094 } else {
2095 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2096 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2097
2098 ret = chip->waitfunc(mtd, chip);
2099 if (ret < 0)
2100 return ret;
2101
2102 status = ret;
2103 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002104
2105 if (status & NAND_STATUS_FAIL)
2106 return -EIO;
2107
2108 return 0;
2109}
2110EXPORT_SYMBOL_GPL(nand_erase_op);
2111
2112/**
2113 * nand_set_features_op - Do a SET FEATURES operation
2114 * @chip: The NAND chip
2115 * @feature: feature id
2116 * @data: 4 bytes of data
2117 *
2118 * This function sends a SET FEATURES command and waits for the NAND to be
2119 * ready before returning.
2120 * This function does not select/unselect the CS line.
2121 *
2122 * Returns 0 on success, a negative error code otherwise.
2123 */
2124static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2125 const void *data)
2126{
2127 struct mtd_info *mtd = nand_to_mtd(chip);
2128 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002129 int i, ret;
2130 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002131
Miquel Raynal8878b122017-11-09 14:16:45 +01002132 if (chip->exec_op) {
2133 const struct nand_sdr_timings *sdr =
2134 nand_get_sdr_timings(&chip->data_interface);
2135 struct nand_op_instr instrs[] = {
2136 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2137 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2138 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2139 PSEC_TO_NSEC(sdr->tWB_max)),
2140 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2141 };
2142 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002143
Miquel Raynal8878b122017-11-09 14:16:45 +01002144 ret = nand_exec_op(chip, &op);
2145 if (ret)
2146 return ret;
2147
2148 ret = nand_status_op(chip, &status);
2149 if (ret)
2150 return ret;
2151 } else {
2152 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
2153 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2154 chip->write_byte(mtd, params[i]);
2155
2156 ret = chip->waitfunc(mtd, chip);
2157 if (ret < 0)
2158 return ret;
2159
2160 status = ret;
2161 }
2162
Boris Brezillon97d90da2017-11-30 18:01:29 +01002163 if (status & NAND_STATUS_FAIL)
2164 return -EIO;
2165
2166 return 0;
2167}
2168
2169/**
2170 * nand_get_features_op - Do a GET FEATURES operation
2171 * @chip: The NAND chip
2172 * @feature: feature id
2173 * @data: 4 bytes of data
2174 *
2175 * This function sends a GET FEATURES command and waits for the NAND to be
2176 * ready before returning.
2177 * This function does not select/unselect the CS line.
2178 *
2179 * Returns 0 on success, a negative error code otherwise.
2180 */
2181static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2182 void *data)
2183{
2184 struct mtd_info *mtd = nand_to_mtd(chip);
2185 u8 *params = data;
2186 int i;
2187
Miquel Raynal8878b122017-11-09 14:16:45 +01002188 if (chip->exec_op) {
2189 const struct nand_sdr_timings *sdr =
2190 nand_get_sdr_timings(&chip->data_interface);
2191 struct nand_op_instr instrs[] = {
2192 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2193 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2194 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2195 PSEC_TO_NSEC(sdr->tRR_min)),
2196 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2197 data, 0),
2198 };
2199 struct nand_operation op = NAND_OPERATION(instrs);
2200
2201 return nand_exec_op(chip, &op);
2202 }
2203
Boris Brezillon97d90da2017-11-30 18:01:29 +01002204 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2205 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2206 params[i] = chip->read_byte(mtd);
2207
2208 return 0;
2209}
2210
2211/**
2212 * nand_reset_op - Do a reset operation
2213 * @chip: The NAND chip
2214 *
2215 * This function sends a RESET command and waits for the NAND to be ready
2216 * before returning.
2217 * This function does not select/unselect the CS line.
2218 *
2219 * Returns 0 on success, a negative error code otherwise.
2220 */
2221int nand_reset_op(struct nand_chip *chip)
2222{
2223 struct mtd_info *mtd = nand_to_mtd(chip);
2224
Miquel Raynal8878b122017-11-09 14:16:45 +01002225 if (chip->exec_op) {
2226 const struct nand_sdr_timings *sdr =
2227 nand_get_sdr_timings(&chip->data_interface);
2228 struct nand_op_instr instrs[] = {
2229 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2230 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2231 };
2232 struct nand_operation op = NAND_OPERATION(instrs);
2233
2234 return nand_exec_op(chip, &op);
2235 }
2236
Boris Brezillon97d90da2017-11-30 18:01:29 +01002237 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2238
2239 return 0;
2240}
2241EXPORT_SYMBOL_GPL(nand_reset_op);
2242
2243/**
2244 * nand_read_data_op - Read data from the NAND
2245 * @chip: The NAND chip
2246 * @buf: buffer used to store the data
2247 * @len: length of the buffer
2248 * @force_8bit: force 8-bit bus access
2249 *
2250 * This function does a raw data read on the bus. Usually used after launching
2251 * another NAND operation like nand_read_page_op().
2252 * This function does not select/unselect the CS line.
2253 *
2254 * Returns 0 on success, a negative error code otherwise.
2255 */
2256int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2257 bool force_8bit)
2258{
2259 struct mtd_info *mtd = nand_to_mtd(chip);
2260
2261 if (!len || !buf)
2262 return -EINVAL;
2263
Miquel Raynal8878b122017-11-09 14:16:45 +01002264 if (chip->exec_op) {
2265 struct nand_op_instr instrs[] = {
2266 NAND_OP_DATA_IN(len, buf, 0),
2267 };
2268 struct nand_operation op = NAND_OPERATION(instrs);
2269
2270 instrs[0].ctx.data.force_8bit = force_8bit;
2271
2272 return nand_exec_op(chip, &op);
2273 }
2274
Boris Brezillon97d90da2017-11-30 18:01:29 +01002275 if (force_8bit) {
2276 u8 *p = buf;
2277 unsigned int i;
2278
2279 for (i = 0; i < len; i++)
2280 p[i] = chip->read_byte(mtd);
2281 } else {
2282 chip->read_buf(mtd, buf, len);
2283 }
2284
2285 return 0;
2286}
2287EXPORT_SYMBOL_GPL(nand_read_data_op);
2288
2289/**
2290 * nand_write_data_op - Write data from the NAND
2291 * @chip: The NAND chip
2292 * @buf: buffer containing the data to send on the bus
2293 * @len: length of the buffer
2294 * @force_8bit: force 8-bit bus access
2295 *
2296 * This function does a raw data write on the bus. Usually used after launching
2297 * another NAND operation like nand_write_page_begin_op().
2298 * This function does not select/unselect the CS line.
2299 *
2300 * Returns 0 on success, a negative error code otherwise.
2301 */
2302int nand_write_data_op(struct nand_chip *chip, const void *buf,
2303 unsigned int len, bool force_8bit)
2304{
2305 struct mtd_info *mtd = nand_to_mtd(chip);
2306
2307 if (!len || !buf)
2308 return -EINVAL;
2309
Miquel Raynal8878b122017-11-09 14:16:45 +01002310 if (chip->exec_op) {
2311 struct nand_op_instr instrs[] = {
2312 NAND_OP_DATA_OUT(len, buf, 0),
2313 };
2314 struct nand_operation op = NAND_OPERATION(instrs);
2315
2316 instrs[0].ctx.data.force_8bit = force_8bit;
2317
2318 return nand_exec_op(chip, &op);
2319 }
2320
Boris Brezillon97d90da2017-11-30 18:01:29 +01002321 if (force_8bit) {
2322 const u8 *p = buf;
2323 unsigned int i;
2324
2325 for (i = 0; i < len; i++)
2326 chip->write_byte(mtd, p[i]);
2327 } else {
2328 chip->write_buf(mtd, buf, len);
2329 }
2330
2331 return 0;
2332}
2333EXPORT_SYMBOL_GPL(nand_write_data_op);
2334
2335/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002336 * struct nand_op_parser_ctx - Context used by the parser
2337 * @instrs: array of all the instructions that must be addressed
2338 * @ninstrs: length of the @instrs array
2339 * @subop: Sub-operation to be passed to the NAND controller
2340 *
2341 * This structure is used by the core to split NAND operations into
2342 * sub-operations that can be handled by the NAND controller.
2343 */
2344struct nand_op_parser_ctx {
2345 const struct nand_op_instr *instrs;
2346 unsigned int ninstrs;
2347 struct nand_subop subop;
2348};
2349
2350/**
2351 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2352 * @pat: the parser pattern element that matches @instr
2353 * @instr: pointer to the instruction to check
2354 * @start_offset: this is an in/out parameter. If @instr has already been
2355 * split, then @start_offset is the offset from which to start
2356 * (either an address cycle or an offset in the data buffer).
2357 * Conversely, if the function returns true (ie. instr must be
2358 * split), this parameter is updated to point to the first
2359 * data/address cycle that has not been taken care of.
2360 *
2361 * Some NAND controllers are limited and cannot send X address cycles with a
2362 * unique operation, or cannot read/write more than Y bytes at the same time.
2363 * In this case, split the instruction that does not fit in a single
2364 * controller-operation into two or more chunks.
2365 *
2366 * Returns true if the instruction must be split, false otherwise.
2367 * The @start_offset parameter is also updated to the offset at which the next
2368 * bundle of instruction must start (if an address or a data instruction).
2369 */
2370static bool
2371nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2372 const struct nand_op_instr *instr,
2373 unsigned int *start_offset)
2374{
2375 switch (pat->type) {
2376 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002377 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002378 break;
2379
2380 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002381 pat->ctx.addr.maxcycles) {
2382 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002383 return true;
2384 }
2385 break;
2386
2387 case NAND_OP_DATA_IN_INSTR:
2388 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002389 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002390 break;
2391
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002392 if (instr->ctx.data.len - *start_offset >
2393 pat->ctx.data.maxlen) {
2394 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002395 return true;
2396 }
2397 break;
2398
2399 default:
2400 break;
2401 }
2402
2403 return false;
2404}
2405
2406/**
2407 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2408 * remaining in the parser context
2409 * @pat: the pattern to test
2410 * @ctx: the parser context structure to match with the pattern @pat
2411 *
2412 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2413 * Returns true if this is the case, false ortherwise. When true is returned,
2414 * @ctx->subop is updated with the set of instructions to be passed to the
2415 * controller driver.
2416 */
2417static bool
2418nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2419 struct nand_op_parser_ctx *ctx)
2420{
2421 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2422 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2423 const struct nand_op_instr *instr = ctx->subop.instrs;
2424 unsigned int i, ninstrs;
2425
2426 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2427 /*
2428 * The pattern instruction does not match the operation
2429 * instruction. If the instruction is marked optional in the
2430 * pattern definition, we skip the pattern element and continue
2431 * to the next one. If the element is mandatory, there's no
2432 * match and we can return false directly.
2433 */
2434 if (instr->type != pat->elems[i].type) {
2435 if (!pat->elems[i].optional)
2436 return false;
2437
2438 continue;
2439 }
2440
2441 /*
2442 * Now check the pattern element constraints. If the pattern is
2443 * not able to handle the whole instruction in a single step,
2444 * we have to split it.
2445 * The last_instr_end_off value comes back updated to point to
2446 * the position where we have to split the instruction (the
2447 * start of the next subop chunk).
2448 */
2449 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2450 &instr_offset)) {
2451 ninstrs++;
2452 i++;
2453 break;
2454 }
2455
2456 instr++;
2457 ninstrs++;
2458 instr_offset = 0;
2459 }
2460
2461 /*
2462 * This can happen if all instructions of a pattern are optional.
2463 * Still, if there's not at least one instruction handled by this
2464 * pattern, this is not a match, and we should try the next one (if
2465 * any).
2466 */
2467 if (!ninstrs)
2468 return false;
2469
2470 /*
2471 * We had a match on the pattern head, but the pattern may be longer
2472 * than the instructions we're asked to execute. We need to make sure
2473 * there's no mandatory elements in the pattern tail.
2474 */
2475 for (; i < pat->nelems; i++) {
2476 if (!pat->elems[i].optional)
2477 return false;
2478 }
2479
2480 /*
2481 * We have a match: update the subop structure accordingly and return
2482 * true.
2483 */
2484 ctx->subop.ninstrs = ninstrs;
2485 ctx->subop.last_instr_end_off = instr_offset;
2486
2487 return true;
2488}
2489
2490#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2491static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2492{
2493 const struct nand_op_instr *instr;
2494 char *prefix = " ";
2495 unsigned int i;
2496
2497 pr_debug("executing subop:\n");
2498
2499 for (i = 0; i < ctx->ninstrs; i++) {
2500 instr = &ctx->instrs[i];
2501
2502 if (instr == &ctx->subop.instrs[0])
2503 prefix = " ->";
2504
2505 switch (instr->type) {
2506 case NAND_OP_CMD_INSTR:
2507 pr_debug("%sCMD [0x%02x]\n", prefix,
2508 instr->ctx.cmd.opcode);
2509 break;
2510 case NAND_OP_ADDR_INSTR:
2511 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2512 instr->ctx.addr.naddrs,
2513 instr->ctx.addr.naddrs < 64 ?
2514 instr->ctx.addr.naddrs : 64,
2515 instr->ctx.addr.addrs);
2516 break;
2517 case NAND_OP_DATA_IN_INSTR:
2518 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2519 instr->ctx.data.len,
2520 instr->ctx.data.force_8bit ?
2521 ", force 8-bit" : "");
2522 break;
2523 case NAND_OP_DATA_OUT_INSTR:
2524 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2525 instr->ctx.data.len,
2526 instr->ctx.data.force_8bit ?
2527 ", force 8-bit" : "");
2528 break;
2529 case NAND_OP_WAITRDY_INSTR:
2530 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2531 instr->ctx.waitrdy.timeout_ms);
2532 break;
2533 }
2534
2535 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2536 prefix = " ";
2537 }
2538}
2539#else
2540static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2541{
2542 /* NOP */
2543}
2544#endif
2545
2546/**
2547 * nand_op_parser_exec_op - exec_op parser
2548 * @chip: the NAND chip
2549 * @parser: patterns description provided by the controller driver
2550 * @op: the NAND operation to address
2551 * @check_only: when true, the function only checks if @op can be handled but
2552 * does not execute the operation
2553 *
2554 * Helper function designed to ease integration of NAND controller drivers that
2555 * only support a limited set of instruction sequences. The supported sequences
2556 * are described in @parser, and the framework takes care of splitting @op into
2557 * multiple sub-operations (if required) and pass them back to the ->exec()
2558 * callback of the matching pattern if @check_only is set to false.
2559 *
2560 * NAND controller drivers should call this function from their own ->exec_op()
2561 * implementation.
2562 *
2563 * Returns 0 on success, a negative error code otherwise. A failure can be
2564 * caused by an unsupported operation (none of the supported patterns is able
2565 * to handle the requested operation), or an error returned by one of the
2566 * matching pattern->exec() hook.
2567 */
2568int nand_op_parser_exec_op(struct nand_chip *chip,
2569 const struct nand_op_parser *parser,
2570 const struct nand_operation *op, bool check_only)
2571{
2572 struct nand_op_parser_ctx ctx = {
2573 .subop.instrs = op->instrs,
2574 .instrs = op->instrs,
2575 .ninstrs = op->ninstrs,
2576 };
2577 unsigned int i;
2578
2579 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2580 int ret;
2581
2582 for (i = 0; i < parser->npatterns; i++) {
2583 const struct nand_op_parser_pattern *pattern;
2584
2585 pattern = &parser->patterns[i];
2586 if (!nand_op_parser_match_pat(pattern, &ctx))
2587 continue;
2588
2589 nand_op_parser_trace(&ctx);
2590
2591 if (check_only)
2592 break;
2593
2594 ret = pattern->exec(chip, &ctx.subop);
2595 if (ret)
2596 return ret;
2597
2598 break;
2599 }
2600
2601 if (i == parser->npatterns) {
2602 pr_debug("->exec_op() parser: pattern not found!\n");
2603 return -ENOTSUPP;
2604 }
2605
2606 /*
2607 * Update the context structure by pointing to the start of the
2608 * next subop.
2609 */
2610 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2611 if (ctx.subop.last_instr_end_off)
2612 ctx.subop.instrs -= 1;
2613
2614 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2615 }
2616
2617 return 0;
2618}
2619EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2620
2621static bool nand_instr_is_data(const struct nand_op_instr *instr)
2622{
2623 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2624 instr->type == NAND_OP_DATA_OUT_INSTR);
2625}
2626
2627static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2628 unsigned int instr_idx)
2629{
2630 return subop && instr_idx < subop->ninstrs;
2631}
2632
2633static int nand_subop_get_start_off(const struct nand_subop *subop,
2634 unsigned int instr_idx)
2635{
2636 if (instr_idx)
2637 return 0;
2638
2639 return subop->first_instr_start_off;
2640}
2641
2642/**
2643 * nand_subop_get_addr_start_off - Get the start offset in an address array
2644 * @subop: The entire sub-operation
2645 * @instr_idx: Index of the instruction inside the sub-operation
2646 *
2647 * During driver development, one could be tempted to directly use the
2648 * ->addr.addrs field of address instructions. This is wrong as address
2649 * instructions might be split.
2650 *
2651 * Given an address instruction, returns the offset of the first cycle to issue.
2652 */
2653int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2654 unsigned int instr_idx)
2655{
2656 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2657 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2658 return -EINVAL;
2659
2660 return nand_subop_get_start_off(subop, instr_idx);
2661}
2662EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2663
2664/**
2665 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2666 * @subop: The entire sub-operation
2667 * @instr_idx: Index of the instruction inside the sub-operation
2668 *
2669 * During driver development, one could be tempted to directly use the
2670 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2671 * might be split.
2672 *
2673 * Given an address instruction, returns the number of address cycle to issue.
2674 */
2675int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2676 unsigned int instr_idx)
2677{
2678 int start_off, end_off;
2679
2680 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2681 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2682 return -EINVAL;
2683
2684 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2685
2686 if (instr_idx == subop->ninstrs - 1 &&
2687 subop->last_instr_end_off)
2688 end_off = subop->last_instr_end_off;
2689 else
2690 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2691
2692 return end_off - start_off;
2693}
2694EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2695
2696/**
2697 * nand_subop_get_data_start_off - Get the start offset in a data array
2698 * @subop: The entire sub-operation
2699 * @instr_idx: Index of the instruction inside the sub-operation
2700 *
2701 * During driver development, one could be tempted to directly use the
2702 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2703 * instructions might be split.
2704 *
2705 * Given a data instruction, returns the offset to start from.
2706 */
2707int nand_subop_get_data_start_off(const struct nand_subop *subop,
2708 unsigned int instr_idx)
2709{
2710 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2711 !nand_instr_is_data(&subop->instrs[instr_idx]))
2712 return -EINVAL;
2713
2714 return nand_subop_get_start_off(subop, instr_idx);
2715}
2716EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2717
2718/**
2719 * nand_subop_get_data_len - Get the number of bytes to retrieve
2720 * @subop: The entire sub-operation
2721 * @instr_idx: Index of the instruction inside the sub-operation
2722 *
2723 * During driver development, one could be tempted to directly use the
2724 * ->data->len field of a data instruction. This is wrong as data instructions
2725 * might be split.
2726 *
2727 * Returns the length of the chunk of data to send/receive.
2728 */
2729int nand_subop_get_data_len(const struct nand_subop *subop,
2730 unsigned int instr_idx)
2731{
2732 int start_off = 0, end_off;
2733
2734 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2735 !nand_instr_is_data(&subop->instrs[instr_idx]))
2736 return -EINVAL;
2737
2738 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2739
2740 if (instr_idx == subop->ninstrs - 1 &&
2741 subop->last_instr_end_off)
2742 end_off = subop->last_instr_end_off;
2743 else
2744 end_off = subop->instrs[instr_idx].ctx.data.len;
2745
2746 return end_off - start_off;
2747}
2748EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002751 * nand_reset - Reset and initialize a NAND device
2752 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002753 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002754 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002755 * Save the timings data structure, then apply SDR timings mode 0 (see
2756 * nand_reset_data_interface for details), do the reset operation, and
2757 * apply back the previous timings.
2758 *
2759 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002760 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002761int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002762{
2763 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal17fa8042017-11-30 18:01:31 +01002764 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002765 int ret;
2766
Boris Brezillon104e4422017-03-16 09:35:58 +01002767 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002768 if (ret)
2769 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002770
Boris Brezillon73f907f2016-10-24 16:46:20 +02002771 /*
2772 * The CS line has to be released before we can apply the new NAND
2773 * interface settings, hence this weird ->select_chip() dance.
2774 */
2775 chip->select_chip(mtd, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002776 ret = nand_reset_op(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02002777 chip->select_chip(mtd, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002778 if (ret)
2779 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002780
Boris Brezillon73f907f2016-10-24 16:46:20 +02002781 chip->select_chip(mtd, chipnr);
Miquel Raynal17fa8042017-11-30 18:01:31 +01002782 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002783 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02002784 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002785 if (ret)
2786 return ret;
2787
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002788 return 0;
2789}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002790EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002791
2792/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002793 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2794 * @buf: buffer to test
2795 * @len: buffer length
2796 * @bitflips_threshold: maximum number of bitflips
2797 *
2798 * Check if a buffer contains only 0xff, which means the underlying region
2799 * has been erased and is ready to be programmed.
2800 * The bitflips_threshold specify the maximum number of bitflips before
2801 * considering the region is not erased.
2802 * Note: The logic of this function has been extracted from the memweight
2803 * implementation, except that nand_check_erased_buf function exit before
2804 * testing the whole buffer if the number of bitflips exceed the
2805 * bitflips_threshold value.
2806 *
2807 * Returns a positive number of bitflips less than or equal to
2808 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2809 * threshold.
2810 */
2811static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2812{
2813 const unsigned char *bitmap = buf;
2814 int bitflips = 0;
2815 int weight;
2816
2817 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2818 len--, bitmap++) {
2819 weight = hweight8(*bitmap);
2820 bitflips += BITS_PER_BYTE - weight;
2821 if (unlikely(bitflips > bitflips_threshold))
2822 return -EBADMSG;
2823 }
2824
2825 for (; len >= sizeof(long);
2826 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002827 unsigned long d = *((unsigned long *)bitmap);
2828 if (d == ~0UL)
2829 continue;
2830 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002831 bitflips += BITS_PER_LONG - weight;
2832 if (unlikely(bitflips > bitflips_threshold))
2833 return -EBADMSG;
2834 }
2835
2836 for (; len > 0; len--, bitmap++) {
2837 weight = hweight8(*bitmap);
2838 bitflips += BITS_PER_BYTE - weight;
2839 if (unlikely(bitflips > bitflips_threshold))
2840 return -EBADMSG;
2841 }
2842
2843 return bitflips;
2844}
2845
2846/**
2847 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2848 * 0xff data
2849 * @data: data buffer to test
2850 * @datalen: data length
2851 * @ecc: ECC buffer
2852 * @ecclen: ECC length
2853 * @extraoob: extra OOB buffer
2854 * @extraooblen: extra OOB length
2855 * @bitflips_threshold: maximum number of bitflips
2856 *
2857 * Check if a data buffer and its associated ECC and OOB data contains only
2858 * 0xff pattern, which means the underlying region has been erased and is
2859 * ready to be programmed.
2860 * The bitflips_threshold specify the maximum number of bitflips before
2861 * considering the region as not erased.
2862 *
2863 * Note:
2864 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2865 * different from the NAND page size. When fixing bitflips, ECC engines will
2866 * report the number of errors per chunk, and the NAND core infrastructure
2867 * expect you to return the maximum number of bitflips for the whole page.
2868 * This is why you should always use this function on a single chunk and
2869 * not on the whole page. After checking each chunk you should update your
2870 * max_bitflips value accordingly.
2871 * 2/ When checking for bitflips in erased pages you should not only check
2872 * the payload data but also their associated ECC data, because a user might
2873 * have programmed almost all bits to 1 but a few. In this case, we
2874 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2875 * this case.
2876 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2877 * data are protected by the ECC engine.
2878 * It could also be used if you support subpages and want to attach some
2879 * extra OOB data to an ECC chunk.
2880 *
2881 * Returns a positive number of bitflips less than or equal to
2882 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2883 * threshold. In case of success, the passed buffers are filled with 0xff.
2884 */
2885int nand_check_erased_ecc_chunk(void *data, int datalen,
2886 void *ecc, int ecclen,
2887 void *extraoob, int extraooblen,
2888 int bitflips_threshold)
2889{
2890 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2891
2892 data_bitflips = nand_check_erased_buf(data, datalen,
2893 bitflips_threshold);
2894 if (data_bitflips < 0)
2895 return data_bitflips;
2896
2897 bitflips_threshold -= data_bitflips;
2898
2899 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2900 if (ecc_bitflips < 0)
2901 return ecc_bitflips;
2902
2903 bitflips_threshold -= ecc_bitflips;
2904
2905 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2906 bitflips_threshold);
2907 if (extraoob_bitflips < 0)
2908 return extraoob_bitflips;
2909
2910 if (data_bitflips)
2911 memset(data, 0xff, datalen);
2912
2913 if (ecc_bitflips)
2914 memset(ecc, 0xff, ecclen);
2915
2916 if (extraoob_bitflips)
2917 memset(extraoob, 0xff, extraooblen);
2918
2919 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2920}
2921EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2922
2923/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002924 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002925 * @mtd: mtd info structure
2926 * @chip: nand chip info structure
2927 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002928 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002929 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002930 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002931 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002932 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002933int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2934 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002935{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002936 int ret;
2937
Boris Brezillon25f815f2017-11-30 18:01:30 +01002938 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002939 if (ret)
2940 return ret;
2941
2942 if (oob_required) {
2943 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2944 false);
2945 if (ret)
2946 return ret;
2947 }
2948
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002949 return 0;
2950}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002951EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002952
2953/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002954 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002955 * @mtd: mtd info structure
2956 * @chip: nand chip info structure
2957 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002958 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002959 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002960 *
2961 * We need a special oob layout and handling even when OOB isn't used.
2962 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002963static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002964 struct nand_chip *chip, uint8_t *buf,
2965 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002966{
2967 int eccsize = chip->ecc.size;
2968 int eccbytes = chip->ecc.bytes;
2969 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002970 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002971
Boris Brezillon25f815f2017-11-30 18:01:30 +01002972 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2973 if (ret)
2974 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08002975
2976 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002977 ret = nand_read_data_op(chip, buf, eccsize, false);
2978 if (ret)
2979 return ret;
2980
David Brownell52ff49d2009-03-04 12:01:36 -08002981 buf += eccsize;
2982
2983 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002984 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2985 false);
2986 if (ret)
2987 return ret;
2988
David Brownell52ff49d2009-03-04 12:01:36 -08002989 oob += chip->ecc.prepad;
2990 }
2991
Boris Brezillon97d90da2017-11-30 18:01:29 +01002992 ret = nand_read_data_op(chip, oob, eccbytes, false);
2993 if (ret)
2994 return ret;
2995
David Brownell52ff49d2009-03-04 12:01:36 -08002996 oob += eccbytes;
2997
2998 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01002999 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3000 false);
3001 if (ret)
3002 return ret;
3003
David Brownell52ff49d2009-03-04 12:01:36 -08003004 oob += chip->ecc.postpad;
3005 }
3006 }
3007
3008 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003009 if (size) {
3010 ret = nand_read_data_op(chip, oob, size, false);
3011 if (ret)
3012 return ret;
3013 }
David Brownell52ff49d2009-03-04 12:01:36 -08003014
3015 return 0;
3016}
3017
3018/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003019 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003020 * @mtd: mtd info structure
3021 * @chip: nand chip info structure
3022 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003023 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003024 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003025 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003026static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003027 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Boris Brezillon846031d2016-02-03 20:11:00 +01003029 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003030 int eccbytes = chip->ecc.bytes;
3031 int eccsteps = chip->ecc.steps;
3032 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003033 uint8_t *ecc_calc = chip->ecc.calc_buf;
3034 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003035 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003036
Brian Norris1fbb9382012-05-02 10:14:55 -07003037 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003038
3039 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3040 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3041
Boris Brezillon846031d2016-02-03 20:11:00 +01003042 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3043 chip->ecc.total);
3044 if (ret)
3045 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003046
3047 eccsteps = chip->ecc.steps;
3048 p = buf;
3049
3050 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3051 int stat;
3052
3053 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003054 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003055 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003056 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003057 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003058 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3059 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003060 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003061 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303065 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003066 * @mtd: mtd info structure
3067 * @chip: nand chip info structure
3068 * @data_offs: offset of requested data within the page
3069 * @readlen: data length
3070 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003071 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003072 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003073static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003074 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3075 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003076{
Boris Brezillon846031d2016-02-03 20:11:00 +01003077 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003078 uint8_t *p;
3079 int data_col_addr, i, gaps = 0;
3080 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3081 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003082 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003083 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003084 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003085
Brian Norris7854d3f2011-06-23 14:12:08 -07003086 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003087 start_step = data_offs / chip->ecc.size;
3088 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3089 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303090 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003091
Brian Norris8b6e50c2011-05-25 14:59:01 -07003092 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003093 datafrag_len = num_steps * chip->ecc.size;
3094 eccfrag_len = num_steps * chip->ecc.bytes;
3095
3096 data_col_addr = start_step * chip->ecc.size;
3097 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003098 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003099 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003100 if (ret)
3101 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003102
Brian Norris8b6e50c2011-05-25 14:59:01 -07003103 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003104 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003105 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003106
Brian Norris8b6e50c2011-05-25 14:59:01 -07003107 /*
3108 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003109 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003110 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003111 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3112 if (ret)
3113 return ret;
3114
3115 if (oobregion.length < eccfrag_len)
3116 gaps = 1;
3117
Alexey Korolev3d459552008-05-15 17:23:18 +01003118 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003119 ret = nand_change_read_column_op(chip, mtd->writesize,
3120 chip->oob_poi, mtd->oobsize,
3121 false);
3122 if (ret)
3123 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003124 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003125 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003126 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003127 * about buswidth alignment in read_buf.
3128 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003129 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003130 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003131 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003132 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003133 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3134 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003135 aligned_len++;
3136
Boris Brezillon97d90da2017-11-30 18:01:29 +01003137 ret = nand_change_read_column_op(chip,
3138 mtd->writesize + aligned_pos,
3139 &chip->oob_poi[aligned_pos],
3140 aligned_len, false);
3141 if (ret)
3142 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003143 }
3144
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003145 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003146 chip->oob_poi, index, eccfrag_len);
3147 if (ret)
3148 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003149
3150 p = bufpoi + data_col_addr;
3151 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3152 int stat;
3153
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003154 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3155 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003156 if (stat == -EBADMSG &&
3157 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3158 /* check for empty pages with bitflips */
3159 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003160 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003161 chip->ecc.bytes,
3162 NULL, 0,
3163 chip->ecc.strength);
3164 }
3165
Mike Dunn3f91e942012-04-25 12:06:09 -07003166 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003167 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003168 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003169 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003170 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3171 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003172 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003173 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003174}
3175
3176/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003177 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003178 * @mtd: mtd info structure
3179 * @chip: nand chip info structure
3180 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003181 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003182 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003183 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003184 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003185 */
3186static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003187 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003188{
Boris Brezillon846031d2016-02-03 20:11:00 +01003189 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003190 int eccbytes = chip->ecc.bytes;
3191 int eccsteps = chip->ecc.steps;
3192 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003193 uint8_t *ecc_calc = chip->ecc.calc_buf;
3194 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003195 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003196
Boris Brezillon25f815f2017-11-30 18:01:30 +01003197 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3198 if (ret)
3199 return ret;
3200
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003201 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3202 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003203
3204 ret = nand_read_data_op(chip, p, eccsize, false);
3205 if (ret)
3206 return ret;
3207
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003208 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3209 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003210
3211 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3212 if (ret)
3213 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003214
Boris Brezillon846031d2016-02-03 20:11:00 +01003215 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3216 chip->ecc.total);
3217 if (ret)
3218 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003219
3220 eccsteps = chip->ecc.steps;
3221 p = buf;
3222
3223 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3224 int stat;
3225
3226 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003227 if (stat == -EBADMSG &&
3228 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3229 /* check for empty pages with bitflips */
3230 stat = nand_check_erased_ecc_chunk(p, eccsize,
3231 &ecc_code[i], eccbytes,
3232 NULL, 0,
3233 chip->ecc.strength);
3234 }
3235
Mike Dunn3f91e942012-04-25 12:06:09 -07003236 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003237 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003238 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003239 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003240 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3241 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003242 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003243 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003244}
3245
3246/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003247 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003248 * @mtd: mtd info structure
3249 * @chip: nand chip info structure
3250 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003251 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003252 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003253 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003254 * Hardware ECC for large page chips, require OOB to be read first. For this
3255 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3256 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3257 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3258 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003259 */
3260static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003261 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003262{
Boris Brezillon846031d2016-02-03 20:11:00 +01003263 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003264 int eccbytes = chip->ecc.bytes;
3265 int eccsteps = chip->ecc.steps;
3266 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003267 uint8_t *ecc_code = chip->ecc.code_buf;
3268 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003269 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003270
3271 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003272 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3273 if (ret)
3274 return ret;
3275
3276 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3277 if (ret)
3278 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003279
Boris Brezillon846031d2016-02-03 20:11:00 +01003280 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3281 chip->ecc.total);
3282 if (ret)
3283 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003284
3285 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3286 int stat;
3287
3288 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003289
3290 ret = nand_read_data_op(chip, p, eccsize, false);
3291 if (ret)
3292 return ret;
3293
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003294 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3295
3296 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003297 if (stat == -EBADMSG &&
3298 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3299 /* check for empty pages with bitflips */
3300 stat = nand_check_erased_ecc_chunk(p, eccsize,
3301 &ecc_code[i], eccbytes,
3302 NULL, 0,
3303 chip->ecc.strength);
3304 }
3305
Mike Dunn3f91e942012-04-25 12:06:09 -07003306 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003307 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003308 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003309 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003310 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3311 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003312 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003313 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003314}
3315
3316/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003317 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003318 * @mtd: mtd info structure
3319 * @chip: nand chip info structure
3320 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003321 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003322 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003323 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003324 * The hw generator calculates the error syndrome automatically. Therefore we
3325 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003326 */
3327static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003328 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003329{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003330 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003331 int eccbytes = chip->ecc.bytes;
3332 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003333 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003334 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003335 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003336 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003337
Boris Brezillon25f815f2017-11-30 18:01:30 +01003338 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3339 if (ret)
3340 return ret;
3341
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003342 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3343 int stat;
3344
3345 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003346
3347 ret = nand_read_data_op(chip, p, eccsize, false);
3348 if (ret)
3349 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003350
3351 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003352 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3353 false);
3354 if (ret)
3355 return ret;
3356
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003357 oob += chip->ecc.prepad;
3358 }
3359
3360 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003361
3362 ret = nand_read_data_op(chip, oob, eccbytes, false);
3363 if (ret)
3364 return ret;
3365
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003366 stat = chip->ecc.correct(mtd, p, oob, NULL);
3367
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003368 oob += eccbytes;
3369
3370 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003371 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3372 false);
3373 if (ret)
3374 return ret;
3375
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003376 oob += chip->ecc.postpad;
3377 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003378
3379 if (stat == -EBADMSG &&
3380 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3381 /* check for empty pages with bitflips */
3382 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3383 oob - eccpadbytes,
3384 eccpadbytes,
3385 NULL, 0,
3386 chip->ecc.strength);
3387 }
3388
3389 if (stat < 0) {
3390 mtd->ecc_stats.failed++;
3391 } else {
3392 mtd->ecc_stats.corrected += stat;
3393 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3394 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003395 }
3396
3397 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003398 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003399 if (i) {
3400 ret = nand_read_data_op(chip, oob, i, false);
3401 if (ret)
3402 return ret;
3403 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003404
Mike Dunn3f91e942012-04-25 12:06:09 -07003405 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003406}
3407
3408/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003409 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003410 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003411 * @oob: oob destination address
3412 * @ops: oob ops structure
3413 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003414 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003415static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003416 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003417{
Boris Brezillon846031d2016-02-03 20:11:00 +01003418 struct nand_chip *chip = mtd_to_nand(mtd);
3419 int ret;
3420
Florian Fainellif8ac0412010-09-07 13:23:43 +02003421 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003422
Brian Norris0612b9d2011-08-30 18:45:40 -07003423 case MTD_OPS_PLACE_OOB:
3424 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003425 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3426 return oob + len;
3427
Boris Brezillon846031d2016-02-03 20:11:00 +01003428 case MTD_OPS_AUTO_OOB:
3429 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3430 ops->ooboffs, len);
3431 BUG_ON(ret);
3432 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003433
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003434 default:
3435 BUG();
3436 }
3437 return NULL;
3438}
3439
3440/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003441 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3442 * @mtd: MTD device structure
3443 * @retry_mode: the retry mode to use
3444 *
3445 * Some vendors supply a special command to shift the Vt threshold, to be used
3446 * when there are too many bitflips in a page (i.e., ECC error). After setting
3447 * a new threshold, the host should retry reading the page.
3448 */
3449static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3450{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003451 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08003452
3453 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3454
3455 if (retry_mode >= chip->read_retries)
3456 return -EINVAL;
3457
3458 if (!chip->setup_read_retry)
3459 return -EOPNOTSUPP;
3460
3461 return chip->setup_read_retry(mtd, retry_mode);
3462}
3463
3464/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003465 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003466 * @mtd: MTD device structure
3467 * @from: offset to read from
3468 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003469 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003470 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003471 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003472static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3473 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003474{
Brian Norrise47f3db2012-05-02 10:14:56 -07003475 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003476 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003477 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003478 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003479 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003480 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003481
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003482 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003483 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003484 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003485 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003486 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003488 chipnr = (int)(from >> chip->chip_shift);
3489 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003491 realpage = (int)(from >> chip->page_shift);
3492 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003494 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003496 buf = ops->datbuf;
3497 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003498 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003499
Florian Fainellif8ac0412010-09-07 13:23:43 +02003500 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003501 unsigned int ecc_failures = mtd->ecc_stats.failed;
3502
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003503 bytes = min(mtd->writesize - col, readlen);
3504 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003505
Kamal Dasu66507c72014-05-01 20:51:19 -04003506 if (!aligned)
3507 use_bufpoi = 1;
3508 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003509 use_bufpoi = !virt_addr_valid(buf) ||
3510 !IS_ALIGNED((unsigned long)buf,
3511 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003512 else
3513 use_bufpoi = 0;
3514
Brian Norris8b6e50c2011-05-25 14:59:01 -07003515 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003516 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003517 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003518
3519 if (use_bufpoi && aligned)
3520 pr_debug("%s: using read bounce buffer for buf@%p\n",
3521 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Brian Norrisba84fb52014-01-03 15:13:33 -08003523read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003524 /*
3525 * Now read the page into the buffer. Absent an error,
3526 * the read methods return max bitflips per ecc step.
3527 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003528 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07003529 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003530 oob_required,
3531 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003532 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3533 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003534 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003535 col, bytes, bufpoi,
3536 page);
David Woodhouse956e9442006-09-25 17:12:39 +01003537 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07003538 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003539 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003540 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003541 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003542 /* Invalidate page cache */
3543 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003544 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003545 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003546
3547 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003548 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003549 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003550 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003551 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003552 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003553 chip->pagebuf_bitflips = ret;
3554 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003555 /* Invalidate page cache */
3556 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003557 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003558 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003560
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003561 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003562 int toread = min(oobreadlen, max_oobsize);
3563
3564 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003565 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003566 oob, ops, toread);
3567 oobreadlen -= toread;
3568 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003569 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003570
3571 if (chip->options & NAND_NEED_READRDY) {
3572 /* Apply delay or wait for ready/busy pin */
3573 if (!chip->dev_ready)
3574 udelay(chip->chip_delay);
3575 else
3576 nand_wait_ready(mtd);
3577 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08003578
Brian Norrisba84fb52014-01-03 15:13:33 -08003579 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003580 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003581 retry_mode++;
3582 ret = nand_setup_read_retry(mtd,
3583 retry_mode);
3584 if (ret < 0)
3585 break;
3586
3587 /* Reset failures; retry */
3588 mtd->ecc_stats.failed = ecc_failures;
3589 goto read_retry;
3590 } else {
3591 /* No more retry modes; real failure */
3592 ecc_fail = true;
3593 }
3594 }
3595
3596 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003597 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003598 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003599 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003600 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003601 max_bitflips = max_t(unsigned int, max_bitflips,
3602 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003605 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003606
Brian Norrisba84fb52014-01-03 15:13:33 -08003607 /* Reset to retry mode 0 */
3608 if (retry_mode) {
3609 ret = nand_setup_read_retry(mtd, 0);
3610 if (ret < 0)
3611 break;
3612 retry_mode = 0;
3613 }
3614
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003615 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003616 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Brian Norris8b6e50c2011-05-25 14:59:01 -07003618 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 col = 0;
3620 /* Increment page address */
3621 realpage++;
3622
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003623 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 /* Check, if we cross a chip boundary */
3625 if (!page) {
3626 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003627 chip->select_chip(mtd, -1);
3628 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003631 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003633 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003634 if (oob)
3635 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Mike Dunn3f91e942012-04-25 12:06:09 -07003637 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003638 return ret;
3639
Brian Norrisb72f3df2013-12-03 11:04:14 -08003640 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003641 return -EBADMSG;
3642
Mike Dunnedbc45402012-04-25 12:06:11 -07003643 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003644}
3645
3646/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003647 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003648 * @mtd: mtd info structure
3649 * @chip: nand chip info structure
3650 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003651 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003652int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003653{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003654 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003655}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003656EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003657
3658/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003659 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003660 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003661 * @mtd: mtd info structure
3662 * @chip: nand chip info structure
3663 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003664 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003665int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3666 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003667{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003668 int length = mtd->oobsize;
3669 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3670 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003671 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003672 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003673
Boris Brezillon97d90da2017-11-30 18:01:29 +01003674 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3675 if (ret)
3676 return ret;
3677
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003678 for (i = 0; i < chip->ecc.steps; i++) {
3679 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003680 int ret;
3681
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003682 pos = eccsize + i * (eccsize + chunk);
3683 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003684 ret = nand_change_read_column_op(chip, pos,
3685 NULL, 0,
3686 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003687 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003688 ret = nand_read_page_op(chip, page, pos, NULL,
3689 0);
3690
3691 if (ret)
3692 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003693 } else
3694 sndrnd = 1;
3695 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003696
3697 ret = nand_read_data_op(chip, bufpoi, toread, false);
3698 if (ret)
3699 return ret;
3700
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003701 bufpoi += toread;
3702 length -= toread;
3703 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003704 if (length > 0) {
3705 ret = nand_read_data_op(chip, bufpoi, length, false);
3706 if (ret)
3707 return ret;
3708 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003709
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003710 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003711}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003712EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003713
3714/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003715 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003716 * @mtd: mtd info structure
3717 * @chip: nand chip info structure
3718 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003719 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003720int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003721{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003722 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3723 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003724}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003725EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003726
3727/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003728 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003729 * with syndrome - only for large page flash
3730 * @mtd: mtd info structure
3731 * @chip: nand chip info structure
3732 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003733 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003734int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3735 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003736{
3737 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3738 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003739 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003740 const uint8_t *bufpoi = chip->oob_poi;
3741
3742 /*
3743 * data-ecc-data-ecc ... ecc-oob
3744 * or
3745 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3746 */
3747 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3748 pos = steps * (eccsize + chunk);
3749 steps = 0;
3750 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003751 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003752
Boris Brezillon97d90da2017-11-30 18:01:29 +01003753 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3754 if (ret)
3755 return ret;
3756
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003757 for (i = 0; i < steps; i++) {
3758 if (sndcmd) {
3759 if (mtd->writesize <= 512) {
3760 uint32_t fill = 0xFFFFFFFF;
3761
3762 len = eccsize;
3763 while (len > 0) {
3764 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003765
3766 ret = nand_write_data_op(chip, &fill,
3767 num, false);
3768 if (ret)
3769 return ret;
3770
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003771 len -= num;
3772 }
3773 } else {
3774 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003775 ret = nand_change_write_column_op(chip, pos,
3776 NULL, 0,
3777 false);
3778 if (ret)
3779 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003780 }
3781 } else
3782 sndcmd = 1;
3783 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003784
3785 ret = nand_write_data_op(chip, bufpoi, len, false);
3786 if (ret)
3787 return ret;
3788
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003789 bufpoi += len;
3790 length -= len;
3791 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003792 if (length > 0) {
3793 ret = nand_write_data_op(chip, bufpoi, length, false);
3794 if (ret)
3795 return ret;
3796 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003797
Boris Brezillon97d90da2017-11-30 18:01:29 +01003798 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003799}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003800EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003801
3802/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003803 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003804 * @mtd: MTD device structure
3805 * @from: offset to read from
3806 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003808 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003810static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3811 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003813 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003814 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003815 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003816 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003817 int readlen = ops->ooblen;
3818 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003819 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003820 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Brian Norris289c0522011-07-19 10:06:09 -07003822 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303823 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
Brian Norris041e4572011-06-23 16:45:24 -07003825 stats = mtd->ecc_stats;
3826
Boris BREZILLON29f10582016-03-07 10:46:52 +01003827 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003828
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003829 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003830 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003832 /* Shift to get page */
3833 realpage = (int)(from >> chip->page_shift);
3834 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Florian Fainellif8ac0412010-09-07 13:23:43 +02003836 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003837 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003838 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003839 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003840 ret = chip->ecc.read_oob(mtd, chip, page);
3841
3842 if (ret < 0)
3843 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003844
3845 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003846 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003847
Brian Norris5bc7c332013-03-13 09:51:31 -07003848 if (chip->options & NAND_NEED_READRDY) {
3849 /* Apply delay or wait for ready/busy pin */
3850 if (!chip->dev_ready)
3851 udelay(chip->chip_delay);
3852 else
3853 nand_wait_ready(mtd);
3854 }
3855
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003856 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3857
Vitaly Wool70145682006-11-03 18:20:38 +03003858 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003859 if (!readlen)
3860 break;
3861
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003862 /* Increment page address */
3863 realpage++;
3864
3865 page = realpage & chip->pagemask;
3866 /* Check, if we cross a chip boundary */
3867 if (!page) {
3868 chipnr++;
3869 chip->select_chip(mtd, -1);
3870 chip->select_chip(mtd, chipnr);
3871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003873 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003875 ops->oobretlen = ops->ooblen - readlen;
3876
3877 if (ret < 0)
3878 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003879
3880 if (mtd->ecc_stats.failed - stats.failed)
3881 return -EBADMSG;
3882
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003883 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
3886/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003887 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003888 * @mtd: MTD device structure
3889 * @from: offset to read from
3890 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003892 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003894static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3895 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003897 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003898
3899 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003901 if (ops->mode != MTD_OPS_PLACE_OOB &&
3902 ops->mode != MTD_OPS_AUTO_OOB &&
3903 ops->mode != MTD_OPS_RAW)
3904 return -ENOTSUPP;
3905
Huang Shijie6a8214a2012-11-19 14:43:30 +08003906 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003908 if (!ops->datbuf)
3909 ret = nand_do_read_oob(mtd, from, ops);
3910 else
3911 ret = nand_do_read_ops(mtd, from, ops);
3912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003914 return ret;
3915}
3916
3917
3918/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003919 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003920 * @mtd: mtd info structure
3921 * @chip: nand chip info structure
3922 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003923 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003924 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003925 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003926 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003927 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003928int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
3929 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003930{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003931 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003932
Boris Brezillon25f815f2017-11-30 18:01:30 +01003933 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003934 if (ret)
3935 return ret;
3936
3937 if (oob_required) {
3938 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3939 false);
3940 if (ret)
3941 return ret;
3942 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003943
Boris Brezillon25f815f2017-11-30 18:01:30 +01003944 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003946EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003948/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003949 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003950 * @mtd: mtd info structure
3951 * @chip: nand chip info structure
3952 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003953 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003954 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003955 *
3956 * We need a special oob layout and handling even when ECC isn't checked.
3957 */
Josh Wufdbad98d2012-06-25 18:07:45 +08003958static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003959 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003960 const uint8_t *buf, int oob_required,
3961 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003962{
3963 int eccsize = chip->ecc.size;
3964 int eccbytes = chip->ecc.bytes;
3965 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003966 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003967
Boris Brezillon25f815f2017-11-30 18:01:30 +01003968 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3969 if (ret)
3970 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003971
3972 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003973 ret = nand_write_data_op(chip, buf, eccsize, false);
3974 if (ret)
3975 return ret;
3976
David Brownell52ff49d2009-03-04 12:01:36 -08003977 buf += eccsize;
3978
3979 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003980 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3981 false);
3982 if (ret)
3983 return ret;
3984
David Brownell52ff49d2009-03-04 12:01:36 -08003985 oob += chip->ecc.prepad;
3986 }
3987
Boris Brezillon97d90da2017-11-30 18:01:29 +01003988 ret = nand_write_data_op(chip, oob, eccbytes, false);
3989 if (ret)
3990 return ret;
3991
David Brownell52ff49d2009-03-04 12:01:36 -08003992 oob += eccbytes;
3993
3994 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003995 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3996 false);
3997 if (ret)
3998 return ret;
3999
David Brownell52ff49d2009-03-04 12:01:36 -08004000 oob += chip->ecc.postpad;
4001 }
4002 }
4003
4004 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004005 if (size) {
4006 ret = nand_write_data_op(chip, oob, size, false);
4007 if (ret)
4008 return ret;
4009 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004010
Boris Brezillon25f815f2017-11-30 18:01:30 +01004011 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004012}
4013/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004014 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004015 * @mtd: mtd info structure
4016 * @chip: nand chip info structure
4017 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004018 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004019 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004020 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004021static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004022 const uint8_t *buf, int oob_required,
4023 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004024{
Boris Brezillon846031d2016-02-03 20:11:00 +01004025 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004026 int eccbytes = chip->ecc.bytes;
4027 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004028 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004029 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004030
Brian Norris7854d3f2011-06-23 14:12:08 -07004031 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004032 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
4033 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004034
Boris Brezillon846031d2016-02-03 20:11:00 +01004035 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4036 chip->ecc.total);
4037 if (ret)
4038 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004039
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004040 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004041}
4042
4043/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004044 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004045 * @mtd: mtd info structure
4046 * @chip: nand chip info structure
4047 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004048 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004049 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004050 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004051static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004052 const uint8_t *buf, int oob_required,
4053 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004054{
Boris Brezillon846031d2016-02-03 20:11:00 +01004055 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004056 int eccbytes = chip->ecc.bytes;
4057 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004058 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004059 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004060
Boris Brezillon25f815f2017-11-30 18:01:30 +01004061 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4062 if (ret)
4063 return ret;
4064
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004065 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4066 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004067
4068 ret = nand_write_data_op(chip, p, eccsize, false);
4069 if (ret)
4070 return ret;
4071
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004072 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
4073 }
4074
Boris Brezillon846031d2016-02-03 20:11:00 +01004075 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4076 chip->ecc.total);
4077 if (ret)
4078 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004079
Boris Brezillon97d90da2017-11-30 18:01:29 +01004080 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4081 if (ret)
4082 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004083
Boris Brezillon25f815f2017-11-30 18:01:30 +01004084 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004085}
4086
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304087
4088/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004089 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304090 * @mtd: mtd info structure
4091 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004092 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304093 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004094 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304095 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004096 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304097 */
4098static int nand_write_subpage_hwecc(struct mtd_info *mtd,
4099 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07004100 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004101 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304102{
4103 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004104 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304105 int ecc_size = chip->ecc.size;
4106 int ecc_bytes = chip->ecc.bytes;
4107 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304108 uint32_t start_step = offset / ecc_size;
4109 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4110 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004111 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304112
Boris Brezillon25f815f2017-11-30 18:01:30 +01004113 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4114 if (ret)
4115 return ret;
4116
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304117 for (step = 0; step < ecc_steps; step++) {
4118 /* configure controller for WRITE access */
4119 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
4120
4121 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004122 ret = nand_write_data_op(chip, buf, ecc_size, false);
4123 if (ret)
4124 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304125
4126 /* mask ECC of un-touched subpages by padding 0xFF */
4127 if ((step < start_step) || (step > end_step))
4128 memset(ecc_calc, 0xff, ecc_bytes);
4129 else
Brian Norrisd6a950802013-08-08 17:16:36 -07004130 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304131
4132 /* mask OOB of un-touched subpages by padding 0xFF */
4133 /* if oob_required, preserve OOB metadata of written subpage */
4134 if (!oob_required || (step < start_step) || (step > end_step))
4135 memset(oob_buf, 0xff, oob_bytes);
4136
Brian Norrisd6a950802013-08-08 17:16:36 -07004137 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304138 ecc_calc += ecc_bytes;
4139 oob_buf += oob_bytes;
4140 }
4141
4142 /* copy calculated ECC for whole page to chip->buffer->oob */
4143 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004144 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004145 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4146 chip->ecc.total);
4147 if (ret)
4148 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304149
4150 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004151 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4152 if (ret)
4153 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304154
Boris Brezillon25f815f2017-11-30 18:01:30 +01004155 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304156}
4157
4158
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004159/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004160 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004161 * @mtd: mtd info structure
4162 * @chip: nand chip info structure
4163 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004164 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004165 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004166 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004167 * The hw generator calculates the error syndrome automatically. Therefore we
4168 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004169 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004170static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07004171 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004172 const uint8_t *buf, int oob_required,
4173 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004174{
4175 int i, eccsize = chip->ecc.size;
4176 int eccbytes = chip->ecc.bytes;
4177 int eccsteps = chip->ecc.steps;
4178 const uint8_t *p = buf;
4179 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004180 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004181
Boris Brezillon25f815f2017-11-30 18:01:30 +01004182 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4183 if (ret)
4184 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004185
4186 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004187 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004188
4189 ret = nand_write_data_op(chip, p, eccsize, false);
4190 if (ret)
4191 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004192
4193 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004194 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4195 false);
4196 if (ret)
4197 return ret;
4198
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004199 oob += chip->ecc.prepad;
4200 }
4201
4202 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004203
4204 ret = nand_write_data_op(chip, oob, eccbytes, false);
4205 if (ret)
4206 return ret;
4207
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004208 oob += eccbytes;
4209
4210 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004211 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4212 false);
4213 if (ret)
4214 return ret;
4215
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004216 oob += chip->ecc.postpad;
4217 }
4218 }
4219
4220 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004221 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004222 if (i) {
4223 ret = nand_write_data_op(chip, oob, i, false);
4224 if (ret)
4225 return ret;
4226 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004227
Boris Brezillon25f815f2017-11-30 18:01:30 +01004228 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004229}
4230
4231/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004232 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004233 * @mtd: MTD device structure
4234 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304235 * @offset: address offset within the page
4236 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004237 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004238 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004239 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004240 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004241 */
4242static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304243 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004244 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004245{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304246 int status, subpage;
4247
4248 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4249 chip->ecc.write_subpage)
4250 subpage = offset || (data_len < mtd->writesize);
4251 else
4252 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004253
David Woodhouse956e9442006-09-25 17:12:39 +01004254 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304255 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004256 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304257 else if (subpage)
4258 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004259 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004260 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004261 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
4262 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004263
4264 if (status < 0)
4265 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004266
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004267 return 0;
4268}
4269
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004270/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004271 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004272 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004273 * @oob: oob data buffer
4274 * @len: oob data write length
4275 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004276 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004277static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4278 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004279{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004280 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004281 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004282
4283 /*
4284 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4285 * data from a previous OOB read.
4286 */
4287 memset(chip->oob_poi, 0xff, mtd->oobsize);
4288
Florian Fainellif8ac0412010-09-07 13:23:43 +02004289 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004290
Brian Norris0612b9d2011-08-30 18:45:40 -07004291 case MTD_OPS_PLACE_OOB:
4292 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004293 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4294 return oob + len;
4295
Boris Brezillon846031d2016-02-03 20:11:00 +01004296 case MTD_OPS_AUTO_OOB:
4297 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4298 ops->ooboffs, len);
4299 BUG_ON(ret);
4300 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004301
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004302 default:
4303 BUG();
4304 }
4305 return NULL;
4306}
4307
Florian Fainellif8ac0412010-09-07 13:23:43 +02004308#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004309
4310/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004311 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004312 * @mtd: MTD device structure
4313 * @to: offset to write to
4314 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004315 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004316 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004317 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004318static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4319 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004320{
Corentin Labbe73600b62017-09-02 10:49:38 +02004321 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004322 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004323 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004324
4325 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004326 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004327
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004328 uint8_t *oob = ops->oobbuf;
4329 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304330 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004331 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004332
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004333 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004334 if (!writelen)
4335 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004336
Brian Norris8b6e50c2011-05-25 14:59:01 -07004337 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004338 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004339 pr_notice("%s: attempt to write non page aligned data\n",
4340 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004341 return -EINVAL;
4342 }
4343
Thomas Gleixner29072b92006-09-28 15:38:36 +02004344 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004345
Thomas Gleixner6a930962006-06-28 00:11:45 +02004346 chipnr = (int)(to >> chip->chip_shift);
4347 chip->select_chip(mtd, chipnr);
4348
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004349 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004350 if (nand_check_wp(mtd)) {
4351 ret = -EIO;
4352 goto err_out;
4353 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004354
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004355 realpage = (int)(to >> chip->page_shift);
4356 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004357
4358 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004359 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4360 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004361 chip->pagebuf = -1;
4362
Maxim Levitsky782ce792010-02-22 20:39:36 +02004363 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004364 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4365 ret = -EINVAL;
4366 goto err_out;
4367 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004368
Florian Fainellif8ac0412010-09-07 13:23:43 +02004369 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004370 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004371 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004372 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004373 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004374
Kamal Dasu66507c72014-05-01 20:51:19 -04004375 if (part_pagewr)
4376 use_bufpoi = 1;
4377 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004378 use_bufpoi = !virt_addr_valid(buf) ||
4379 !IS_ALIGNED((unsigned long)buf,
4380 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004381 else
4382 use_bufpoi = 0;
4383
4384 /* Partial page write?, or need to use bounce buffer */
4385 if (use_bufpoi) {
4386 pr_debug("%s: using write bounce buffer for buf@%p\n",
4387 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004388 if (part_pagewr)
4389 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004390 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004391 memset(chip->data_buf, 0xff, mtd->writesize);
4392 memcpy(&chip->data_buf[column], buf, bytes);
4393 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004394 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004395
Maxim Levitsky782ce792010-02-22 20:39:36 +02004396 if (unlikely(oob)) {
4397 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004398 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004399 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004400 } else {
4401 /* We still need to erase leftover OOB data */
4402 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004403 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004404
4405 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004406 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004407 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004408 if (ret)
4409 break;
4410
4411 writelen -= bytes;
4412 if (!writelen)
4413 break;
4414
Thomas Gleixner29072b92006-09-28 15:38:36 +02004415 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004416 buf += bytes;
4417 realpage++;
4418
4419 page = realpage & chip->pagemask;
4420 /* Check, if we cross a chip boundary */
4421 if (!page) {
4422 chipnr++;
4423 chip->select_chip(mtd, -1);
4424 chip->select_chip(mtd, chipnr);
4425 }
4426 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004427
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004428 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004429 if (unlikely(oob))
4430 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004431
4432err_out:
4433 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004434 return ret;
4435}
4436
4437/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004438 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004439 * @mtd: MTD device structure
4440 * @to: offset to write to
4441 * @len: number of bytes to write
4442 * @retlen: pointer to variable to store the number of written bytes
4443 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004444 *
4445 * NAND write with ECC. Used when performing writes in interrupt context, this
4446 * may for example be called by mtdoops when writing an oops while in panic.
4447 */
4448static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4449 size_t *retlen, const uint8_t *buf)
4450{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004451 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004452 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004453 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004454 int ret;
4455
Brian Norris8b6e50c2011-05-25 14:59:01 -07004456 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004457 panic_nand_get_device(chip, mtd, FL_WRITING);
4458
Brent Taylor30863e382017-10-30 22:32:45 -05004459 chip->select_chip(mtd, chipnr);
4460
4461 /* Wait for the device to get ready */
4462 panic_nand_wait(mtd, chip, 400);
4463
Brian Norris0ec56dc2015-02-28 02:02:30 -08004464 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004465 ops.len = len;
4466 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004467 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004468
Brian Norris4a89ff82011-08-30 18:45:45 -07004469 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004470
Brian Norris4a89ff82011-08-30 18:45:45 -07004471 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004472 return ret;
4473}
4474
4475/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004476 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004477 * @mtd: MTD device structure
4478 * @to: offset to write to
4479 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004480 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004481 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004482 */
4483static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4484 struct mtd_oob_ops *ops)
4485{
Adrian Hunter03736152007-01-31 17:58:29 +02004486 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004487 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Brian Norris289c0522011-07-19 10:06:09 -07004489 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304490 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Boris BREZILLON29f10582016-03-07 10:46:52 +01004492 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004495 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004496 pr_debug("%s: attempt to write past end of page\n",
4497 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 return -EINVAL;
4499 }
4500
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004501 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004502
4503 /*
4504 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4505 * of my DiskOnChip 2000 test units) will clear the whole data page too
4506 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4507 * it in the doc2000 driver in August 1999. dwmw2.
4508 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004509 nand_reset(chip, chipnr);
4510
4511 chip->select_chip(mtd, chipnr);
4512
4513 /* Shift to get page */
4514 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
4516 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004517 if (nand_check_wp(mtd)) {
4518 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004519 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004520 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004523 if (page == chip->pagebuf)
4524 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004526 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004527
Brian Norris0612b9d2011-08-30 18:45:40 -07004528 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07004529 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
4530 else
4531 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004532
Huang Shijieb0bb6902012-11-19 14:43:29 +08004533 chip->select_chip(mtd, -1);
4534
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004535 if (status)
4536 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
Vitaly Wool70145682006-11-03 18:20:38 +03004538 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004540 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004541}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004543/**
4544 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004545 * @mtd: MTD device structure
4546 * @to: offset to write to
4547 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004548 */
4549static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4550 struct mtd_oob_ops *ops)
4551{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004552 int ret = -ENOTSUPP;
4553
4554 ops->retlen = 0;
4555
Huang Shijie6a8214a2012-11-19 14:43:30 +08004556 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004557
Florian Fainellif8ac0412010-09-07 13:23:43 +02004558 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004559 case MTD_OPS_PLACE_OOB:
4560 case MTD_OPS_AUTO_OOB:
4561 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004562 break;
4563
4564 default:
4565 goto out;
4566 }
4567
4568 if (!ops->datbuf)
4569 ret = nand_do_write_oob(mtd, to, ops);
4570 else
4571 ret = nand_do_write_ops(mtd, to, ops);
4572
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004573out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004574 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 return ret;
4576}
4577
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578/**
Brian Norris49c50b92014-05-06 16:02:19 -07004579 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004580 * @mtd: MTD device structure
4581 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 *
Brian Norris49c50b92014-05-06 16:02:19 -07004583 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 */
Brian Norris49c50b92014-05-06 16:02:19 -07004585static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004587 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004588 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004591 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004592
Boris Brezillon97d90da2017-11-30 18:01:29 +01004593 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594}
4595
4596/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004598 * @mtd: MTD device structure
4599 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004601 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004603static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604{
David Woodhousee0c7d762006-05-13 18:07:53 +01004605 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004609 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004610 * @mtd: MTD device structure
4611 * @instr: erase instruction
4612 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004614 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004616int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4617 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
Adrian Hunter69423d92008-12-10 13:37:21 +00004619 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004620 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00004621 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Brian Norris289c0522011-07-19 10:06:09 -07004623 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4624 __func__, (unsigned long long)instr->addr,
4625 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304627 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004631 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
4633 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004634 page = (int)(instr->addr >> chip->page_shift);
4635 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
4637 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004638 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
4640 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004641 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 /* Check, if it is write protected */
4644 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004645 pr_debug("%s: device is write protected!\n",
4646 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 instr->state = MTD_ERASE_FAILED;
4648 goto erase_exit;
4649 }
4650
4651 /* Loop through the pages */
4652 len = instr->len;
4653
4654 instr->state = MTD_ERASING;
4655
4656 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004657 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004658 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304659 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004660 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4661 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 instr->state = MTD_ERASE_FAILED;
4663 goto erase_exit;
4664 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004665
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004666 /*
4667 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004668 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004669 */
4670 if (page <= chip->pagebuf && chip->pagebuf <
4671 (page + pages_per_block))
4672 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Brian Norris49c50b92014-05-06 16:02:19 -07004674 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
4676 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004677 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004678 pr_debug("%s: failed erase, page 0x%08x\n",
4679 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00004681 instr->fail_addr =
4682 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 goto erase_exit;
4684 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004687 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 page += pages_per_block;
4689
4690 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004691 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004693 chip->select_chip(mtd, -1);
4694 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 }
4696 }
4697 instr->state = MTD_ERASE_DONE;
4698
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004699erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
4703 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004704 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 nand_release_device(mtd);
4706
David Woodhouse49defc02007-10-06 15:01:59 -04004707 /* Do call back function */
4708 if (!ret)
4709 mtd_erase_callback(instr);
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 /* Return more or less happy */
4712 return ret;
4713}
4714
4715/**
4716 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004717 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004719 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004721static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722{
Brian Norris289c0522011-07-19 10:06:09 -07004723 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
4725 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004726 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004728 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729}
4730
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004732 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004733 * @mtd: MTD device structure
4734 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004736static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304738 struct nand_chip *chip = mtd_to_nand(mtd);
4739 int chipnr = (int)(offs >> chip->chip_shift);
4740 int ret;
4741
4742 /* Select the NAND device */
4743 nand_get_device(mtd, FL_READING);
4744 chip->select_chip(mtd, chipnr);
4745
4746 ret = nand_block_checkbad(mtd, offs, 0);
4747
4748 chip->select_chip(mtd, -1);
4749 nand_release_device(mtd);
4750
4751 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752}
4753
4754/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004755 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004756 * @mtd: MTD device structure
4757 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004759static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 int ret;
4762
Florian Fainellif8ac0412010-09-07 13:23:43 +02004763 ret = nand_block_isbad(mtd, ofs);
4764 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004765 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 if (ret > 0)
4767 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004768 return ret;
4769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Brian Norris5a0edb22013-07-30 17:52:58 -07004771 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772}
4773
4774/**
Zach Brown56718422017-01-10 13:30:20 -06004775 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4776 * @mtd: MTD device structure
4777 * @ofs: offset relative to mtd start
4778 * @len: length of mtd
4779 */
4780static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4781{
4782 struct nand_chip *chip = mtd_to_nand(mtd);
4783 u32 part_start_block;
4784 u32 part_end_block;
4785 u32 part_start_die;
4786 u32 part_end_die;
4787
4788 /*
4789 * max_bb_per_die and blocks_per_die used to determine
4790 * the maximum bad block count.
4791 */
4792 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4793 return -ENOTSUPP;
4794
4795 /* Get the start and end of the partition in erase blocks. */
4796 part_start_block = mtd_div_by_eb(ofs, mtd);
4797 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4798
4799 /* Get the start and end LUNs of the partition. */
4800 part_start_die = part_start_block / chip->blocks_per_die;
4801 part_end_die = part_end_block / chip->blocks_per_die;
4802
4803 /*
4804 * Look up the bad blocks per unit and multiply by the number of units
4805 * that the partition spans.
4806 */
4807 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4808}
4809
4810/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004811 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004812 * @mtd: MTD device structure
4813 * @chip: nand chip info structure
4814 * @addr: feature address.
4815 * @subfeature_param: the subfeature parameters, a four bytes array.
4816 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004817static int nand_default_set_features(struct mtd_info *mtd,
4818 struct nand_chip *chip, int addr,
4819 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004820{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004821 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004822}
4823
4824/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004825 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004826 * @mtd: MTD device structure
4827 * @chip: nand chip info structure
4828 * @addr: feature address.
4829 * @subfeature_param: the subfeature parameters, a four bytes array.
4830 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004831static int nand_default_get_features(struct mtd_info *mtd,
4832 struct nand_chip *chip, int addr,
4833 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004834{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004835 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004836}
4837
4838/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004839 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004840 * @mtd: MTD device structure
4841 * @chip: nand chip info structure
4842 * @addr: feature address.
4843 * @subfeature_param: the subfeature parameters, a four bytes array.
4844 *
4845 * Should be used by NAND controller drivers that do not support the SET/GET
4846 * FEATURES operations.
4847 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004848int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4849 int addr, u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004850{
4851 return -ENOTSUPP;
4852}
Miquel Raynalb9587582018-03-19 14:47:19 +01004853EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004854
4855/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004856 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004857 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004858 */
4859static int nand_suspend(struct mtd_info *mtd)
4860{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004861 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004862}
4863
4864/**
4865 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004866 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004867 */
4868static void nand_resume(struct mtd_info *mtd)
4869{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004870 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004871
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004872 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004873 nand_release_device(mtd);
4874 else
Brian Norrisd0370212011-07-19 10:06:08 -07004875 pr_err("%s called for a chip which is not in suspended state\n",
4876 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004877}
4878
Scott Branden72ea4032014-11-20 11:18:05 -08004879/**
4880 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4881 * prevent further operations
4882 * @mtd: MTD device structure
4883 */
4884static void nand_shutdown(struct mtd_info *mtd)
4885{
Brian Norris9ca641b2015-11-09 16:37:28 -08004886 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004887}
4888
Brian Norris8b6e50c2011-05-25 14:59:01 -07004889/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004890static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004891{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004892 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004895 if (!chip->chip_delay)
4896 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004899 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004900 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004903 if (chip->waitfunc == NULL)
4904 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004906 if (!chip->select_chip)
4907 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004908
Huang Shijie4204ccc2013-08-16 10:10:07 +08004909 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004910 if (!chip->set_features)
4911 chip->set_features = nand_default_set_features;
4912 if (!chip->get_features)
4913 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004914
Brian Norris68e80782013-07-18 01:17:02 -07004915 /* If called twice, pointers that depend on busw may need to be reset */
4916 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004917 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4918 if (!chip->read_word)
4919 chip->read_word = nand_read_word;
4920 if (!chip->block_bad)
4921 chip->block_bad = nand_block_bad;
4922 if (!chip->block_markbad)
4923 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004924 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004925 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004926 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4927 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004928 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004929 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004930 if (!chip->scan_bbt)
4931 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004932
4933 if (!chip->controller) {
4934 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02004935 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004936 }
4937
Masahiro Yamada477544c2017-03-30 17:15:05 +09004938 if (!chip->buf_align)
4939 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004940}
4941
Brian Norris8b6e50c2011-05-25 14:59:01 -07004942/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004943static void sanitize_string(uint8_t *s, size_t len)
4944{
4945 ssize_t i;
4946
Brian Norris8b6e50c2011-05-25 14:59:01 -07004947 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004948 s[len - 1] = 0;
4949
Brian Norris8b6e50c2011-05-25 14:59:01 -07004950 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004951 for (i = 0; i < len - 1; i++) {
4952 if (s[i] < ' ' || s[i] > 127)
4953 s[i] = '?';
4954 }
4955
Brian Norris8b6e50c2011-05-25 14:59:01 -07004956 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004957 strim(s);
4958}
4959
4960static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4961{
4962 int i;
4963 while (len--) {
4964 crc ^= *p++ << 8;
4965 for (i = 0; i < 8; i++)
4966 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
4967 }
4968
4969 return crc;
4970}
4971
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004972/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004973static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
4974 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004975{
4976 struct onfi_ext_param_page *ep;
4977 struct onfi_ext_section *s;
4978 struct onfi_ext_ecc_info *ecc;
4979 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004980 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004981 int len;
4982 int i;
4983
4984 len = le16_to_cpu(p->ext_param_page_length) * 16;
4985 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07004986 if (!ep)
4987 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004988
4989 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004990 ret = nand_read_param_page_op(chip, 0, NULL, 0);
4991 if (ret)
4992 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004993
4994 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004995 ret = nand_change_read_column_op(chip,
4996 sizeof(*p) * p->num_of_param_pages,
4997 ep, len, true);
4998 if (ret)
4999 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005000
Boris Brezillon97d90da2017-11-30 18:01:29 +01005001 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005002 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5003 != le16_to_cpu(ep->crc))) {
5004 pr_debug("fail in the CRC.\n");
5005 goto ext_out;
5006 }
5007
5008 /*
5009 * Check the signature.
5010 * Do not strictly follow the ONFI spec, maybe changed in future.
5011 */
5012 if (strncmp(ep->sig, "EPPS", 4)) {
5013 pr_debug("The signature is invalid.\n");
5014 goto ext_out;
5015 }
5016
5017 /* find the ECC section. */
5018 cursor = (uint8_t *)(ep + 1);
5019 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5020 s = ep->sections + i;
5021 if (s->type == ONFI_SECTION_TYPE_2)
5022 break;
5023 cursor += s->length * 16;
5024 }
5025 if (i == ONFI_EXT_SECTION_MAX) {
5026 pr_debug("We can not find the ECC section.\n");
5027 goto ext_out;
5028 }
5029
5030 /* get the info we want. */
5031 ecc = (struct onfi_ext_ecc_info *)cursor;
5032
Brian Norris4ae7d222013-09-16 18:20:21 -07005033 if (!ecc->codeword_size) {
5034 pr_debug("Invalid codeword size\n");
5035 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005036 }
5037
Brian Norris4ae7d222013-09-16 18:20:21 -07005038 chip->ecc_strength_ds = ecc->ecc_bits;
5039 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005040 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005041
5042ext_out:
5043 kfree(ep);
5044 return ret;
5045}
5046
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005047/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005048 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005049 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005050static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005051{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005052 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005053 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005054 char id[4];
5055 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005056
Brian Norris7854d3f2011-06-23 14:12:08 -07005057 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005058 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5059 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005060 return 0;
5061
Boris Brezillon97d90da2017-11-30 18:01:29 +01005062 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5063 if (ret)
5064 return 0;
5065
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005066 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005067 ret = nand_read_data_op(chip, p, sizeof(*p), true);
5068 if (ret)
5069 return 0;
5070
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005071 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
5072 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005073 break;
5074 }
5075 }
5076
Brian Norrisc7f23a72013-08-13 10:51:55 -07005077 if (i == 3) {
5078 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005079 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07005080 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005081
Brian Norris8b6e50c2011-05-25 14:59:01 -07005082 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005083 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005084 if (val & (1 << 5))
5085 chip->onfi_version = 23;
5086 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005087 chip->onfi_version = 22;
5088 else if (val & (1 << 3))
5089 chip->onfi_version = 21;
5090 else if (val & (1 << 2))
5091 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005092 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005093 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005094
5095 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005096 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005097 return 0;
5098 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005099
5100 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5101 sanitize_string(p->model, sizeof(p->model));
5102 if (!mtd->name)
5103 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07005104
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005105 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005106
5107 /*
5108 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5109 * (don't ask me who thought of this...). MTD assumes that these
5110 * dimensions will be power-of-2, so just truncate the remaining area.
5111 */
5112 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5113 mtd->erasesize *= mtd->writesize;
5114
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005115 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005116
5117 /* See erasesize comment */
5118 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005119 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005120 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005121
Zach Brown34da5f52017-01-10 13:30:21 -06005122 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5123 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5124
Huang Shijiee2985fc2013-05-17 11:17:30 +08005125 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005126 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005127
Huang Shijie10c86ba2013-05-17 11:17:26 +08005128 if (p->ecc_bits != 0xff) {
5129 chip->ecc_strength_ds = p->ecc_bits;
5130 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005131 } else if (chip->onfi_version >= 21 &&
5132 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
5133
5134 /*
5135 * The nand_flash_detect_ext_param_page() uses the
5136 * Change Read Column command which maybe not supported
5137 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5138 * now. We do not replace user supplied command function.
5139 */
5140 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5141 chip->cmdfunc = nand_command_lp;
5142
5143 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005144 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005145 pr_warn("Failed to detect ONFI extended param page\n");
5146 } else {
5147 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005148 }
5149
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005150 return 1;
5151}
5152
5153/*
Huang Shijie91361812014-02-21 13:39:40 +08005154 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5155 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005156static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005157{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005158 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08005159 struct nand_jedec_params *p = &chip->jedec_params;
5160 struct jedec_ecc_info *ecc;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005161 char id[5];
5162 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005163
5164 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005165 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5166 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005167 return 0;
5168
Boris Brezillon97d90da2017-11-30 18:01:29 +01005169 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
5170 if (ret)
5171 return 0;
5172
Huang Shijie91361812014-02-21 13:39:40 +08005173 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005174 ret = nand_read_data_op(chip, p, sizeof(*p), true);
5175 if (ret)
5176 return 0;
Huang Shijie91361812014-02-21 13:39:40 +08005177
5178 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5179 le16_to_cpu(p->crc))
5180 break;
5181 }
5182
5183 if (i == 3) {
5184 pr_err("Could not find valid JEDEC parameter page; aborting\n");
5185 return 0;
5186 }
5187
5188 /* Check version */
5189 val = le16_to_cpu(p->revision);
5190 if (val & (1 << 2))
5191 chip->jedec_version = 10;
5192 else if (val & (1 << 1))
5193 chip->jedec_version = 1; /* vendor specific version */
5194
5195 if (!chip->jedec_version) {
5196 pr_info("unsupported JEDEC version: %d\n", val);
5197 return 0;
5198 }
5199
5200 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5201 sanitize_string(p->model, sizeof(p->model));
5202 if (!mtd->name)
5203 mtd->name = p->model;
5204
5205 mtd->writesize = le32_to_cpu(p->byte_per_page);
5206
5207 /* Please reference to the comment for nand_flash_detect_onfi. */
5208 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5209 mtd->erasesize *= mtd->writesize;
5210
5211 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5212
5213 /* Please reference to the comment for nand_flash_detect_onfi. */
5214 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5215 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5216 chip->bits_per_cell = p->bits_per_cell;
5217
5218 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005219 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005220
5221 /* ECC info */
5222 ecc = &p->ecc_info[0];
5223
5224 if (ecc->codeword_size >= 9) {
5225 chip->ecc_strength_ds = ecc->ecc_bits;
5226 chip->ecc_step_ds = 1 << ecc->codeword_size;
5227 } else {
5228 pr_warn("Invalid codeword size\n");
5229 }
5230
5231 return 1;
5232}
5233
5234/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005235 * nand_id_has_period - Check if an ID string has a given wraparound period
5236 * @id_data: the ID string
5237 * @arrlen: the length of the @id_data array
5238 * @period: the period of repitition
5239 *
5240 * Check if an ID string is repeated within a given sequence of bytes at
5241 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005242 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005243 * if the repetition has a period of @period; otherwise, returns zero.
5244 */
5245static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5246{
5247 int i, j;
5248 for (i = 0; i < period; i++)
5249 for (j = i + period; j < arrlen; j += period)
5250 if (id_data[i] != id_data[j])
5251 return 0;
5252 return 1;
5253}
5254
5255/*
5256 * nand_id_len - Get the length of an ID string returned by CMD_READID
5257 * @id_data: the ID string
5258 * @arrlen: the length of the @id_data array
5259
5260 * Returns the length of the ID string, according to known wraparound/trailing
5261 * zero patterns. If no pattern exists, returns the length of the array.
5262 */
5263static int nand_id_len(u8 *id_data, int arrlen)
5264{
5265 int last_nonzero, period;
5266
5267 /* Find last non-zero byte */
5268 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5269 if (id_data[last_nonzero])
5270 break;
5271
5272 /* All zeros */
5273 if (last_nonzero < 0)
5274 return 0;
5275
5276 /* Calculate wraparound period */
5277 for (period = 1; period < arrlen; period++)
5278 if (nand_id_has_period(id_data, arrlen, period))
5279 break;
5280
5281 /* There's a repeated pattern */
5282 if (period < arrlen)
5283 return period;
5284
5285 /* There are trailing zeros */
5286 if (last_nonzero < arrlen - 1)
5287 return last_nonzero + 1;
5288
5289 /* No pattern detected */
5290 return arrlen;
5291}
5292
Huang Shijie7db906b2013-09-25 14:58:11 +08005293/* Extract the bits of per cell from the 3rd byte of the extended ID */
5294static int nand_get_bits_per_cell(u8 cellinfo)
5295{
5296 int bits;
5297
5298 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5299 bits >>= NAND_CI_CELLTYPE_SHIFT;
5300 return bits + 1;
5301}
5302
Brian Norrise3b88bd2012-09-24 20:40:52 -07005303/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005304 * Many new NAND share similar device ID codes, which represent the size of the
5305 * chip. The rest of the parameters must be decoded according to generic or
5306 * manufacturer-specific "extended ID" decoding patterns.
5307 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005308void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005309{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005310 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005311 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005312 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005313 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005314 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005315 /* The 4th id byte is the important one */
5316 extid = id_data[3];
5317
Boris Brezillon01389b62016-06-08 10:30:18 +02005318 /* Calc pagesize */
5319 mtd->writesize = 1024 << (extid & 0x03);
5320 extid >>= 2;
5321 /* Calc oobsize */
5322 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5323 extid >>= 2;
5324 /* Calc blocksize. Blocksize is multiples of 64KiB */
5325 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5326 extid >>= 2;
5327 /* Get buswidth information */
5328 if (extid & 0x1)
5329 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005330}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005331EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005332
5333/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005334 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5335 * decodes a matching ID table entry and assigns the MTD size parameters for
5336 * the chip.
5337 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005338static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005339{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005340 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005341
5342 mtd->erasesize = type->erasesize;
5343 mtd->writesize = type->pagesize;
5344 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005345
Huang Shijie1c195e92013-09-25 14:58:12 +08005346 /* All legacy ID NAND are small-page, SLC */
5347 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005348}
5349
5350/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005351 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5352 * heuristic patterns using various detected parameters (e.g., manufacturer,
5353 * page size, cell-type information).
5354 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005355static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005356{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005357 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005358
5359 /* Set the bad block position */
5360 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5361 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5362 else
5363 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005364}
5365
Huang Shijieec6e87e2013-03-15 11:01:00 +08005366static inline bool is_full_id_nand(struct nand_flash_dev *type)
5367{
5368 return type->id_len;
5369}
5370
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005371static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005372 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005373{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005374 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005375 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005376
Huang Shijieec6e87e2013-03-15 11:01:00 +08005377 if (!strncmp(type->id, id_data, type->id_len)) {
5378 mtd->writesize = type->pagesize;
5379 mtd->erasesize = type->erasesize;
5380 mtd->oobsize = type->oobsize;
5381
Huang Shijie7db906b2013-09-25 14:58:11 +08005382 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005383 chip->chipsize = (uint64_t)type->chipsize << 20;
5384 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005385 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5386 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005387 chip->onfi_timing_mode_default =
5388 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005389
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005390 if (!mtd->name)
5391 mtd->name = type->name;
5392
Huang Shijieec6e87e2013-03-15 11:01:00 +08005393 return true;
5394 }
5395 return false;
5396}
5397
Brian Norris7e74c2d2012-09-24 20:40:49 -07005398/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005399 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5400 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5401 * table.
5402 */
5403static void nand_manufacturer_detect(struct nand_chip *chip)
5404{
5405 /*
5406 * Try manufacturer detection if available and use
5407 * nand_decode_ext_id() otherwise.
5408 */
5409 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005410 chip->manufacturer.desc->ops->detect) {
5411 /* The 3rd id byte holds MLC / multichip data */
5412 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005413 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005414 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005415 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005416 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005417}
5418
5419/*
5420 * Manufacturer initialization. This function is called for all NANDs including
5421 * ONFI and JEDEC compliant ones.
5422 * Manufacturer drivers should put all their specific initialization code in
5423 * their ->init() hook.
5424 */
5425static int nand_manufacturer_init(struct nand_chip *chip)
5426{
5427 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5428 !chip->manufacturer.desc->ops->init)
5429 return 0;
5430
5431 return chip->manufacturer.desc->ops->init(chip);
5432}
5433
5434/*
5435 * Manufacturer cleanup. This function is called for all NANDs including
5436 * ONFI and JEDEC compliant ones.
5437 * Manufacturer drivers should put all their specific cleanup code in their
5438 * ->cleanup() hook.
5439 */
5440static void nand_manufacturer_cleanup(struct nand_chip *chip)
5441{
5442 /* Release manufacturer private data */
5443 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5444 chip->manufacturer.desc->ops->cleanup)
5445 chip->manufacturer.desc->ops->cleanup(chip);
5446}
5447
5448/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005449 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005450 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005451static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005452{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005453 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005454 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005455 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005456 u8 *id_data = chip->id.data;
5457 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Karl Beldanef89a882008-09-15 14:37:29 +02005459 /*
5460 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005461 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005462 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005463 ret = nand_reset(chip, 0);
5464 if (ret)
5465 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005466
5467 /* Select the device */
5468 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005469
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005471 ret = nand_readid_op(chip, 0, id_data, 2);
5472 if (ret)
5473 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
5475 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005476 maf_id = id_data[0];
5477 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
Brian Norris8b6e50c2011-05-25 14:59:01 -07005479 /*
5480 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005481 * interface concerns can cause random data which looks like a
5482 * possibly credible NAND flash to appear. If the two results do
5483 * not match, ignore the device completely.
5484 */
5485
Brian Norris4aef9b72012-09-24 20:40:48 -07005486 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005487 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5488 if (ret)
5489 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005490
Boris Brezillon7f501f02016-05-24 19:20:05 +02005491 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005492 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005493 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005494 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005495 }
5496
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005497 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005498
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005499 /* Try to identify manufacturer */
5500 manufacturer = nand_get_manufacturer(maf_id);
5501 chip->manufacturer.desc = manufacturer;
5502
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005503 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005504 type = nand_flash_ids;
5505
Boris Brezillon29a198a2016-05-24 20:17:48 +02005506 /*
5507 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5508 * override it.
5509 * This is required to make sure initial NAND bus width set by the
5510 * NAND controller driver is coherent with the real NAND bus width
5511 * (extracted by auto-detection code).
5512 */
5513 busw = chip->options & NAND_BUSWIDTH_16;
5514
5515 /*
5516 * The flag is only set (never cleared), reset it to its default value
5517 * before starting auto-detection.
5518 */
5519 chip->options &= ~NAND_BUSWIDTH_16;
5520
Huang Shijieec6e87e2013-03-15 11:01:00 +08005521 for (; type->name != NULL; type++) {
5522 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005523 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005524 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005525 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005526 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005527 }
5528 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005529
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005530 chip->onfi_version = 0;
5531 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005532 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005533 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005534 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005535
5536 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005537 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08005538 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005539 }
5540
David Woodhouse5e81e882010-02-26 18:32:56 +00005541 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005542 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005543
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02005544 if (!mtd->name)
5545 mtd->name = type->name;
5546
Adrian Hunter69423d92008-12-10 13:37:21 +00005547 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005548
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005549 if (!type->pagesize)
5550 nand_manufacturer_detect(chip);
5551 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005552 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005553
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005554 /* Get chip options */
5555 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005556
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005557ident_done:
5558
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005559 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005560 WARN_ON(busw & NAND_BUSWIDTH_16);
5561 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005562 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5563 /*
5564 * Check, if buswidth is correct. Hardware drivers should set
5565 * chip correct!
5566 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005567 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005568 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005569 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5570 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005571 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5572 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005573 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005574 }
5575
Boris Brezillon7f501f02016-05-24 19:20:05 +02005576 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005577
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005578 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005579 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005580 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005581 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005582
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005583 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005584 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005585 if (chip->chipsize & 0xffffffff)
5586 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005587 else {
5588 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5589 chip->chip_shift += 32 - 1;
5590 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005591
Masahiro Yamada14157f82017-09-13 11:05:50 +09005592 if (chip->chip_shift - chip->page_shift > 16)
5593 chip->options |= NAND_ROW_ADDR_3;
5594
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005595 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005596 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005597
Brian Norris8b6e50c2011-05-25 14:59:01 -07005598 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005599 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5600 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005601
Ezequiel Garcia20171642013-11-25 08:30:31 -03005602 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005603 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005604
5605 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005606 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5607 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005608 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005609 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5610 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005611 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005612 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5613 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005614
Rafał Miłecki3755a992014-10-21 00:01:04 +02005615 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005616 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005617 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005618 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005619}
5620
Boris Brezillond48f62b2016-04-01 14:54:32 +02005621static const char * const nand_ecc_modes[] = {
5622 [NAND_ECC_NONE] = "none",
5623 [NAND_ECC_SOFT] = "soft",
5624 [NAND_ECC_HW] = "hw",
5625 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5626 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005627 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005628};
5629
5630static int of_get_nand_ecc_mode(struct device_node *np)
5631{
5632 const char *pm;
5633 int err, i;
5634
5635 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5636 if (err < 0)
5637 return err;
5638
5639 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5640 if (!strcasecmp(pm, nand_ecc_modes[i]))
5641 return i;
5642
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005643 /*
5644 * For backward compatibility we support few obsoleted values that don't
5645 * have their mappings into nand_ecc_modes_t anymore (they were merged
5646 * with other enums).
5647 */
5648 if (!strcasecmp(pm, "soft_bch"))
5649 return NAND_ECC_SOFT;
5650
Boris Brezillond48f62b2016-04-01 14:54:32 +02005651 return -ENODEV;
5652}
5653
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005654static const char * const nand_ecc_algos[] = {
5655 [NAND_ECC_HAMMING] = "hamming",
5656 [NAND_ECC_BCH] = "bch",
5657};
5658
Boris Brezillond48f62b2016-04-01 14:54:32 +02005659static int of_get_nand_ecc_algo(struct device_node *np)
5660{
5661 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005662 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005663
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005664 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5665 if (!err) {
5666 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5667 if (!strcasecmp(pm, nand_ecc_algos[i]))
5668 return i;
5669 return -ENODEV;
5670 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005671
5672 /*
5673 * For backward compatibility we also read "nand-ecc-mode" checking
5674 * for some obsoleted values that were specifying ECC algorithm.
5675 */
5676 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5677 if (err < 0)
5678 return err;
5679
5680 if (!strcasecmp(pm, "soft"))
5681 return NAND_ECC_HAMMING;
5682 else if (!strcasecmp(pm, "soft_bch"))
5683 return NAND_ECC_BCH;
5684
5685 return -ENODEV;
5686}
5687
5688static int of_get_nand_ecc_step_size(struct device_node *np)
5689{
5690 int ret;
5691 u32 val;
5692
5693 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5694 return ret ? ret : val;
5695}
5696
5697static int of_get_nand_ecc_strength(struct device_node *np)
5698{
5699 int ret;
5700 u32 val;
5701
5702 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5703 return ret ? ret : val;
5704}
5705
5706static int of_get_nand_bus_width(struct device_node *np)
5707{
5708 u32 val;
5709
5710 if (of_property_read_u32(np, "nand-bus-width", &val))
5711 return 8;
5712
5713 switch (val) {
5714 case 8:
5715 case 16:
5716 return val;
5717 default:
5718 return -EIO;
5719 }
5720}
5721
5722static bool of_get_nand_on_flash_bbt(struct device_node *np)
5723{
5724 return of_property_read_bool(np, "nand-on-flash-bbt");
5725}
5726
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005727static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005728{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005729 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005730 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005731
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005732 if (!dn)
5733 return 0;
5734
Brian Norris5844fee2015-01-23 00:22:27 -08005735 if (of_get_nand_bus_width(dn) == 16)
5736 chip->options |= NAND_BUSWIDTH_16;
5737
5738 if (of_get_nand_on_flash_bbt(dn))
5739 chip->bbt_options |= NAND_BBT_USE_FLASH;
5740
5741 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005742 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005743 ecc_strength = of_get_nand_ecc_strength(dn);
5744 ecc_step = of_get_nand_ecc_step_size(dn);
5745
Brian Norris5844fee2015-01-23 00:22:27 -08005746 if (ecc_mode >= 0)
5747 chip->ecc.mode = ecc_mode;
5748
Rafał Miłecki79082452016-03-23 11:19:02 +01005749 if (ecc_algo >= 0)
5750 chip->ecc.algo = ecc_algo;
5751
Brian Norris5844fee2015-01-23 00:22:27 -08005752 if (ecc_strength >= 0)
5753 chip->ecc.strength = ecc_strength;
5754
5755 if (ecc_step > 0)
5756 chip->ecc.size = ecc_step;
5757
Boris Brezillonba78ee02016-06-08 17:04:22 +02005758 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5759 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5760
Brian Norris5844fee2015-01-23 00:22:27 -08005761 return 0;
5762}
5763
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005764/**
David Woodhouse3b85c322006-09-25 17:06:53 +01005765 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005766 * @mtd: MTD device structure
5767 * @maxchips: number of chips to scan for
5768 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005769 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005770 * This is the first phase of the normal nand_scan() function. It reads the
5771 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005772 *
5773 */
David Woodhouse5e81e882010-02-26 18:32:56 +00005774int nand_scan_ident(struct mtd_info *mtd, int maxchips,
5775 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005776{
Cai Zhiyongbb770822013-12-25 20:11:15 +08005777 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01005778 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08005779 int ret;
5780
Miquel Raynal17fa8042017-11-30 18:01:31 +01005781 /* Enforce the right timings for reset/detection */
5782 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5783
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005784 ret = nand_dt_init(chip);
5785 if (ret)
5786 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005787
Brian Norrisf7a8e382016-01-05 10:39:45 -08005788 if (!mtd->name && mtd->dev.parent)
5789 mtd->name = dev_name(mtd->dev.parent);
5790
Miquel Raynal8878b122017-11-09 14:16:45 +01005791 /*
5792 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5793 * populated.
5794 */
5795 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005796 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005797 * Default functions assigned for ->cmdfunc() and
5798 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005799 */
Miquel Raynal8878b122017-11-09 14:16:45 +01005800 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5801 pr_err("->cmd_ctrl() should be provided\n");
5802 return -EINVAL;
5803 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005804 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005805
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005806 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005807 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005808
5809 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005810 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005811 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005812 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005813 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005814 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005815 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 }
5817
Boris Brezillon7f501f02016-05-24 19:20:05 +02005818 nand_maf_id = chip->id.data[0];
5819 nand_dev_id = chip->id.data[1];
5820
Huang Shijie07300162012-11-09 16:23:45 +08005821 chip->select_chip(mtd, -1);
5822
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005823 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005824 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005825 u8 id[2];
5826
Karl Beldanef89a882008-09-15 14:37:29 +02005827 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005828 nand_reset(chip, i);
5829
5830 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005832 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005834 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08005835 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 break;
Huang Shijie07300162012-11-09 16:23:45 +08005837 }
5838 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 }
5840 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005841 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005842
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005844 chip->numchips = i;
5845 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
David Woodhouse3b85c322006-09-25 17:06:53 +01005847 return 0;
5848}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005849EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01005850
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005851static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
5852{
5853 struct nand_chip *chip = mtd_to_nand(mtd);
5854 struct nand_ecc_ctrl *ecc = &chip->ecc;
5855
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005856 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005857 return -EINVAL;
5858
5859 switch (ecc->algo) {
5860 case NAND_ECC_HAMMING:
5861 ecc->calculate = nand_calculate_ecc;
5862 ecc->correct = nand_correct_data;
5863 ecc->read_page = nand_read_page_swecc;
5864 ecc->read_subpage = nand_read_subpage;
5865 ecc->write_page = nand_write_page_swecc;
5866 ecc->read_page_raw = nand_read_page_raw;
5867 ecc->write_page_raw = nand_write_page_raw;
5868 ecc->read_oob = nand_read_oob_std;
5869 ecc->write_oob = nand_write_oob_std;
5870 if (!ecc->size)
5871 ecc->size = 256;
5872 ecc->bytes = 3;
5873 ecc->strength = 1;
5874 return 0;
5875 case NAND_ECC_BCH:
5876 if (!mtd_nand_has_bch()) {
5877 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5878 return -EINVAL;
5879 }
5880 ecc->calculate = nand_bch_calculate_ecc;
5881 ecc->correct = nand_bch_correct_data;
5882 ecc->read_page = nand_read_page_swecc;
5883 ecc->read_subpage = nand_read_subpage;
5884 ecc->write_page = nand_write_page_swecc;
5885 ecc->read_page_raw = nand_read_page_raw;
5886 ecc->write_page_raw = nand_write_page_raw;
5887 ecc->read_oob = nand_read_oob_std;
5888 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005889
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005890 /*
5891 * Board driver should supply ecc.size and ecc.strength
5892 * values to select how many bits are correctable.
5893 * Otherwise, default to 4 bits for large page devices.
5894 */
5895 if (!ecc->size && (mtd->oobsize >= 64)) {
5896 ecc->size = 512;
5897 ecc->strength = 4;
5898 }
5899
5900 /*
5901 * if no ecc placement scheme was provided pickup the default
5902 * large page one.
5903 */
5904 if (!mtd->ooblayout) {
5905 /* handle large page devices only */
5906 if (mtd->oobsize < 64) {
5907 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5908 return -EINVAL;
5909 }
5910
5911 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005912
5913 }
5914
5915 /*
5916 * We can only maximize ECC config when the default layout is
5917 * used, otherwise we don't know how many bytes can really be
5918 * used.
5919 */
5920 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5921 ecc->options & NAND_ECC_MAXIMIZE) {
5922 int steps, bytes;
5923
5924 /* Always prefer 1k blocks over 512bytes ones */
5925 ecc->size = 1024;
5926 steps = mtd->writesize / ecc->size;
5927
5928 /* Reserve 2 bytes for the BBM */
5929 bytes = (mtd->oobsize - 2) / steps;
5930 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005931 }
5932
5933 /* See nand_bch_init() for details. */
5934 ecc->bytes = 0;
5935 ecc->priv = nand_bch_init(mtd);
5936 if (!ecc->priv) {
5937 WARN(1, "BCH ECC initialization failed!\n");
5938 return -EINVAL;
5939 }
5940 return 0;
5941 default:
5942 WARN(1, "Unsupported ECC algorithm!\n");
5943 return -EINVAL;
5944 }
5945}
5946
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005947/**
5948 * nand_check_ecc_caps - check the sanity of preset ECC settings
5949 * @chip: nand chip info structure
5950 * @caps: ECC caps info structure
5951 * @oobavail: OOB size that the ECC engine can use
5952 *
5953 * When ECC step size and strength are already set, check if they are supported
5954 * by the controller and the calculated ECC bytes fit within the chip's OOB.
5955 * On success, the calculated ECC bytes is set.
5956 */
5957int nand_check_ecc_caps(struct nand_chip *chip,
5958 const struct nand_ecc_caps *caps, int oobavail)
5959{
5960 struct mtd_info *mtd = nand_to_mtd(chip);
5961 const struct nand_ecc_step_info *stepinfo;
5962 int preset_step = chip->ecc.size;
5963 int preset_strength = chip->ecc.strength;
5964 int nsteps, ecc_bytes;
5965 int i, j;
5966
5967 if (WARN_ON(oobavail < 0))
5968 return -EINVAL;
5969
5970 if (!preset_step || !preset_strength)
5971 return -ENODATA;
5972
5973 nsteps = mtd->writesize / preset_step;
5974
5975 for (i = 0; i < caps->nstepinfos; i++) {
5976 stepinfo = &caps->stepinfos[i];
5977
5978 if (stepinfo->stepsize != preset_step)
5979 continue;
5980
5981 for (j = 0; j < stepinfo->nstrengths; j++) {
5982 if (stepinfo->strengths[j] != preset_strength)
5983 continue;
5984
5985 ecc_bytes = caps->calc_ecc_bytes(preset_step,
5986 preset_strength);
5987 if (WARN_ON_ONCE(ecc_bytes < 0))
5988 return ecc_bytes;
5989
5990 if (ecc_bytes * nsteps > oobavail) {
5991 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
5992 preset_step, preset_strength);
5993 return -ENOSPC;
5994 }
5995
5996 chip->ecc.bytes = ecc_bytes;
5997
5998 return 0;
5999 }
6000 }
6001
6002 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6003 preset_step, preset_strength);
6004
6005 return -ENOTSUPP;
6006}
6007EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
6008
6009/**
6010 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6011 * @chip: nand chip info structure
6012 * @caps: ECC engine caps info structure
6013 * @oobavail: OOB size that the ECC engine can use
6014 *
6015 * If a chip's ECC requirement is provided, try to meet it with the least
6016 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6017 * On success, the chosen ECC settings are set.
6018 */
6019int nand_match_ecc_req(struct nand_chip *chip,
6020 const struct nand_ecc_caps *caps, int oobavail)
6021{
6022 struct mtd_info *mtd = nand_to_mtd(chip);
6023 const struct nand_ecc_step_info *stepinfo;
6024 int req_step = chip->ecc_step_ds;
6025 int req_strength = chip->ecc_strength_ds;
6026 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6027 int best_step, best_strength, best_ecc_bytes;
6028 int best_ecc_bytes_total = INT_MAX;
6029 int i, j;
6030
6031 if (WARN_ON(oobavail < 0))
6032 return -EINVAL;
6033
6034 /* No information provided by the NAND chip */
6035 if (!req_step || !req_strength)
6036 return -ENOTSUPP;
6037
6038 /* number of correctable bits the chip requires in a page */
6039 req_corr = mtd->writesize / req_step * req_strength;
6040
6041 for (i = 0; i < caps->nstepinfos; i++) {
6042 stepinfo = &caps->stepinfos[i];
6043 step_size = stepinfo->stepsize;
6044
6045 for (j = 0; j < stepinfo->nstrengths; j++) {
6046 strength = stepinfo->strengths[j];
6047
6048 /*
6049 * If both step size and strength are smaller than the
6050 * chip's requirement, it is not easy to compare the
6051 * resulted reliability.
6052 */
6053 if (step_size < req_step && strength < req_strength)
6054 continue;
6055
6056 if (mtd->writesize % step_size)
6057 continue;
6058
6059 nsteps = mtd->writesize / step_size;
6060
6061 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6062 if (WARN_ON_ONCE(ecc_bytes < 0))
6063 continue;
6064 ecc_bytes_total = ecc_bytes * nsteps;
6065
6066 if (ecc_bytes_total > oobavail ||
6067 strength * nsteps < req_corr)
6068 continue;
6069
6070 /*
6071 * We assume the best is to meet the chip's requrement
6072 * with the least number of ECC bytes.
6073 */
6074 if (ecc_bytes_total < best_ecc_bytes_total) {
6075 best_ecc_bytes_total = ecc_bytes_total;
6076 best_step = step_size;
6077 best_strength = strength;
6078 best_ecc_bytes = ecc_bytes;
6079 }
6080 }
6081 }
6082
6083 if (best_ecc_bytes_total == INT_MAX)
6084 return -ENOTSUPP;
6085
6086 chip->ecc.size = best_step;
6087 chip->ecc.strength = best_strength;
6088 chip->ecc.bytes = best_ecc_bytes;
6089
6090 return 0;
6091}
6092EXPORT_SYMBOL_GPL(nand_match_ecc_req);
6093
6094/**
6095 * nand_maximize_ecc - choose the max ECC strength available
6096 * @chip: nand chip info structure
6097 * @caps: ECC engine caps info structure
6098 * @oobavail: OOB size that the ECC engine can use
6099 *
6100 * Choose the max ECC strength that is supported on the controller, and can fit
6101 * within the chip's OOB. On success, the chosen ECC settings are set.
6102 */
6103int nand_maximize_ecc(struct nand_chip *chip,
6104 const struct nand_ecc_caps *caps, int oobavail)
6105{
6106 struct mtd_info *mtd = nand_to_mtd(chip);
6107 const struct nand_ecc_step_info *stepinfo;
6108 int step_size, strength, nsteps, ecc_bytes, corr;
6109 int best_corr = 0;
6110 int best_step = 0;
6111 int best_strength, best_ecc_bytes;
6112 int i, j;
6113
6114 if (WARN_ON(oobavail < 0))
6115 return -EINVAL;
6116
6117 for (i = 0; i < caps->nstepinfos; i++) {
6118 stepinfo = &caps->stepinfos[i];
6119 step_size = stepinfo->stepsize;
6120
6121 /* If chip->ecc.size is already set, respect it */
6122 if (chip->ecc.size && step_size != chip->ecc.size)
6123 continue;
6124
6125 for (j = 0; j < stepinfo->nstrengths; j++) {
6126 strength = stepinfo->strengths[j];
6127
6128 if (mtd->writesize % step_size)
6129 continue;
6130
6131 nsteps = mtd->writesize / step_size;
6132
6133 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6134 if (WARN_ON_ONCE(ecc_bytes < 0))
6135 continue;
6136
6137 if (ecc_bytes * nsteps > oobavail)
6138 continue;
6139
6140 corr = strength * nsteps;
6141
6142 /*
6143 * If the number of correctable bits is the same,
6144 * bigger step_size has more reliability.
6145 */
6146 if (corr > best_corr ||
6147 (corr == best_corr && step_size > best_step)) {
6148 best_corr = corr;
6149 best_step = step_size;
6150 best_strength = strength;
6151 best_ecc_bytes = ecc_bytes;
6152 }
6153 }
6154 }
6155
6156 if (!best_corr)
6157 return -ENOTSUPP;
6158
6159 chip->ecc.size = best_step;
6160 chip->ecc.strength = best_strength;
6161 chip->ecc.bytes = best_ecc_bytes;
6162
6163 return 0;
6164}
6165EXPORT_SYMBOL_GPL(nand_maximize_ecc);
6166
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006167/*
6168 * Check if the chip configuration meet the datasheet requirements.
6169
6170 * If our configuration corrects A bits per B bytes and the minimum
6171 * required correction level is X bits per Y bytes, then we must ensure
6172 * both of the following are true:
6173 *
6174 * (1) A / B >= X / Y
6175 * (2) A >= X
6176 *
6177 * Requirement (1) ensures we can correct for the required bitflip density.
6178 * Requirement (2) ensures we can correct even when all bitflips are clumped
6179 * in the same sector.
6180 */
6181static bool nand_ecc_strength_good(struct mtd_info *mtd)
6182{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006183 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006184 struct nand_ecc_ctrl *ecc = &chip->ecc;
6185 int corr, ds_corr;
6186
6187 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6188 /* Not enough information */
6189 return true;
6190
6191 /*
6192 * We get the number of corrected bits per page to compare
6193 * the correction density.
6194 */
6195 corr = (mtd->writesize * ecc->strength) / ecc->size;
6196 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6197
6198 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6199}
David Woodhouse3b85c322006-09-25 17:06:53 +01006200
6201/**
6202 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006203 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01006204 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006205 * This is the second phase of the normal nand_scan() function. It fills out
6206 * all the uninitialized function pointers with the defaults and scans for a
6207 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006208 */
6209int nand_scan_tail(struct mtd_info *mtd)
6210{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006211 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08006212 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006213 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006214
Brian Norrise2414f42012-02-06 13:44:00 -08006215 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006216 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006217 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006218 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006219 }
Brian Norrise2414f42012-02-06 13:44:00 -08006220
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006221 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006222 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006223 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006224
Boris Brezillonf84674b2017-06-02 12:18:24 +02006225 /*
6226 * FIXME: some NAND manufacturer drivers expect the first die to be
6227 * selected when manufacturer->init() is called. They should be fixed
6228 * to explictly select the relevant die when interacting with the NAND
6229 * chip.
6230 */
6231 chip->select_chip(mtd, 0);
6232 ret = nand_manufacturer_init(chip);
6233 chip->select_chip(mtd, -1);
6234 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006235 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006236
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006237 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006238 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006239
6240 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006241 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006242 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006243 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006244 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006245 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006248 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 break;
6250 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006251 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006252 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006255 /*
6256 * Expose the whole OOB area to users if ECC_NONE
6257 * is passed. We could do that for all kind of
6258 * ->oobsize, but we must keep the old large/small
6259 * page with ECC layout when ->oobsize <= 128 for
6260 * compatibility reasons.
6261 */
6262 if (ecc->mode == NAND_ECC_NONE) {
6263 mtd_set_ooblayout(mtd,
6264 &nand_ooblayout_lp_ops);
6265 break;
6266 }
6267
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006268 WARN(1, "No oob scheme defined for oobsize %d\n",
6269 mtd->oobsize);
6270 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006271 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 }
6273 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006274
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006275 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006276 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006277 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006278 */
David Woodhouse956e9442006-09-25 17:12:39 +01006279
Huang Shijie97de79e02013-10-18 14:20:53 +08006280 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006281 case NAND_ECC_HW_OOB_FIRST:
6282 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006283 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006284 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6285 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006286 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006287 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006288 if (!ecc->read_page)
6289 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006290
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006291 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006292 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006293 if (!ecc->read_page)
6294 ecc->read_page = nand_read_page_hwecc;
6295 if (!ecc->write_page)
6296 ecc->write_page = nand_write_page_hwecc;
6297 if (!ecc->read_page_raw)
6298 ecc->read_page_raw = nand_read_page_raw;
6299 if (!ecc->write_page_raw)
6300 ecc->write_page_raw = nand_write_page_raw;
6301 if (!ecc->read_oob)
6302 ecc->read_oob = nand_read_oob_std;
6303 if (!ecc->write_oob)
6304 ecc->write_oob = nand_write_oob_std;
6305 if (!ecc->read_subpage)
6306 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006307 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006308 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006309
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006310 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006311 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6312 (!ecc->read_page ||
6313 ecc->read_page == nand_read_page_hwecc ||
6314 !ecc->write_page ||
6315 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006316 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6317 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006318 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006319 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006320 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006321 if (!ecc->read_page)
6322 ecc->read_page = nand_read_page_syndrome;
6323 if (!ecc->write_page)
6324 ecc->write_page = nand_write_page_syndrome;
6325 if (!ecc->read_page_raw)
6326 ecc->read_page_raw = nand_read_page_raw_syndrome;
6327 if (!ecc->write_page_raw)
6328 ecc->write_page_raw = nand_write_page_raw_syndrome;
6329 if (!ecc->read_oob)
6330 ecc->read_oob = nand_read_oob_syndrome;
6331 if (!ecc->write_oob)
6332 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006333
Huang Shijie97de79e02013-10-18 14:20:53 +08006334 if (mtd->writesize >= ecc->size) {
6335 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006336 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6337 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006338 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006339 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006340 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006341 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006342 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6343 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006344 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006345 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006347 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006348 ret = nand_set_ecc_soft_ops(mtd);
6349 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006350 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006351 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006352 }
6353 break;
6354
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006355 case NAND_ECC_ON_DIE:
6356 if (!ecc->read_page || !ecc->write_page) {
6357 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6358 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006359 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006360 }
6361 if (!ecc->read_oob)
6362 ecc->read_oob = nand_read_oob_std;
6363 if (!ecc->write_oob)
6364 ecc->write_oob = nand_write_oob_std;
6365 break;
6366
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006367 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006368 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006369 ecc->read_page = nand_read_page_raw;
6370 ecc->write_page = nand_write_page_raw;
6371 ecc->read_oob = nand_read_oob_std;
6372 ecc->read_page_raw = nand_read_page_raw;
6373 ecc->write_page_raw = nand_write_page_raw;
6374 ecc->write_oob = nand_write_oob_std;
6375 ecc->size = mtd->writesize;
6376 ecc->bytes = 0;
6377 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006379
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006381 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6382 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006383 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006386 if (ecc->correct || ecc->calculate) {
6387 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6388 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6389 if (!ecc->calc_buf || !ecc->code_buf) {
6390 ret = -ENOMEM;
6391 goto err_nand_manuf_cleanup;
6392 }
6393 }
6394
Brian Norris9ce244b2011-08-30 18:45:37 -07006395 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006396 if (!ecc->read_oob_raw)
6397 ecc->read_oob_raw = ecc->read_oob;
6398 if (!ecc->write_oob_raw)
6399 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006400
Boris Brezillon846031d2016-02-03 20:11:00 +01006401 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006402 mtd->ecc_strength = ecc->strength;
6403 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006404
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006405 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006406 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006407 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006408 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006409 ecc->steps = mtd->writesize / ecc->size;
6410 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006411 WARN(1, "Invalid ECC parameters\n");
6412 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006413 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006415 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006416 if (ecc->total > mtd->oobsize) {
6417 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6418 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006419 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006420 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006421
Boris Brezillon846031d2016-02-03 20:11:00 +01006422 /*
6423 * The number of bytes available for a client to place data into
6424 * the out of band area.
6425 */
6426 ret = mtd_ooblayout_count_freebytes(mtd);
6427 if (ret < 0)
6428 ret = 0;
6429
6430 mtd->oobavail = ret;
6431
6432 /* ECC sanity check: warn if it's too weak */
6433 if (!nand_ecc_strength_good(mtd))
6434 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6435 mtd->name);
6436
Brian Norris8b6e50c2011-05-25 14:59:01 -07006437 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006438 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006439 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006440 case 2:
6441 mtd->subpage_sft = 1;
6442 break;
6443 case 4:
6444 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006445 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006446 mtd->subpage_sft = 2;
6447 break;
6448 }
6449 }
6450 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6451
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006452 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006453 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006456 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006458 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306459 switch (ecc->mode) {
6460 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306461 if (chip->page_shift > 9)
6462 chip->options |= NAND_SUBPAGE_READ;
6463 break;
6464
6465 default:
6466 break;
6467 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006468
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006470 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006471 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6472 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006473 mtd->_erase = nand_erase;
6474 mtd->_point = NULL;
6475 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006476 mtd->_panic_write = panic_nand_write;
6477 mtd->_read_oob = nand_read_oob;
6478 mtd->_write_oob = nand_write_oob;
6479 mtd->_sync = nand_sync;
6480 mtd->_lock = NULL;
6481 mtd->_unlock = NULL;
6482 mtd->_suspend = nand_suspend;
6483 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006484 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006485 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006486 mtd->_block_isbad = nand_block_isbad;
6487 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006488 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006489 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006491 /*
6492 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6493 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6494 * properly set.
6495 */
6496 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006497 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498
Boris Brezillonf84674b2017-06-02 12:18:24 +02006499 /* Initialize the ->data_interface field. */
6500 ret = nand_init_data_interface(chip);
6501 if (ret)
6502 goto err_nand_manuf_cleanup;
6503
6504 /* Enter fastest possible mode on all dies. */
6505 for (i = 0; i < chip->numchips; i++) {
6506 chip->select_chip(mtd, i);
6507 ret = nand_setup_data_interface(chip, i);
6508 chip->select_chip(mtd, -1);
6509
6510 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006511 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006512 }
6513
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006514 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006515 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517
6518 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07006519 ret = chip->scan_bbt(mtd);
6520 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006521 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006522
Brian Norris44d41822017-05-01 17:04:50 -07006523 return 0;
6524
Boris Brezillonf84674b2017-06-02 12:18:24 +02006525
6526err_nand_manuf_cleanup:
6527 nand_manufacturer_cleanup(chip);
6528
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006529err_free_buf:
6530 kfree(chip->data_buf);
6531 kfree(ecc->code_buf);
6532 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006533
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006534 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006536EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537
Brian Norris8b6e50c2011-05-25 14:59:01 -07006538/*
6539 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006540 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07006541 * to call us from in-kernel code if the core NAND support is modular.
6542 */
David Woodhouse3b85c322006-09-25 17:06:53 +01006543#ifdef MODULE
6544#define caller_is_module() (1)
6545#else
6546#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06006547 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01006548#endif
6549
6550/**
6551 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006552 * @mtd: MTD device structure
6553 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01006554 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006555 * This fills out all the uninitialized function pointers with the defaults.
6556 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006557 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006558 */
6559int nand_scan(struct mtd_info *mtd, int maxchips)
6560{
6561 int ret;
6562
David Woodhouse5e81e882010-02-26 18:32:56 +00006563 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01006564 if (!ret)
6565 ret = nand_scan_tail(mtd);
6566 return ret;
6567}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006568EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01006569
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006571 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6572 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006573 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006574void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006576 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006577 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006578 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6579
Jesper Juhlfa671642005-11-07 01:01:27 -08006580 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006581 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006582 kfree(chip->data_buf);
6583 kfree(chip->ecc.code_buf);
6584 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006585
6586 /* Free bad block descriptor memory */
6587 if (chip->badblock_pattern && chip->badblock_pattern->options
6588 & NAND_BBT_DYNAMICSTRUCT)
6589 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006590
6591 /* Free manufacturer priv data. */
6592 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006594EXPORT_SYMBOL_GPL(nand_cleanup);
6595
6596/**
6597 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6598 * held by the NAND device
6599 * @mtd: MTD device structure
6600 */
6601void nand_release(struct mtd_info *mtd)
6602{
6603 mtd_device_unregister(mtd);
6604 nand_cleanup(mtd_to_nand(mtd));
6605}
David Woodhousee0c7d762006-05-13 18:07:53 +01006606EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006607
David Woodhousee0c7d762006-05-13 18:07:53 +01006608MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006609MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6610MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006611MODULE_DESCRIPTION("Generic NAND flash driver code");