blob: 4099d8a1e25e628324decd4eb25e02bc9452d3aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010039#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/types.h>
41#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020042#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010044#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020047#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020049#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010050
Huang Shijie6a8214a2012-11-19 14:43:30 +080051static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020053static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
Boris Brezillon41b207a2016-02-03 19:06:15 +010056/* Define default oob placement schemes for large and small page devices */
57static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59{
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020068 if (mtd->oobsize == 16)
69 oobregion->length = 4;
70 else
71 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010072 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020073 if (mtd->oobsize == 8)
74 return -ERANGE;
75
Boris Brezillon41b207a2016-02-03 19:06:15 +010076 oobregion->offset = 6;
77 oobregion->length = ecc->total - 4;
78 }
79
80 return 0;
81}
82
83static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85{
86 if (section > 1)
87 return -ERANGE;
88
89 if (mtd->oobsize == 16) {
90 if (section)
91 return -ERANGE;
92
93 oobregion->length = 8;
94 oobregion->offset = 8;
95 } else {
96 oobregion->length = 2;
97 if (!section)
98 oobregion->offset = 3;
99 else
100 oobregion->offset = 6;
101 }
102
103 return 0;
104}
105
106const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107 .ecc = nand_ooblayout_ecc_sp,
108 .free = nand_ooblayout_free_sp,
109};
110EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
111
112static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113 struct mtd_oob_region *oobregion)
114{
115 struct nand_chip *chip = mtd_to_nand(mtd);
116 struct nand_ecc_ctrl *ecc = &chip->ecc;
117
Miquel Raynal882fd152017-08-26 17:19:15 +0200118 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100119 return -ERANGE;
120
121 oobregion->length = ecc->total;
122 oobregion->offset = mtd->oobsize - oobregion->length;
123
124 return 0;
125}
126
127static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128 struct mtd_oob_region *oobregion)
129{
130 struct nand_chip *chip = mtd_to_nand(mtd);
131 struct nand_ecc_ctrl *ecc = &chip->ecc;
132
133 if (section)
134 return -ERANGE;
135
136 oobregion->length = mtd->oobsize - ecc->total - 2;
137 oobregion->offset = 2;
138
139 return 0;
140}
141
142const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143 .ecc = nand_ooblayout_ecc_lp,
144 .free = nand_ooblayout_free_lp,
145};
146EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200147
Alexander Couzens6a623e02017-05-02 12:19:00 +0200148/*
149 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
150 * are placed at a fixed offset.
151 */
152static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153 struct mtd_oob_region *oobregion)
154{
155 struct nand_chip *chip = mtd_to_nand(mtd);
156 struct nand_ecc_ctrl *ecc = &chip->ecc;
157
158 if (section)
159 return -ERANGE;
160
161 switch (mtd->oobsize) {
162 case 64:
163 oobregion->offset = 40;
164 break;
165 case 128:
166 oobregion->offset = 80;
167 break;
168 default:
169 return -EINVAL;
170 }
171
172 oobregion->length = ecc->total;
173 if (oobregion->offset + oobregion->length > mtd->oobsize)
174 return -ERANGE;
175
176 return 0;
177}
178
179static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180 struct mtd_oob_region *oobregion)
181{
182 struct nand_chip *chip = mtd_to_nand(mtd);
183 struct nand_ecc_ctrl *ecc = &chip->ecc;
184 int ecc_offset = 0;
185
186 if (section < 0 || section > 1)
187 return -ERANGE;
188
189 switch (mtd->oobsize) {
190 case 64:
191 ecc_offset = 40;
192 break;
193 case 128:
194 ecc_offset = 80;
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 if (section == 0) {
201 oobregion->offset = 2;
202 oobregion->length = ecc_offset - 2;
203 } else {
204 oobregion->offset = ecc_offset + ecc->total;
205 oobregion->length = mtd->oobsize - oobregion->offset;
206 }
207
208 return 0;
209}
210
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100211static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200212 .ecc = nand_ooblayout_ecc_lp_hamming,
213 .free = nand_ooblayout_free_lp_hamming,
214};
215
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530216static int check_offs_len(struct mtd_info *mtd,
217 loff_t ofs, uint64_t len)
218{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100219 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530220 int ret = 0;
221
222 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
228 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300229 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700230 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530231 ret = -EINVAL;
232 }
233
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530234 return ret;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700239 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800241 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100243static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100245 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200247 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 spin_lock(&chip->controller->lock);
249 chip->controller->active = NULL;
250 chip->state = FL_READY;
251 wake_up(&chip->controller->wq);
252 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700257 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700259 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200261static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100263 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200264 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/**
Masanari Iida064a7692012-11-09 23:20:58 +0900268 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700269 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700271 * Default read function for 16bit buswidth with endianness conversion.
272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200274static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100276 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700282 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700284 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286static u16 nand_read_word(struct mtd_info *mtd)
287{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100288 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200289 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700294 * @mtd: MTD device structure
295 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * Default select function for 1 chip devices.
298 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200299static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100301 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200302
303 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200305 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309
310 default:
311 BUG();
312 }
313}
314
315/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100316 * nand_write_byte - [DEFAULT] write single byte to chip
317 * @mtd: MTD device structure
318 * @byte: value to write
319 *
320 * Default function to write a byte to I/O[7:0]
321 */
322static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
323{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100324 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100325
326 chip->write_buf(mtd, &byte, 1);
327}
328
329/**
330 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
331 * @mtd: MTD device structure
332 * @byte: value to write
333 *
334 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
335 */
336static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
337{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100338 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100339 uint16_t word = byte;
340
341 /*
342 * It's not entirely clear what should happen to I/O[15:8] when writing
343 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
344 *
345 * When the host supports a 16-bit bus width, only data is
346 * transferred at the 16-bit width. All address and command line
347 * transfers shall use only the lower 8-bits of the data bus. During
348 * command transfers, the host may place any value on the upper
349 * 8-bits of the data bus. During address transfers, the host shall
350 * set the upper 8-bits of the data bus to 00h.
351 *
Miquel Raynalb9587582018-03-19 14:47:19 +0100352 * One user of the write_byte callback is nand_set_features. The
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100353 * four parameters are specified to be written to I/O[7:0], but this is
354 * neither an address nor a command transfer. Let's assume a 0 on the
355 * upper I/O lines is OK.
356 */
357 chip->write_buf(mtd, (uint8_t *)&word, 2);
358}
359
360/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700362 * @mtd: MTD device structure
363 * @buf: data buffer
364 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700366 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200368static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100370 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Alexander Shiyan76413832013-04-13 09:32:13 +0400372 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000376 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700377 * @mtd: MTD device structure
378 * @buf: buffer to store date
379 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700381 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200383static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100385 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alexander Shiyan76413832013-04-13 09:32:13 +0400387 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700392 * @mtd: MTD device structure
393 * @buf: data buffer
394 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700396 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200398static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100400 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000402
Alexander Shiyan76413832013-04-13 09:32:13 +0400403 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000407 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700408 * @mtd: MTD device structure
409 * @buf: buffer to store date
410 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700412 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200414static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100416 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Alexander Shiyan76413832013-04-13 09:32:13 +0400419 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700424 * @mtd: MTD device structure
425 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000427 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530429static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Masahiro Yamadac120e752017-03-23 05:07:01 +0900431 int page, page_end, res;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100432 struct nand_chip *chip = mtd_to_nand(mtd);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900433 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Brian Norris5fb15492011-05-31 16:31:21 -0700435 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700436 ofs += mtd->erasesize - mtd->writesize;
437
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100438 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900439 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100440
Masahiro Yamadac120e752017-03-23 05:07:01 +0900441 for (; page < page_end; page++) {
442 res = chip->ecc.read_oob(mtd, chip, page);
443 if (res)
444 return res;
445
446 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000447
Brian Norriscdbec052012-01-13 18:11:48 -0800448 if (likely(chip->badblockbits == 8))
449 res = bad != 0xFF;
450 else
451 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900452 if (res)
453 return res;
454 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200455
Masahiro Yamadac120e752017-03-23 05:07:01 +0900456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700460 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700461 * @mtd: MTD device structure
462 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700464 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700465 * specific driver. It provides the details for writing a bad block marker to a
466 * block.
467 */
468static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
469{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100470 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700471 struct mtd_oob_ops ops;
472 uint8_t buf[2] = { 0, 0 };
473 int ret = 0, res, i = 0;
474
Brian Norris0ec56dc2015-02-28 02:02:30 -0800475 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700476 ops.oobbuf = buf;
477 ops.ooboffs = chip->badblockpos;
478 if (chip->options & NAND_BUSWIDTH_16) {
479 ops.ooboffs &= ~0x01;
480 ops.len = ops.ooblen = 2;
481 } else {
482 ops.len = ops.ooblen = 1;
483 }
484 ops.mode = MTD_OPS_PLACE_OOB;
485
486 /* Write to first/last page(s) if necessary */
487 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
488 ofs += mtd->erasesize - mtd->writesize;
489 do {
490 res = nand_do_write_oob(mtd, ofs, &ops);
491 if (!ret)
492 ret = res;
493
494 i++;
495 ofs += mtd->writesize;
496 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
497
498 return ret;
499}
500
501/**
502 * nand_block_markbad_lowlevel - mark a block bad
503 * @mtd: MTD device structure
504 * @ofs: offset from device start
505 *
506 * This function performs the generic NAND bad block marking steps (i.e., bad
507 * block table(s) and/or marker(s)). We only allow the hardware driver to
508 * specify how to write bad block markers to OOB (chip->block_markbad).
509 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700510 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300511 *
Brian Norrise2414f42012-02-06 13:44:00 -0800512 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700513 * (2) write bad block marker to OOB area of affected block (unless flag
514 * NAND_BBT_NO_OOB_BBM is present)
515 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300516 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700517 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800518 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700520static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100522 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700523 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000524
Brian Norrisb32843b2013-07-30 17:52:59 -0700525 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800526 struct erase_info einfo;
527
528 /* Attempt erase before marking OOB */
529 memset(&einfo, 0, sizeof(einfo));
530 einfo.mtd = mtd;
531 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300532 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800533 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800534
Brian Norrisb32843b2013-07-30 17:52:59 -0700535 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800536 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700537 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300538 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200539 }
Brian Norrise2414f42012-02-06 13:44:00 -0800540
Brian Norrisb32843b2013-07-30 17:52:59 -0700541 /* Mark block bad in BBT */
542 if (chip->bbt) {
543 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800544 if (!ret)
545 ret = res;
546 }
547
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200548 if (!ret)
549 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300550
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000554/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700556 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700558 * Check, if the device is write protected. The function expects, that the
559 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100561static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100563 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100564 u8 status;
565 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200566
Brian Norris8b6e50c2011-05-25 14:59:01 -0700567 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200568 if (chip->options & NAND_BROKEN_XD)
569 return 0;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100572 ret = nand_status_op(chip, &status);
573 if (ret)
574 return ret;
575
576 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800580 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700581 * @mtd: MTD device structure
582 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300583 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800584 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300585 */
586static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
587{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100588 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300589
590 if (!chip->bbt)
591 return 0;
592 /* Return info from the table */
593 return nand_isreserved_bbt(mtd, ofs);
594}
595
596/**
597 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
598 * @mtd: MTD device structure
599 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700600 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
602 * Check, if the block is bad. Either by reading the bad block table or
603 * calling of the scan function.
604 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530605static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100607 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000608
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200609 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530610 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100613 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200616/**
617 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700618 * @mtd: MTD device structure
619 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200620 *
621 * Helper function for nand_wait_ready used when needing to wait in interrupt
622 * context.
623 */
624static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
625{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100626 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200627 int i;
628
629 /* Wait for the device to get ready */
630 for (i = 0; i < timeo; i++) {
631 if (chip->dev_ready(mtd))
632 break;
633 touch_softlockup_watchdog();
634 mdelay(1);
635 }
636}
637
Alex Smithb70af9b2015-10-06 14:52:07 +0100638/**
639 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
640 * @mtd: MTD device structure
641 *
642 * Wait for the ready pin after a command, and warn if a timeout occurs.
643 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100644void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000645{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100646 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100647 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000648
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200649 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100650 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200651
Brian Norris7854d3f2011-06-23 14:12:08 -0700652 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100653 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000654 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300656 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100657 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000658 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100659
Brian Norris9ebfdf52016-03-04 17:19:23 -0800660 if (!chip->dev_ready(mtd))
661 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000662}
David Woodhouse4b648b02006-09-25 17:05:24 +0100663EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200666 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
667 * @mtd: MTD device structure
668 * @timeo: Timeout in ms
669 *
670 * Wait for status ready (i.e. command done) or timeout.
671 */
672static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
673{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100674 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100675 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200676
677 timeo = jiffies + msecs_to_jiffies(timeo);
678 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100679 u8 status;
680
681 ret = nand_read_data_op(chip, &status, sizeof(status), true);
682 if (ret)
683 return;
684
685 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200686 break;
687 touch_softlockup_watchdog();
688 } while (time_before(jiffies, timeo));
689};
690
691/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100692 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
693 * @chip: NAND chip structure
694 * @timeout_ms: Timeout in ms
695 *
696 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
697 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
698 * returned.
699 *
700 * This helper is intended to be used when the controller does not have access
701 * to the NAND R/B pin.
702 *
703 * Be aware that calling this helper from an ->exec_op() implementation means
704 * ->exec_op() must be re-entrant.
705 *
706 * Return 0 if the NAND chip is ready, a negative error otherwise.
707 */
708int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
709{
710 u8 status = 0;
711 int ret;
712
713 if (!chip->exec_op)
714 return -ENOTSUPP;
715
716 ret = nand_status_op(chip, NULL);
717 if (ret)
718 return ret;
719
720 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
721 do {
722 ret = nand_read_data_op(chip, &status, sizeof(status), true);
723 if (ret)
724 break;
725
726 if (status & NAND_STATUS_READY)
727 break;
728
729 /*
730 * Typical lowest execution time for a tR on most NANDs is 10us,
731 * use this as polling delay before doing something smarter (ie.
732 * deriving a delay from the timeout value, timeout_ms/ratio).
733 */
734 udelay(10);
735 } while (time_before(jiffies, timeout_ms));
736
737 /*
738 * We have to exit READ_STATUS mode in order to read real data on the
739 * bus in case the WAITRDY instruction is preceding a DATA_IN
740 * instruction.
741 */
742 nand_exit_status_op(chip);
743
744 if (ret)
745 return ret;
746
747 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
748};
749EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
750
751/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700753 * @mtd: MTD device structure
754 * @command: the command to be sent
755 * @column: the column address for this command, -1 if none
756 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700758 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200759 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200761static void nand_command(struct mtd_info *mtd, unsigned int command,
762 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100764 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200765 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Brian Norris8b6e50c2011-05-25 14:59:01 -0700767 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (command == NAND_CMD_SEQIN) {
769 int readcmd;
770
Joern Engel28318772006-05-22 23:18:05 +0200771 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200773 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 readcmd = NAND_CMD_READOOB;
775 } else if (column < 256) {
776 /* First 256 bytes --> READ0 */
777 readcmd = NAND_CMD_READ0;
778 } else {
779 column -= 256;
780 readcmd = NAND_CMD_READ1;
781 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200782 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200783 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100785 if (command != NAND_CMD_NONE)
786 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Brian Norris8b6e50c2011-05-25 14:59:01 -0700788 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200789 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
790 /* Serially input address */
791 if (column != -1) {
792 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800793 if (chip->options & NAND_BUSWIDTH_16 &&
794 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200795 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200796 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200797 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200799 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200800 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200801 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200802 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900803 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200804 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200805 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200806 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000807
808 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700809 * Program and erase have their own busy handlers status and sequential
810 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100811 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000813
Miquel Raynaldf467892017-11-08 17:00:27 +0100814 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 case NAND_CMD_PAGEPROG:
816 case NAND_CMD_ERASE1:
817 case NAND_CMD_ERASE2:
818 case NAND_CMD_SEQIN:
819 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900820 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900821 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return;
823
824 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200825 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200827 udelay(chip->chip_delay);
828 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200829 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200830 chip->cmd_ctrl(mtd,
831 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200832 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
833 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return;
835
David Woodhousee0c7d762006-05-13 18:07:53 +0100836 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200837 case NAND_CMD_READ0:
838 /*
839 * READ0 is sometimes used to exit GET STATUS mode. When this
840 * is the case no address cycles are requested, and we can use
841 * this information to detect that we should not wait for the
842 * device to be ready.
843 */
844 if (column == -1 && page_addr == -1)
845 return;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000848 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * If we don't have access to the busy pin, we apply the given
850 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100851 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200852 if (!chip->dev_ready) {
853 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700857 /*
858 * Apply this short delay always to ensure that we do wait tWB in
859 * any case on any machine.
860 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100861 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000862
863 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200866static void nand_ccs_delay(struct nand_chip *chip)
867{
868 /*
869 * The controller already takes care of waiting for tCCS when the RNDIN
870 * or RNDOUT command is sent, return directly.
871 */
872 if (!(chip->options & NAND_WAIT_TCCS))
873 return;
874
875 /*
876 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
877 * (which should be safe for all NANDs).
878 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100879 if (chip->setup_data_interface)
880 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200881 else
882 ndelay(500);
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/**
886 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700887 * @mtd: MTD device structure
888 * @command: the command to be sent
889 * @column: the column address for this command, -1 if none
890 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200892 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700893 * devices. We don't have the separate regions as we have in the small page
894 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200896static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
897 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100899 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* Emulate NAND_CMD_READOOB */
902 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200903 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 command = NAND_CMD_READ0;
905 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000906
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200907 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100908 if (command != NAND_CMD_NONE)
909 chip->cmd_ctrl(mtd, command,
910 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200913 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /* Serially input address */
916 if (column != -1) {
917 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800918 if (chip->options & NAND_BUSWIDTH_16 &&
919 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200921 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200922 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200923
Brian Norrisf5b88de2016-10-03 09:49:35 -0700924 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200925 if (!nand_opcode_8bits(command))
926 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200929 chip->cmd_ctrl(mtd, page_addr, ctrl);
930 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200931 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900932 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200933 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200934 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200937 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000938
939 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700940 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100941 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000944
Miquel Raynaldf467892017-11-08 17:00:27 +0100945 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 case NAND_CMD_CACHEDPROG:
947 case NAND_CMD_PAGEPROG:
948 case NAND_CMD_ERASE1:
949 case NAND_CMD_ERASE2:
950 case NAND_CMD_SEQIN:
951 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900952 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900953 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000954 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200956 case NAND_CMD_RNDIN:
957 nand_ccs_delay(chip);
958 return;
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200961 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200963 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200964 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
965 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
966 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
967 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200968 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
969 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return;
971
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200972 case NAND_CMD_RNDOUT:
973 /* No ready / busy check necessary */
974 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
975 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
976 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
977 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200978
979 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200980 return;
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200983 /*
984 * READ0 is sometimes used to exit GET STATUS mode. When this
985 * is the case no address cycles are requested, and we can use
986 * this information to detect that READSTART should not be
987 * issued.
988 */
989 if (column == -1 && page_addr == -1)
990 return;
991
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200992 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
993 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
994 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
995 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000996
David Woodhousee0c7d762006-05-13 18:07:53 +0100997 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000999 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -07001001 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +01001002 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001003 if (!chip->dev_ready) {
1004 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Thomas Gleixner3b887752005-02-22 21:56:49 +00001008
Brian Norris8b6e50c2011-05-25 14:59:01 -07001009 /*
1010 * Apply this short delay always to ensure that we do wait tWB in
1011 * any case on any machine.
1012 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001013 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +00001014
1015 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001019 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001020 * @chip: the nand chip descriptor
1021 * @mtd: MTD device structure
1022 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001023 *
1024 * Used when in panic, no locks are taken.
1025 */
1026static void panic_nand_get_device(struct nand_chip *chip,
1027 struct mtd_info *mtd, int new_state)
1028{
Brian Norris7854d3f2011-06-23 14:12:08 -07001029 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001030 chip->controller->active = chip;
1031 chip->state = new_state;
1032}
1033
1034/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001036 * @mtd: MTD device structure
1037 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 *
1039 * Get the device and lock it for exclusive access
1040 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001041static int
Huang Shijie6a8214a2012-11-19 14:43:30 +08001042nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001044 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001045 spinlock_t *lock = &chip->controller->lock;
1046 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +01001047 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001048retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001049 spin_lock(lock);
1050
vimal singhb8b3ee92009-07-09 20:41:22 +05301051 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001052 if (!chip->controller->active)
1053 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001054
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001055 if (chip->controller->active == chip && chip->state == FL_READY) {
1056 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001057 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01001058 return 0;
1059 }
1060 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001061 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1062 chip->state = FL_PM_SUSPENDED;
1063 spin_unlock(lock);
1064 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001065 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001066 }
1067 set_current_state(TASK_UNINTERRUPTIBLE);
1068 add_wait_queue(wq, &wait);
1069 spin_unlock(lock);
1070 schedule();
1071 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 goto retry;
1073}
1074
1075/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001076 * panic_nand_wait - [GENERIC] wait until the command is done
1077 * @mtd: MTD device structure
1078 * @chip: NAND chip structure
1079 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001080 *
1081 * Wait for command done. This is a helper function for nand_wait used when
1082 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001083 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001084 */
1085static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1086 unsigned long timeo)
1087{
1088 int i;
1089 for (i = 0; i < timeo; i++) {
1090 if (chip->dev_ready) {
1091 if (chip->dev_ready(mtd))
1092 break;
1093 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001094 int ret;
1095 u8 status;
1096
1097 ret = nand_read_data_op(chip, &status, sizeof(status),
1098 true);
1099 if (ret)
1100 return;
1101
1102 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001103 break;
1104 }
1105 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001106 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001107}
1108
1109/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001110 * nand_wait - [DEFAULT] wait until the command is done
1111 * @mtd: MTD device structure
1112 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001114 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001115 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001116static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118
Alex Smithb70af9b2015-10-06 14:52:07 +01001119 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001120 u8 status;
1121 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Brian Norris8b6e50c2011-05-25 14:59:01 -07001123 /*
1124 * Apply this short delay always to ensure that we do wait tWB in any
1125 * case on any machine.
1126 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001127 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Boris Brezillon97d90da2017-11-30 18:01:29 +01001129 ret = nand_status_op(chip, NULL);
1130 if (ret)
1131 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001133 if (in_interrupt() || oops_in_progress)
1134 panic_nand_wait(mtd, chip, timeo);
1135 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001136 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001137 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001138 if (chip->dev_ready) {
1139 if (chip->dev_ready(mtd))
1140 break;
1141 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001142 ret = nand_read_data_op(chip, &status,
1143 sizeof(status), true);
1144 if (ret)
1145 return ret;
1146
1147 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001148 break;
1149 }
1150 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001151 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001153
Boris Brezillon97d90da2017-11-30 18:01:29 +01001154 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1155 if (ret)
1156 return ret;
1157
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001158 /* This can happen if in case of timeout or buggy dev_ready */
1159 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return status;
1161}
1162
Miquel Raynal97baea12018-03-19 14:47:20 +01001163static bool nand_supports_set_get_features(struct nand_chip *chip)
1164{
1165 return (chip->onfi_version &&
1166 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1167 ONFI_OPT_CMD_SET_GET_FEATURES));
1168}
1169
1170/**
1171 * nand_get_features - wrapper to perform a GET_FEATURE
1172 * @chip: NAND chip info structure
1173 * @addr: feature address
1174 * @subfeature_param: the subfeature parameters, a four bytes array
1175 *
1176 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1177 * operation cannot be handled.
1178 */
1179int nand_get_features(struct nand_chip *chip, int addr,
1180 u8 *subfeature_param)
1181{
1182 struct mtd_info *mtd = nand_to_mtd(chip);
1183
1184 if (!nand_supports_set_get_features(chip))
1185 return -ENOTSUPP;
1186
1187 return chip->get_features(mtd, chip, addr, subfeature_param);
1188}
1189EXPORT_SYMBOL_GPL(nand_get_features);
1190
1191/**
1192 * nand_set_features - wrapper to perform a SET_FEATURE
1193 * @chip: NAND chip info structure
1194 * @addr: feature address
1195 * @subfeature_param: the subfeature parameters, a four bytes array
1196 *
1197 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1198 * operation cannot be handled.
1199 */
1200int nand_set_features(struct nand_chip *chip, int addr,
1201 u8 *subfeature_param)
1202{
1203 struct mtd_info *mtd = nand_to_mtd(chip);
1204
1205 if (!nand_supports_set_get_features(chip))
1206 return -ENOTSUPP;
1207
1208 return chip->set_features(mtd, chip, addr, subfeature_param);
1209}
1210EXPORT_SYMBOL_GPL(nand_set_features);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001213 * nand_reset_data_interface - Reset data interface and timings
1214 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001215 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001216 *
1217 * Reset the Data interface and timings to ONFI mode 0.
1218 *
1219 * Returns 0 for success or negative error code otherwise.
1220 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001221static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001222{
1223 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001224 int ret;
1225
1226 if (!chip->setup_data_interface)
1227 return 0;
1228
1229 /*
1230 * The ONFI specification says:
1231 * "
1232 * To transition from NV-DDR or NV-DDR2 to the SDR data
1233 * interface, the host shall use the Reset (FFh) command
1234 * using SDR timing mode 0. A device in any timing mode is
1235 * required to recognize Reset (FFh) command issued in SDR
1236 * timing mode 0.
1237 * "
1238 *
1239 * Configure the data interface in SDR mode and set the
1240 * timings to timing mode 0.
1241 */
1242
Miquel Raynal17fa8042017-11-30 18:01:31 +01001243 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1244 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001245 if (ret)
1246 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1247
1248 return ret;
1249}
1250
1251/**
1252 * nand_setup_data_interface - Setup the best data interface and timings
1253 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001254 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001255 *
1256 * Find and configure the best data interface and NAND timings supported by
1257 * the chip and the driver.
1258 * First tries to retrieve supported timing modes from ONFI information,
1259 * and if the NAND chip does not support ONFI, relies on the
1260 * ->onfi_timing_mode_default specified in the nand_ids table.
1261 *
1262 * Returns 0 for success or negative error code otherwise.
1263 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001264static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001265{
1266 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal97baea12018-03-19 14:47:20 +01001267 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1268 chip->onfi_timing_mode_default,
1269 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001270 int ret;
1271
Miquel Raynal17fa8042017-11-30 18:01:31 +01001272 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001273 return 0;
1274
Miquel Raynal993447b2018-03-19 14:47:21 +01001275 /* Change the mode on the chip side (if supported by the NAND chip) */
1276 if (nand_supports_set_get_features(chip)) {
Miquel Raynal29714d62018-03-19 14:47:23 +01001277 chip->select_chip(mtd, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001278 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1279 tmode_param);
Miquel Raynal29714d62018-03-19 14:47:23 +01001280 chip->select_chip(mtd, -1);
Miquel Raynal993447b2018-03-19 14:47:21 +01001281 if (ret)
1282 return ret;
1283 }
Boris Brezillond8e725d2016-09-15 10:32:50 +02001284
Miquel Raynal97baea12018-03-19 14:47:20 +01001285 /* Change the mode on the controller side */
Miquel Raynal415ae782018-03-19 14:47:24 +01001286 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
1287 if (ret)
1288 return ret;
1289
1290 /* Check the mode has been accepted by the chip, if supported */
1291 if (!nand_supports_set_get_features(chip))
1292 return 0;
1293
1294 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
1295 chip->select_chip(mtd, chipnr);
1296 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1297 tmode_param);
1298 chip->select_chip(mtd, -1);
1299 if (ret)
1300 goto err_reset_chip;
1301
1302 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1303 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1304 chip->onfi_timing_mode_default);
1305 goto err_reset_chip;
1306 }
1307
1308 return 0;
1309
1310err_reset_chip:
1311 /*
1312 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1313 * timing mode.
1314 */
1315 nand_reset_data_interface(chip, chipnr);
1316 chip->select_chip(mtd, chipnr);
1317 nand_reset_op(chip);
1318 chip->select_chip(mtd, -1);
1319
1320 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001321}
1322
1323/**
1324 * nand_init_data_interface - find the best data interface and timings
1325 * @chip: The NAND chip
1326 *
1327 * Find the best data interface and NAND timings supported by the chip
1328 * and the driver.
1329 * First tries to retrieve supported timing modes from ONFI information,
1330 * and if the NAND chip does not support ONFI, relies on the
1331 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1332 * function nand_chip->data_interface is initialized with the best timing mode
1333 * available.
1334 *
1335 * Returns 0 for success or negative error code otherwise.
1336 */
1337static int nand_init_data_interface(struct nand_chip *chip)
1338{
1339 struct mtd_info *mtd = nand_to_mtd(chip);
1340 int modes, mode, ret;
1341
1342 if (!chip->setup_data_interface)
1343 return 0;
1344
1345 /*
1346 * First try to identify the best timings from ONFI parameters and
1347 * if the NAND does not support ONFI, fallback to the default ONFI
1348 * timing mode.
1349 */
1350 modes = onfi_get_async_timing_mode(chip);
1351 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1352 if (!chip->onfi_timing_mode_default)
1353 return 0;
1354
1355 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1356 }
1357
Boris Brezillond8e725d2016-09-15 10:32:50 +02001358
1359 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001360 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001361 if (ret)
1362 continue;
1363
Miquel Raynald787b8b2017-12-22 18:12:41 +01001364 /*
1365 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1366 * controller supports the requested timings.
1367 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001368 ret = chip->setup_data_interface(mtd,
1369 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001370 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001371 if (!ret) {
1372 chip->onfi_timing_mode_default = mode;
1373 break;
1374 }
1375 }
1376
1377 return 0;
1378}
1379
Boris Brezillond8e725d2016-09-15 10:32:50 +02001380/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001381 * nand_fill_column_cycles - fill the column cycles of an address
1382 * @chip: The NAND chip
1383 * @addrs: Array of address cycles to fill
1384 * @offset_in_page: The offset in the page
1385 *
1386 * Fills the first or the first two bytes of the @addrs field depending
1387 * on the NAND bus width and the page size.
1388 *
1389 * Returns the number of cycles needed to encode the column, or a negative
1390 * error code in case one of the arguments is invalid.
1391 */
1392static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1393 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Miquel Raynal8878b122017-11-09 14:16:45 +01001395 struct mtd_info *mtd = nand_to_mtd(chip);
1396
1397 /* Make sure the offset is less than the actual page size. */
1398 if (offset_in_page > mtd->writesize + mtd->oobsize)
1399 return -EINVAL;
1400
1401 /*
1402 * On small page NANDs, there's a dedicated command to access the OOB
1403 * area, and the column address is relative to the start of the OOB
1404 * area, not the start of the page. Asjust the address accordingly.
1405 */
1406 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1407 offset_in_page -= mtd->writesize;
1408
1409 /*
1410 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1411 * wide, then it must be divided by 2.
1412 */
1413 if (chip->options & NAND_BUSWIDTH_16) {
1414 if (WARN_ON(offset_in_page % 2))
1415 return -EINVAL;
1416
1417 offset_in_page /= 2;
1418 }
1419
1420 addrs[0] = offset_in_page;
1421
1422 /*
1423 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1424 * need 2
1425 */
1426 if (mtd->writesize <= 512)
1427 return 1;
1428
1429 addrs[1] = offset_in_page >> 8;
1430
1431 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Miquel Raynal8878b122017-11-09 14:16:45 +01001434static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1435 unsigned int offset_in_page, void *buf,
1436 unsigned int len)
1437{
1438 struct mtd_info *mtd = nand_to_mtd(chip);
1439 const struct nand_sdr_timings *sdr =
1440 nand_get_sdr_timings(&chip->data_interface);
1441 u8 addrs[4];
1442 struct nand_op_instr instrs[] = {
1443 NAND_OP_CMD(NAND_CMD_READ0, 0),
1444 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1445 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1446 PSEC_TO_NSEC(sdr->tRR_min)),
1447 NAND_OP_DATA_IN(len, buf, 0),
1448 };
1449 struct nand_operation op = NAND_OPERATION(instrs);
1450 int ret;
1451
1452 /* Drop the DATA_IN instruction if len is set to 0. */
1453 if (!len)
1454 op.ninstrs--;
1455
1456 if (offset_in_page >= mtd->writesize)
1457 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1458 else if (offset_in_page >= 256 &&
1459 !(chip->options & NAND_BUSWIDTH_16))
1460 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1461
1462 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1463 if (ret < 0)
1464 return ret;
1465
1466 addrs[1] = page;
1467 addrs[2] = page >> 8;
1468
1469 if (chip->options & NAND_ROW_ADDR_3) {
1470 addrs[3] = page >> 16;
1471 instrs[1].ctx.addr.naddrs++;
1472 }
1473
1474 return nand_exec_op(chip, &op);
1475}
1476
1477static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1478 unsigned int offset_in_page, void *buf,
1479 unsigned int len)
1480{
1481 const struct nand_sdr_timings *sdr =
1482 nand_get_sdr_timings(&chip->data_interface);
1483 u8 addrs[5];
1484 struct nand_op_instr instrs[] = {
1485 NAND_OP_CMD(NAND_CMD_READ0, 0),
1486 NAND_OP_ADDR(4, addrs, 0),
1487 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1488 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1489 PSEC_TO_NSEC(sdr->tRR_min)),
1490 NAND_OP_DATA_IN(len, buf, 0),
1491 };
1492 struct nand_operation op = NAND_OPERATION(instrs);
1493 int ret;
1494
1495 /* Drop the DATA_IN instruction if len is set to 0. */
1496 if (!len)
1497 op.ninstrs--;
1498
1499 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1500 if (ret < 0)
1501 return ret;
1502
1503 addrs[2] = page;
1504 addrs[3] = page >> 8;
1505
1506 if (chip->options & NAND_ROW_ADDR_3) {
1507 addrs[4] = page >> 16;
1508 instrs[1].ctx.addr.naddrs++;
1509 }
1510
1511 return nand_exec_op(chip, &op);
1512}
1513
1514/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001515 * nand_read_page_op - Do a READ PAGE operation
1516 * @chip: The NAND chip
1517 * @page: page to read
1518 * @offset_in_page: offset within the page
1519 * @buf: buffer used to store the data
1520 * @len: length of the buffer
1521 *
1522 * This function issues a READ PAGE operation.
1523 * This function does not select/unselect the CS line.
1524 *
1525 * Returns 0 on success, a negative error code otherwise.
1526 */
1527int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1528 unsigned int offset_in_page, void *buf, unsigned int len)
1529{
1530 struct mtd_info *mtd = nand_to_mtd(chip);
1531
1532 if (len && !buf)
1533 return -EINVAL;
1534
1535 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1536 return -EINVAL;
1537
Miquel Raynal8878b122017-11-09 14:16:45 +01001538 if (chip->exec_op) {
1539 if (mtd->writesize > 512)
1540 return nand_lp_exec_read_page_op(chip, page,
1541 offset_in_page, buf,
1542 len);
1543
1544 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1545 buf, len);
1546 }
1547
Boris Brezillon97d90da2017-11-30 18:01:29 +01001548 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1549 if (len)
1550 chip->read_buf(mtd, buf, len);
1551
1552 return 0;
1553}
1554EXPORT_SYMBOL_GPL(nand_read_page_op);
1555
1556/**
1557 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1558 * @chip: The NAND chip
1559 * @page: parameter page to read
1560 * @buf: buffer used to store the data
1561 * @len: length of the buffer
1562 *
1563 * This function issues a READ PARAMETER PAGE operation.
1564 * This function does not select/unselect the CS line.
1565 *
1566 * Returns 0 on success, a negative error code otherwise.
1567 */
1568static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1569 unsigned int len)
1570{
1571 struct mtd_info *mtd = nand_to_mtd(chip);
1572 unsigned int i;
1573 u8 *p = buf;
1574
1575 if (len && !buf)
1576 return -EINVAL;
1577
Miquel Raynal8878b122017-11-09 14:16:45 +01001578 if (chip->exec_op) {
1579 const struct nand_sdr_timings *sdr =
1580 nand_get_sdr_timings(&chip->data_interface);
1581 struct nand_op_instr instrs[] = {
1582 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1583 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1584 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1585 PSEC_TO_NSEC(sdr->tRR_min)),
1586 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1587 };
1588 struct nand_operation op = NAND_OPERATION(instrs);
1589
1590 /* Drop the DATA_IN instruction if len is set to 0. */
1591 if (!len)
1592 op.ninstrs--;
1593
1594 return nand_exec_op(chip, &op);
1595 }
1596
Boris Brezillon97d90da2017-11-30 18:01:29 +01001597 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1598 for (i = 0; i < len; i++)
1599 p[i] = chip->read_byte(mtd);
1600
1601 return 0;
1602}
1603
1604/**
1605 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1606 * @chip: The NAND chip
1607 * @offset_in_page: offset within the page
1608 * @buf: buffer used to store the data
1609 * @len: length of the buffer
1610 * @force_8bit: force 8-bit bus access
1611 *
1612 * This function issues a CHANGE READ COLUMN operation.
1613 * This function does not select/unselect the CS line.
1614 *
1615 * Returns 0 on success, a negative error code otherwise.
1616 */
1617int nand_change_read_column_op(struct nand_chip *chip,
1618 unsigned int offset_in_page, void *buf,
1619 unsigned int len, bool force_8bit)
1620{
1621 struct mtd_info *mtd = nand_to_mtd(chip);
1622
1623 if (len && !buf)
1624 return -EINVAL;
1625
1626 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1627 return -EINVAL;
1628
Miquel Raynal8878b122017-11-09 14:16:45 +01001629 /* Small page NANDs do not support column change. */
1630 if (mtd->writesize <= 512)
1631 return -ENOTSUPP;
1632
1633 if (chip->exec_op) {
1634 const struct nand_sdr_timings *sdr =
1635 nand_get_sdr_timings(&chip->data_interface);
1636 u8 addrs[2] = {};
1637 struct nand_op_instr instrs[] = {
1638 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1639 NAND_OP_ADDR(2, addrs, 0),
1640 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1641 PSEC_TO_NSEC(sdr->tCCS_min)),
1642 NAND_OP_DATA_IN(len, buf, 0),
1643 };
1644 struct nand_operation op = NAND_OPERATION(instrs);
1645 int ret;
1646
1647 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1648 if (ret < 0)
1649 return ret;
1650
1651 /* Drop the DATA_IN instruction if len is set to 0. */
1652 if (!len)
1653 op.ninstrs--;
1654
1655 instrs[3].ctx.data.force_8bit = force_8bit;
1656
1657 return nand_exec_op(chip, &op);
1658 }
1659
Boris Brezillon97d90da2017-11-30 18:01:29 +01001660 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1661 if (len)
1662 chip->read_buf(mtd, buf, len);
1663
1664 return 0;
1665}
1666EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1667
1668/**
1669 * nand_read_oob_op - Do a READ OOB operation
1670 * @chip: The NAND chip
1671 * @page: page to read
1672 * @offset_in_oob: offset within the OOB area
1673 * @buf: buffer used to store the data
1674 * @len: length of the buffer
1675 *
1676 * This function issues a READ OOB operation.
1677 * This function does not select/unselect the CS line.
1678 *
1679 * Returns 0 on success, a negative error code otherwise.
1680 */
1681int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1682 unsigned int offset_in_oob, void *buf, unsigned int len)
1683{
1684 struct mtd_info *mtd = nand_to_mtd(chip);
1685
1686 if (len && !buf)
1687 return -EINVAL;
1688
1689 if (offset_in_oob + len > mtd->oobsize)
1690 return -EINVAL;
1691
Miquel Raynal8878b122017-11-09 14:16:45 +01001692 if (chip->exec_op)
1693 return nand_read_page_op(chip, page,
1694 mtd->writesize + offset_in_oob,
1695 buf, len);
1696
Boris Brezillon97d90da2017-11-30 18:01:29 +01001697 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1698 if (len)
1699 chip->read_buf(mtd, buf, len);
1700
1701 return 0;
1702}
1703EXPORT_SYMBOL_GPL(nand_read_oob_op);
1704
Miquel Raynal8878b122017-11-09 14:16:45 +01001705static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1706 unsigned int offset_in_page, const void *buf,
1707 unsigned int len, bool prog)
1708{
1709 struct mtd_info *mtd = nand_to_mtd(chip);
1710 const struct nand_sdr_timings *sdr =
1711 nand_get_sdr_timings(&chip->data_interface);
1712 u8 addrs[5] = {};
1713 struct nand_op_instr instrs[] = {
1714 /*
1715 * The first instruction will be dropped if we're dealing
1716 * with a large page NAND and adjusted if we're dealing
1717 * with a small page NAND and the page offset is > 255.
1718 */
1719 NAND_OP_CMD(NAND_CMD_READ0, 0),
1720 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1721 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1722 NAND_OP_DATA_OUT(len, buf, 0),
1723 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1724 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1725 };
1726 struct nand_operation op = NAND_OPERATION(instrs);
1727 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1728 int ret;
1729 u8 status;
1730
1731 if (naddrs < 0)
1732 return naddrs;
1733
1734 addrs[naddrs++] = page;
1735 addrs[naddrs++] = page >> 8;
1736 if (chip->options & NAND_ROW_ADDR_3)
1737 addrs[naddrs++] = page >> 16;
1738
1739 instrs[2].ctx.addr.naddrs = naddrs;
1740
1741 /* Drop the last two instructions if we're not programming the page. */
1742 if (!prog) {
1743 op.ninstrs -= 2;
1744 /* Also drop the DATA_OUT instruction if empty. */
1745 if (!len)
1746 op.ninstrs--;
1747 }
1748
1749 if (mtd->writesize <= 512) {
1750 /*
1751 * Small pages need some more tweaking: we have to adjust the
1752 * first instruction depending on the page offset we're trying
1753 * to access.
1754 */
1755 if (offset_in_page >= mtd->writesize)
1756 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1757 else if (offset_in_page >= 256 &&
1758 !(chip->options & NAND_BUSWIDTH_16))
1759 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1760 } else {
1761 /*
1762 * Drop the first command if we're dealing with a large page
1763 * NAND.
1764 */
1765 op.instrs++;
1766 op.ninstrs--;
1767 }
1768
1769 ret = nand_exec_op(chip, &op);
1770 if (!prog || ret)
1771 return ret;
1772
1773 ret = nand_status_op(chip, &status);
1774 if (ret)
1775 return ret;
1776
1777 return status;
1778}
1779
Boris Brezillon97d90da2017-11-30 18:01:29 +01001780/**
1781 * nand_prog_page_begin_op - starts a PROG PAGE operation
1782 * @chip: The NAND chip
1783 * @page: page to write
1784 * @offset_in_page: offset within the page
1785 * @buf: buffer containing the data to write to the page
1786 * @len: length of the buffer
1787 *
1788 * This function issues the first half of a PROG PAGE operation.
1789 * This function does not select/unselect the CS line.
1790 *
1791 * Returns 0 on success, a negative error code otherwise.
1792 */
1793int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1794 unsigned int offset_in_page, const void *buf,
1795 unsigned int len)
1796{
1797 struct mtd_info *mtd = nand_to_mtd(chip);
1798
1799 if (len && !buf)
1800 return -EINVAL;
1801
1802 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1803 return -EINVAL;
1804
Miquel Raynal8878b122017-11-09 14:16:45 +01001805 if (chip->exec_op)
1806 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1807 len, false);
1808
Boris Brezillon97d90da2017-11-30 18:01:29 +01001809 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1810
1811 if (buf)
1812 chip->write_buf(mtd, buf, len);
1813
1814 return 0;
1815}
1816EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1817
1818/**
1819 * nand_prog_page_end_op - ends a PROG PAGE operation
1820 * @chip: The NAND chip
1821 *
1822 * This function issues the second half of a PROG PAGE operation.
1823 * This function does not select/unselect the CS line.
1824 *
1825 * Returns 0 on success, a negative error code otherwise.
1826 */
1827int nand_prog_page_end_op(struct nand_chip *chip)
1828{
1829 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001830 int ret;
1831 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001832
Miquel Raynal8878b122017-11-09 14:16:45 +01001833 if (chip->exec_op) {
1834 const struct nand_sdr_timings *sdr =
1835 nand_get_sdr_timings(&chip->data_interface);
1836 struct nand_op_instr instrs[] = {
1837 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1838 PSEC_TO_NSEC(sdr->tWB_max)),
1839 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1840 };
1841 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001842
Miquel Raynal8878b122017-11-09 14:16:45 +01001843 ret = nand_exec_op(chip, &op);
1844 if (ret)
1845 return ret;
1846
1847 ret = nand_status_op(chip, &status);
1848 if (ret)
1849 return ret;
1850 } else {
1851 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1852 ret = chip->waitfunc(mtd, chip);
1853 if (ret < 0)
1854 return ret;
1855
1856 status = ret;
1857 }
1858
Boris Brezillon97d90da2017-11-30 18:01:29 +01001859 if (status & NAND_STATUS_FAIL)
1860 return -EIO;
1861
1862 return 0;
1863}
1864EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1865
1866/**
1867 * nand_prog_page_op - Do a full PROG PAGE operation
1868 * @chip: The NAND chip
1869 * @page: page to write
1870 * @offset_in_page: offset within the page
1871 * @buf: buffer containing the data to write to the page
1872 * @len: length of the buffer
1873 *
1874 * This function issues a full PROG PAGE operation.
1875 * This function does not select/unselect the CS line.
1876 *
1877 * Returns 0 on success, a negative error code otherwise.
1878 */
1879int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1880 unsigned int offset_in_page, const void *buf,
1881 unsigned int len)
1882{
1883 struct mtd_info *mtd = nand_to_mtd(chip);
1884 int status;
1885
1886 if (!len || !buf)
1887 return -EINVAL;
1888
1889 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1890 return -EINVAL;
1891
Miquel Raynal8878b122017-11-09 14:16:45 +01001892 if (chip->exec_op) {
1893 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1894 len, true);
1895 } else {
1896 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1897 chip->write_buf(mtd, buf, len);
1898 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1899 status = chip->waitfunc(mtd, chip);
1900 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001901
Boris Brezillon97d90da2017-11-30 18:01:29 +01001902 if (status & NAND_STATUS_FAIL)
1903 return -EIO;
1904
1905 return 0;
1906}
1907EXPORT_SYMBOL_GPL(nand_prog_page_op);
1908
1909/**
1910 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1911 * @chip: The NAND chip
1912 * @offset_in_page: offset within the page
1913 * @buf: buffer containing the data to send to the NAND
1914 * @len: length of the buffer
1915 * @force_8bit: force 8-bit bus access
1916 *
1917 * This function issues a CHANGE WRITE COLUMN operation.
1918 * This function does not select/unselect the CS line.
1919 *
1920 * Returns 0 on success, a negative error code otherwise.
1921 */
1922int nand_change_write_column_op(struct nand_chip *chip,
1923 unsigned int offset_in_page,
1924 const void *buf, unsigned int len,
1925 bool force_8bit)
1926{
1927 struct mtd_info *mtd = nand_to_mtd(chip);
1928
1929 if (len && !buf)
1930 return -EINVAL;
1931
1932 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1933 return -EINVAL;
1934
Miquel Raynal8878b122017-11-09 14:16:45 +01001935 /* Small page NANDs do not support column change. */
1936 if (mtd->writesize <= 512)
1937 return -ENOTSUPP;
1938
1939 if (chip->exec_op) {
1940 const struct nand_sdr_timings *sdr =
1941 nand_get_sdr_timings(&chip->data_interface);
1942 u8 addrs[2];
1943 struct nand_op_instr instrs[] = {
1944 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1945 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1946 NAND_OP_DATA_OUT(len, buf, 0),
1947 };
1948 struct nand_operation op = NAND_OPERATION(instrs);
1949 int ret;
1950
1951 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1952 if (ret < 0)
1953 return ret;
1954
1955 instrs[2].ctx.data.force_8bit = force_8bit;
1956
1957 /* Drop the DATA_OUT instruction if len is set to 0. */
1958 if (!len)
1959 op.ninstrs--;
1960
1961 return nand_exec_op(chip, &op);
1962 }
1963
Boris Brezillon97d90da2017-11-30 18:01:29 +01001964 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1965 if (len)
1966 chip->write_buf(mtd, buf, len);
1967
1968 return 0;
1969}
1970EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1971
1972/**
1973 * nand_readid_op - Do a READID operation
1974 * @chip: The NAND chip
1975 * @addr: address cycle to pass after the READID command
1976 * @buf: buffer used to store the ID
1977 * @len: length of the buffer
1978 *
1979 * This function sends a READID command and reads back the ID returned by the
1980 * NAND.
1981 * This function does not select/unselect the CS line.
1982 *
1983 * Returns 0 on success, a negative error code otherwise.
1984 */
1985int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1986 unsigned int len)
1987{
1988 struct mtd_info *mtd = nand_to_mtd(chip);
1989 unsigned int i;
1990 u8 *id = buf;
1991
1992 if (len && !buf)
1993 return -EINVAL;
1994
Miquel Raynal8878b122017-11-09 14:16:45 +01001995 if (chip->exec_op) {
1996 const struct nand_sdr_timings *sdr =
1997 nand_get_sdr_timings(&chip->data_interface);
1998 struct nand_op_instr instrs[] = {
1999 NAND_OP_CMD(NAND_CMD_READID, 0),
2000 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
2001 NAND_OP_8BIT_DATA_IN(len, buf, 0),
2002 };
2003 struct nand_operation op = NAND_OPERATION(instrs);
2004
2005 /* Drop the DATA_IN instruction if len is set to 0. */
2006 if (!len)
2007 op.ninstrs--;
2008
2009 return nand_exec_op(chip, &op);
2010 }
2011
Boris Brezillon97d90da2017-11-30 18:01:29 +01002012 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
2013
2014 for (i = 0; i < len; i++)
2015 id[i] = chip->read_byte(mtd);
2016
2017 return 0;
2018}
2019EXPORT_SYMBOL_GPL(nand_readid_op);
2020
2021/**
2022 * nand_status_op - Do a STATUS operation
2023 * @chip: The NAND chip
2024 * @status: out variable to store the NAND status
2025 *
2026 * This function sends a STATUS command and reads back the status returned by
2027 * the NAND.
2028 * This function does not select/unselect the CS line.
2029 *
2030 * Returns 0 on success, a negative error code otherwise.
2031 */
2032int nand_status_op(struct nand_chip *chip, u8 *status)
2033{
2034 struct mtd_info *mtd = nand_to_mtd(chip);
2035
Miquel Raynal8878b122017-11-09 14:16:45 +01002036 if (chip->exec_op) {
2037 const struct nand_sdr_timings *sdr =
2038 nand_get_sdr_timings(&chip->data_interface);
2039 struct nand_op_instr instrs[] = {
2040 NAND_OP_CMD(NAND_CMD_STATUS,
2041 PSEC_TO_NSEC(sdr->tADL_min)),
2042 NAND_OP_8BIT_DATA_IN(1, status, 0),
2043 };
2044 struct nand_operation op = NAND_OPERATION(instrs);
2045
2046 if (!status)
2047 op.ninstrs--;
2048
2049 return nand_exec_op(chip, &op);
2050 }
2051
Boris Brezillon97d90da2017-11-30 18:01:29 +01002052 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
2053 if (status)
2054 *status = chip->read_byte(mtd);
2055
2056 return 0;
2057}
2058EXPORT_SYMBOL_GPL(nand_status_op);
2059
2060/**
2061 * nand_exit_status_op - Exit a STATUS operation
2062 * @chip: The NAND chip
2063 *
2064 * This function sends a READ0 command to cancel the effect of the STATUS
2065 * command to avoid reading only the status until a new read command is sent.
2066 *
2067 * This function does not select/unselect the CS line.
2068 *
2069 * Returns 0 on success, a negative error code otherwise.
2070 */
2071int nand_exit_status_op(struct nand_chip *chip)
2072{
2073 struct mtd_info *mtd = nand_to_mtd(chip);
2074
Miquel Raynal8878b122017-11-09 14:16:45 +01002075 if (chip->exec_op) {
2076 struct nand_op_instr instrs[] = {
2077 NAND_OP_CMD(NAND_CMD_READ0, 0),
2078 };
2079 struct nand_operation op = NAND_OPERATION(instrs);
2080
2081 return nand_exec_op(chip, &op);
2082 }
2083
Boris Brezillon97d90da2017-11-30 18:01:29 +01002084 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2085
2086 return 0;
2087}
2088EXPORT_SYMBOL_GPL(nand_exit_status_op);
2089
2090/**
2091 * nand_erase_op - Do an erase operation
2092 * @chip: The NAND chip
2093 * @eraseblock: block to erase
2094 *
2095 * This function sends an ERASE command and waits for the NAND to be ready
2096 * before returning.
2097 * This function does not select/unselect the CS line.
2098 *
2099 * Returns 0 on success, a negative error code otherwise.
2100 */
2101int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2102{
2103 struct mtd_info *mtd = nand_to_mtd(chip);
2104 unsigned int page = eraseblock <<
2105 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002106 int ret;
2107 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002108
Miquel Raynal8878b122017-11-09 14:16:45 +01002109 if (chip->exec_op) {
2110 const struct nand_sdr_timings *sdr =
2111 nand_get_sdr_timings(&chip->data_interface);
2112 u8 addrs[3] = { page, page >> 8, page >> 16 };
2113 struct nand_op_instr instrs[] = {
2114 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2115 NAND_OP_ADDR(2, addrs, 0),
2116 NAND_OP_CMD(NAND_CMD_ERASE2,
2117 PSEC_TO_MSEC(sdr->tWB_max)),
2118 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2119 };
2120 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002121
Miquel Raynal8878b122017-11-09 14:16:45 +01002122 if (chip->options & NAND_ROW_ADDR_3)
2123 instrs[1].ctx.addr.naddrs++;
2124
2125 ret = nand_exec_op(chip, &op);
2126 if (ret)
2127 return ret;
2128
2129 ret = nand_status_op(chip, &status);
2130 if (ret)
2131 return ret;
2132 } else {
2133 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2134 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2135
2136 ret = chip->waitfunc(mtd, chip);
2137 if (ret < 0)
2138 return ret;
2139
2140 status = ret;
2141 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002142
2143 if (status & NAND_STATUS_FAIL)
2144 return -EIO;
2145
2146 return 0;
2147}
2148EXPORT_SYMBOL_GPL(nand_erase_op);
2149
2150/**
2151 * nand_set_features_op - Do a SET FEATURES operation
2152 * @chip: The NAND chip
2153 * @feature: feature id
2154 * @data: 4 bytes of data
2155 *
2156 * This function sends a SET FEATURES command and waits for the NAND to be
2157 * ready before returning.
2158 * This function does not select/unselect the CS line.
2159 *
2160 * Returns 0 on success, a negative error code otherwise.
2161 */
2162static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2163 const void *data)
2164{
2165 struct mtd_info *mtd = nand_to_mtd(chip);
2166 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002167 int i, ret;
2168 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002169
Miquel Raynal8878b122017-11-09 14:16:45 +01002170 if (chip->exec_op) {
2171 const struct nand_sdr_timings *sdr =
2172 nand_get_sdr_timings(&chip->data_interface);
2173 struct nand_op_instr instrs[] = {
2174 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2175 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2176 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2177 PSEC_TO_NSEC(sdr->tWB_max)),
2178 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2179 };
2180 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002181
Miquel Raynal8878b122017-11-09 14:16:45 +01002182 ret = nand_exec_op(chip, &op);
2183 if (ret)
2184 return ret;
2185
2186 ret = nand_status_op(chip, &status);
2187 if (ret)
2188 return ret;
2189 } else {
2190 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
2191 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2192 chip->write_byte(mtd, params[i]);
2193
2194 ret = chip->waitfunc(mtd, chip);
2195 if (ret < 0)
2196 return ret;
2197
2198 status = ret;
2199 }
2200
Boris Brezillon97d90da2017-11-30 18:01:29 +01002201 if (status & NAND_STATUS_FAIL)
2202 return -EIO;
2203
2204 return 0;
2205}
2206
2207/**
2208 * nand_get_features_op - Do a GET FEATURES operation
2209 * @chip: The NAND chip
2210 * @feature: feature id
2211 * @data: 4 bytes of data
2212 *
2213 * This function sends a GET FEATURES command and waits for the NAND to be
2214 * ready before returning.
2215 * This function does not select/unselect the CS line.
2216 *
2217 * Returns 0 on success, a negative error code otherwise.
2218 */
2219static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2220 void *data)
2221{
2222 struct mtd_info *mtd = nand_to_mtd(chip);
2223 u8 *params = data;
2224 int i;
2225
Miquel Raynal8878b122017-11-09 14:16:45 +01002226 if (chip->exec_op) {
2227 const struct nand_sdr_timings *sdr =
2228 nand_get_sdr_timings(&chip->data_interface);
2229 struct nand_op_instr instrs[] = {
2230 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2231 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2232 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2233 PSEC_TO_NSEC(sdr->tRR_min)),
2234 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2235 data, 0),
2236 };
2237 struct nand_operation op = NAND_OPERATION(instrs);
2238
2239 return nand_exec_op(chip, &op);
2240 }
2241
Boris Brezillon97d90da2017-11-30 18:01:29 +01002242 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2243 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2244 params[i] = chip->read_byte(mtd);
2245
2246 return 0;
2247}
2248
2249/**
2250 * nand_reset_op - Do a reset operation
2251 * @chip: The NAND chip
2252 *
2253 * This function sends a RESET command and waits for the NAND to be ready
2254 * before returning.
2255 * This function does not select/unselect the CS line.
2256 *
2257 * Returns 0 on success, a negative error code otherwise.
2258 */
2259int nand_reset_op(struct nand_chip *chip)
2260{
2261 struct mtd_info *mtd = nand_to_mtd(chip);
2262
Miquel Raynal8878b122017-11-09 14:16:45 +01002263 if (chip->exec_op) {
2264 const struct nand_sdr_timings *sdr =
2265 nand_get_sdr_timings(&chip->data_interface);
2266 struct nand_op_instr instrs[] = {
2267 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2268 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2269 };
2270 struct nand_operation op = NAND_OPERATION(instrs);
2271
2272 return nand_exec_op(chip, &op);
2273 }
2274
Boris Brezillon97d90da2017-11-30 18:01:29 +01002275 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2276
2277 return 0;
2278}
2279EXPORT_SYMBOL_GPL(nand_reset_op);
2280
2281/**
2282 * nand_read_data_op - Read data from the NAND
2283 * @chip: The NAND chip
2284 * @buf: buffer used to store the data
2285 * @len: length of the buffer
2286 * @force_8bit: force 8-bit bus access
2287 *
2288 * This function does a raw data read on the bus. Usually used after launching
2289 * another NAND operation like nand_read_page_op().
2290 * This function does not select/unselect the CS line.
2291 *
2292 * Returns 0 on success, a negative error code otherwise.
2293 */
2294int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2295 bool force_8bit)
2296{
2297 struct mtd_info *mtd = nand_to_mtd(chip);
2298
2299 if (!len || !buf)
2300 return -EINVAL;
2301
Miquel Raynal8878b122017-11-09 14:16:45 +01002302 if (chip->exec_op) {
2303 struct nand_op_instr instrs[] = {
2304 NAND_OP_DATA_IN(len, buf, 0),
2305 };
2306 struct nand_operation op = NAND_OPERATION(instrs);
2307
2308 instrs[0].ctx.data.force_8bit = force_8bit;
2309
2310 return nand_exec_op(chip, &op);
2311 }
2312
Boris Brezillon97d90da2017-11-30 18:01:29 +01002313 if (force_8bit) {
2314 u8 *p = buf;
2315 unsigned int i;
2316
2317 for (i = 0; i < len; i++)
2318 p[i] = chip->read_byte(mtd);
2319 } else {
2320 chip->read_buf(mtd, buf, len);
2321 }
2322
2323 return 0;
2324}
2325EXPORT_SYMBOL_GPL(nand_read_data_op);
2326
2327/**
2328 * nand_write_data_op - Write data from the NAND
2329 * @chip: The NAND chip
2330 * @buf: buffer containing the data to send on the bus
2331 * @len: length of the buffer
2332 * @force_8bit: force 8-bit bus access
2333 *
2334 * This function does a raw data write on the bus. Usually used after launching
2335 * another NAND operation like nand_write_page_begin_op().
2336 * This function does not select/unselect the CS line.
2337 *
2338 * Returns 0 on success, a negative error code otherwise.
2339 */
2340int nand_write_data_op(struct nand_chip *chip, const void *buf,
2341 unsigned int len, bool force_8bit)
2342{
2343 struct mtd_info *mtd = nand_to_mtd(chip);
2344
2345 if (!len || !buf)
2346 return -EINVAL;
2347
Miquel Raynal8878b122017-11-09 14:16:45 +01002348 if (chip->exec_op) {
2349 struct nand_op_instr instrs[] = {
2350 NAND_OP_DATA_OUT(len, buf, 0),
2351 };
2352 struct nand_operation op = NAND_OPERATION(instrs);
2353
2354 instrs[0].ctx.data.force_8bit = force_8bit;
2355
2356 return nand_exec_op(chip, &op);
2357 }
2358
Boris Brezillon97d90da2017-11-30 18:01:29 +01002359 if (force_8bit) {
2360 const u8 *p = buf;
2361 unsigned int i;
2362
2363 for (i = 0; i < len; i++)
2364 chip->write_byte(mtd, p[i]);
2365 } else {
2366 chip->write_buf(mtd, buf, len);
2367 }
2368
2369 return 0;
2370}
2371EXPORT_SYMBOL_GPL(nand_write_data_op);
2372
2373/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002374 * struct nand_op_parser_ctx - Context used by the parser
2375 * @instrs: array of all the instructions that must be addressed
2376 * @ninstrs: length of the @instrs array
2377 * @subop: Sub-operation to be passed to the NAND controller
2378 *
2379 * This structure is used by the core to split NAND operations into
2380 * sub-operations that can be handled by the NAND controller.
2381 */
2382struct nand_op_parser_ctx {
2383 const struct nand_op_instr *instrs;
2384 unsigned int ninstrs;
2385 struct nand_subop subop;
2386};
2387
2388/**
2389 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2390 * @pat: the parser pattern element that matches @instr
2391 * @instr: pointer to the instruction to check
2392 * @start_offset: this is an in/out parameter. If @instr has already been
2393 * split, then @start_offset is the offset from which to start
2394 * (either an address cycle or an offset in the data buffer).
2395 * Conversely, if the function returns true (ie. instr must be
2396 * split), this parameter is updated to point to the first
2397 * data/address cycle that has not been taken care of.
2398 *
2399 * Some NAND controllers are limited and cannot send X address cycles with a
2400 * unique operation, or cannot read/write more than Y bytes at the same time.
2401 * In this case, split the instruction that does not fit in a single
2402 * controller-operation into two or more chunks.
2403 *
2404 * Returns true if the instruction must be split, false otherwise.
2405 * The @start_offset parameter is also updated to the offset at which the next
2406 * bundle of instruction must start (if an address or a data instruction).
2407 */
2408static bool
2409nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2410 const struct nand_op_instr *instr,
2411 unsigned int *start_offset)
2412{
2413 switch (pat->type) {
2414 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002415 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002416 break;
2417
2418 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002419 pat->ctx.addr.maxcycles) {
2420 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002421 return true;
2422 }
2423 break;
2424
2425 case NAND_OP_DATA_IN_INSTR:
2426 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002427 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002428 break;
2429
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002430 if (instr->ctx.data.len - *start_offset >
2431 pat->ctx.data.maxlen) {
2432 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002433 return true;
2434 }
2435 break;
2436
2437 default:
2438 break;
2439 }
2440
2441 return false;
2442}
2443
2444/**
2445 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2446 * remaining in the parser context
2447 * @pat: the pattern to test
2448 * @ctx: the parser context structure to match with the pattern @pat
2449 *
2450 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2451 * Returns true if this is the case, false ortherwise. When true is returned,
2452 * @ctx->subop is updated with the set of instructions to be passed to the
2453 * controller driver.
2454 */
2455static bool
2456nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2457 struct nand_op_parser_ctx *ctx)
2458{
2459 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2460 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2461 const struct nand_op_instr *instr = ctx->subop.instrs;
2462 unsigned int i, ninstrs;
2463
2464 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2465 /*
2466 * The pattern instruction does not match the operation
2467 * instruction. If the instruction is marked optional in the
2468 * pattern definition, we skip the pattern element and continue
2469 * to the next one. If the element is mandatory, there's no
2470 * match and we can return false directly.
2471 */
2472 if (instr->type != pat->elems[i].type) {
2473 if (!pat->elems[i].optional)
2474 return false;
2475
2476 continue;
2477 }
2478
2479 /*
2480 * Now check the pattern element constraints. If the pattern is
2481 * not able to handle the whole instruction in a single step,
2482 * we have to split it.
2483 * The last_instr_end_off value comes back updated to point to
2484 * the position where we have to split the instruction (the
2485 * start of the next subop chunk).
2486 */
2487 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2488 &instr_offset)) {
2489 ninstrs++;
2490 i++;
2491 break;
2492 }
2493
2494 instr++;
2495 ninstrs++;
2496 instr_offset = 0;
2497 }
2498
2499 /*
2500 * This can happen if all instructions of a pattern are optional.
2501 * Still, if there's not at least one instruction handled by this
2502 * pattern, this is not a match, and we should try the next one (if
2503 * any).
2504 */
2505 if (!ninstrs)
2506 return false;
2507
2508 /*
2509 * We had a match on the pattern head, but the pattern may be longer
2510 * than the instructions we're asked to execute. We need to make sure
2511 * there's no mandatory elements in the pattern tail.
2512 */
2513 for (; i < pat->nelems; i++) {
2514 if (!pat->elems[i].optional)
2515 return false;
2516 }
2517
2518 /*
2519 * We have a match: update the subop structure accordingly and return
2520 * true.
2521 */
2522 ctx->subop.ninstrs = ninstrs;
2523 ctx->subop.last_instr_end_off = instr_offset;
2524
2525 return true;
2526}
2527
2528#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2529static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2530{
2531 const struct nand_op_instr *instr;
2532 char *prefix = " ";
2533 unsigned int i;
2534
2535 pr_debug("executing subop:\n");
2536
2537 for (i = 0; i < ctx->ninstrs; i++) {
2538 instr = &ctx->instrs[i];
2539
2540 if (instr == &ctx->subop.instrs[0])
2541 prefix = " ->";
2542
2543 switch (instr->type) {
2544 case NAND_OP_CMD_INSTR:
2545 pr_debug("%sCMD [0x%02x]\n", prefix,
2546 instr->ctx.cmd.opcode);
2547 break;
2548 case NAND_OP_ADDR_INSTR:
2549 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2550 instr->ctx.addr.naddrs,
2551 instr->ctx.addr.naddrs < 64 ?
2552 instr->ctx.addr.naddrs : 64,
2553 instr->ctx.addr.addrs);
2554 break;
2555 case NAND_OP_DATA_IN_INSTR:
2556 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2557 instr->ctx.data.len,
2558 instr->ctx.data.force_8bit ?
2559 ", force 8-bit" : "");
2560 break;
2561 case NAND_OP_DATA_OUT_INSTR:
2562 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2563 instr->ctx.data.len,
2564 instr->ctx.data.force_8bit ?
2565 ", force 8-bit" : "");
2566 break;
2567 case NAND_OP_WAITRDY_INSTR:
2568 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2569 instr->ctx.waitrdy.timeout_ms);
2570 break;
2571 }
2572
2573 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2574 prefix = " ";
2575 }
2576}
2577#else
2578static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2579{
2580 /* NOP */
2581}
2582#endif
2583
2584/**
2585 * nand_op_parser_exec_op - exec_op parser
2586 * @chip: the NAND chip
2587 * @parser: patterns description provided by the controller driver
2588 * @op: the NAND operation to address
2589 * @check_only: when true, the function only checks if @op can be handled but
2590 * does not execute the operation
2591 *
2592 * Helper function designed to ease integration of NAND controller drivers that
2593 * only support a limited set of instruction sequences. The supported sequences
2594 * are described in @parser, and the framework takes care of splitting @op into
2595 * multiple sub-operations (if required) and pass them back to the ->exec()
2596 * callback of the matching pattern if @check_only is set to false.
2597 *
2598 * NAND controller drivers should call this function from their own ->exec_op()
2599 * implementation.
2600 *
2601 * Returns 0 on success, a negative error code otherwise. A failure can be
2602 * caused by an unsupported operation (none of the supported patterns is able
2603 * to handle the requested operation), or an error returned by one of the
2604 * matching pattern->exec() hook.
2605 */
2606int nand_op_parser_exec_op(struct nand_chip *chip,
2607 const struct nand_op_parser *parser,
2608 const struct nand_operation *op, bool check_only)
2609{
2610 struct nand_op_parser_ctx ctx = {
2611 .subop.instrs = op->instrs,
2612 .instrs = op->instrs,
2613 .ninstrs = op->ninstrs,
2614 };
2615 unsigned int i;
2616
2617 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2618 int ret;
2619
2620 for (i = 0; i < parser->npatterns; i++) {
2621 const struct nand_op_parser_pattern *pattern;
2622
2623 pattern = &parser->patterns[i];
2624 if (!nand_op_parser_match_pat(pattern, &ctx))
2625 continue;
2626
2627 nand_op_parser_trace(&ctx);
2628
2629 if (check_only)
2630 break;
2631
2632 ret = pattern->exec(chip, &ctx.subop);
2633 if (ret)
2634 return ret;
2635
2636 break;
2637 }
2638
2639 if (i == parser->npatterns) {
2640 pr_debug("->exec_op() parser: pattern not found!\n");
2641 return -ENOTSUPP;
2642 }
2643
2644 /*
2645 * Update the context structure by pointing to the start of the
2646 * next subop.
2647 */
2648 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2649 if (ctx.subop.last_instr_end_off)
2650 ctx.subop.instrs -= 1;
2651
2652 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2653 }
2654
2655 return 0;
2656}
2657EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2658
2659static bool nand_instr_is_data(const struct nand_op_instr *instr)
2660{
2661 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2662 instr->type == NAND_OP_DATA_OUT_INSTR);
2663}
2664
2665static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2666 unsigned int instr_idx)
2667{
2668 return subop && instr_idx < subop->ninstrs;
2669}
2670
2671static int nand_subop_get_start_off(const struct nand_subop *subop,
2672 unsigned int instr_idx)
2673{
2674 if (instr_idx)
2675 return 0;
2676
2677 return subop->first_instr_start_off;
2678}
2679
2680/**
2681 * nand_subop_get_addr_start_off - Get the start offset in an address array
2682 * @subop: The entire sub-operation
2683 * @instr_idx: Index of the instruction inside the sub-operation
2684 *
2685 * During driver development, one could be tempted to directly use the
2686 * ->addr.addrs field of address instructions. This is wrong as address
2687 * instructions might be split.
2688 *
2689 * Given an address instruction, returns the offset of the first cycle to issue.
2690 */
2691int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2692 unsigned int instr_idx)
2693{
2694 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2695 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2696 return -EINVAL;
2697
2698 return nand_subop_get_start_off(subop, instr_idx);
2699}
2700EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2701
2702/**
2703 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2704 * @subop: The entire sub-operation
2705 * @instr_idx: Index of the instruction inside the sub-operation
2706 *
2707 * During driver development, one could be tempted to directly use the
2708 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2709 * might be split.
2710 *
2711 * Given an address instruction, returns the number of address cycle to issue.
2712 */
2713int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2714 unsigned int instr_idx)
2715{
2716 int start_off, end_off;
2717
2718 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2719 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2720 return -EINVAL;
2721
2722 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2723
2724 if (instr_idx == subop->ninstrs - 1 &&
2725 subop->last_instr_end_off)
2726 end_off = subop->last_instr_end_off;
2727 else
2728 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2729
2730 return end_off - start_off;
2731}
2732EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2733
2734/**
2735 * nand_subop_get_data_start_off - Get the start offset in a data array
2736 * @subop: The entire sub-operation
2737 * @instr_idx: Index of the instruction inside the sub-operation
2738 *
2739 * During driver development, one could be tempted to directly use the
2740 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2741 * instructions might be split.
2742 *
2743 * Given a data instruction, returns the offset to start from.
2744 */
2745int nand_subop_get_data_start_off(const struct nand_subop *subop,
2746 unsigned int instr_idx)
2747{
2748 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2749 !nand_instr_is_data(&subop->instrs[instr_idx]))
2750 return -EINVAL;
2751
2752 return nand_subop_get_start_off(subop, instr_idx);
2753}
2754EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2755
2756/**
2757 * nand_subop_get_data_len - Get the number of bytes to retrieve
2758 * @subop: The entire sub-operation
2759 * @instr_idx: Index of the instruction inside the sub-operation
2760 *
2761 * During driver development, one could be tempted to directly use the
2762 * ->data->len field of a data instruction. This is wrong as data instructions
2763 * might be split.
2764 *
2765 * Returns the length of the chunk of data to send/receive.
2766 */
2767int nand_subop_get_data_len(const struct nand_subop *subop,
2768 unsigned int instr_idx)
2769{
2770 int start_off = 0, end_off;
2771
2772 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2773 !nand_instr_is_data(&subop->instrs[instr_idx]))
2774 return -EINVAL;
2775
2776 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2777
2778 if (instr_idx == subop->ninstrs - 1 &&
2779 subop->last_instr_end_off)
2780 end_off = subop->last_instr_end_off;
2781 else
2782 end_off = subop->instrs[instr_idx].ctx.data.len;
2783
2784 return end_off - start_off;
2785}
2786EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002789 * nand_reset - Reset and initialize a NAND device
2790 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002791 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002792 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002793 * Save the timings data structure, then apply SDR timings mode 0 (see
2794 * nand_reset_data_interface for details), do the reset operation, and
2795 * apply back the previous timings.
2796 *
2797 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002798 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002799int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002800{
2801 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal17fa8042017-11-30 18:01:31 +01002802 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002803 int ret;
2804
Boris Brezillon104e4422017-03-16 09:35:58 +01002805 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002806 if (ret)
2807 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002808
Boris Brezillon73f907f2016-10-24 16:46:20 +02002809 /*
2810 * The CS line has to be released before we can apply the new NAND
2811 * interface settings, hence this weird ->select_chip() dance.
2812 */
2813 chip->select_chip(mtd, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002814 ret = nand_reset_op(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02002815 chip->select_chip(mtd, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002816 if (ret)
2817 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002818
Miquel Raynal107b7d62018-03-19 14:47:25 +01002819 /*
2820 * A nand_reset_data_interface() put both the NAND chip and the NAND
2821 * controller in timings mode 0. If the default mode for this chip is
2822 * also 0, no need to proceed to the change again. Plus, at probe time,
2823 * nand_setup_data_interface() uses ->set/get_features() which would
2824 * fail anyway as the parameter page is not available yet.
2825 */
2826 if (!chip->onfi_timing_mode_default)
2827 return 0;
2828
Miquel Raynal17fa8042017-11-30 18:01:31 +01002829 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002830 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002831 if (ret)
2832 return ret;
2833
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002834 return 0;
2835}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002836EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002837
2838/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002839 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2840 * @buf: buffer to test
2841 * @len: buffer length
2842 * @bitflips_threshold: maximum number of bitflips
2843 *
2844 * Check if a buffer contains only 0xff, which means the underlying region
2845 * has been erased and is ready to be programmed.
2846 * The bitflips_threshold specify the maximum number of bitflips before
2847 * considering the region is not erased.
2848 * Note: The logic of this function has been extracted from the memweight
2849 * implementation, except that nand_check_erased_buf function exit before
2850 * testing the whole buffer if the number of bitflips exceed the
2851 * bitflips_threshold value.
2852 *
2853 * Returns a positive number of bitflips less than or equal to
2854 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2855 * threshold.
2856 */
2857static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2858{
2859 const unsigned char *bitmap = buf;
2860 int bitflips = 0;
2861 int weight;
2862
2863 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2864 len--, bitmap++) {
2865 weight = hweight8(*bitmap);
2866 bitflips += BITS_PER_BYTE - weight;
2867 if (unlikely(bitflips > bitflips_threshold))
2868 return -EBADMSG;
2869 }
2870
2871 for (; len >= sizeof(long);
2872 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002873 unsigned long d = *((unsigned long *)bitmap);
2874 if (d == ~0UL)
2875 continue;
2876 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002877 bitflips += BITS_PER_LONG - weight;
2878 if (unlikely(bitflips > bitflips_threshold))
2879 return -EBADMSG;
2880 }
2881
2882 for (; len > 0; len--, bitmap++) {
2883 weight = hweight8(*bitmap);
2884 bitflips += BITS_PER_BYTE - weight;
2885 if (unlikely(bitflips > bitflips_threshold))
2886 return -EBADMSG;
2887 }
2888
2889 return bitflips;
2890}
2891
2892/**
2893 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2894 * 0xff data
2895 * @data: data buffer to test
2896 * @datalen: data length
2897 * @ecc: ECC buffer
2898 * @ecclen: ECC length
2899 * @extraoob: extra OOB buffer
2900 * @extraooblen: extra OOB length
2901 * @bitflips_threshold: maximum number of bitflips
2902 *
2903 * Check if a data buffer and its associated ECC and OOB data contains only
2904 * 0xff pattern, which means the underlying region has been erased and is
2905 * ready to be programmed.
2906 * The bitflips_threshold specify the maximum number of bitflips before
2907 * considering the region as not erased.
2908 *
2909 * Note:
2910 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2911 * different from the NAND page size. When fixing bitflips, ECC engines will
2912 * report the number of errors per chunk, and the NAND core infrastructure
2913 * expect you to return the maximum number of bitflips for the whole page.
2914 * This is why you should always use this function on a single chunk and
2915 * not on the whole page. After checking each chunk you should update your
2916 * max_bitflips value accordingly.
2917 * 2/ When checking for bitflips in erased pages you should not only check
2918 * the payload data but also their associated ECC data, because a user might
2919 * have programmed almost all bits to 1 but a few. In this case, we
2920 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2921 * this case.
2922 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2923 * data are protected by the ECC engine.
2924 * It could also be used if you support subpages and want to attach some
2925 * extra OOB data to an ECC chunk.
2926 *
2927 * Returns a positive number of bitflips less than or equal to
2928 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2929 * threshold. In case of success, the passed buffers are filled with 0xff.
2930 */
2931int nand_check_erased_ecc_chunk(void *data, int datalen,
2932 void *ecc, int ecclen,
2933 void *extraoob, int extraooblen,
2934 int bitflips_threshold)
2935{
2936 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2937
2938 data_bitflips = nand_check_erased_buf(data, datalen,
2939 bitflips_threshold);
2940 if (data_bitflips < 0)
2941 return data_bitflips;
2942
2943 bitflips_threshold -= data_bitflips;
2944
2945 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2946 if (ecc_bitflips < 0)
2947 return ecc_bitflips;
2948
2949 bitflips_threshold -= ecc_bitflips;
2950
2951 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2952 bitflips_threshold);
2953 if (extraoob_bitflips < 0)
2954 return extraoob_bitflips;
2955
2956 if (data_bitflips)
2957 memset(data, 0xff, datalen);
2958
2959 if (ecc_bitflips)
2960 memset(ecc, 0xff, ecclen);
2961
2962 if (extraoob_bitflips)
2963 memset(extraoob, 0xff, extraooblen);
2964
2965 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2966}
2967EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2968
2969/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002970 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002971 * @mtd: mtd info structure
2972 * @chip: nand chip info structure
2973 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002974 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002975 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002976 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002977 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002978 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002979int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2980 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002981{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002982 int ret;
2983
Boris Brezillon25f815f2017-11-30 18:01:30 +01002984 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002985 if (ret)
2986 return ret;
2987
2988 if (oob_required) {
2989 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2990 false);
2991 if (ret)
2992 return ret;
2993 }
2994
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002995 return 0;
2996}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002997EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002998
2999/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003000 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07003001 * @mtd: mtd info structure
3002 * @chip: nand chip info structure
3003 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003004 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003005 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003006 *
3007 * We need a special oob layout and handling even when OOB isn't used.
3008 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003009static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003010 struct nand_chip *chip, uint8_t *buf,
3011 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003012{
3013 int eccsize = chip->ecc.size;
3014 int eccbytes = chip->ecc.bytes;
3015 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003016 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003017
Boris Brezillon25f815f2017-11-30 18:01:30 +01003018 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3019 if (ret)
3020 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003021
3022 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003023 ret = nand_read_data_op(chip, buf, eccsize, false);
3024 if (ret)
3025 return ret;
3026
David Brownell52ff49d2009-03-04 12:01:36 -08003027 buf += eccsize;
3028
3029 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003030 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3031 false);
3032 if (ret)
3033 return ret;
3034
David Brownell52ff49d2009-03-04 12:01:36 -08003035 oob += chip->ecc.prepad;
3036 }
3037
Boris Brezillon97d90da2017-11-30 18:01:29 +01003038 ret = nand_read_data_op(chip, oob, eccbytes, false);
3039 if (ret)
3040 return ret;
3041
David Brownell52ff49d2009-03-04 12:01:36 -08003042 oob += eccbytes;
3043
3044 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003045 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3046 false);
3047 if (ret)
3048 return ret;
3049
David Brownell52ff49d2009-03-04 12:01:36 -08003050 oob += chip->ecc.postpad;
3051 }
3052 }
3053
3054 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003055 if (size) {
3056 ret = nand_read_data_op(chip, oob, size, false);
3057 if (ret)
3058 return ret;
3059 }
David Brownell52ff49d2009-03-04 12:01:36 -08003060
3061 return 0;
3062}
3063
3064/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003065 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003066 * @mtd: mtd info structure
3067 * @chip: nand chip info structure
3068 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003069 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003070 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003071 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003072static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003073 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
Boris Brezillon846031d2016-02-03 20:11:00 +01003075 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003076 int eccbytes = chip->ecc.bytes;
3077 int eccsteps = chip->ecc.steps;
3078 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003079 uint8_t *ecc_calc = chip->ecc.calc_buf;
3080 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003081 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003082
Brian Norris1fbb9382012-05-02 10:14:55 -07003083 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003084
3085 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3086 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3087
Boris Brezillon846031d2016-02-03 20:11:00 +01003088 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3089 chip->ecc.total);
3090 if (ret)
3091 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003092
3093 eccsteps = chip->ecc.steps;
3094 p = buf;
3095
3096 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3097 int stat;
3098
3099 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003100 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003101 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003102 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003103 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003104 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3105 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003106 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003107 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303111 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003112 * @mtd: mtd info structure
3113 * @chip: nand chip info structure
3114 * @data_offs: offset of requested data within the page
3115 * @readlen: data length
3116 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003117 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003118 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003119static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003120 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3121 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003122{
Boris Brezillon846031d2016-02-03 20:11:00 +01003123 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003124 uint8_t *p;
3125 int data_col_addr, i, gaps = 0;
3126 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3127 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003128 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003129 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003130 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003131
Brian Norris7854d3f2011-06-23 14:12:08 -07003132 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003133 start_step = data_offs / chip->ecc.size;
3134 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3135 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303136 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003137
Brian Norris8b6e50c2011-05-25 14:59:01 -07003138 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003139 datafrag_len = num_steps * chip->ecc.size;
3140 eccfrag_len = num_steps * chip->ecc.bytes;
3141
3142 data_col_addr = start_step * chip->ecc.size;
3143 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003144 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003145 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003146 if (ret)
3147 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003148
Brian Norris8b6e50c2011-05-25 14:59:01 -07003149 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003150 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003151 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003152
Brian Norris8b6e50c2011-05-25 14:59:01 -07003153 /*
3154 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003155 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003156 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003157 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3158 if (ret)
3159 return ret;
3160
3161 if (oobregion.length < eccfrag_len)
3162 gaps = 1;
3163
Alexey Korolev3d459552008-05-15 17:23:18 +01003164 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003165 ret = nand_change_read_column_op(chip, mtd->writesize,
3166 chip->oob_poi, mtd->oobsize,
3167 false);
3168 if (ret)
3169 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003170 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003171 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003172 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003173 * about buswidth alignment in read_buf.
3174 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003175 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003176 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003177 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003178 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003179 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3180 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003181 aligned_len++;
3182
Boris Brezillon97d90da2017-11-30 18:01:29 +01003183 ret = nand_change_read_column_op(chip,
3184 mtd->writesize + aligned_pos,
3185 &chip->oob_poi[aligned_pos],
3186 aligned_len, false);
3187 if (ret)
3188 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003189 }
3190
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003191 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003192 chip->oob_poi, index, eccfrag_len);
3193 if (ret)
3194 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003195
3196 p = bufpoi + data_col_addr;
3197 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3198 int stat;
3199
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003200 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3201 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003202 if (stat == -EBADMSG &&
3203 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3204 /* check for empty pages with bitflips */
3205 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003206 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003207 chip->ecc.bytes,
3208 NULL, 0,
3209 chip->ecc.strength);
3210 }
3211
Mike Dunn3f91e942012-04-25 12:06:09 -07003212 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003213 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003214 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003215 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003216 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3217 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003218 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003219 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003220}
3221
3222/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003223 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003224 * @mtd: mtd info structure
3225 * @chip: nand chip info structure
3226 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003227 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003228 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003229 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003230 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003231 */
3232static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003233 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003234{
Boris Brezillon846031d2016-02-03 20:11:00 +01003235 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003236 int eccbytes = chip->ecc.bytes;
3237 int eccsteps = chip->ecc.steps;
3238 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003239 uint8_t *ecc_calc = chip->ecc.calc_buf;
3240 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003241 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003242
Boris Brezillon25f815f2017-11-30 18:01:30 +01003243 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3244 if (ret)
3245 return ret;
3246
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003247 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3248 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003249
3250 ret = nand_read_data_op(chip, p, eccsize, false);
3251 if (ret)
3252 return ret;
3253
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003254 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3255 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003256
3257 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3258 if (ret)
3259 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003260
Boris Brezillon846031d2016-02-03 20:11:00 +01003261 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3262 chip->ecc.total);
3263 if (ret)
3264 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003265
3266 eccsteps = chip->ecc.steps;
3267 p = buf;
3268
3269 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3270 int stat;
3271
3272 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003273 if (stat == -EBADMSG &&
3274 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3275 /* check for empty pages with bitflips */
3276 stat = nand_check_erased_ecc_chunk(p, eccsize,
3277 &ecc_code[i], eccbytes,
3278 NULL, 0,
3279 chip->ecc.strength);
3280 }
3281
Mike Dunn3f91e942012-04-25 12:06:09 -07003282 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003283 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003284 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003285 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003286 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3287 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003288 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003289 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003290}
3291
3292/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003293 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003294 * @mtd: mtd info structure
3295 * @chip: nand chip info structure
3296 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003297 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003298 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003299 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003300 * Hardware ECC for large page chips, require OOB to be read first. For this
3301 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3302 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3303 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3304 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003305 */
3306static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003307 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003308{
Boris Brezillon846031d2016-02-03 20:11:00 +01003309 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003310 int eccbytes = chip->ecc.bytes;
3311 int eccsteps = chip->ecc.steps;
3312 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003313 uint8_t *ecc_code = chip->ecc.code_buf;
3314 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003315 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003316
3317 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003318 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3319 if (ret)
3320 return ret;
3321
3322 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3323 if (ret)
3324 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003325
Boris Brezillon846031d2016-02-03 20:11:00 +01003326 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3327 chip->ecc.total);
3328 if (ret)
3329 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003330
3331 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3332 int stat;
3333
3334 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003335
3336 ret = nand_read_data_op(chip, p, eccsize, false);
3337 if (ret)
3338 return ret;
3339
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003340 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3341
3342 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003343 if (stat == -EBADMSG &&
3344 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3345 /* check for empty pages with bitflips */
3346 stat = nand_check_erased_ecc_chunk(p, eccsize,
3347 &ecc_code[i], eccbytes,
3348 NULL, 0,
3349 chip->ecc.strength);
3350 }
3351
Mike Dunn3f91e942012-04-25 12:06:09 -07003352 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003353 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003354 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003355 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003356 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3357 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003358 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003359 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003360}
3361
3362/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003363 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003364 * @mtd: mtd info structure
3365 * @chip: nand chip info structure
3366 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003367 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003368 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003369 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003370 * The hw generator calculates the error syndrome automatically. Therefore we
3371 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003372 */
3373static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003374 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003375{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003376 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003377 int eccbytes = chip->ecc.bytes;
3378 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003379 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003380 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003381 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003382 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003383
Boris Brezillon25f815f2017-11-30 18:01:30 +01003384 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3385 if (ret)
3386 return ret;
3387
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003388 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3389 int stat;
3390
3391 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003392
3393 ret = nand_read_data_op(chip, p, eccsize, false);
3394 if (ret)
3395 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003396
3397 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003398 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3399 false);
3400 if (ret)
3401 return ret;
3402
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003403 oob += chip->ecc.prepad;
3404 }
3405
3406 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003407
3408 ret = nand_read_data_op(chip, oob, eccbytes, false);
3409 if (ret)
3410 return ret;
3411
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003412 stat = chip->ecc.correct(mtd, p, oob, NULL);
3413
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003414 oob += eccbytes;
3415
3416 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003417 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3418 false);
3419 if (ret)
3420 return ret;
3421
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003422 oob += chip->ecc.postpad;
3423 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003424
3425 if (stat == -EBADMSG &&
3426 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3427 /* check for empty pages with bitflips */
3428 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3429 oob - eccpadbytes,
3430 eccpadbytes,
3431 NULL, 0,
3432 chip->ecc.strength);
3433 }
3434
3435 if (stat < 0) {
3436 mtd->ecc_stats.failed++;
3437 } else {
3438 mtd->ecc_stats.corrected += stat;
3439 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3440 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003441 }
3442
3443 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003444 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003445 if (i) {
3446 ret = nand_read_data_op(chip, oob, i, false);
3447 if (ret)
3448 return ret;
3449 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003450
Mike Dunn3f91e942012-04-25 12:06:09 -07003451 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003452}
3453
3454/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003455 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003456 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003457 * @oob: oob destination address
3458 * @ops: oob ops structure
3459 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003460 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003461static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003462 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003463{
Boris Brezillon846031d2016-02-03 20:11:00 +01003464 struct nand_chip *chip = mtd_to_nand(mtd);
3465 int ret;
3466
Florian Fainellif8ac0412010-09-07 13:23:43 +02003467 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003468
Brian Norris0612b9d2011-08-30 18:45:40 -07003469 case MTD_OPS_PLACE_OOB:
3470 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003471 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3472 return oob + len;
3473
Boris Brezillon846031d2016-02-03 20:11:00 +01003474 case MTD_OPS_AUTO_OOB:
3475 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3476 ops->ooboffs, len);
3477 BUG_ON(ret);
3478 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003479
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003480 default:
3481 BUG();
3482 }
3483 return NULL;
3484}
3485
3486/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003487 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3488 * @mtd: MTD device structure
3489 * @retry_mode: the retry mode to use
3490 *
3491 * Some vendors supply a special command to shift the Vt threshold, to be used
3492 * when there are too many bitflips in a page (i.e., ECC error). After setting
3493 * a new threshold, the host should retry reading the page.
3494 */
3495static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3496{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003497 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08003498
3499 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3500
3501 if (retry_mode >= chip->read_retries)
3502 return -EINVAL;
3503
3504 if (!chip->setup_read_retry)
3505 return -EOPNOTSUPP;
3506
3507 return chip->setup_read_retry(mtd, retry_mode);
3508}
3509
3510/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003511 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003512 * @mtd: MTD device structure
3513 * @from: offset to read from
3514 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003515 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003516 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003517 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003518static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3519 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003520{
Brian Norrise47f3db2012-05-02 10:14:56 -07003521 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003522 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003523 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003524 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003525 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003526 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003527
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003528 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003529 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003530 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003531 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003532 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003534 chipnr = (int)(from >> chip->chip_shift);
3535 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003537 realpage = (int)(from >> chip->page_shift);
3538 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003540 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003542 buf = ops->datbuf;
3543 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003544 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003545
Florian Fainellif8ac0412010-09-07 13:23:43 +02003546 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003547 unsigned int ecc_failures = mtd->ecc_stats.failed;
3548
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003549 bytes = min(mtd->writesize - col, readlen);
3550 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003551
Kamal Dasu66507c72014-05-01 20:51:19 -04003552 if (!aligned)
3553 use_bufpoi = 1;
3554 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003555 use_bufpoi = !virt_addr_valid(buf) ||
3556 !IS_ALIGNED((unsigned long)buf,
3557 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003558 else
3559 use_bufpoi = 0;
3560
Brian Norris8b6e50c2011-05-25 14:59:01 -07003561 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003562 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003563 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003564
3565 if (use_bufpoi && aligned)
3566 pr_debug("%s: using read bounce buffer for buf@%p\n",
3567 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Brian Norrisba84fb52014-01-03 15:13:33 -08003569read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003570 /*
3571 * Now read the page into the buffer. Absent an error,
3572 * the read methods return max bitflips per ecc step.
3573 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003574 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07003575 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003576 oob_required,
3577 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003578 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3579 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003580 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003581 col, bytes, bufpoi,
3582 page);
David Woodhouse956e9442006-09-25 17:12:39 +01003583 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07003584 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003585 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003586 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003587 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003588 /* Invalidate page cache */
3589 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003590 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003591 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003592
3593 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003594 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003595 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003596 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003597 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003598 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003599 chip->pagebuf_bitflips = ret;
3600 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003601 /* Invalidate page cache */
3602 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003603 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003604 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003606
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003607 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003608 int toread = min(oobreadlen, max_oobsize);
3609
3610 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003611 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003612 oob, ops, toread);
3613 oobreadlen -= toread;
3614 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003615 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003616
3617 if (chip->options & NAND_NEED_READRDY) {
3618 /* Apply delay or wait for ready/busy pin */
3619 if (!chip->dev_ready)
3620 udelay(chip->chip_delay);
3621 else
3622 nand_wait_ready(mtd);
3623 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08003624
Brian Norrisba84fb52014-01-03 15:13:33 -08003625 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003626 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003627 retry_mode++;
3628 ret = nand_setup_read_retry(mtd,
3629 retry_mode);
3630 if (ret < 0)
3631 break;
3632
3633 /* Reset failures; retry */
3634 mtd->ecc_stats.failed = ecc_failures;
3635 goto read_retry;
3636 } else {
3637 /* No more retry modes; real failure */
3638 ecc_fail = true;
3639 }
3640 }
3641
3642 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003643 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003644 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003645 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003646 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003647 max_bitflips = max_t(unsigned int, max_bitflips,
3648 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003651 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003652
Brian Norrisba84fb52014-01-03 15:13:33 -08003653 /* Reset to retry mode 0 */
3654 if (retry_mode) {
3655 ret = nand_setup_read_retry(mtd, 0);
3656 if (ret < 0)
3657 break;
3658 retry_mode = 0;
3659 }
3660
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003661 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003662 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
Brian Norris8b6e50c2011-05-25 14:59:01 -07003664 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 col = 0;
3666 /* Increment page address */
3667 realpage++;
3668
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003669 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 /* Check, if we cross a chip boundary */
3671 if (!page) {
3672 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003673 chip->select_chip(mtd, -1);
3674 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003677 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003679 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003680 if (oob)
3681 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Mike Dunn3f91e942012-04-25 12:06:09 -07003683 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003684 return ret;
3685
Brian Norrisb72f3df2013-12-03 11:04:14 -08003686 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003687 return -EBADMSG;
3688
Mike Dunnedbc45402012-04-25 12:06:11 -07003689 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003690}
3691
3692/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003693 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003694 * @mtd: mtd info structure
3695 * @chip: nand chip info structure
3696 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003697 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003698int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003699{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003700 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003701}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003702EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003703
3704/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003705 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003706 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003707 * @mtd: mtd info structure
3708 * @chip: nand chip info structure
3709 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003710 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003711int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3712 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003713{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003714 int length = mtd->oobsize;
3715 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3716 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003717 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003718 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003719
Boris Brezillon97d90da2017-11-30 18:01:29 +01003720 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3721 if (ret)
3722 return ret;
3723
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003724 for (i = 0; i < chip->ecc.steps; i++) {
3725 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003726 int ret;
3727
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003728 pos = eccsize + i * (eccsize + chunk);
3729 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003730 ret = nand_change_read_column_op(chip, pos,
3731 NULL, 0,
3732 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003733 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003734 ret = nand_read_page_op(chip, page, pos, NULL,
3735 0);
3736
3737 if (ret)
3738 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003739 } else
3740 sndrnd = 1;
3741 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003742
3743 ret = nand_read_data_op(chip, bufpoi, toread, false);
3744 if (ret)
3745 return ret;
3746
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003747 bufpoi += toread;
3748 length -= toread;
3749 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003750 if (length > 0) {
3751 ret = nand_read_data_op(chip, bufpoi, length, false);
3752 if (ret)
3753 return ret;
3754 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003755
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003756 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003757}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003758EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003759
3760/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003761 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003762 * @mtd: mtd info structure
3763 * @chip: nand chip info structure
3764 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003765 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003766int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003767{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003768 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3769 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003770}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003771EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003772
3773/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003774 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003775 * with syndrome - only for large page flash
3776 * @mtd: mtd info structure
3777 * @chip: nand chip info structure
3778 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003779 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003780int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3781 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003782{
3783 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3784 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003785 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003786 const uint8_t *bufpoi = chip->oob_poi;
3787
3788 /*
3789 * data-ecc-data-ecc ... ecc-oob
3790 * or
3791 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3792 */
3793 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3794 pos = steps * (eccsize + chunk);
3795 steps = 0;
3796 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003797 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003798
Boris Brezillon97d90da2017-11-30 18:01:29 +01003799 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3800 if (ret)
3801 return ret;
3802
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003803 for (i = 0; i < steps; i++) {
3804 if (sndcmd) {
3805 if (mtd->writesize <= 512) {
3806 uint32_t fill = 0xFFFFFFFF;
3807
3808 len = eccsize;
3809 while (len > 0) {
3810 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003811
3812 ret = nand_write_data_op(chip, &fill,
3813 num, false);
3814 if (ret)
3815 return ret;
3816
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003817 len -= num;
3818 }
3819 } else {
3820 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003821 ret = nand_change_write_column_op(chip, pos,
3822 NULL, 0,
3823 false);
3824 if (ret)
3825 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003826 }
3827 } else
3828 sndcmd = 1;
3829 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003830
3831 ret = nand_write_data_op(chip, bufpoi, len, false);
3832 if (ret)
3833 return ret;
3834
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003835 bufpoi += len;
3836 length -= len;
3837 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003838 if (length > 0) {
3839 ret = nand_write_data_op(chip, bufpoi, length, false);
3840 if (ret)
3841 return ret;
3842 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003843
Boris Brezillon97d90da2017-11-30 18:01:29 +01003844 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003845}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003846EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003847
3848/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003849 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003850 * @mtd: MTD device structure
3851 * @from: offset to read from
3852 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003854 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003856static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3857 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003859 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003860 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003861 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003862 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003863 int readlen = ops->ooblen;
3864 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003865 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003866 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Brian Norris289c0522011-07-19 10:06:09 -07003868 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303869 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Brian Norris041e4572011-06-23 16:45:24 -07003871 stats = mtd->ecc_stats;
3872
Boris BREZILLON29f10582016-03-07 10:46:52 +01003873 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003874
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003875 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003876 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003878 /* Shift to get page */
3879 realpage = (int)(from >> chip->page_shift);
3880 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Florian Fainellif8ac0412010-09-07 13:23:43 +02003882 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003883 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003884 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003885 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003886 ret = chip->ecc.read_oob(mtd, chip, page);
3887
3888 if (ret < 0)
3889 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003890
3891 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003892 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003893
Brian Norris5bc7c332013-03-13 09:51:31 -07003894 if (chip->options & NAND_NEED_READRDY) {
3895 /* Apply delay or wait for ready/busy pin */
3896 if (!chip->dev_ready)
3897 udelay(chip->chip_delay);
3898 else
3899 nand_wait_ready(mtd);
3900 }
3901
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003902 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3903
Vitaly Wool70145682006-11-03 18:20:38 +03003904 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003905 if (!readlen)
3906 break;
3907
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003908 /* Increment page address */
3909 realpage++;
3910
3911 page = realpage & chip->pagemask;
3912 /* Check, if we cross a chip boundary */
3913 if (!page) {
3914 chipnr++;
3915 chip->select_chip(mtd, -1);
3916 chip->select_chip(mtd, chipnr);
3917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003919 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003921 ops->oobretlen = ops->ooblen - readlen;
3922
3923 if (ret < 0)
3924 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003925
3926 if (mtd->ecc_stats.failed - stats.failed)
3927 return -EBADMSG;
3928
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003929 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930}
3931
3932/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003933 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003934 * @mtd: MTD device structure
3935 * @from: offset to read from
3936 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003938 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003940static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3941 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003943 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003944
3945 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003947 if (ops->mode != MTD_OPS_PLACE_OOB &&
3948 ops->mode != MTD_OPS_AUTO_OOB &&
3949 ops->mode != MTD_OPS_RAW)
3950 return -ENOTSUPP;
3951
Huang Shijie6a8214a2012-11-19 14:43:30 +08003952 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003954 if (!ops->datbuf)
3955 ret = nand_do_read_oob(mtd, from, ops);
3956 else
3957 ret = nand_do_read_ops(mtd, from, ops);
3958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003960 return ret;
3961}
3962
3963
3964/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003965 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003966 * @mtd: mtd info structure
3967 * @chip: nand chip info structure
3968 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003969 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003970 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003971 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003972 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003973 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003974int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
3975 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003976{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003977 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003978
Boris Brezillon25f815f2017-11-30 18:01:30 +01003979 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003980 if (ret)
3981 return ret;
3982
3983 if (oob_required) {
3984 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3985 false);
3986 if (ret)
3987 return ret;
3988 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003989
Boris Brezillon25f815f2017-11-30 18:01:30 +01003990 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003992EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003994/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003995 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003996 * @mtd: mtd info structure
3997 * @chip: nand chip info structure
3998 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003999 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004000 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004001 *
4002 * We need a special oob layout and handling even when ECC isn't checked.
4003 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004004static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004005 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004006 const uint8_t *buf, int oob_required,
4007 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004008{
4009 int eccsize = chip->ecc.size;
4010 int eccbytes = chip->ecc.bytes;
4011 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004012 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004013
Boris Brezillon25f815f2017-11-30 18:01:30 +01004014 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4015 if (ret)
4016 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004017
4018 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004019 ret = nand_write_data_op(chip, buf, eccsize, false);
4020 if (ret)
4021 return ret;
4022
David Brownell52ff49d2009-03-04 12:01:36 -08004023 buf += eccsize;
4024
4025 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004026 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4027 false);
4028 if (ret)
4029 return ret;
4030
David Brownell52ff49d2009-03-04 12:01:36 -08004031 oob += chip->ecc.prepad;
4032 }
4033
Boris Brezillon97d90da2017-11-30 18:01:29 +01004034 ret = nand_write_data_op(chip, oob, eccbytes, false);
4035 if (ret)
4036 return ret;
4037
David Brownell52ff49d2009-03-04 12:01:36 -08004038 oob += eccbytes;
4039
4040 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004041 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4042 false);
4043 if (ret)
4044 return ret;
4045
David Brownell52ff49d2009-03-04 12:01:36 -08004046 oob += chip->ecc.postpad;
4047 }
4048 }
4049
4050 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004051 if (size) {
4052 ret = nand_write_data_op(chip, oob, size, false);
4053 if (ret)
4054 return ret;
4055 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004056
Boris Brezillon25f815f2017-11-30 18:01:30 +01004057 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004058}
4059/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004060 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004061 * @mtd: mtd info structure
4062 * @chip: nand chip info structure
4063 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004064 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004065 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004066 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004067static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004068 const uint8_t *buf, int oob_required,
4069 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004070{
Boris Brezillon846031d2016-02-03 20:11:00 +01004071 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004072 int eccbytes = chip->ecc.bytes;
4073 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004074 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004075 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004076
Brian Norris7854d3f2011-06-23 14:12:08 -07004077 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004078 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
4079 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004080
Boris Brezillon846031d2016-02-03 20:11:00 +01004081 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4082 chip->ecc.total);
4083 if (ret)
4084 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004085
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004086 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004087}
4088
4089/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004090 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004091 * @mtd: mtd info structure
4092 * @chip: nand chip info structure
4093 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004094 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004095 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004096 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004097static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004098 const uint8_t *buf, int oob_required,
4099 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004100{
Boris Brezillon846031d2016-02-03 20:11:00 +01004101 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004102 int eccbytes = chip->ecc.bytes;
4103 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004104 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004105 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004106
Boris Brezillon25f815f2017-11-30 18:01:30 +01004107 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4108 if (ret)
4109 return ret;
4110
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004111 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4112 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004113
4114 ret = nand_write_data_op(chip, p, eccsize, false);
4115 if (ret)
4116 return ret;
4117
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004118 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
4119 }
4120
Boris Brezillon846031d2016-02-03 20:11:00 +01004121 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4122 chip->ecc.total);
4123 if (ret)
4124 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004125
Boris Brezillon97d90da2017-11-30 18:01:29 +01004126 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4127 if (ret)
4128 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004129
Boris Brezillon25f815f2017-11-30 18:01:30 +01004130 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004131}
4132
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304133
4134/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004135 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304136 * @mtd: mtd info structure
4137 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004138 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304139 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004140 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304141 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004142 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304143 */
4144static int nand_write_subpage_hwecc(struct mtd_info *mtd,
4145 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07004146 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004147 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304148{
4149 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004150 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304151 int ecc_size = chip->ecc.size;
4152 int ecc_bytes = chip->ecc.bytes;
4153 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304154 uint32_t start_step = offset / ecc_size;
4155 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4156 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004157 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304158
Boris Brezillon25f815f2017-11-30 18:01:30 +01004159 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4160 if (ret)
4161 return ret;
4162
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304163 for (step = 0; step < ecc_steps; step++) {
4164 /* configure controller for WRITE access */
4165 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
4166
4167 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004168 ret = nand_write_data_op(chip, buf, ecc_size, false);
4169 if (ret)
4170 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304171
4172 /* mask ECC of un-touched subpages by padding 0xFF */
4173 if ((step < start_step) || (step > end_step))
4174 memset(ecc_calc, 0xff, ecc_bytes);
4175 else
Brian Norrisd6a950802013-08-08 17:16:36 -07004176 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304177
4178 /* mask OOB of un-touched subpages by padding 0xFF */
4179 /* if oob_required, preserve OOB metadata of written subpage */
4180 if (!oob_required || (step < start_step) || (step > end_step))
4181 memset(oob_buf, 0xff, oob_bytes);
4182
Brian Norrisd6a950802013-08-08 17:16:36 -07004183 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304184 ecc_calc += ecc_bytes;
4185 oob_buf += oob_bytes;
4186 }
4187
4188 /* copy calculated ECC for whole page to chip->buffer->oob */
4189 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004190 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004191 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4192 chip->ecc.total);
4193 if (ret)
4194 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304195
4196 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004197 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4198 if (ret)
4199 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304200
Boris Brezillon25f815f2017-11-30 18:01:30 +01004201 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304202}
4203
4204
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004205/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004206 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004207 * @mtd: mtd info structure
4208 * @chip: nand chip info structure
4209 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004210 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004211 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004212 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004213 * The hw generator calculates the error syndrome automatically. Therefore we
4214 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004215 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004216static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07004217 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004218 const uint8_t *buf, int oob_required,
4219 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004220{
4221 int i, eccsize = chip->ecc.size;
4222 int eccbytes = chip->ecc.bytes;
4223 int eccsteps = chip->ecc.steps;
4224 const uint8_t *p = buf;
4225 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004226 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004227
Boris Brezillon25f815f2017-11-30 18:01:30 +01004228 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4229 if (ret)
4230 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004231
4232 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004233 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004234
4235 ret = nand_write_data_op(chip, p, eccsize, false);
4236 if (ret)
4237 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004238
4239 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004240 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4241 false);
4242 if (ret)
4243 return ret;
4244
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004245 oob += chip->ecc.prepad;
4246 }
4247
4248 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004249
4250 ret = nand_write_data_op(chip, oob, eccbytes, false);
4251 if (ret)
4252 return ret;
4253
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004254 oob += eccbytes;
4255
4256 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004257 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4258 false);
4259 if (ret)
4260 return ret;
4261
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004262 oob += chip->ecc.postpad;
4263 }
4264 }
4265
4266 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004267 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004268 if (i) {
4269 ret = nand_write_data_op(chip, oob, i, false);
4270 if (ret)
4271 return ret;
4272 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004273
Boris Brezillon25f815f2017-11-30 18:01:30 +01004274 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004275}
4276
4277/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004278 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004279 * @mtd: MTD device structure
4280 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304281 * @offset: address offset within the page
4282 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004283 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004284 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004285 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004286 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004287 */
4288static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304289 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004290 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004291{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304292 int status, subpage;
4293
4294 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4295 chip->ecc.write_subpage)
4296 subpage = offset || (data_len < mtd->writesize);
4297 else
4298 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004299
David Woodhouse956e9442006-09-25 17:12:39 +01004300 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304301 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004302 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304303 else if (subpage)
4304 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004305 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004306 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004307 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
4308 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004309
4310 if (status < 0)
4311 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004312
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004313 return 0;
4314}
4315
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004316/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004317 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004318 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004319 * @oob: oob data buffer
4320 * @len: oob data write length
4321 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004322 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004323static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4324 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004325{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004326 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004327 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004328
4329 /*
4330 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4331 * data from a previous OOB read.
4332 */
4333 memset(chip->oob_poi, 0xff, mtd->oobsize);
4334
Florian Fainellif8ac0412010-09-07 13:23:43 +02004335 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004336
Brian Norris0612b9d2011-08-30 18:45:40 -07004337 case MTD_OPS_PLACE_OOB:
4338 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004339 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4340 return oob + len;
4341
Boris Brezillon846031d2016-02-03 20:11:00 +01004342 case MTD_OPS_AUTO_OOB:
4343 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4344 ops->ooboffs, len);
4345 BUG_ON(ret);
4346 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004347
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004348 default:
4349 BUG();
4350 }
4351 return NULL;
4352}
4353
Florian Fainellif8ac0412010-09-07 13:23:43 +02004354#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004355
4356/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004357 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004358 * @mtd: MTD device structure
4359 * @to: offset to write to
4360 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004361 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004362 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004363 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004364static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4365 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004366{
Corentin Labbe73600b62017-09-02 10:49:38 +02004367 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004368 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004369 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004370
4371 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004372 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004373
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004374 uint8_t *oob = ops->oobbuf;
4375 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304376 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004377 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004378
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004379 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004380 if (!writelen)
4381 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004382
Brian Norris8b6e50c2011-05-25 14:59:01 -07004383 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004384 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004385 pr_notice("%s: attempt to write non page aligned data\n",
4386 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004387 return -EINVAL;
4388 }
4389
Thomas Gleixner29072b92006-09-28 15:38:36 +02004390 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004391
Thomas Gleixner6a930962006-06-28 00:11:45 +02004392 chipnr = (int)(to >> chip->chip_shift);
4393 chip->select_chip(mtd, chipnr);
4394
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004395 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004396 if (nand_check_wp(mtd)) {
4397 ret = -EIO;
4398 goto err_out;
4399 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004400
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004401 realpage = (int)(to >> chip->page_shift);
4402 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004403
4404 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004405 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4406 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004407 chip->pagebuf = -1;
4408
Maxim Levitsky782ce792010-02-22 20:39:36 +02004409 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004410 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4411 ret = -EINVAL;
4412 goto err_out;
4413 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004414
Florian Fainellif8ac0412010-09-07 13:23:43 +02004415 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004416 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004417 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004418 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004419 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004420
Kamal Dasu66507c72014-05-01 20:51:19 -04004421 if (part_pagewr)
4422 use_bufpoi = 1;
4423 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004424 use_bufpoi = !virt_addr_valid(buf) ||
4425 !IS_ALIGNED((unsigned long)buf,
4426 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004427 else
4428 use_bufpoi = 0;
4429
4430 /* Partial page write?, or need to use bounce buffer */
4431 if (use_bufpoi) {
4432 pr_debug("%s: using write bounce buffer for buf@%p\n",
4433 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004434 if (part_pagewr)
4435 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004436 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004437 memset(chip->data_buf, 0xff, mtd->writesize);
4438 memcpy(&chip->data_buf[column], buf, bytes);
4439 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004440 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004441
Maxim Levitsky782ce792010-02-22 20:39:36 +02004442 if (unlikely(oob)) {
4443 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004444 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004445 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004446 } else {
4447 /* We still need to erase leftover OOB data */
4448 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004449 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004450
4451 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004452 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004453 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004454 if (ret)
4455 break;
4456
4457 writelen -= bytes;
4458 if (!writelen)
4459 break;
4460
Thomas Gleixner29072b92006-09-28 15:38:36 +02004461 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004462 buf += bytes;
4463 realpage++;
4464
4465 page = realpage & chip->pagemask;
4466 /* Check, if we cross a chip boundary */
4467 if (!page) {
4468 chipnr++;
4469 chip->select_chip(mtd, -1);
4470 chip->select_chip(mtd, chipnr);
4471 }
4472 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004473
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004474 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004475 if (unlikely(oob))
4476 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004477
4478err_out:
4479 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004480 return ret;
4481}
4482
4483/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004484 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004485 * @mtd: MTD device structure
4486 * @to: offset to write to
4487 * @len: number of bytes to write
4488 * @retlen: pointer to variable to store the number of written bytes
4489 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004490 *
4491 * NAND write with ECC. Used when performing writes in interrupt context, this
4492 * may for example be called by mtdoops when writing an oops while in panic.
4493 */
4494static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4495 size_t *retlen, const uint8_t *buf)
4496{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004497 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004498 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004499 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004500 int ret;
4501
Brian Norris8b6e50c2011-05-25 14:59:01 -07004502 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004503 panic_nand_get_device(chip, mtd, FL_WRITING);
4504
Brent Taylor30863e382017-10-30 22:32:45 -05004505 chip->select_chip(mtd, chipnr);
4506
4507 /* Wait for the device to get ready */
4508 panic_nand_wait(mtd, chip, 400);
4509
Brian Norris0ec56dc2015-02-28 02:02:30 -08004510 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004511 ops.len = len;
4512 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004513 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004514
Brian Norris4a89ff82011-08-30 18:45:45 -07004515 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004516
Brian Norris4a89ff82011-08-30 18:45:45 -07004517 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004518 return ret;
4519}
4520
4521/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004522 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004523 * @mtd: MTD device structure
4524 * @to: offset to write to
4525 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004526 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004527 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004528 */
4529static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4530 struct mtd_oob_ops *ops)
4531{
Adrian Hunter03736152007-01-31 17:58:29 +02004532 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004533 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Brian Norris289c0522011-07-19 10:06:09 -07004535 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304536 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
Boris BREZILLON29f10582016-03-07 10:46:52 +01004538 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004541 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004542 pr_debug("%s: attempt to write past end of page\n",
4543 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 return -EINVAL;
4545 }
4546
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004547 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004548
4549 /*
4550 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4551 * of my DiskOnChip 2000 test units) will clear the whole data page too
4552 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4553 * it in the doc2000 driver in August 1999. dwmw2.
4554 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004555 nand_reset(chip, chipnr);
4556
4557 chip->select_chip(mtd, chipnr);
4558
4559 /* Shift to get page */
4560 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004563 if (nand_check_wp(mtd)) {
4564 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004565 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004566 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004569 if (page == chip->pagebuf)
4570 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004572 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004573
Brian Norris0612b9d2011-08-30 18:45:40 -07004574 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07004575 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
4576 else
4577 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004578
Huang Shijieb0bb6902012-11-19 14:43:29 +08004579 chip->select_chip(mtd, -1);
4580
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004581 if (status)
4582 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Vitaly Wool70145682006-11-03 18:20:38 +03004584 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004586 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004587}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004589/**
4590 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004591 * @mtd: MTD device structure
4592 * @to: offset to write to
4593 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004594 */
4595static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4596 struct mtd_oob_ops *ops)
4597{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004598 int ret = -ENOTSUPP;
4599
4600 ops->retlen = 0;
4601
Huang Shijie6a8214a2012-11-19 14:43:30 +08004602 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004603
Florian Fainellif8ac0412010-09-07 13:23:43 +02004604 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004605 case MTD_OPS_PLACE_OOB:
4606 case MTD_OPS_AUTO_OOB:
4607 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004608 break;
4609
4610 default:
4611 goto out;
4612 }
4613
4614 if (!ops->datbuf)
4615 ret = nand_do_write_oob(mtd, to, ops);
4616 else
4617 ret = nand_do_write_ops(mtd, to, ops);
4618
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004619out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004620 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 return ret;
4622}
4623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624/**
Brian Norris49c50b92014-05-06 16:02:19 -07004625 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004626 * @mtd: MTD device structure
4627 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 *
Brian Norris49c50b92014-05-06 16:02:19 -07004629 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 */
Brian Norris49c50b92014-05-06 16:02:19 -07004631static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004633 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004634 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004635
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004637 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004638
Boris Brezillon97d90da2017-11-30 18:01:29 +01004639 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640}
4641
4642/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004644 * @mtd: MTD device structure
4645 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004647 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004649static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650{
David Woodhousee0c7d762006-05-13 18:07:53 +01004651 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004655 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004656 * @mtd: MTD device structure
4657 * @instr: erase instruction
4658 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004660 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004662int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4663 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
Adrian Hunter69423d92008-12-10 13:37:21 +00004665 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004666 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00004667 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Brian Norris289c0522011-07-19 10:06:09 -07004669 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4670 __func__, (unsigned long long)instr->addr,
4671 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304673 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004677 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
4679 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004680 page = (int)(instr->addr >> chip->page_shift);
4681 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
4683 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004684 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
4686 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004687 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 /* Check, if it is write protected */
4690 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004691 pr_debug("%s: device is write protected!\n",
4692 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 instr->state = MTD_ERASE_FAILED;
4694 goto erase_exit;
4695 }
4696
4697 /* Loop through the pages */
4698 len = instr->len;
4699
4700 instr->state = MTD_ERASING;
4701
4702 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004703 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004704 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304705 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004706 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4707 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 instr->state = MTD_ERASE_FAILED;
4709 goto erase_exit;
4710 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004711
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004712 /*
4713 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004714 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004715 */
4716 if (page <= chip->pagebuf && chip->pagebuf <
4717 (page + pages_per_block))
4718 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Brian Norris49c50b92014-05-06 16:02:19 -07004720 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
4722 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004723 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004724 pr_debug("%s: failed erase, page 0x%08x\n",
4725 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00004727 instr->fail_addr =
4728 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 goto erase_exit;
4730 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004731
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004733 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 page += pages_per_block;
4735
4736 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004737 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004739 chip->select_chip(mtd, -1);
4740 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 }
4742 }
4743 instr->state = MTD_ERASE_DONE;
4744
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004745erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
4747 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748
4749 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004750 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 nand_release_device(mtd);
4752
David Woodhouse49defc02007-10-06 15:01:59 -04004753 /* Do call back function */
4754 if (!ret)
4755 mtd_erase_callback(instr);
4756
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 /* Return more or less happy */
4758 return ret;
4759}
4760
4761/**
4762 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004763 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004765 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004767static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
Brian Norris289c0522011-07-19 10:06:09 -07004769 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
4771 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004772 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004774 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775}
4776
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004778 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004779 * @mtd: MTD device structure
4780 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004782static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304784 struct nand_chip *chip = mtd_to_nand(mtd);
4785 int chipnr = (int)(offs >> chip->chip_shift);
4786 int ret;
4787
4788 /* Select the NAND device */
4789 nand_get_device(mtd, FL_READING);
4790 chip->select_chip(mtd, chipnr);
4791
4792 ret = nand_block_checkbad(mtd, offs, 0);
4793
4794 chip->select_chip(mtd, -1);
4795 nand_release_device(mtd);
4796
4797 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798}
4799
4800/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004801 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004802 * @mtd: MTD device structure
4803 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004805static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 int ret;
4808
Florian Fainellif8ac0412010-09-07 13:23:43 +02004809 ret = nand_block_isbad(mtd, ofs);
4810 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004811 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 if (ret > 0)
4813 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004814 return ret;
4815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Brian Norris5a0edb22013-07-30 17:52:58 -07004817 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818}
4819
4820/**
Zach Brown56718422017-01-10 13:30:20 -06004821 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4822 * @mtd: MTD device structure
4823 * @ofs: offset relative to mtd start
4824 * @len: length of mtd
4825 */
4826static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4827{
4828 struct nand_chip *chip = mtd_to_nand(mtd);
4829 u32 part_start_block;
4830 u32 part_end_block;
4831 u32 part_start_die;
4832 u32 part_end_die;
4833
4834 /*
4835 * max_bb_per_die and blocks_per_die used to determine
4836 * the maximum bad block count.
4837 */
4838 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4839 return -ENOTSUPP;
4840
4841 /* Get the start and end of the partition in erase blocks. */
4842 part_start_block = mtd_div_by_eb(ofs, mtd);
4843 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4844
4845 /* Get the start and end LUNs of the partition. */
4846 part_start_die = part_start_block / chip->blocks_per_die;
4847 part_end_die = part_end_block / chip->blocks_per_die;
4848
4849 /*
4850 * Look up the bad blocks per unit and multiply by the number of units
4851 * that the partition spans.
4852 */
4853 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4854}
4855
4856/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004857 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004858 * @mtd: MTD device structure
4859 * @chip: nand chip info structure
4860 * @addr: feature address.
4861 * @subfeature_param: the subfeature parameters, a four bytes array.
4862 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004863static int nand_default_set_features(struct mtd_info *mtd,
4864 struct nand_chip *chip, int addr,
4865 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004866{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004867 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004868}
4869
4870/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004871 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004872 * @mtd: MTD device structure
4873 * @chip: nand chip info structure
4874 * @addr: feature address.
4875 * @subfeature_param: the subfeature parameters, a four bytes array.
4876 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004877static int nand_default_get_features(struct mtd_info *mtd,
4878 struct nand_chip *chip, int addr,
4879 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004880{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004881 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004882}
4883
4884/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004885 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004886 * @mtd: MTD device structure
4887 * @chip: nand chip info structure
4888 * @addr: feature address.
4889 * @subfeature_param: the subfeature parameters, a four bytes array.
4890 *
4891 * Should be used by NAND controller drivers that do not support the SET/GET
4892 * FEATURES operations.
4893 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004894int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4895 int addr, u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004896{
4897 return -ENOTSUPP;
4898}
Miquel Raynalb9587582018-03-19 14:47:19 +01004899EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004900
4901/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004902 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004903 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004904 */
4905static int nand_suspend(struct mtd_info *mtd)
4906{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004907 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004908}
4909
4910/**
4911 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004912 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004913 */
4914static void nand_resume(struct mtd_info *mtd)
4915{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004916 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004917
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004918 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004919 nand_release_device(mtd);
4920 else
Brian Norrisd0370212011-07-19 10:06:08 -07004921 pr_err("%s called for a chip which is not in suspended state\n",
4922 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004923}
4924
Scott Branden72ea4032014-11-20 11:18:05 -08004925/**
4926 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4927 * prevent further operations
4928 * @mtd: MTD device structure
4929 */
4930static void nand_shutdown(struct mtd_info *mtd)
4931{
Brian Norris9ca641b2015-11-09 16:37:28 -08004932 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004933}
4934
Brian Norris8b6e50c2011-05-25 14:59:01 -07004935/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004936static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004937{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004938 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004941 if (!chip->chip_delay)
4942 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
4944 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004945 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004946 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
4948 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004949 if (chip->waitfunc == NULL)
4950 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004952 if (!chip->select_chip)
4953 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004954
Huang Shijie4204ccc2013-08-16 10:10:07 +08004955 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004956 if (!chip->set_features)
4957 chip->set_features = nand_default_set_features;
4958 if (!chip->get_features)
4959 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004960
Brian Norris68e80782013-07-18 01:17:02 -07004961 /* If called twice, pointers that depend on busw may need to be reset */
4962 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004963 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4964 if (!chip->read_word)
4965 chip->read_word = nand_read_word;
4966 if (!chip->block_bad)
4967 chip->block_bad = nand_block_bad;
4968 if (!chip->block_markbad)
4969 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004970 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004971 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004972 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4973 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004974 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004975 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004976 if (!chip->scan_bbt)
4977 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004978
4979 if (!chip->controller) {
4980 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02004981 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004982 }
4983
Masahiro Yamada477544c2017-03-30 17:15:05 +09004984 if (!chip->buf_align)
4985 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004986}
4987
Brian Norris8b6e50c2011-05-25 14:59:01 -07004988/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004989static void sanitize_string(uint8_t *s, size_t len)
4990{
4991 ssize_t i;
4992
Brian Norris8b6e50c2011-05-25 14:59:01 -07004993 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004994 s[len - 1] = 0;
4995
Brian Norris8b6e50c2011-05-25 14:59:01 -07004996 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004997 for (i = 0; i < len - 1; i++) {
4998 if (s[i] < ' ' || s[i] > 127)
4999 s[i] = '?';
5000 }
5001
Brian Norris8b6e50c2011-05-25 14:59:01 -07005002 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005003 strim(s);
5004}
5005
5006static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
5007{
5008 int i;
5009 while (len--) {
5010 crc ^= *p++ << 8;
5011 for (i = 0; i < 8; i++)
5012 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
5013 }
5014
5015 return crc;
5016}
5017
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005018/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005019static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
5020 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005021{
5022 struct onfi_ext_param_page *ep;
5023 struct onfi_ext_section *s;
5024 struct onfi_ext_ecc_info *ecc;
5025 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005026 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005027 int len;
5028 int i;
5029
5030 len = le16_to_cpu(p->ext_param_page_length) * 16;
5031 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005032 if (!ep)
5033 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005034
5035 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005036 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5037 if (ret)
5038 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005039
5040 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005041 ret = nand_change_read_column_op(chip,
5042 sizeof(*p) * p->num_of_param_pages,
5043 ep, len, true);
5044 if (ret)
5045 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005046
Boris Brezillon97d90da2017-11-30 18:01:29 +01005047 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005048 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5049 != le16_to_cpu(ep->crc))) {
5050 pr_debug("fail in the CRC.\n");
5051 goto ext_out;
5052 }
5053
5054 /*
5055 * Check the signature.
5056 * Do not strictly follow the ONFI spec, maybe changed in future.
5057 */
5058 if (strncmp(ep->sig, "EPPS", 4)) {
5059 pr_debug("The signature is invalid.\n");
5060 goto ext_out;
5061 }
5062
5063 /* find the ECC section. */
5064 cursor = (uint8_t *)(ep + 1);
5065 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5066 s = ep->sections + i;
5067 if (s->type == ONFI_SECTION_TYPE_2)
5068 break;
5069 cursor += s->length * 16;
5070 }
5071 if (i == ONFI_EXT_SECTION_MAX) {
5072 pr_debug("We can not find the ECC section.\n");
5073 goto ext_out;
5074 }
5075
5076 /* get the info we want. */
5077 ecc = (struct onfi_ext_ecc_info *)cursor;
5078
Brian Norris4ae7d222013-09-16 18:20:21 -07005079 if (!ecc->codeword_size) {
5080 pr_debug("Invalid codeword size\n");
5081 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005082 }
5083
Brian Norris4ae7d222013-09-16 18:20:21 -07005084 chip->ecc_strength_ds = ecc->ecc_bits;
5085 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005086 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005087
5088ext_out:
5089 kfree(ep);
5090 return ret;
5091}
5092
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005093/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005094 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005095 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005096static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005097{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005098 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005099 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005100 char id[4];
5101 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005102
Brian Norris7854d3f2011-06-23 14:12:08 -07005103 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005104 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5105 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005106 return 0;
5107
Boris Brezillon97d90da2017-11-30 18:01:29 +01005108 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5109 if (ret)
5110 return 0;
5111
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005112 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005113 ret = nand_read_data_op(chip, p, sizeof(*p), true);
5114 if (ret)
5115 return 0;
5116
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005117 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
5118 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005119 break;
5120 }
5121 }
5122
Brian Norrisc7f23a72013-08-13 10:51:55 -07005123 if (i == 3) {
5124 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005125 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07005126 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005127
Brian Norris8b6e50c2011-05-25 14:59:01 -07005128 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005129 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005130 if (val & (1 << 5))
5131 chip->onfi_version = 23;
5132 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005133 chip->onfi_version = 22;
5134 else if (val & (1 << 3))
5135 chip->onfi_version = 21;
5136 else if (val & (1 << 2))
5137 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005138 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005139 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005140
5141 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005142 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005143 return 0;
5144 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005145
5146 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5147 sanitize_string(p->model, sizeof(p->model));
5148 if (!mtd->name)
5149 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07005150
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005151 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005152
5153 /*
5154 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5155 * (don't ask me who thought of this...). MTD assumes that these
5156 * dimensions will be power-of-2, so just truncate the remaining area.
5157 */
5158 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5159 mtd->erasesize *= mtd->writesize;
5160
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005161 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005162
5163 /* See erasesize comment */
5164 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005165 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005166 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005167
Zach Brown34da5f52017-01-10 13:30:21 -06005168 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5169 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5170
Huang Shijiee2985fc2013-05-17 11:17:30 +08005171 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005172 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005173
Huang Shijie10c86ba2013-05-17 11:17:26 +08005174 if (p->ecc_bits != 0xff) {
5175 chip->ecc_strength_ds = p->ecc_bits;
5176 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005177 } else if (chip->onfi_version >= 21 &&
5178 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
5179
5180 /*
5181 * The nand_flash_detect_ext_param_page() uses the
5182 * Change Read Column command which maybe not supported
5183 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5184 * now. We do not replace user supplied command function.
5185 */
5186 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5187 chip->cmdfunc = nand_command_lp;
5188
5189 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005190 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005191 pr_warn("Failed to detect ONFI extended param page\n");
5192 } else {
5193 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005194 }
5195
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005196 return 1;
5197}
5198
5199/*
Huang Shijie91361812014-02-21 13:39:40 +08005200 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5201 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005202static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005203{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005204 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08005205 struct nand_jedec_params *p = &chip->jedec_params;
5206 struct jedec_ecc_info *ecc;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005207 char id[5];
5208 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005209
5210 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005211 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5212 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005213 return 0;
5214
Boris Brezillon97d90da2017-11-30 18:01:29 +01005215 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
5216 if (ret)
5217 return 0;
5218
Huang Shijie91361812014-02-21 13:39:40 +08005219 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005220 ret = nand_read_data_op(chip, p, sizeof(*p), true);
5221 if (ret)
5222 return 0;
Huang Shijie91361812014-02-21 13:39:40 +08005223
5224 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5225 le16_to_cpu(p->crc))
5226 break;
5227 }
5228
5229 if (i == 3) {
5230 pr_err("Could not find valid JEDEC parameter page; aborting\n");
5231 return 0;
5232 }
5233
5234 /* Check version */
5235 val = le16_to_cpu(p->revision);
5236 if (val & (1 << 2))
5237 chip->jedec_version = 10;
5238 else if (val & (1 << 1))
5239 chip->jedec_version = 1; /* vendor specific version */
5240
5241 if (!chip->jedec_version) {
5242 pr_info("unsupported JEDEC version: %d\n", val);
5243 return 0;
5244 }
5245
5246 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5247 sanitize_string(p->model, sizeof(p->model));
5248 if (!mtd->name)
5249 mtd->name = p->model;
5250
5251 mtd->writesize = le32_to_cpu(p->byte_per_page);
5252
5253 /* Please reference to the comment for nand_flash_detect_onfi. */
5254 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5255 mtd->erasesize *= mtd->writesize;
5256
5257 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5258
5259 /* Please reference to the comment for nand_flash_detect_onfi. */
5260 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5261 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5262 chip->bits_per_cell = p->bits_per_cell;
5263
5264 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005265 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005266
5267 /* ECC info */
5268 ecc = &p->ecc_info[0];
5269
5270 if (ecc->codeword_size >= 9) {
5271 chip->ecc_strength_ds = ecc->ecc_bits;
5272 chip->ecc_step_ds = 1 << ecc->codeword_size;
5273 } else {
5274 pr_warn("Invalid codeword size\n");
5275 }
5276
5277 return 1;
5278}
5279
5280/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005281 * nand_id_has_period - Check if an ID string has a given wraparound period
5282 * @id_data: the ID string
5283 * @arrlen: the length of the @id_data array
5284 * @period: the period of repitition
5285 *
5286 * Check if an ID string is repeated within a given sequence of bytes at
5287 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005288 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005289 * if the repetition has a period of @period; otherwise, returns zero.
5290 */
5291static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5292{
5293 int i, j;
5294 for (i = 0; i < period; i++)
5295 for (j = i + period; j < arrlen; j += period)
5296 if (id_data[i] != id_data[j])
5297 return 0;
5298 return 1;
5299}
5300
5301/*
5302 * nand_id_len - Get the length of an ID string returned by CMD_READID
5303 * @id_data: the ID string
5304 * @arrlen: the length of the @id_data array
5305
5306 * Returns the length of the ID string, according to known wraparound/trailing
5307 * zero patterns. If no pattern exists, returns the length of the array.
5308 */
5309static int nand_id_len(u8 *id_data, int arrlen)
5310{
5311 int last_nonzero, period;
5312
5313 /* Find last non-zero byte */
5314 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5315 if (id_data[last_nonzero])
5316 break;
5317
5318 /* All zeros */
5319 if (last_nonzero < 0)
5320 return 0;
5321
5322 /* Calculate wraparound period */
5323 for (period = 1; period < arrlen; period++)
5324 if (nand_id_has_period(id_data, arrlen, period))
5325 break;
5326
5327 /* There's a repeated pattern */
5328 if (period < arrlen)
5329 return period;
5330
5331 /* There are trailing zeros */
5332 if (last_nonzero < arrlen - 1)
5333 return last_nonzero + 1;
5334
5335 /* No pattern detected */
5336 return arrlen;
5337}
5338
Huang Shijie7db906b2013-09-25 14:58:11 +08005339/* Extract the bits of per cell from the 3rd byte of the extended ID */
5340static int nand_get_bits_per_cell(u8 cellinfo)
5341{
5342 int bits;
5343
5344 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5345 bits >>= NAND_CI_CELLTYPE_SHIFT;
5346 return bits + 1;
5347}
5348
Brian Norrise3b88bd2012-09-24 20:40:52 -07005349/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005350 * Many new NAND share similar device ID codes, which represent the size of the
5351 * chip. The rest of the parameters must be decoded according to generic or
5352 * manufacturer-specific "extended ID" decoding patterns.
5353 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005354void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005355{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005356 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005357 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005358 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005359 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005360 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005361 /* The 4th id byte is the important one */
5362 extid = id_data[3];
5363
Boris Brezillon01389b62016-06-08 10:30:18 +02005364 /* Calc pagesize */
5365 mtd->writesize = 1024 << (extid & 0x03);
5366 extid >>= 2;
5367 /* Calc oobsize */
5368 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5369 extid >>= 2;
5370 /* Calc blocksize. Blocksize is multiples of 64KiB */
5371 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5372 extid >>= 2;
5373 /* Get buswidth information */
5374 if (extid & 0x1)
5375 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005376}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005377EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005378
5379/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005380 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5381 * decodes a matching ID table entry and assigns the MTD size parameters for
5382 * the chip.
5383 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005384static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005385{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005386 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005387
5388 mtd->erasesize = type->erasesize;
5389 mtd->writesize = type->pagesize;
5390 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005391
Huang Shijie1c195e92013-09-25 14:58:12 +08005392 /* All legacy ID NAND are small-page, SLC */
5393 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005394}
5395
5396/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005397 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5398 * heuristic patterns using various detected parameters (e.g., manufacturer,
5399 * page size, cell-type information).
5400 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005401static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005402{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005403 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005404
5405 /* Set the bad block position */
5406 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5407 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5408 else
5409 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005410}
5411
Huang Shijieec6e87e2013-03-15 11:01:00 +08005412static inline bool is_full_id_nand(struct nand_flash_dev *type)
5413{
5414 return type->id_len;
5415}
5416
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005417static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005418 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005419{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005420 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005421 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005422
Huang Shijieec6e87e2013-03-15 11:01:00 +08005423 if (!strncmp(type->id, id_data, type->id_len)) {
5424 mtd->writesize = type->pagesize;
5425 mtd->erasesize = type->erasesize;
5426 mtd->oobsize = type->oobsize;
5427
Huang Shijie7db906b2013-09-25 14:58:11 +08005428 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005429 chip->chipsize = (uint64_t)type->chipsize << 20;
5430 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005431 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5432 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005433 chip->onfi_timing_mode_default =
5434 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005435
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005436 if (!mtd->name)
5437 mtd->name = type->name;
5438
Huang Shijieec6e87e2013-03-15 11:01:00 +08005439 return true;
5440 }
5441 return false;
5442}
5443
Brian Norris7e74c2d2012-09-24 20:40:49 -07005444/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005445 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5446 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5447 * table.
5448 */
5449static void nand_manufacturer_detect(struct nand_chip *chip)
5450{
5451 /*
5452 * Try manufacturer detection if available and use
5453 * nand_decode_ext_id() otherwise.
5454 */
5455 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005456 chip->manufacturer.desc->ops->detect) {
5457 /* The 3rd id byte holds MLC / multichip data */
5458 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005459 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005460 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005461 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005462 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005463}
5464
5465/*
5466 * Manufacturer initialization. This function is called for all NANDs including
5467 * ONFI and JEDEC compliant ones.
5468 * Manufacturer drivers should put all their specific initialization code in
5469 * their ->init() hook.
5470 */
5471static int nand_manufacturer_init(struct nand_chip *chip)
5472{
5473 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5474 !chip->manufacturer.desc->ops->init)
5475 return 0;
5476
5477 return chip->manufacturer.desc->ops->init(chip);
5478}
5479
5480/*
5481 * Manufacturer cleanup. This function is called for all NANDs including
5482 * ONFI and JEDEC compliant ones.
5483 * Manufacturer drivers should put all their specific cleanup code in their
5484 * ->cleanup() hook.
5485 */
5486static void nand_manufacturer_cleanup(struct nand_chip *chip)
5487{
5488 /* Release manufacturer private data */
5489 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5490 chip->manufacturer.desc->ops->cleanup)
5491 chip->manufacturer.desc->ops->cleanup(chip);
5492}
5493
5494/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005495 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005496 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005497static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005498{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005499 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005500 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005501 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005502 u8 *id_data = chip->id.data;
5503 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
Karl Beldanef89a882008-09-15 14:37:29 +02005505 /*
5506 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005507 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005508 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005509 ret = nand_reset(chip, 0);
5510 if (ret)
5511 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005512
5513 /* Select the device */
5514 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005515
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005517 ret = nand_readid_op(chip, 0, id_data, 2);
5518 if (ret)
5519 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
5521 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005522 maf_id = id_data[0];
5523 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524
Brian Norris8b6e50c2011-05-25 14:59:01 -07005525 /*
5526 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005527 * interface concerns can cause random data which looks like a
5528 * possibly credible NAND flash to appear. If the two results do
5529 * not match, ignore the device completely.
5530 */
5531
Brian Norris4aef9b72012-09-24 20:40:48 -07005532 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005533 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5534 if (ret)
5535 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005536
Boris Brezillon7f501f02016-05-24 19:20:05 +02005537 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005538 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005539 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005540 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005541 }
5542
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005543 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005544
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005545 /* Try to identify manufacturer */
5546 manufacturer = nand_get_manufacturer(maf_id);
5547 chip->manufacturer.desc = manufacturer;
5548
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005549 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005550 type = nand_flash_ids;
5551
Boris Brezillon29a198a2016-05-24 20:17:48 +02005552 /*
5553 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5554 * override it.
5555 * This is required to make sure initial NAND bus width set by the
5556 * NAND controller driver is coherent with the real NAND bus width
5557 * (extracted by auto-detection code).
5558 */
5559 busw = chip->options & NAND_BUSWIDTH_16;
5560
5561 /*
5562 * The flag is only set (never cleared), reset it to its default value
5563 * before starting auto-detection.
5564 */
5565 chip->options &= ~NAND_BUSWIDTH_16;
5566
Huang Shijieec6e87e2013-03-15 11:01:00 +08005567 for (; type->name != NULL; type++) {
5568 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005569 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005570 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005571 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005572 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005573 }
5574 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005575
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005576 chip->onfi_version = 0;
5577 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005578 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005579 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005580 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005581
5582 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005583 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08005584 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005585 }
5586
David Woodhouse5e81e882010-02-26 18:32:56 +00005587 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005588 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005589
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02005590 if (!mtd->name)
5591 mtd->name = type->name;
5592
Adrian Hunter69423d92008-12-10 13:37:21 +00005593 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005594
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005595 if (!type->pagesize)
5596 nand_manufacturer_detect(chip);
5597 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005598 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005599
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005600 /* Get chip options */
5601 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005602
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005603ident_done:
5604
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005605 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005606 WARN_ON(busw & NAND_BUSWIDTH_16);
5607 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005608 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5609 /*
5610 * Check, if buswidth is correct. Hardware drivers should set
5611 * chip correct!
5612 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005613 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005614 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005615 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5616 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005617 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5618 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005619 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005620 }
5621
Boris Brezillon7f501f02016-05-24 19:20:05 +02005622 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005623
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005624 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005625 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005626 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005627 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005628
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005629 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005630 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005631 if (chip->chipsize & 0xffffffff)
5632 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005633 else {
5634 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5635 chip->chip_shift += 32 - 1;
5636 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005637
Masahiro Yamada14157f82017-09-13 11:05:50 +09005638 if (chip->chip_shift - chip->page_shift > 16)
5639 chip->options |= NAND_ROW_ADDR_3;
5640
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005641 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005642 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005643
Brian Norris8b6e50c2011-05-25 14:59:01 -07005644 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005645 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5646 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005647
Ezequiel Garcia20171642013-11-25 08:30:31 -03005648 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005649 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005650
5651 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005652 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5653 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005654 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005655 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5656 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005657 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005658 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5659 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08005660
Rafał Miłecki3755a992014-10-21 00:01:04 +02005661 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005662 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005663 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005664 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005665}
5666
Boris Brezillond48f62b2016-04-01 14:54:32 +02005667static const char * const nand_ecc_modes[] = {
5668 [NAND_ECC_NONE] = "none",
5669 [NAND_ECC_SOFT] = "soft",
5670 [NAND_ECC_HW] = "hw",
5671 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5672 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005673 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005674};
5675
5676static int of_get_nand_ecc_mode(struct device_node *np)
5677{
5678 const char *pm;
5679 int err, i;
5680
5681 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5682 if (err < 0)
5683 return err;
5684
5685 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5686 if (!strcasecmp(pm, nand_ecc_modes[i]))
5687 return i;
5688
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005689 /*
5690 * For backward compatibility we support few obsoleted values that don't
5691 * have their mappings into nand_ecc_modes_t anymore (they were merged
5692 * with other enums).
5693 */
5694 if (!strcasecmp(pm, "soft_bch"))
5695 return NAND_ECC_SOFT;
5696
Boris Brezillond48f62b2016-04-01 14:54:32 +02005697 return -ENODEV;
5698}
5699
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005700static const char * const nand_ecc_algos[] = {
5701 [NAND_ECC_HAMMING] = "hamming",
5702 [NAND_ECC_BCH] = "bch",
5703};
5704
Boris Brezillond48f62b2016-04-01 14:54:32 +02005705static int of_get_nand_ecc_algo(struct device_node *np)
5706{
5707 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005708 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005709
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005710 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5711 if (!err) {
5712 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5713 if (!strcasecmp(pm, nand_ecc_algos[i]))
5714 return i;
5715 return -ENODEV;
5716 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005717
5718 /*
5719 * For backward compatibility we also read "nand-ecc-mode" checking
5720 * for some obsoleted values that were specifying ECC algorithm.
5721 */
5722 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5723 if (err < 0)
5724 return err;
5725
5726 if (!strcasecmp(pm, "soft"))
5727 return NAND_ECC_HAMMING;
5728 else if (!strcasecmp(pm, "soft_bch"))
5729 return NAND_ECC_BCH;
5730
5731 return -ENODEV;
5732}
5733
5734static int of_get_nand_ecc_step_size(struct device_node *np)
5735{
5736 int ret;
5737 u32 val;
5738
5739 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5740 return ret ? ret : val;
5741}
5742
5743static int of_get_nand_ecc_strength(struct device_node *np)
5744{
5745 int ret;
5746 u32 val;
5747
5748 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5749 return ret ? ret : val;
5750}
5751
5752static int of_get_nand_bus_width(struct device_node *np)
5753{
5754 u32 val;
5755
5756 if (of_property_read_u32(np, "nand-bus-width", &val))
5757 return 8;
5758
5759 switch (val) {
5760 case 8:
5761 case 16:
5762 return val;
5763 default:
5764 return -EIO;
5765 }
5766}
5767
5768static bool of_get_nand_on_flash_bbt(struct device_node *np)
5769{
5770 return of_property_read_bool(np, "nand-on-flash-bbt");
5771}
5772
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005773static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005774{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005775 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005776 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005777
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005778 if (!dn)
5779 return 0;
5780
Brian Norris5844fee2015-01-23 00:22:27 -08005781 if (of_get_nand_bus_width(dn) == 16)
5782 chip->options |= NAND_BUSWIDTH_16;
5783
5784 if (of_get_nand_on_flash_bbt(dn))
5785 chip->bbt_options |= NAND_BBT_USE_FLASH;
5786
5787 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005788 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005789 ecc_strength = of_get_nand_ecc_strength(dn);
5790 ecc_step = of_get_nand_ecc_step_size(dn);
5791
Brian Norris5844fee2015-01-23 00:22:27 -08005792 if (ecc_mode >= 0)
5793 chip->ecc.mode = ecc_mode;
5794
Rafał Miłecki79082452016-03-23 11:19:02 +01005795 if (ecc_algo >= 0)
5796 chip->ecc.algo = ecc_algo;
5797
Brian Norris5844fee2015-01-23 00:22:27 -08005798 if (ecc_strength >= 0)
5799 chip->ecc.strength = ecc_strength;
5800
5801 if (ecc_step > 0)
5802 chip->ecc.size = ecc_step;
5803
Boris Brezillonba78ee02016-06-08 17:04:22 +02005804 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5805 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5806
Brian Norris5844fee2015-01-23 00:22:27 -08005807 return 0;
5808}
5809
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005810/**
David Woodhouse3b85c322006-09-25 17:06:53 +01005811 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005812 * @mtd: MTD device structure
5813 * @maxchips: number of chips to scan for
5814 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005815 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005816 * This is the first phase of the normal nand_scan() function. It reads the
5817 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005818 *
5819 */
David Woodhouse5e81e882010-02-26 18:32:56 +00005820int nand_scan_ident(struct mtd_info *mtd, int maxchips,
5821 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005822{
Cai Zhiyongbb770822013-12-25 20:11:15 +08005823 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01005824 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08005825 int ret;
5826
Miquel Raynal17fa8042017-11-30 18:01:31 +01005827 /* Enforce the right timings for reset/detection */
5828 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5829
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005830 ret = nand_dt_init(chip);
5831 if (ret)
5832 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005833
Brian Norrisf7a8e382016-01-05 10:39:45 -08005834 if (!mtd->name && mtd->dev.parent)
5835 mtd->name = dev_name(mtd->dev.parent);
5836
Miquel Raynal8878b122017-11-09 14:16:45 +01005837 /*
5838 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5839 * populated.
5840 */
5841 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005842 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005843 * Default functions assigned for ->cmdfunc() and
5844 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005845 */
Miquel Raynal8878b122017-11-09 14:16:45 +01005846 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5847 pr_err("->cmd_ctrl() should be provided\n");
5848 return -EINVAL;
5849 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005850 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005851
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005852 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005853 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005854
5855 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005856 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005857 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005858 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005859 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005860 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005861 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 }
5863
Boris Brezillon7f501f02016-05-24 19:20:05 +02005864 nand_maf_id = chip->id.data[0];
5865 nand_dev_id = chip->id.data[1];
5866
Huang Shijie07300162012-11-09 16:23:45 +08005867 chip->select_chip(mtd, -1);
5868
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005869 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005870 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005871 u8 id[2];
5872
Karl Beldanef89a882008-09-15 14:37:29 +02005873 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005874 nand_reset(chip, i);
5875
5876 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005878 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005880 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08005881 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 break;
Huang Shijie07300162012-11-09 16:23:45 +08005883 }
5884 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 }
5886 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005887 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005888
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005890 chip->numchips = i;
5891 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
David Woodhouse3b85c322006-09-25 17:06:53 +01005893 return 0;
5894}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005895EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01005896
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005897static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
5898{
5899 struct nand_chip *chip = mtd_to_nand(mtd);
5900 struct nand_ecc_ctrl *ecc = &chip->ecc;
5901
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005902 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005903 return -EINVAL;
5904
5905 switch (ecc->algo) {
5906 case NAND_ECC_HAMMING:
5907 ecc->calculate = nand_calculate_ecc;
5908 ecc->correct = nand_correct_data;
5909 ecc->read_page = nand_read_page_swecc;
5910 ecc->read_subpage = nand_read_subpage;
5911 ecc->write_page = nand_write_page_swecc;
5912 ecc->read_page_raw = nand_read_page_raw;
5913 ecc->write_page_raw = nand_write_page_raw;
5914 ecc->read_oob = nand_read_oob_std;
5915 ecc->write_oob = nand_write_oob_std;
5916 if (!ecc->size)
5917 ecc->size = 256;
5918 ecc->bytes = 3;
5919 ecc->strength = 1;
5920 return 0;
5921 case NAND_ECC_BCH:
5922 if (!mtd_nand_has_bch()) {
5923 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5924 return -EINVAL;
5925 }
5926 ecc->calculate = nand_bch_calculate_ecc;
5927 ecc->correct = nand_bch_correct_data;
5928 ecc->read_page = nand_read_page_swecc;
5929 ecc->read_subpage = nand_read_subpage;
5930 ecc->write_page = nand_write_page_swecc;
5931 ecc->read_page_raw = nand_read_page_raw;
5932 ecc->write_page_raw = nand_write_page_raw;
5933 ecc->read_oob = nand_read_oob_std;
5934 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005935
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005936 /*
5937 * Board driver should supply ecc.size and ecc.strength
5938 * values to select how many bits are correctable.
5939 * Otherwise, default to 4 bits for large page devices.
5940 */
5941 if (!ecc->size && (mtd->oobsize >= 64)) {
5942 ecc->size = 512;
5943 ecc->strength = 4;
5944 }
5945
5946 /*
5947 * if no ecc placement scheme was provided pickup the default
5948 * large page one.
5949 */
5950 if (!mtd->ooblayout) {
5951 /* handle large page devices only */
5952 if (mtd->oobsize < 64) {
5953 WARN(1, "OOB layout is required when using software BCH on small pages\n");
5954 return -EINVAL;
5955 }
5956
5957 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02005958
5959 }
5960
5961 /*
5962 * We can only maximize ECC config when the default layout is
5963 * used, otherwise we don't know how many bytes can really be
5964 * used.
5965 */
5966 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
5967 ecc->options & NAND_ECC_MAXIMIZE) {
5968 int steps, bytes;
5969
5970 /* Always prefer 1k blocks over 512bytes ones */
5971 ecc->size = 1024;
5972 steps = mtd->writesize / ecc->size;
5973
5974 /* Reserve 2 bytes for the BBM */
5975 bytes = (mtd->oobsize - 2) / steps;
5976 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005977 }
5978
5979 /* See nand_bch_init() for details. */
5980 ecc->bytes = 0;
5981 ecc->priv = nand_bch_init(mtd);
5982 if (!ecc->priv) {
5983 WARN(1, "BCH ECC initialization failed!\n");
5984 return -EINVAL;
5985 }
5986 return 0;
5987 default:
5988 WARN(1, "Unsupported ECC algorithm!\n");
5989 return -EINVAL;
5990 }
5991}
5992
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09005993/**
5994 * nand_check_ecc_caps - check the sanity of preset ECC settings
5995 * @chip: nand chip info structure
5996 * @caps: ECC caps info structure
5997 * @oobavail: OOB size that the ECC engine can use
5998 *
5999 * When ECC step size and strength are already set, check if they are supported
6000 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6001 * On success, the calculated ECC bytes is set.
6002 */
6003int nand_check_ecc_caps(struct nand_chip *chip,
6004 const struct nand_ecc_caps *caps, int oobavail)
6005{
6006 struct mtd_info *mtd = nand_to_mtd(chip);
6007 const struct nand_ecc_step_info *stepinfo;
6008 int preset_step = chip->ecc.size;
6009 int preset_strength = chip->ecc.strength;
6010 int nsteps, ecc_bytes;
6011 int i, j;
6012
6013 if (WARN_ON(oobavail < 0))
6014 return -EINVAL;
6015
6016 if (!preset_step || !preset_strength)
6017 return -ENODATA;
6018
6019 nsteps = mtd->writesize / preset_step;
6020
6021 for (i = 0; i < caps->nstepinfos; i++) {
6022 stepinfo = &caps->stepinfos[i];
6023
6024 if (stepinfo->stepsize != preset_step)
6025 continue;
6026
6027 for (j = 0; j < stepinfo->nstrengths; j++) {
6028 if (stepinfo->strengths[j] != preset_strength)
6029 continue;
6030
6031 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6032 preset_strength);
6033 if (WARN_ON_ONCE(ecc_bytes < 0))
6034 return ecc_bytes;
6035
6036 if (ecc_bytes * nsteps > oobavail) {
6037 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6038 preset_step, preset_strength);
6039 return -ENOSPC;
6040 }
6041
6042 chip->ecc.bytes = ecc_bytes;
6043
6044 return 0;
6045 }
6046 }
6047
6048 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6049 preset_step, preset_strength);
6050
6051 return -ENOTSUPP;
6052}
6053EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
6054
6055/**
6056 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6057 * @chip: nand chip info structure
6058 * @caps: ECC engine caps info structure
6059 * @oobavail: OOB size that the ECC engine can use
6060 *
6061 * If a chip's ECC requirement is provided, try to meet it with the least
6062 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6063 * On success, the chosen ECC settings are set.
6064 */
6065int nand_match_ecc_req(struct nand_chip *chip,
6066 const struct nand_ecc_caps *caps, int oobavail)
6067{
6068 struct mtd_info *mtd = nand_to_mtd(chip);
6069 const struct nand_ecc_step_info *stepinfo;
6070 int req_step = chip->ecc_step_ds;
6071 int req_strength = chip->ecc_strength_ds;
6072 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6073 int best_step, best_strength, best_ecc_bytes;
6074 int best_ecc_bytes_total = INT_MAX;
6075 int i, j;
6076
6077 if (WARN_ON(oobavail < 0))
6078 return -EINVAL;
6079
6080 /* No information provided by the NAND chip */
6081 if (!req_step || !req_strength)
6082 return -ENOTSUPP;
6083
6084 /* number of correctable bits the chip requires in a page */
6085 req_corr = mtd->writesize / req_step * req_strength;
6086
6087 for (i = 0; i < caps->nstepinfos; i++) {
6088 stepinfo = &caps->stepinfos[i];
6089 step_size = stepinfo->stepsize;
6090
6091 for (j = 0; j < stepinfo->nstrengths; j++) {
6092 strength = stepinfo->strengths[j];
6093
6094 /*
6095 * If both step size and strength are smaller than the
6096 * chip's requirement, it is not easy to compare the
6097 * resulted reliability.
6098 */
6099 if (step_size < req_step && strength < req_strength)
6100 continue;
6101
6102 if (mtd->writesize % step_size)
6103 continue;
6104
6105 nsteps = mtd->writesize / step_size;
6106
6107 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6108 if (WARN_ON_ONCE(ecc_bytes < 0))
6109 continue;
6110 ecc_bytes_total = ecc_bytes * nsteps;
6111
6112 if (ecc_bytes_total > oobavail ||
6113 strength * nsteps < req_corr)
6114 continue;
6115
6116 /*
6117 * We assume the best is to meet the chip's requrement
6118 * with the least number of ECC bytes.
6119 */
6120 if (ecc_bytes_total < best_ecc_bytes_total) {
6121 best_ecc_bytes_total = ecc_bytes_total;
6122 best_step = step_size;
6123 best_strength = strength;
6124 best_ecc_bytes = ecc_bytes;
6125 }
6126 }
6127 }
6128
6129 if (best_ecc_bytes_total == INT_MAX)
6130 return -ENOTSUPP;
6131
6132 chip->ecc.size = best_step;
6133 chip->ecc.strength = best_strength;
6134 chip->ecc.bytes = best_ecc_bytes;
6135
6136 return 0;
6137}
6138EXPORT_SYMBOL_GPL(nand_match_ecc_req);
6139
6140/**
6141 * nand_maximize_ecc - choose the max ECC strength available
6142 * @chip: nand chip info structure
6143 * @caps: ECC engine caps info structure
6144 * @oobavail: OOB size that the ECC engine can use
6145 *
6146 * Choose the max ECC strength that is supported on the controller, and can fit
6147 * within the chip's OOB. On success, the chosen ECC settings are set.
6148 */
6149int nand_maximize_ecc(struct nand_chip *chip,
6150 const struct nand_ecc_caps *caps, int oobavail)
6151{
6152 struct mtd_info *mtd = nand_to_mtd(chip);
6153 const struct nand_ecc_step_info *stepinfo;
6154 int step_size, strength, nsteps, ecc_bytes, corr;
6155 int best_corr = 0;
6156 int best_step = 0;
6157 int best_strength, best_ecc_bytes;
6158 int i, j;
6159
6160 if (WARN_ON(oobavail < 0))
6161 return -EINVAL;
6162
6163 for (i = 0; i < caps->nstepinfos; i++) {
6164 stepinfo = &caps->stepinfos[i];
6165 step_size = stepinfo->stepsize;
6166
6167 /* If chip->ecc.size is already set, respect it */
6168 if (chip->ecc.size && step_size != chip->ecc.size)
6169 continue;
6170
6171 for (j = 0; j < stepinfo->nstrengths; j++) {
6172 strength = stepinfo->strengths[j];
6173
6174 if (mtd->writesize % step_size)
6175 continue;
6176
6177 nsteps = mtd->writesize / step_size;
6178
6179 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6180 if (WARN_ON_ONCE(ecc_bytes < 0))
6181 continue;
6182
6183 if (ecc_bytes * nsteps > oobavail)
6184 continue;
6185
6186 corr = strength * nsteps;
6187
6188 /*
6189 * If the number of correctable bits is the same,
6190 * bigger step_size has more reliability.
6191 */
6192 if (corr > best_corr ||
6193 (corr == best_corr && step_size > best_step)) {
6194 best_corr = corr;
6195 best_step = step_size;
6196 best_strength = strength;
6197 best_ecc_bytes = ecc_bytes;
6198 }
6199 }
6200 }
6201
6202 if (!best_corr)
6203 return -ENOTSUPP;
6204
6205 chip->ecc.size = best_step;
6206 chip->ecc.strength = best_strength;
6207 chip->ecc.bytes = best_ecc_bytes;
6208
6209 return 0;
6210}
6211EXPORT_SYMBOL_GPL(nand_maximize_ecc);
6212
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006213/*
6214 * Check if the chip configuration meet the datasheet requirements.
6215
6216 * If our configuration corrects A bits per B bytes and the minimum
6217 * required correction level is X bits per Y bytes, then we must ensure
6218 * both of the following are true:
6219 *
6220 * (1) A / B >= X / Y
6221 * (2) A >= X
6222 *
6223 * Requirement (1) ensures we can correct for the required bitflip density.
6224 * Requirement (2) ensures we can correct even when all bitflips are clumped
6225 * in the same sector.
6226 */
6227static bool nand_ecc_strength_good(struct mtd_info *mtd)
6228{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006229 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006230 struct nand_ecc_ctrl *ecc = &chip->ecc;
6231 int corr, ds_corr;
6232
6233 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6234 /* Not enough information */
6235 return true;
6236
6237 /*
6238 * We get the number of corrected bits per page to compare
6239 * the correction density.
6240 */
6241 corr = (mtd->writesize * ecc->strength) / ecc->size;
6242 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6243
6244 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6245}
David Woodhouse3b85c322006-09-25 17:06:53 +01006246
6247/**
6248 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006249 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01006250 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006251 * This is the second phase of the normal nand_scan() function. It fills out
6252 * all the uninitialized function pointers with the defaults and scans for a
6253 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006254 */
6255int nand_scan_tail(struct mtd_info *mtd)
6256{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006257 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08006258 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006259 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006260
Brian Norrise2414f42012-02-06 13:44:00 -08006261 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006262 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006263 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006264 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006265 }
Brian Norrise2414f42012-02-06 13:44:00 -08006266
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006267 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006268 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006269 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006270
Boris Brezillonf84674b2017-06-02 12:18:24 +02006271 /*
6272 * FIXME: some NAND manufacturer drivers expect the first die to be
6273 * selected when manufacturer->init() is called. They should be fixed
6274 * to explictly select the relevant die when interacting with the NAND
6275 * chip.
6276 */
6277 chip->select_chip(mtd, 0);
6278 ret = nand_manufacturer_init(chip);
6279 chip->select_chip(mtd, -1);
6280 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006281 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006282
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006283 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006284 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006285
6286 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006287 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006288 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006289 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006290 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006291 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006294 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 break;
6296 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006297 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006298 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006301 /*
6302 * Expose the whole OOB area to users if ECC_NONE
6303 * is passed. We could do that for all kind of
6304 * ->oobsize, but we must keep the old large/small
6305 * page with ECC layout when ->oobsize <= 128 for
6306 * compatibility reasons.
6307 */
6308 if (ecc->mode == NAND_ECC_NONE) {
6309 mtd_set_ooblayout(mtd,
6310 &nand_ooblayout_lp_ops);
6311 break;
6312 }
6313
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006314 WARN(1, "No oob scheme defined for oobsize %d\n",
6315 mtd->oobsize);
6316 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006317 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 }
6319 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006320
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006321 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006322 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006323 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006324 */
David Woodhouse956e9442006-09-25 17:12:39 +01006325
Huang Shijie97de79e02013-10-18 14:20:53 +08006326 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006327 case NAND_ECC_HW_OOB_FIRST:
6328 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006329 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006330 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6331 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006332 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006333 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006334 if (!ecc->read_page)
6335 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006336
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006337 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006338 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006339 if (!ecc->read_page)
6340 ecc->read_page = nand_read_page_hwecc;
6341 if (!ecc->write_page)
6342 ecc->write_page = nand_write_page_hwecc;
6343 if (!ecc->read_page_raw)
6344 ecc->read_page_raw = nand_read_page_raw;
6345 if (!ecc->write_page_raw)
6346 ecc->write_page_raw = nand_write_page_raw;
6347 if (!ecc->read_oob)
6348 ecc->read_oob = nand_read_oob_std;
6349 if (!ecc->write_oob)
6350 ecc->write_oob = nand_write_oob_std;
6351 if (!ecc->read_subpage)
6352 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006353 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006354 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006355
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006356 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006357 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6358 (!ecc->read_page ||
6359 ecc->read_page == nand_read_page_hwecc ||
6360 !ecc->write_page ||
6361 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006362 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6363 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006364 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006365 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006366 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006367 if (!ecc->read_page)
6368 ecc->read_page = nand_read_page_syndrome;
6369 if (!ecc->write_page)
6370 ecc->write_page = nand_write_page_syndrome;
6371 if (!ecc->read_page_raw)
6372 ecc->read_page_raw = nand_read_page_raw_syndrome;
6373 if (!ecc->write_page_raw)
6374 ecc->write_page_raw = nand_write_page_raw_syndrome;
6375 if (!ecc->read_oob)
6376 ecc->read_oob = nand_read_oob_syndrome;
6377 if (!ecc->write_oob)
6378 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006379
Huang Shijie97de79e02013-10-18 14:20:53 +08006380 if (mtd->writesize >= ecc->size) {
6381 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006382 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6383 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006384 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006385 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006386 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006387 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006388 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6389 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006390 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006391 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006393 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006394 ret = nand_set_ecc_soft_ops(mtd);
6395 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006396 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006397 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006398 }
6399 break;
6400
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006401 case NAND_ECC_ON_DIE:
6402 if (!ecc->read_page || !ecc->write_page) {
6403 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6404 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006405 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006406 }
6407 if (!ecc->read_oob)
6408 ecc->read_oob = nand_read_oob_std;
6409 if (!ecc->write_oob)
6410 ecc->write_oob = nand_write_oob_std;
6411 break;
6412
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006413 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006414 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006415 ecc->read_page = nand_read_page_raw;
6416 ecc->write_page = nand_write_page_raw;
6417 ecc->read_oob = nand_read_oob_std;
6418 ecc->read_page_raw = nand_read_page_raw;
6419 ecc->write_page_raw = nand_write_page_raw;
6420 ecc->write_oob = nand_write_oob_std;
6421 ecc->size = mtd->writesize;
6422 ecc->bytes = 0;
6423 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006425
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006427 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6428 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006429 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006432 if (ecc->correct || ecc->calculate) {
6433 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6434 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6435 if (!ecc->calc_buf || !ecc->code_buf) {
6436 ret = -ENOMEM;
6437 goto err_nand_manuf_cleanup;
6438 }
6439 }
6440
Brian Norris9ce244b2011-08-30 18:45:37 -07006441 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006442 if (!ecc->read_oob_raw)
6443 ecc->read_oob_raw = ecc->read_oob;
6444 if (!ecc->write_oob_raw)
6445 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006446
Boris Brezillon846031d2016-02-03 20:11:00 +01006447 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006448 mtd->ecc_strength = ecc->strength;
6449 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006450
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006451 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006452 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006453 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006454 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006455 ecc->steps = mtd->writesize / ecc->size;
6456 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006457 WARN(1, "Invalid ECC parameters\n");
6458 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006459 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006461 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006462 if (ecc->total > mtd->oobsize) {
6463 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6464 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006465 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006466 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006467
Boris Brezillon846031d2016-02-03 20:11:00 +01006468 /*
6469 * The number of bytes available for a client to place data into
6470 * the out of band area.
6471 */
6472 ret = mtd_ooblayout_count_freebytes(mtd);
6473 if (ret < 0)
6474 ret = 0;
6475
6476 mtd->oobavail = ret;
6477
6478 /* ECC sanity check: warn if it's too weak */
6479 if (!nand_ecc_strength_good(mtd))
6480 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6481 mtd->name);
6482
Brian Norris8b6e50c2011-05-25 14:59:01 -07006483 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006484 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006485 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006486 case 2:
6487 mtd->subpage_sft = 1;
6488 break;
6489 case 4:
6490 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006491 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006492 mtd->subpage_sft = 2;
6493 break;
6494 }
6495 }
6496 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6497
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006498 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006499 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006502 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006504 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306505 switch (ecc->mode) {
6506 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306507 if (chip->page_shift > 9)
6508 chip->options |= NAND_SUBPAGE_READ;
6509 break;
6510
6511 default:
6512 break;
6513 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006514
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006516 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006517 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6518 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006519 mtd->_erase = nand_erase;
6520 mtd->_point = NULL;
6521 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006522 mtd->_panic_write = panic_nand_write;
6523 mtd->_read_oob = nand_read_oob;
6524 mtd->_write_oob = nand_write_oob;
6525 mtd->_sync = nand_sync;
6526 mtd->_lock = NULL;
6527 mtd->_unlock = NULL;
6528 mtd->_suspend = nand_suspend;
6529 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006530 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006531 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006532 mtd->_block_isbad = nand_block_isbad;
6533 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006534 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006535 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006537 /*
6538 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6539 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6540 * properly set.
6541 */
6542 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006543 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544
Boris Brezillonf84674b2017-06-02 12:18:24 +02006545 /* Initialize the ->data_interface field. */
6546 ret = nand_init_data_interface(chip);
6547 if (ret)
6548 goto err_nand_manuf_cleanup;
6549
6550 /* Enter fastest possible mode on all dies. */
6551 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006552 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006553 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006554 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006555 }
6556
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006557 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006558 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560
6561 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07006562 ret = chip->scan_bbt(mtd);
6563 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006564 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006565
Brian Norris44d41822017-05-01 17:04:50 -07006566 return 0;
6567
Boris Brezillonf84674b2017-06-02 12:18:24 +02006568
6569err_nand_manuf_cleanup:
6570 nand_manufacturer_cleanup(chip);
6571
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006572err_free_buf:
6573 kfree(chip->data_buf);
6574 kfree(ecc->code_buf);
6575 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006576
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006579EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
Brian Norris8b6e50c2011-05-25 14:59:01 -07006581/*
6582 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006583 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07006584 * to call us from in-kernel code if the core NAND support is modular.
6585 */
David Woodhouse3b85c322006-09-25 17:06:53 +01006586#ifdef MODULE
6587#define caller_is_module() (1)
6588#else
6589#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06006590 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01006591#endif
6592
6593/**
6594 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006595 * @mtd: MTD device structure
6596 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01006597 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006598 * This fills out all the uninitialized function pointers with the defaults.
6599 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006600 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006601 */
6602int nand_scan(struct mtd_info *mtd, int maxchips)
6603{
6604 int ret;
6605
David Woodhouse5e81e882010-02-26 18:32:56 +00006606 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01006607 if (!ret)
6608 ret = nand_scan_tail(mtd);
6609 return ret;
6610}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006611EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01006612
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006614 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6615 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006616 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006617void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006619 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006620 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006621 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6622
Jesper Juhlfa671642005-11-07 01:01:27 -08006623 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006624 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006625 kfree(chip->data_buf);
6626 kfree(chip->ecc.code_buf);
6627 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006628
6629 /* Free bad block descriptor memory */
6630 if (chip->badblock_pattern && chip->badblock_pattern->options
6631 & NAND_BBT_DYNAMICSTRUCT)
6632 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006633
6634 /* Free manufacturer priv data. */
6635 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006637EXPORT_SYMBOL_GPL(nand_cleanup);
6638
6639/**
6640 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6641 * held by the NAND device
6642 * @mtd: MTD device structure
6643 */
6644void nand_release(struct mtd_info *mtd)
6645{
6646 mtd_device_unregister(mtd);
6647 nand_cleanup(mtd_to_nand(mtd));
6648}
David Woodhousee0c7d762006-05-13 18:07:53 +01006649EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006650
David Woodhousee0c7d762006-05-13 18:07:53 +01006651MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006652MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6653MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006654MODULE_DESCRIPTION("Generic NAND flash driver code");