blob: d0b993fcf3a522bdc0713cd39a65a24551c6efc6 [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 Raynal789157e2018-03-19 14:47:28 +01001163static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +01001164{
Miquel Raynal789157e2018-03-19 14:47:28 +01001165 return (chip->parameters.supports_set_get_features &&
1166 test_bit(addr, chip->parameters.get_feature_list));
1167}
1168
1169static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1170{
1171 return (chip->parameters.supports_set_get_features &&
1172 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +01001173}
1174
1175/**
1176 * nand_get_features - wrapper to perform a GET_FEATURE
1177 * @chip: NAND chip info structure
1178 * @addr: feature address
1179 * @subfeature_param: the subfeature parameters, a four bytes array
1180 *
1181 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1182 * operation cannot be handled.
1183 */
1184int nand_get_features(struct nand_chip *chip, int addr,
1185 u8 *subfeature_param)
1186{
1187 struct mtd_info *mtd = nand_to_mtd(chip);
1188
Miquel Raynal789157e2018-03-19 14:47:28 +01001189 if (!nand_supports_get_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001190 return -ENOTSUPP;
1191
1192 return chip->get_features(mtd, chip, addr, subfeature_param);
1193}
1194EXPORT_SYMBOL_GPL(nand_get_features);
1195
1196/**
1197 * nand_set_features - wrapper to perform a SET_FEATURE
1198 * @chip: NAND chip info structure
1199 * @addr: feature address
1200 * @subfeature_param: the subfeature parameters, a four bytes array
1201 *
1202 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1203 * operation cannot be handled.
1204 */
1205int nand_set_features(struct nand_chip *chip, int addr,
1206 u8 *subfeature_param)
1207{
1208 struct mtd_info *mtd = nand_to_mtd(chip);
1209
Miquel Raynal789157e2018-03-19 14:47:28 +01001210 if (!nand_supports_set_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001211 return -ENOTSUPP;
1212
1213 return chip->set_features(mtd, chip, addr, subfeature_param);
1214}
1215EXPORT_SYMBOL_GPL(nand_set_features);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001218 * nand_reset_data_interface - Reset data interface and timings
1219 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001220 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001221 *
1222 * Reset the Data interface and timings to ONFI mode 0.
1223 *
1224 * Returns 0 for success or negative error code otherwise.
1225 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001226static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001227{
1228 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001229 int ret;
1230
1231 if (!chip->setup_data_interface)
1232 return 0;
1233
1234 /*
1235 * The ONFI specification says:
1236 * "
1237 * To transition from NV-DDR or NV-DDR2 to the SDR data
1238 * interface, the host shall use the Reset (FFh) command
1239 * using SDR timing mode 0. A device in any timing mode is
1240 * required to recognize Reset (FFh) command issued in SDR
1241 * timing mode 0.
1242 * "
1243 *
1244 * Configure the data interface in SDR mode and set the
1245 * timings to timing mode 0.
1246 */
1247
Miquel Raynal17fa8042017-11-30 18:01:31 +01001248 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1249 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001250 if (ret)
1251 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1252
1253 return ret;
1254}
1255
1256/**
1257 * nand_setup_data_interface - Setup the best data interface and timings
1258 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001259 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001260 *
1261 * Find and configure the best data interface and NAND timings supported by
1262 * the chip and the driver.
1263 * First tries to retrieve supported timing modes from ONFI information,
1264 * and if the NAND chip does not support ONFI, relies on the
1265 * ->onfi_timing_mode_default specified in the nand_ids table.
1266 *
1267 * Returns 0 for success or negative error code otherwise.
1268 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001269static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001270{
1271 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal97baea12018-03-19 14:47:20 +01001272 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1273 chip->onfi_timing_mode_default,
1274 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001275 int ret;
1276
Miquel Raynal17fa8042017-11-30 18:01:31 +01001277 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001278 return 0;
1279
Miquel Raynal993447b2018-03-19 14:47:21 +01001280 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +01001281 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Miquel Raynal29714d62018-03-19 14:47:23 +01001282 chip->select_chip(mtd, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001283 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1284 tmode_param);
Miquel Raynal29714d62018-03-19 14:47:23 +01001285 chip->select_chip(mtd, -1);
Miquel Raynal993447b2018-03-19 14:47:21 +01001286 if (ret)
1287 return ret;
1288 }
Boris Brezillond8e725d2016-09-15 10:32:50 +02001289
Miquel Raynal97baea12018-03-19 14:47:20 +01001290 /* Change the mode on the controller side */
Miquel Raynal415ae782018-03-19 14:47:24 +01001291 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
1292 if (ret)
1293 return ret;
1294
1295 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +01001296 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +01001297 return 0;
1298
1299 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
1300 chip->select_chip(mtd, chipnr);
1301 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1302 tmode_param);
1303 chip->select_chip(mtd, -1);
1304 if (ret)
1305 goto err_reset_chip;
1306
1307 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1308 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1309 chip->onfi_timing_mode_default);
1310 goto err_reset_chip;
1311 }
1312
1313 return 0;
1314
1315err_reset_chip:
1316 /*
1317 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1318 * timing mode.
1319 */
1320 nand_reset_data_interface(chip, chipnr);
1321 chip->select_chip(mtd, chipnr);
1322 nand_reset_op(chip);
1323 chip->select_chip(mtd, -1);
1324
1325 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001326}
1327
1328/**
1329 * nand_init_data_interface - find the best data interface and timings
1330 * @chip: The NAND chip
1331 *
1332 * Find the best data interface and NAND timings supported by the chip
1333 * and the driver.
1334 * First tries to retrieve supported timing modes from ONFI information,
1335 * and if the NAND chip does not support ONFI, relies on the
1336 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1337 * function nand_chip->data_interface is initialized with the best timing mode
1338 * available.
1339 *
1340 * Returns 0 for success or negative error code otherwise.
1341 */
1342static int nand_init_data_interface(struct nand_chip *chip)
1343{
1344 struct mtd_info *mtd = nand_to_mtd(chip);
1345 int modes, mode, ret;
1346
1347 if (!chip->setup_data_interface)
1348 return 0;
1349
1350 /*
1351 * First try to identify the best timings from ONFI parameters and
1352 * if the NAND does not support ONFI, fallback to the default ONFI
1353 * timing mode.
1354 */
1355 modes = onfi_get_async_timing_mode(chip);
1356 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1357 if (!chip->onfi_timing_mode_default)
1358 return 0;
1359
1360 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1361 }
1362
Boris Brezillond8e725d2016-09-15 10:32:50 +02001363
1364 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001365 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001366 if (ret)
1367 continue;
1368
Miquel Raynald787b8b2017-12-22 18:12:41 +01001369 /*
1370 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1371 * controller supports the requested timings.
1372 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001373 ret = chip->setup_data_interface(mtd,
1374 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001375 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001376 if (!ret) {
1377 chip->onfi_timing_mode_default = mode;
1378 break;
1379 }
1380 }
1381
1382 return 0;
1383}
1384
Boris Brezillond8e725d2016-09-15 10:32:50 +02001385/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001386 * nand_fill_column_cycles - fill the column cycles of an address
1387 * @chip: The NAND chip
1388 * @addrs: Array of address cycles to fill
1389 * @offset_in_page: The offset in the page
1390 *
1391 * Fills the first or the first two bytes of the @addrs field depending
1392 * on the NAND bus width and the page size.
1393 *
1394 * Returns the number of cycles needed to encode the column, or a negative
1395 * error code in case one of the arguments is invalid.
1396 */
1397static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1398 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Miquel Raynal8878b122017-11-09 14:16:45 +01001400 struct mtd_info *mtd = nand_to_mtd(chip);
1401
1402 /* Make sure the offset is less than the actual page size. */
1403 if (offset_in_page > mtd->writesize + mtd->oobsize)
1404 return -EINVAL;
1405
1406 /*
1407 * On small page NANDs, there's a dedicated command to access the OOB
1408 * area, and the column address is relative to the start of the OOB
1409 * area, not the start of the page. Asjust the address accordingly.
1410 */
1411 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1412 offset_in_page -= mtd->writesize;
1413
1414 /*
1415 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1416 * wide, then it must be divided by 2.
1417 */
1418 if (chip->options & NAND_BUSWIDTH_16) {
1419 if (WARN_ON(offset_in_page % 2))
1420 return -EINVAL;
1421
1422 offset_in_page /= 2;
1423 }
1424
1425 addrs[0] = offset_in_page;
1426
1427 /*
1428 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1429 * need 2
1430 */
1431 if (mtd->writesize <= 512)
1432 return 1;
1433
1434 addrs[1] = offset_in_page >> 8;
1435
1436 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
Miquel Raynal8878b122017-11-09 14:16:45 +01001439static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1440 unsigned int offset_in_page, void *buf,
1441 unsigned int len)
1442{
1443 struct mtd_info *mtd = nand_to_mtd(chip);
1444 const struct nand_sdr_timings *sdr =
1445 nand_get_sdr_timings(&chip->data_interface);
1446 u8 addrs[4];
1447 struct nand_op_instr instrs[] = {
1448 NAND_OP_CMD(NAND_CMD_READ0, 0),
1449 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1450 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1451 PSEC_TO_NSEC(sdr->tRR_min)),
1452 NAND_OP_DATA_IN(len, buf, 0),
1453 };
1454 struct nand_operation op = NAND_OPERATION(instrs);
1455 int ret;
1456
1457 /* Drop the DATA_IN instruction if len is set to 0. */
1458 if (!len)
1459 op.ninstrs--;
1460
1461 if (offset_in_page >= mtd->writesize)
1462 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1463 else if (offset_in_page >= 256 &&
1464 !(chip->options & NAND_BUSWIDTH_16))
1465 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1466
1467 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1468 if (ret < 0)
1469 return ret;
1470
1471 addrs[1] = page;
1472 addrs[2] = page >> 8;
1473
1474 if (chip->options & NAND_ROW_ADDR_3) {
1475 addrs[3] = page >> 16;
1476 instrs[1].ctx.addr.naddrs++;
1477 }
1478
1479 return nand_exec_op(chip, &op);
1480}
1481
1482static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1483 unsigned int offset_in_page, void *buf,
1484 unsigned int len)
1485{
1486 const struct nand_sdr_timings *sdr =
1487 nand_get_sdr_timings(&chip->data_interface);
1488 u8 addrs[5];
1489 struct nand_op_instr instrs[] = {
1490 NAND_OP_CMD(NAND_CMD_READ0, 0),
1491 NAND_OP_ADDR(4, addrs, 0),
1492 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1493 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1494 PSEC_TO_NSEC(sdr->tRR_min)),
1495 NAND_OP_DATA_IN(len, buf, 0),
1496 };
1497 struct nand_operation op = NAND_OPERATION(instrs);
1498 int ret;
1499
1500 /* Drop the DATA_IN instruction if len is set to 0. */
1501 if (!len)
1502 op.ninstrs--;
1503
1504 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1505 if (ret < 0)
1506 return ret;
1507
1508 addrs[2] = page;
1509 addrs[3] = page >> 8;
1510
1511 if (chip->options & NAND_ROW_ADDR_3) {
1512 addrs[4] = page >> 16;
1513 instrs[1].ctx.addr.naddrs++;
1514 }
1515
1516 return nand_exec_op(chip, &op);
1517}
1518
1519/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001520 * nand_read_page_op - Do a READ PAGE operation
1521 * @chip: The NAND chip
1522 * @page: page to read
1523 * @offset_in_page: offset within the page
1524 * @buf: buffer used to store the data
1525 * @len: length of the buffer
1526 *
1527 * This function issues a READ PAGE operation.
1528 * This function does not select/unselect the CS line.
1529 *
1530 * Returns 0 on success, a negative error code otherwise.
1531 */
1532int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1533 unsigned int offset_in_page, void *buf, unsigned int len)
1534{
1535 struct mtd_info *mtd = nand_to_mtd(chip);
1536
1537 if (len && !buf)
1538 return -EINVAL;
1539
1540 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1541 return -EINVAL;
1542
Miquel Raynal8878b122017-11-09 14:16:45 +01001543 if (chip->exec_op) {
1544 if (mtd->writesize > 512)
1545 return nand_lp_exec_read_page_op(chip, page,
1546 offset_in_page, buf,
1547 len);
1548
1549 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1550 buf, len);
1551 }
1552
Boris Brezillon97d90da2017-11-30 18:01:29 +01001553 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1554 if (len)
1555 chip->read_buf(mtd, buf, len);
1556
1557 return 0;
1558}
1559EXPORT_SYMBOL_GPL(nand_read_page_op);
1560
1561/**
1562 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1563 * @chip: The NAND chip
1564 * @page: parameter page to read
1565 * @buf: buffer used to store the data
1566 * @len: length of the buffer
1567 *
1568 * This function issues a READ PARAMETER PAGE operation.
1569 * This function does not select/unselect the CS line.
1570 *
1571 * Returns 0 on success, a negative error code otherwise.
1572 */
1573static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1574 unsigned int len)
1575{
1576 struct mtd_info *mtd = nand_to_mtd(chip);
1577 unsigned int i;
1578 u8 *p = buf;
1579
1580 if (len && !buf)
1581 return -EINVAL;
1582
Miquel Raynal8878b122017-11-09 14:16:45 +01001583 if (chip->exec_op) {
1584 const struct nand_sdr_timings *sdr =
1585 nand_get_sdr_timings(&chip->data_interface);
1586 struct nand_op_instr instrs[] = {
1587 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1588 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1589 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1590 PSEC_TO_NSEC(sdr->tRR_min)),
1591 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1592 };
1593 struct nand_operation op = NAND_OPERATION(instrs);
1594
1595 /* Drop the DATA_IN instruction if len is set to 0. */
1596 if (!len)
1597 op.ninstrs--;
1598
1599 return nand_exec_op(chip, &op);
1600 }
1601
Boris Brezillon97d90da2017-11-30 18:01:29 +01001602 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1603 for (i = 0; i < len; i++)
1604 p[i] = chip->read_byte(mtd);
1605
1606 return 0;
1607}
1608
1609/**
1610 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1611 * @chip: The NAND chip
1612 * @offset_in_page: offset within the page
1613 * @buf: buffer used to store the data
1614 * @len: length of the buffer
1615 * @force_8bit: force 8-bit bus access
1616 *
1617 * This function issues a CHANGE READ COLUMN operation.
1618 * This function does not select/unselect the CS line.
1619 *
1620 * Returns 0 on success, a negative error code otherwise.
1621 */
1622int nand_change_read_column_op(struct nand_chip *chip,
1623 unsigned int offset_in_page, void *buf,
1624 unsigned int len, bool force_8bit)
1625{
1626 struct mtd_info *mtd = nand_to_mtd(chip);
1627
1628 if (len && !buf)
1629 return -EINVAL;
1630
1631 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1632 return -EINVAL;
1633
Miquel Raynal8878b122017-11-09 14:16:45 +01001634 /* Small page NANDs do not support column change. */
1635 if (mtd->writesize <= 512)
1636 return -ENOTSUPP;
1637
1638 if (chip->exec_op) {
1639 const struct nand_sdr_timings *sdr =
1640 nand_get_sdr_timings(&chip->data_interface);
1641 u8 addrs[2] = {};
1642 struct nand_op_instr instrs[] = {
1643 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1644 NAND_OP_ADDR(2, addrs, 0),
1645 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1646 PSEC_TO_NSEC(sdr->tCCS_min)),
1647 NAND_OP_DATA_IN(len, buf, 0),
1648 };
1649 struct nand_operation op = NAND_OPERATION(instrs);
1650 int ret;
1651
1652 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1653 if (ret < 0)
1654 return ret;
1655
1656 /* Drop the DATA_IN instruction if len is set to 0. */
1657 if (!len)
1658 op.ninstrs--;
1659
1660 instrs[3].ctx.data.force_8bit = force_8bit;
1661
1662 return nand_exec_op(chip, &op);
1663 }
1664
Boris Brezillon97d90da2017-11-30 18:01:29 +01001665 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1666 if (len)
1667 chip->read_buf(mtd, buf, len);
1668
1669 return 0;
1670}
1671EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1672
1673/**
1674 * nand_read_oob_op - Do a READ OOB operation
1675 * @chip: The NAND chip
1676 * @page: page to read
1677 * @offset_in_oob: offset within the OOB area
1678 * @buf: buffer used to store the data
1679 * @len: length of the buffer
1680 *
1681 * This function issues a READ OOB operation.
1682 * This function does not select/unselect the CS line.
1683 *
1684 * Returns 0 on success, a negative error code otherwise.
1685 */
1686int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1687 unsigned int offset_in_oob, void *buf, unsigned int len)
1688{
1689 struct mtd_info *mtd = nand_to_mtd(chip);
1690
1691 if (len && !buf)
1692 return -EINVAL;
1693
1694 if (offset_in_oob + len > mtd->oobsize)
1695 return -EINVAL;
1696
Miquel Raynal8878b122017-11-09 14:16:45 +01001697 if (chip->exec_op)
1698 return nand_read_page_op(chip, page,
1699 mtd->writesize + offset_in_oob,
1700 buf, len);
1701
Boris Brezillon97d90da2017-11-30 18:01:29 +01001702 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1703 if (len)
1704 chip->read_buf(mtd, buf, len);
1705
1706 return 0;
1707}
1708EXPORT_SYMBOL_GPL(nand_read_oob_op);
1709
Miquel Raynal8878b122017-11-09 14:16:45 +01001710static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1711 unsigned int offset_in_page, const void *buf,
1712 unsigned int len, bool prog)
1713{
1714 struct mtd_info *mtd = nand_to_mtd(chip);
1715 const struct nand_sdr_timings *sdr =
1716 nand_get_sdr_timings(&chip->data_interface);
1717 u8 addrs[5] = {};
1718 struct nand_op_instr instrs[] = {
1719 /*
1720 * The first instruction will be dropped if we're dealing
1721 * with a large page NAND and adjusted if we're dealing
1722 * with a small page NAND and the page offset is > 255.
1723 */
1724 NAND_OP_CMD(NAND_CMD_READ0, 0),
1725 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1726 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1727 NAND_OP_DATA_OUT(len, buf, 0),
1728 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1729 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1730 };
1731 struct nand_operation op = NAND_OPERATION(instrs);
1732 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1733 int ret;
1734 u8 status;
1735
1736 if (naddrs < 0)
1737 return naddrs;
1738
1739 addrs[naddrs++] = page;
1740 addrs[naddrs++] = page >> 8;
1741 if (chip->options & NAND_ROW_ADDR_3)
1742 addrs[naddrs++] = page >> 16;
1743
1744 instrs[2].ctx.addr.naddrs = naddrs;
1745
1746 /* Drop the last two instructions if we're not programming the page. */
1747 if (!prog) {
1748 op.ninstrs -= 2;
1749 /* Also drop the DATA_OUT instruction if empty. */
1750 if (!len)
1751 op.ninstrs--;
1752 }
1753
1754 if (mtd->writesize <= 512) {
1755 /*
1756 * Small pages need some more tweaking: we have to adjust the
1757 * first instruction depending on the page offset we're trying
1758 * to access.
1759 */
1760 if (offset_in_page >= mtd->writesize)
1761 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1762 else if (offset_in_page >= 256 &&
1763 !(chip->options & NAND_BUSWIDTH_16))
1764 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1765 } else {
1766 /*
1767 * Drop the first command if we're dealing with a large page
1768 * NAND.
1769 */
1770 op.instrs++;
1771 op.ninstrs--;
1772 }
1773
1774 ret = nand_exec_op(chip, &op);
1775 if (!prog || ret)
1776 return ret;
1777
1778 ret = nand_status_op(chip, &status);
1779 if (ret)
1780 return ret;
1781
1782 return status;
1783}
1784
Boris Brezillon97d90da2017-11-30 18:01:29 +01001785/**
1786 * nand_prog_page_begin_op - starts a PROG PAGE operation
1787 * @chip: The NAND chip
1788 * @page: page to write
1789 * @offset_in_page: offset within the page
1790 * @buf: buffer containing the data to write to the page
1791 * @len: length of the buffer
1792 *
1793 * This function issues the first half of a PROG PAGE operation.
1794 * This function does not select/unselect the CS line.
1795 *
1796 * Returns 0 on success, a negative error code otherwise.
1797 */
1798int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1799 unsigned int offset_in_page, const void *buf,
1800 unsigned int len)
1801{
1802 struct mtd_info *mtd = nand_to_mtd(chip);
1803
1804 if (len && !buf)
1805 return -EINVAL;
1806
1807 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1808 return -EINVAL;
1809
Miquel Raynal8878b122017-11-09 14:16:45 +01001810 if (chip->exec_op)
1811 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1812 len, false);
1813
Boris Brezillon97d90da2017-11-30 18:01:29 +01001814 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1815
1816 if (buf)
1817 chip->write_buf(mtd, buf, len);
1818
1819 return 0;
1820}
1821EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1822
1823/**
1824 * nand_prog_page_end_op - ends a PROG PAGE operation
1825 * @chip: The NAND chip
1826 *
1827 * This function issues the second half of a PROG PAGE operation.
1828 * This function does not select/unselect the CS line.
1829 *
1830 * Returns 0 on success, a negative error code otherwise.
1831 */
1832int nand_prog_page_end_op(struct nand_chip *chip)
1833{
1834 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001835 int ret;
1836 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001837
Miquel Raynal8878b122017-11-09 14:16:45 +01001838 if (chip->exec_op) {
1839 const struct nand_sdr_timings *sdr =
1840 nand_get_sdr_timings(&chip->data_interface);
1841 struct nand_op_instr instrs[] = {
1842 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1843 PSEC_TO_NSEC(sdr->tWB_max)),
1844 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1845 };
1846 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001847
Miquel Raynal8878b122017-11-09 14:16:45 +01001848 ret = nand_exec_op(chip, &op);
1849 if (ret)
1850 return ret;
1851
1852 ret = nand_status_op(chip, &status);
1853 if (ret)
1854 return ret;
1855 } else {
1856 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1857 ret = chip->waitfunc(mtd, chip);
1858 if (ret < 0)
1859 return ret;
1860
1861 status = ret;
1862 }
1863
Boris Brezillon97d90da2017-11-30 18:01:29 +01001864 if (status & NAND_STATUS_FAIL)
1865 return -EIO;
1866
1867 return 0;
1868}
1869EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1870
1871/**
1872 * nand_prog_page_op - Do a full PROG PAGE operation
1873 * @chip: The NAND chip
1874 * @page: page to write
1875 * @offset_in_page: offset within the page
1876 * @buf: buffer containing the data to write to the page
1877 * @len: length of the buffer
1878 *
1879 * This function issues a full PROG PAGE operation.
1880 * This function does not select/unselect the CS line.
1881 *
1882 * Returns 0 on success, a negative error code otherwise.
1883 */
1884int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1885 unsigned int offset_in_page, const void *buf,
1886 unsigned int len)
1887{
1888 struct mtd_info *mtd = nand_to_mtd(chip);
1889 int status;
1890
1891 if (!len || !buf)
1892 return -EINVAL;
1893
1894 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1895 return -EINVAL;
1896
Miquel Raynal8878b122017-11-09 14:16:45 +01001897 if (chip->exec_op) {
1898 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1899 len, true);
1900 } else {
1901 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1902 chip->write_buf(mtd, buf, len);
1903 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1904 status = chip->waitfunc(mtd, chip);
1905 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001906
Boris Brezillon97d90da2017-11-30 18:01:29 +01001907 if (status & NAND_STATUS_FAIL)
1908 return -EIO;
1909
1910 return 0;
1911}
1912EXPORT_SYMBOL_GPL(nand_prog_page_op);
1913
1914/**
1915 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1916 * @chip: The NAND chip
1917 * @offset_in_page: offset within the page
1918 * @buf: buffer containing the data to send to the NAND
1919 * @len: length of the buffer
1920 * @force_8bit: force 8-bit bus access
1921 *
1922 * This function issues a CHANGE WRITE COLUMN operation.
1923 * This function does not select/unselect the CS line.
1924 *
1925 * Returns 0 on success, a negative error code otherwise.
1926 */
1927int nand_change_write_column_op(struct nand_chip *chip,
1928 unsigned int offset_in_page,
1929 const void *buf, unsigned int len,
1930 bool force_8bit)
1931{
1932 struct mtd_info *mtd = nand_to_mtd(chip);
1933
1934 if (len && !buf)
1935 return -EINVAL;
1936
1937 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1938 return -EINVAL;
1939
Miquel Raynal8878b122017-11-09 14:16:45 +01001940 /* Small page NANDs do not support column change. */
1941 if (mtd->writesize <= 512)
1942 return -ENOTSUPP;
1943
1944 if (chip->exec_op) {
1945 const struct nand_sdr_timings *sdr =
1946 nand_get_sdr_timings(&chip->data_interface);
1947 u8 addrs[2];
1948 struct nand_op_instr instrs[] = {
1949 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1950 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1951 NAND_OP_DATA_OUT(len, buf, 0),
1952 };
1953 struct nand_operation op = NAND_OPERATION(instrs);
1954 int ret;
1955
1956 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1957 if (ret < 0)
1958 return ret;
1959
1960 instrs[2].ctx.data.force_8bit = force_8bit;
1961
1962 /* Drop the DATA_OUT instruction if len is set to 0. */
1963 if (!len)
1964 op.ninstrs--;
1965
1966 return nand_exec_op(chip, &op);
1967 }
1968
Boris Brezillon97d90da2017-11-30 18:01:29 +01001969 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1970 if (len)
1971 chip->write_buf(mtd, buf, len);
1972
1973 return 0;
1974}
1975EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1976
1977/**
1978 * nand_readid_op - Do a READID operation
1979 * @chip: The NAND chip
1980 * @addr: address cycle to pass after the READID command
1981 * @buf: buffer used to store the ID
1982 * @len: length of the buffer
1983 *
1984 * This function sends a READID command and reads back the ID returned by the
1985 * NAND.
1986 * This function does not select/unselect the CS line.
1987 *
1988 * Returns 0 on success, a negative error code otherwise.
1989 */
1990int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1991 unsigned int len)
1992{
1993 struct mtd_info *mtd = nand_to_mtd(chip);
1994 unsigned int i;
1995 u8 *id = buf;
1996
1997 if (len && !buf)
1998 return -EINVAL;
1999
Miquel Raynal8878b122017-11-09 14:16:45 +01002000 if (chip->exec_op) {
2001 const struct nand_sdr_timings *sdr =
2002 nand_get_sdr_timings(&chip->data_interface);
2003 struct nand_op_instr instrs[] = {
2004 NAND_OP_CMD(NAND_CMD_READID, 0),
2005 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
2006 NAND_OP_8BIT_DATA_IN(len, buf, 0),
2007 };
2008 struct nand_operation op = NAND_OPERATION(instrs);
2009
2010 /* Drop the DATA_IN instruction if len is set to 0. */
2011 if (!len)
2012 op.ninstrs--;
2013
2014 return nand_exec_op(chip, &op);
2015 }
2016
Boris Brezillon97d90da2017-11-30 18:01:29 +01002017 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
2018
2019 for (i = 0; i < len; i++)
2020 id[i] = chip->read_byte(mtd);
2021
2022 return 0;
2023}
2024EXPORT_SYMBOL_GPL(nand_readid_op);
2025
2026/**
2027 * nand_status_op - Do a STATUS operation
2028 * @chip: The NAND chip
2029 * @status: out variable to store the NAND status
2030 *
2031 * This function sends a STATUS command and reads back the status returned by
2032 * the NAND.
2033 * This function does not select/unselect the CS line.
2034 *
2035 * Returns 0 on success, a negative error code otherwise.
2036 */
2037int nand_status_op(struct nand_chip *chip, u8 *status)
2038{
2039 struct mtd_info *mtd = nand_to_mtd(chip);
2040
Miquel Raynal8878b122017-11-09 14:16:45 +01002041 if (chip->exec_op) {
2042 const struct nand_sdr_timings *sdr =
2043 nand_get_sdr_timings(&chip->data_interface);
2044 struct nand_op_instr instrs[] = {
2045 NAND_OP_CMD(NAND_CMD_STATUS,
2046 PSEC_TO_NSEC(sdr->tADL_min)),
2047 NAND_OP_8BIT_DATA_IN(1, status, 0),
2048 };
2049 struct nand_operation op = NAND_OPERATION(instrs);
2050
2051 if (!status)
2052 op.ninstrs--;
2053
2054 return nand_exec_op(chip, &op);
2055 }
2056
Boris Brezillon97d90da2017-11-30 18:01:29 +01002057 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
2058 if (status)
2059 *status = chip->read_byte(mtd);
2060
2061 return 0;
2062}
2063EXPORT_SYMBOL_GPL(nand_status_op);
2064
2065/**
2066 * nand_exit_status_op - Exit a STATUS operation
2067 * @chip: The NAND chip
2068 *
2069 * This function sends a READ0 command to cancel the effect of the STATUS
2070 * command to avoid reading only the status until a new read command is sent.
2071 *
2072 * This function does not select/unselect the CS line.
2073 *
2074 * Returns 0 on success, a negative error code otherwise.
2075 */
2076int nand_exit_status_op(struct nand_chip *chip)
2077{
2078 struct mtd_info *mtd = nand_to_mtd(chip);
2079
Miquel Raynal8878b122017-11-09 14:16:45 +01002080 if (chip->exec_op) {
2081 struct nand_op_instr instrs[] = {
2082 NAND_OP_CMD(NAND_CMD_READ0, 0),
2083 };
2084 struct nand_operation op = NAND_OPERATION(instrs);
2085
2086 return nand_exec_op(chip, &op);
2087 }
2088
Boris Brezillon97d90da2017-11-30 18:01:29 +01002089 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2090
2091 return 0;
2092}
2093EXPORT_SYMBOL_GPL(nand_exit_status_op);
2094
2095/**
2096 * nand_erase_op - Do an erase operation
2097 * @chip: The NAND chip
2098 * @eraseblock: block to erase
2099 *
2100 * This function sends an ERASE command and waits for the NAND to be ready
2101 * before returning.
2102 * This function does not select/unselect the CS line.
2103 *
2104 * Returns 0 on success, a negative error code otherwise.
2105 */
2106int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2107{
2108 struct mtd_info *mtd = nand_to_mtd(chip);
2109 unsigned int page = eraseblock <<
2110 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002111 int ret;
2112 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002113
Miquel Raynal8878b122017-11-09 14:16:45 +01002114 if (chip->exec_op) {
2115 const struct nand_sdr_timings *sdr =
2116 nand_get_sdr_timings(&chip->data_interface);
2117 u8 addrs[3] = { page, page >> 8, page >> 16 };
2118 struct nand_op_instr instrs[] = {
2119 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2120 NAND_OP_ADDR(2, addrs, 0),
2121 NAND_OP_CMD(NAND_CMD_ERASE2,
2122 PSEC_TO_MSEC(sdr->tWB_max)),
2123 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2124 };
2125 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002126
Miquel Raynal8878b122017-11-09 14:16:45 +01002127 if (chip->options & NAND_ROW_ADDR_3)
2128 instrs[1].ctx.addr.naddrs++;
2129
2130 ret = nand_exec_op(chip, &op);
2131 if (ret)
2132 return ret;
2133
2134 ret = nand_status_op(chip, &status);
2135 if (ret)
2136 return ret;
2137 } else {
2138 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2139 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2140
2141 ret = chip->waitfunc(mtd, chip);
2142 if (ret < 0)
2143 return ret;
2144
2145 status = ret;
2146 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002147
2148 if (status & NAND_STATUS_FAIL)
2149 return -EIO;
2150
2151 return 0;
2152}
2153EXPORT_SYMBOL_GPL(nand_erase_op);
2154
2155/**
2156 * nand_set_features_op - Do a SET FEATURES operation
2157 * @chip: The NAND chip
2158 * @feature: feature id
2159 * @data: 4 bytes of data
2160 *
2161 * This function sends a SET FEATURES command and waits for the NAND to be
2162 * ready before returning.
2163 * This function does not select/unselect the CS line.
2164 *
2165 * Returns 0 on success, a negative error code otherwise.
2166 */
2167static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2168 const void *data)
2169{
2170 struct mtd_info *mtd = nand_to_mtd(chip);
2171 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002172 int i, ret;
2173 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002174
Miquel Raynal8878b122017-11-09 14:16:45 +01002175 if (chip->exec_op) {
2176 const struct nand_sdr_timings *sdr =
2177 nand_get_sdr_timings(&chip->data_interface);
2178 struct nand_op_instr instrs[] = {
2179 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2180 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2181 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2182 PSEC_TO_NSEC(sdr->tWB_max)),
2183 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2184 };
2185 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002186
Miquel Raynal8878b122017-11-09 14:16:45 +01002187 ret = nand_exec_op(chip, &op);
2188 if (ret)
2189 return ret;
2190
2191 ret = nand_status_op(chip, &status);
2192 if (ret)
2193 return ret;
2194 } else {
2195 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
2196 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2197 chip->write_byte(mtd, params[i]);
2198
2199 ret = chip->waitfunc(mtd, chip);
2200 if (ret < 0)
2201 return ret;
2202
2203 status = ret;
2204 }
2205
Boris Brezillon97d90da2017-11-30 18:01:29 +01002206 if (status & NAND_STATUS_FAIL)
2207 return -EIO;
2208
2209 return 0;
2210}
2211
2212/**
2213 * nand_get_features_op - Do a GET FEATURES operation
2214 * @chip: The NAND chip
2215 * @feature: feature id
2216 * @data: 4 bytes of data
2217 *
2218 * This function sends a GET FEATURES command and waits for the NAND to be
2219 * ready before returning.
2220 * This function does not select/unselect the CS line.
2221 *
2222 * Returns 0 on success, a negative error code otherwise.
2223 */
2224static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2225 void *data)
2226{
2227 struct mtd_info *mtd = nand_to_mtd(chip);
2228 u8 *params = data;
2229 int i;
2230
Miquel Raynal8878b122017-11-09 14:16:45 +01002231 if (chip->exec_op) {
2232 const struct nand_sdr_timings *sdr =
2233 nand_get_sdr_timings(&chip->data_interface);
2234 struct nand_op_instr instrs[] = {
2235 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2236 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2237 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2238 PSEC_TO_NSEC(sdr->tRR_min)),
2239 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2240 data, 0),
2241 };
2242 struct nand_operation op = NAND_OPERATION(instrs);
2243
2244 return nand_exec_op(chip, &op);
2245 }
2246
Boris Brezillon97d90da2017-11-30 18:01:29 +01002247 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2248 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2249 params[i] = chip->read_byte(mtd);
2250
2251 return 0;
2252}
2253
2254/**
2255 * nand_reset_op - Do a reset operation
2256 * @chip: The NAND chip
2257 *
2258 * This function sends a RESET command and waits for the NAND to be ready
2259 * before returning.
2260 * This function does not select/unselect the CS line.
2261 *
2262 * Returns 0 on success, a negative error code otherwise.
2263 */
2264int nand_reset_op(struct nand_chip *chip)
2265{
2266 struct mtd_info *mtd = nand_to_mtd(chip);
2267
Miquel Raynal8878b122017-11-09 14:16:45 +01002268 if (chip->exec_op) {
2269 const struct nand_sdr_timings *sdr =
2270 nand_get_sdr_timings(&chip->data_interface);
2271 struct nand_op_instr instrs[] = {
2272 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2273 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2274 };
2275 struct nand_operation op = NAND_OPERATION(instrs);
2276
2277 return nand_exec_op(chip, &op);
2278 }
2279
Boris Brezillon97d90da2017-11-30 18:01:29 +01002280 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2281
2282 return 0;
2283}
2284EXPORT_SYMBOL_GPL(nand_reset_op);
2285
2286/**
2287 * nand_read_data_op - Read data from the NAND
2288 * @chip: The NAND chip
2289 * @buf: buffer used to store the data
2290 * @len: length of the buffer
2291 * @force_8bit: force 8-bit bus access
2292 *
2293 * This function does a raw data read on the bus. Usually used after launching
2294 * another NAND operation like nand_read_page_op().
2295 * This function does not select/unselect the CS line.
2296 *
2297 * Returns 0 on success, a negative error code otherwise.
2298 */
2299int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2300 bool force_8bit)
2301{
2302 struct mtd_info *mtd = nand_to_mtd(chip);
2303
2304 if (!len || !buf)
2305 return -EINVAL;
2306
Miquel Raynal8878b122017-11-09 14:16:45 +01002307 if (chip->exec_op) {
2308 struct nand_op_instr instrs[] = {
2309 NAND_OP_DATA_IN(len, buf, 0),
2310 };
2311 struct nand_operation op = NAND_OPERATION(instrs);
2312
2313 instrs[0].ctx.data.force_8bit = force_8bit;
2314
2315 return nand_exec_op(chip, &op);
2316 }
2317
Boris Brezillon97d90da2017-11-30 18:01:29 +01002318 if (force_8bit) {
2319 u8 *p = buf;
2320 unsigned int i;
2321
2322 for (i = 0; i < len; i++)
2323 p[i] = chip->read_byte(mtd);
2324 } else {
2325 chip->read_buf(mtd, buf, len);
2326 }
2327
2328 return 0;
2329}
2330EXPORT_SYMBOL_GPL(nand_read_data_op);
2331
2332/**
2333 * nand_write_data_op - Write data from the NAND
2334 * @chip: The NAND chip
2335 * @buf: buffer containing the data to send on the bus
2336 * @len: length of the buffer
2337 * @force_8bit: force 8-bit bus access
2338 *
2339 * This function does a raw data write on the bus. Usually used after launching
2340 * another NAND operation like nand_write_page_begin_op().
2341 * This function does not select/unselect the CS line.
2342 *
2343 * Returns 0 on success, a negative error code otherwise.
2344 */
2345int nand_write_data_op(struct nand_chip *chip, const void *buf,
2346 unsigned int len, bool force_8bit)
2347{
2348 struct mtd_info *mtd = nand_to_mtd(chip);
2349
2350 if (!len || !buf)
2351 return -EINVAL;
2352
Miquel Raynal8878b122017-11-09 14:16:45 +01002353 if (chip->exec_op) {
2354 struct nand_op_instr instrs[] = {
2355 NAND_OP_DATA_OUT(len, buf, 0),
2356 };
2357 struct nand_operation op = NAND_OPERATION(instrs);
2358
2359 instrs[0].ctx.data.force_8bit = force_8bit;
2360
2361 return nand_exec_op(chip, &op);
2362 }
2363
Boris Brezillon97d90da2017-11-30 18:01:29 +01002364 if (force_8bit) {
2365 const u8 *p = buf;
2366 unsigned int i;
2367
2368 for (i = 0; i < len; i++)
2369 chip->write_byte(mtd, p[i]);
2370 } else {
2371 chip->write_buf(mtd, buf, len);
2372 }
2373
2374 return 0;
2375}
2376EXPORT_SYMBOL_GPL(nand_write_data_op);
2377
2378/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002379 * struct nand_op_parser_ctx - Context used by the parser
2380 * @instrs: array of all the instructions that must be addressed
2381 * @ninstrs: length of the @instrs array
2382 * @subop: Sub-operation to be passed to the NAND controller
2383 *
2384 * This structure is used by the core to split NAND operations into
2385 * sub-operations that can be handled by the NAND controller.
2386 */
2387struct nand_op_parser_ctx {
2388 const struct nand_op_instr *instrs;
2389 unsigned int ninstrs;
2390 struct nand_subop subop;
2391};
2392
2393/**
2394 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2395 * @pat: the parser pattern element that matches @instr
2396 * @instr: pointer to the instruction to check
2397 * @start_offset: this is an in/out parameter. If @instr has already been
2398 * split, then @start_offset is the offset from which to start
2399 * (either an address cycle or an offset in the data buffer).
2400 * Conversely, if the function returns true (ie. instr must be
2401 * split), this parameter is updated to point to the first
2402 * data/address cycle that has not been taken care of.
2403 *
2404 * Some NAND controllers are limited and cannot send X address cycles with a
2405 * unique operation, or cannot read/write more than Y bytes at the same time.
2406 * In this case, split the instruction that does not fit in a single
2407 * controller-operation into two or more chunks.
2408 *
2409 * Returns true if the instruction must be split, false otherwise.
2410 * The @start_offset parameter is also updated to the offset at which the next
2411 * bundle of instruction must start (if an address or a data instruction).
2412 */
2413static bool
2414nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2415 const struct nand_op_instr *instr,
2416 unsigned int *start_offset)
2417{
2418 switch (pat->type) {
2419 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002420 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002421 break;
2422
2423 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002424 pat->ctx.addr.maxcycles) {
2425 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002426 return true;
2427 }
2428 break;
2429
2430 case NAND_OP_DATA_IN_INSTR:
2431 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002432 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002433 break;
2434
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002435 if (instr->ctx.data.len - *start_offset >
2436 pat->ctx.data.maxlen) {
2437 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002438 return true;
2439 }
2440 break;
2441
2442 default:
2443 break;
2444 }
2445
2446 return false;
2447}
2448
2449/**
2450 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2451 * remaining in the parser context
2452 * @pat: the pattern to test
2453 * @ctx: the parser context structure to match with the pattern @pat
2454 *
2455 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2456 * Returns true if this is the case, false ortherwise. When true is returned,
2457 * @ctx->subop is updated with the set of instructions to be passed to the
2458 * controller driver.
2459 */
2460static bool
2461nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2462 struct nand_op_parser_ctx *ctx)
2463{
2464 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2465 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2466 const struct nand_op_instr *instr = ctx->subop.instrs;
2467 unsigned int i, ninstrs;
2468
2469 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2470 /*
2471 * The pattern instruction does not match the operation
2472 * instruction. If the instruction is marked optional in the
2473 * pattern definition, we skip the pattern element and continue
2474 * to the next one. If the element is mandatory, there's no
2475 * match and we can return false directly.
2476 */
2477 if (instr->type != pat->elems[i].type) {
2478 if (!pat->elems[i].optional)
2479 return false;
2480
2481 continue;
2482 }
2483
2484 /*
2485 * Now check the pattern element constraints. If the pattern is
2486 * not able to handle the whole instruction in a single step,
2487 * we have to split it.
2488 * The last_instr_end_off value comes back updated to point to
2489 * the position where we have to split the instruction (the
2490 * start of the next subop chunk).
2491 */
2492 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2493 &instr_offset)) {
2494 ninstrs++;
2495 i++;
2496 break;
2497 }
2498
2499 instr++;
2500 ninstrs++;
2501 instr_offset = 0;
2502 }
2503
2504 /*
2505 * This can happen if all instructions of a pattern are optional.
2506 * Still, if there's not at least one instruction handled by this
2507 * pattern, this is not a match, and we should try the next one (if
2508 * any).
2509 */
2510 if (!ninstrs)
2511 return false;
2512
2513 /*
2514 * We had a match on the pattern head, but the pattern may be longer
2515 * than the instructions we're asked to execute. We need to make sure
2516 * there's no mandatory elements in the pattern tail.
2517 */
2518 for (; i < pat->nelems; i++) {
2519 if (!pat->elems[i].optional)
2520 return false;
2521 }
2522
2523 /*
2524 * We have a match: update the subop structure accordingly and return
2525 * true.
2526 */
2527 ctx->subop.ninstrs = ninstrs;
2528 ctx->subop.last_instr_end_off = instr_offset;
2529
2530 return true;
2531}
2532
2533#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2534static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2535{
2536 const struct nand_op_instr *instr;
2537 char *prefix = " ";
2538 unsigned int i;
2539
2540 pr_debug("executing subop:\n");
2541
2542 for (i = 0; i < ctx->ninstrs; i++) {
2543 instr = &ctx->instrs[i];
2544
2545 if (instr == &ctx->subop.instrs[0])
2546 prefix = " ->";
2547
2548 switch (instr->type) {
2549 case NAND_OP_CMD_INSTR:
2550 pr_debug("%sCMD [0x%02x]\n", prefix,
2551 instr->ctx.cmd.opcode);
2552 break;
2553 case NAND_OP_ADDR_INSTR:
2554 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2555 instr->ctx.addr.naddrs,
2556 instr->ctx.addr.naddrs < 64 ?
2557 instr->ctx.addr.naddrs : 64,
2558 instr->ctx.addr.addrs);
2559 break;
2560 case NAND_OP_DATA_IN_INSTR:
2561 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2562 instr->ctx.data.len,
2563 instr->ctx.data.force_8bit ?
2564 ", force 8-bit" : "");
2565 break;
2566 case NAND_OP_DATA_OUT_INSTR:
2567 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2568 instr->ctx.data.len,
2569 instr->ctx.data.force_8bit ?
2570 ", force 8-bit" : "");
2571 break;
2572 case NAND_OP_WAITRDY_INSTR:
2573 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2574 instr->ctx.waitrdy.timeout_ms);
2575 break;
2576 }
2577
2578 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2579 prefix = " ";
2580 }
2581}
2582#else
2583static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2584{
2585 /* NOP */
2586}
2587#endif
2588
2589/**
2590 * nand_op_parser_exec_op - exec_op parser
2591 * @chip: the NAND chip
2592 * @parser: patterns description provided by the controller driver
2593 * @op: the NAND operation to address
2594 * @check_only: when true, the function only checks if @op can be handled but
2595 * does not execute the operation
2596 *
2597 * Helper function designed to ease integration of NAND controller drivers that
2598 * only support a limited set of instruction sequences. The supported sequences
2599 * are described in @parser, and the framework takes care of splitting @op into
2600 * multiple sub-operations (if required) and pass them back to the ->exec()
2601 * callback of the matching pattern if @check_only is set to false.
2602 *
2603 * NAND controller drivers should call this function from their own ->exec_op()
2604 * implementation.
2605 *
2606 * Returns 0 on success, a negative error code otherwise. A failure can be
2607 * caused by an unsupported operation (none of the supported patterns is able
2608 * to handle the requested operation), or an error returned by one of the
2609 * matching pattern->exec() hook.
2610 */
2611int nand_op_parser_exec_op(struct nand_chip *chip,
2612 const struct nand_op_parser *parser,
2613 const struct nand_operation *op, bool check_only)
2614{
2615 struct nand_op_parser_ctx ctx = {
2616 .subop.instrs = op->instrs,
2617 .instrs = op->instrs,
2618 .ninstrs = op->ninstrs,
2619 };
2620 unsigned int i;
2621
2622 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2623 int ret;
2624
2625 for (i = 0; i < parser->npatterns; i++) {
2626 const struct nand_op_parser_pattern *pattern;
2627
2628 pattern = &parser->patterns[i];
2629 if (!nand_op_parser_match_pat(pattern, &ctx))
2630 continue;
2631
2632 nand_op_parser_trace(&ctx);
2633
2634 if (check_only)
2635 break;
2636
2637 ret = pattern->exec(chip, &ctx.subop);
2638 if (ret)
2639 return ret;
2640
2641 break;
2642 }
2643
2644 if (i == parser->npatterns) {
2645 pr_debug("->exec_op() parser: pattern not found!\n");
2646 return -ENOTSUPP;
2647 }
2648
2649 /*
2650 * Update the context structure by pointing to the start of the
2651 * next subop.
2652 */
2653 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2654 if (ctx.subop.last_instr_end_off)
2655 ctx.subop.instrs -= 1;
2656
2657 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2658 }
2659
2660 return 0;
2661}
2662EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2663
2664static bool nand_instr_is_data(const struct nand_op_instr *instr)
2665{
2666 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2667 instr->type == NAND_OP_DATA_OUT_INSTR);
2668}
2669
2670static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2671 unsigned int instr_idx)
2672{
2673 return subop && instr_idx < subop->ninstrs;
2674}
2675
2676static int nand_subop_get_start_off(const struct nand_subop *subop,
2677 unsigned int instr_idx)
2678{
2679 if (instr_idx)
2680 return 0;
2681
2682 return subop->first_instr_start_off;
2683}
2684
2685/**
2686 * nand_subop_get_addr_start_off - Get the start offset in an address array
2687 * @subop: The entire sub-operation
2688 * @instr_idx: Index of the instruction inside the sub-operation
2689 *
2690 * During driver development, one could be tempted to directly use the
2691 * ->addr.addrs field of address instructions. This is wrong as address
2692 * instructions might be split.
2693 *
2694 * Given an address instruction, returns the offset of the first cycle to issue.
2695 */
2696int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2697 unsigned int instr_idx)
2698{
2699 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2700 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2701 return -EINVAL;
2702
2703 return nand_subop_get_start_off(subop, instr_idx);
2704}
2705EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2706
2707/**
2708 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2709 * @subop: The entire sub-operation
2710 * @instr_idx: Index of the instruction inside the sub-operation
2711 *
2712 * During driver development, one could be tempted to directly use the
2713 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2714 * might be split.
2715 *
2716 * Given an address instruction, returns the number of address cycle to issue.
2717 */
2718int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2719 unsigned int instr_idx)
2720{
2721 int start_off, end_off;
2722
2723 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2724 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)
2725 return -EINVAL;
2726
2727 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2728
2729 if (instr_idx == subop->ninstrs - 1 &&
2730 subop->last_instr_end_off)
2731 end_off = subop->last_instr_end_off;
2732 else
2733 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2734
2735 return end_off - start_off;
2736}
2737EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2738
2739/**
2740 * nand_subop_get_data_start_off - Get the start offset in a data array
2741 * @subop: The entire sub-operation
2742 * @instr_idx: Index of the instruction inside the sub-operation
2743 *
2744 * During driver development, one could be tempted to directly use the
2745 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2746 * instructions might be split.
2747 *
2748 * Given a data instruction, returns the offset to start from.
2749 */
2750int nand_subop_get_data_start_off(const struct nand_subop *subop,
2751 unsigned int instr_idx)
2752{
2753 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2754 !nand_instr_is_data(&subop->instrs[instr_idx]))
2755 return -EINVAL;
2756
2757 return nand_subop_get_start_off(subop, instr_idx);
2758}
2759EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2760
2761/**
2762 * nand_subop_get_data_len - Get the number of bytes to retrieve
2763 * @subop: The entire sub-operation
2764 * @instr_idx: Index of the instruction inside the sub-operation
2765 *
2766 * During driver development, one could be tempted to directly use the
2767 * ->data->len field of a data instruction. This is wrong as data instructions
2768 * might be split.
2769 *
2770 * Returns the length of the chunk of data to send/receive.
2771 */
2772int nand_subop_get_data_len(const struct nand_subop *subop,
2773 unsigned int instr_idx)
2774{
2775 int start_off = 0, end_off;
2776
2777 if (!nand_subop_instr_is_valid(subop, instr_idx) ||
2778 !nand_instr_is_data(&subop->instrs[instr_idx]))
2779 return -EINVAL;
2780
2781 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2782
2783 if (instr_idx == subop->ninstrs - 1 &&
2784 subop->last_instr_end_off)
2785 end_off = subop->last_instr_end_off;
2786 else
2787 end_off = subop->instrs[instr_idx].ctx.data.len;
2788
2789 return end_off - start_off;
2790}
2791EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002794 * nand_reset - Reset and initialize a NAND device
2795 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002796 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002797 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002798 * Save the timings data structure, then apply SDR timings mode 0 (see
2799 * nand_reset_data_interface for details), do the reset operation, and
2800 * apply back the previous timings.
2801 *
2802 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002803 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002804int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002805{
2806 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal17fa8042017-11-30 18:01:31 +01002807 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002808 int ret;
2809
Boris Brezillon104e4422017-03-16 09:35:58 +01002810 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002811 if (ret)
2812 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002813
Boris Brezillon73f907f2016-10-24 16:46:20 +02002814 /*
2815 * The CS line has to be released before we can apply the new NAND
2816 * interface settings, hence this weird ->select_chip() dance.
2817 */
2818 chip->select_chip(mtd, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002819 ret = nand_reset_op(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02002820 chip->select_chip(mtd, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002821 if (ret)
2822 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002823
Miquel Raynal107b7d62018-03-19 14:47:25 +01002824 /*
2825 * A nand_reset_data_interface() put both the NAND chip and the NAND
2826 * controller in timings mode 0. If the default mode for this chip is
2827 * also 0, no need to proceed to the change again. Plus, at probe time,
2828 * nand_setup_data_interface() uses ->set/get_features() which would
2829 * fail anyway as the parameter page is not available yet.
2830 */
2831 if (!chip->onfi_timing_mode_default)
2832 return 0;
2833
Miquel Raynal17fa8042017-11-30 18:01:31 +01002834 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002835 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002836 if (ret)
2837 return ret;
2838
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002839 return 0;
2840}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002841EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002842
2843/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002844 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2845 * @buf: buffer to test
2846 * @len: buffer length
2847 * @bitflips_threshold: maximum number of bitflips
2848 *
2849 * Check if a buffer contains only 0xff, which means the underlying region
2850 * has been erased and is ready to be programmed.
2851 * The bitflips_threshold specify the maximum number of bitflips before
2852 * considering the region is not erased.
2853 * Note: The logic of this function has been extracted from the memweight
2854 * implementation, except that nand_check_erased_buf function exit before
2855 * testing the whole buffer if the number of bitflips exceed the
2856 * bitflips_threshold value.
2857 *
2858 * Returns a positive number of bitflips less than or equal to
2859 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2860 * threshold.
2861 */
2862static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2863{
2864 const unsigned char *bitmap = buf;
2865 int bitflips = 0;
2866 int weight;
2867
2868 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2869 len--, bitmap++) {
2870 weight = hweight8(*bitmap);
2871 bitflips += BITS_PER_BYTE - weight;
2872 if (unlikely(bitflips > bitflips_threshold))
2873 return -EBADMSG;
2874 }
2875
2876 for (; len >= sizeof(long);
2877 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002878 unsigned long d = *((unsigned long *)bitmap);
2879 if (d == ~0UL)
2880 continue;
2881 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002882 bitflips += BITS_PER_LONG - weight;
2883 if (unlikely(bitflips > bitflips_threshold))
2884 return -EBADMSG;
2885 }
2886
2887 for (; len > 0; len--, bitmap++) {
2888 weight = hweight8(*bitmap);
2889 bitflips += BITS_PER_BYTE - weight;
2890 if (unlikely(bitflips > bitflips_threshold))
2891 return -EBADMSG;
2892 }
2893
2894 return bitflips;
2895}
2896
2897/**
2898 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2899 * 0xff data
2900 * @data: data buffer to test
2901 * @datalen: data length
2902 * @ecc: ECC buffer
2903 * @ecclen: ECC length
2904 * @extraoob: extra OOB buffer
2905 * @extraooblen: extra OOB length
2906 * @bitflips_threshold: maximum number of bitflips
2907 *
2908 * Check if a data buffer and its associated ECC and OOB data contains only
2909 * 0xff pattern, which means the underlying region has been erased and is
2910 * ready to be programmed.
2911 * The bitflips_threshold specify the maximum number of bitflips before
2912 * considering the region as not erased.
2913 *
2914 * Note:
2915 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2916 * different from the NAND page size. When fixing bitflips, ECC engines will
2917 * report the number of errors per chunk, and the NAND core infrastructure
2918 * expect you to return the maximum number of bitflips for the whole page.
2919 * This is why you should always use this function on a single chunk and
2920 * not on the whole page. After checking each chunk you should update your
2921 * max_bitflips value accordingly.
2922 * 2/ When checking for bitflips in erased pages you should not only check
2923 * the payload data but also their associated ECC data, because a user might
2924 * have programmed almost all bits to 1 but a few. In this case, we
2925 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2926 * this case.
2927 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2928 * data are protected by the ECC engine.
2929 * It could also be used if you support subpages and want to attach some
2930 * extra OOB data to an ECC chunk.
2931 *
2932 * Returns a positive number of bitflips less than or equal to
2933 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2934 * threshold. In case of success, the passed buffers are filled with 0xff.
2935 */
2936int nand_check_erased_ecc_chunk(void *data, int datalen,
2937 void *ecc, int ecclen,
2938 void *extraoob, int extraooblen,
2939 int bitflips_threshold)
2940{
2941 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2942
2943 data_bitflips = nand_check_erased_buf(data, datalen,
2944 bitflips_threshold);
2945 if (data_bitflips < 0)
2946 return data_bitflips;
2947
2948 bitflips_threshold -= data_bitflips;
2949
2950 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2951 if (ecc_bitflips < 0)
2952 return ecc_bitflips;
2953
2954 bitflips_threshold -= ecc_bitflips;
2955
2956 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2957 bitflips_threshold);
2958 if (extraoob_bitflips < 0)
2959 return extraoob_bitflips;
2960
2961 if (data_bitflips)
2962 memset(data, 0xff, datalen);
2963
2964 if (ecc_bitflips)
2965 memset(ecc, 0xff, ecclen);
2966
2967 if (extraoob_bitflips)
2968 memset(extraoob, 0xff, extraooblen);
2969
2970 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2971}
2972EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2973
2974/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002975 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002976 * @mtd: mtd info structure
2977 * @chip: nand chip info structure
2978 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002979 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002980 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002981 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002982 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002983 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002984int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2985 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002986{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002987 int ret;
2988
Boris Brezillon25f815f2017-11-30 18:01:30 +01002989 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002990 if (ret)
2991 return ret;
2992
2993 if (oob_required) {
2994 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2995 false);
2996 if (ret)
2997 return ret;
2998 }
2999
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003000 return 0;
3001}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003002EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003003
3004/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003005 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07003006 * @mtd: mtd info structure
3007 * @chip: nand chip info structure
3008 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003009 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003010 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003011 *
3012 * We need a special oob layout and handling even when OOB isn't used.
3013 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003014static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003015 struct nand_chip *chip, uint8_t *buf,
3016 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003017{
3018 int eccsize = chip->ecc.size;
3019 int eccbytes = chip->ecc.bytes;
3020 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003021 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003022
Boris Brezillon25f815f2017-11-30 18:01:30 +01003023 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3024 if (ret)
3025 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003026
3027 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003028 ret = nand_read_data_op(chip, buf, eccsize, false);
3029 if (ret)
3030 return ret;
3031
David Brownell52ff49d2009-03-04 12:01:36 -08003032 buf += eccsize;
3033
3034 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003035 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3036 false);
3037 if (ret)
3038 return ret;
3039
David Brownell52ff49d2009-03-04 12:01:36 -08003040 oob += chip->ecc.prepad;
3041 }
3042
Boris Brezillon97d90da2017-11-30 18:01:29 +01003043 ret = nand_read_data_op(chip, oob, eccbytes, false);
3044 if (ret)
3045 return ret;
3046
David Brownell52ff49d2009-03-04 12:01:36 -08003047 oob += eccbytes;
3048
3049 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003050 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3051 false);
3052 if (ret)
3053 return ret;
3054
David Brownell52ff49d2009-03-04 12:01:36 -08003055 oob += chip->ecc.postpad;
3056 }
3057 }
3058
3059 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003060 if (size) {
3061 ret = nand_read_data_op(chip, oob, size, false);
3062 if (ret)
3063 return ret;
3064 }
David Brownell52ff49d2009-03-04 12:01:36 -08003065
3066 return 0;
3067}
3068
3069/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003070 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003071 * @mtd: mtd info structure
3072 * @chip: nand chip info structure
3073 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003074 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003075 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003076 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003077static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003078 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
Boris Brezillon846031d2016-02-03 20:11:00 +01003080 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003081 int eccbytes = chip->ecc.bytes;
3082 int eccsteps = chip->ecc.steps;
3083 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003084 uint8_t *ecc_calc = chip->ecc.calc_buf;
3085 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003086 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003087
Brian Norris1fbb9382012-05-02 10:14:55 -07003088 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003089
3090 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3091 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3092
Boris Brezillon846031d2016-02-03 20:11:00 +01003093 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3094 chip->ecc.total);
3095 if (ret)
3096 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003097
3098 eccsteps = chip->ecc.steps;
3099 p = buf;
3100
3101 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3102 int stat;
3103
3104 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003105 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003106 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003107 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003108 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003109 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3110 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003111 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003112 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303116 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003117 * @mtd: mtd info structure
3118 * @chip: nand chip info structure
3119 * @data_offs: offset of requested data within the page
3120 * @readlen: data length
3121 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003122 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003123 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003124static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003125 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3126 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003127{
Boris Brezillon846031d2016-02-03 20:11:00 +01003128 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003129 uint8_t *p;
3130 int data_col_addr, i, gaps = 0;
3131 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3132 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003133 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003134 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003135 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003136
Brian Norris7854d3f2011-06-23 14:12:08 -07003137 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003138 start_step = data_offs / chip->ecc.size;
3139 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3140 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303141 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003142
Brian Norris8b6e50c2011-05-25 14:59:01 -07003143 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003144 datafrag_len = num_steps * chip->ecc.size;
3145 eccfrag_len = num_steps * chip->ecc.bytes;
3146
3147 data_col_addr = start_step * chip->ecc.size;
3148 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003149 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003150 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003151 if (ret)
3152 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003153
Brian Norris8b6e50c2011-05-25 14:59:01 -07003154 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003155 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003156 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003157
Brian Norris8b6e50c2011-05-25 14:59:01 -07003158 /*
3159 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003160 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003161 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003162 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3163 if (ret)
3164 return ret;
3165
3166 if (oobregion.length < eccfrag_len)
3167 gaps = 1;
3168
Alexey Korolev3d459552008-05-15 17:23:18 +01003169 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003170 ret = nand_change_read_column_op(chip, mtd->writesize,
3171 chip->oob_poi, mtd->oobsize,
3172 false);
3173 if (ret)
3174 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003175 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003176 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003177 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003178 * about buswidth alignment in read_buf.
3179 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003180 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003181 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003182 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003183 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003184 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3185 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003186 aligned_len++;
3187
Boris Brezillon97d90da2017-11-30 18:01:29 +01003188 ret = nand_change_read_column_op(chip,
3189 mtd->writesize + aligned_pos,
3190 &chip->oob_poi[aligned_pos],
3191 aligned_len, false);
3192 if (ret)
3193 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003194 }
3195
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003196 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003197 chip->oob_poi, index, eccfrag_len);
3198 if (ret)
3199 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003200
3201 p = bufpoi + data_col_addr;
3202 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3203 int stat;
3204
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003205 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3206 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003207 if (stat == -EBADMSG &&
3208 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3209 /* check for empty pages with bitflips */
3210 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003211 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003212 chip->ecc.bytes,
3213 NULL, 0,
3214 chip->ecc.strength);
3215 }
3216
Mike Dunn3f91e942012-04-25 12:06:09 -07003217 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003218 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003219 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003220 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003221 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3222 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003223 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003224 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003225}
3226
3227/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003228 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003229 * @mtd: mtd info structure
3230 * @chip: nand chip info structure
3231 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003232 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003233 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003234 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003235 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003236 */
3237static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003238 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003239{
Boris Brezillon846031d2016-02-03 20:11:00 +01003240 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003241 int eccbytes = chip->ecc.bytes;
3242 int eccsteps = chip->ecc.steps;
3243 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003244 uint8_t *ecc_calc = chip->ecc.calc_buf;
3245 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003246 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003247
Boris Brezillon25f815f2017-11-30 18:01:30 +01003248 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3249 if (ret)
3250 return ret;
3251
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003252 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3253 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003254
3255 ret = nand_read_data_op(chip, p, eccsize, false);
3256 if (ret)
3257 return ret;
3258
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003259 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3260 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003261
3262 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3263 if (ret)
3264 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003265
Boris Brezillon846031d2016-02-03 20:11:00 +01003266 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3267 chip->ecc.total);
3268 if (ret)
3269 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003270
3271 eccsteps = chip->ecc.steps;
3272 p = buf;
3273
3274 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3275 int stat;
3276
3277 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003278 if (stat == -EBADMSG &&
3279 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3280 /* check for empty pages with bitflips */
3281 stat = nand_check_erased_ecc_chunk(p, eccsize,
3282 &ecc_code[i], eccbytes,
3283 NULL, 0,
3284 chip->ecc.strength);
3285 }
3286
Mike Dunn3f91e942012-04-25 12:06:09 -07003287 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003288 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003289 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003290 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003291 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3292 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003293 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003294 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003295}
3296
3297/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003298 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003299 * @mtd: mtd info structure
3300 * @chip: nand chip info structure
3301 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003302 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003303 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003304 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003305 * Hardware ECC for large page chips, require OOB to be read first. For this
3306 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3307 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3308 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3309 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003310 */
3311static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003312 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003313{
Boris Brezillon846031d2016-02-03 20:11:00 +01003314 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003315 int eccbytes = chip->ecc.bytes;
3316 int eccsteps = chip->ecc.steps;
3317 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003318 uint8_t *ecc_code = chip->ecc.code_buf;
3319 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003320 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003321
3322 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003323 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3324 if (ret)
3325 return ret;
3326
3327 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3328 if (ret)
3329 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003330
Boris Brezillon846031d2016-02-03 20:11:00 +01003331 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3332 chip->ecc.total);
3333 if (ret)
3334 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003335
3336 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3337 int stat;
3338
3339 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003340
3341 ret = nand_read_data_op(chip, p, eccsize, false);
3342 if (ret)
3343 return ret;
3344
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003345 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3346
3347 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003348 if (stat == -EBADMSG &&
3349 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3350 /* check for empty pages with bitflips */
3351 stat = nand_check_erased_ecc_chunk(p, eccsize,
3352 &ecc_code[i], eccbytes,
3353 NULL, 0,
3354 chip->ecc.strength);
3355 }
3356
Mike Dunn3f91e942012-04-25 12:06:09 -07003357 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003358 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003359 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003360 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003361 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3362 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003363 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003364 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003365}
3366
3367/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003368 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003369 * @mtd: mtd info structure
3370 * @chip: nand chip info structure
3371 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003372 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003373 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003374 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003375 * The hw generator calculates the error syndrome automatically. Therefore we
3376 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003377 */
3378static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003379 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003380{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003381 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003382 int eccbytes = chip->ecc.bytes;
3383 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003384 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003385 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003386 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003387 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003388
Boris Brezillon25f815f2017-11-30 18:01:30 +01003389 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3390 if (ret)
3391 return ret;
3392
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003393 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3394 int stat;
3395
3396 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003397
3398 ret = nand_read_data_op(chip, p, eccsize, false);
3399 if (ret)
3400 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003401
3402 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003403 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3404 false);
3405 if (ret)
3406 return ret;
3407
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003408 oob += chip->ecc.prepad;
3409 }
3410
3411 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003412
3413 ret = nand_read_data_op(chip, oob, eccbytes, false);
3414 if (ret)
3415 return ret;
3416
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003417 stat = chip->ecc.correct(mtd, p, oob, NULL);
3418
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003419 oob += eccbytes;
3420
3421 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003422 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3423 false);
3424 if (ret)
3425 return ret;
3426
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003427 oob += chip->ecc.postpad;
3428 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003429
3430 if (stat == -EBADMSG &&
3431 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3432 /* check for empty pages with bitflips */
3433 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3434 oob - eccpadbytes,
3435 eccpadbytes,
3436 NULL, 0,
3437 chip->ecc.strength);
3438 }
3439
3440 if (stat < 0) {
3441 mtd->ecc_stats.failed++;
3442 } else {
3443 mtd->ecc_stats.corrected += stat;
3444 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3445 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003446 }
3447
3448 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003449 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003450 if (i) {
3451 ret = nand_read_data_op(chip, oob, i, false);
3452 if (ret)
3453 return ret;
3454 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003455
Mike Dunn3f91e942012-04-25 12:06:09 -07003456 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003457}
3458
3459/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003460 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003461 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003462 * @oob: oob destination address
3463 * @ops: oob ops structure
3464 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003465 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003466static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003467 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003468{
Boris Brezillon846031d2016-02-03 20:11:00 +01003469 struct nand_chip *chip = mtd_to_nand(mtd);
3470 int ret;
3471
Florian Fainellif8ac0412010-09-07 13:23:43 +02003472 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003473
Brian Norris0612b9d2011-08-30 18:45:40 -07003474 case MTD_OPS_PLACE_OOB:
3475 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003476 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3477 return oob + len;
3478
Boris Brezillon846031d2016-02-03 20:11:00 +01003479 case MTD_OPS_AUTO_OOB:
3480 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3481 ops->ooboffs, len);
3482 BUG_ON(ret);
3483 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003484
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003485 default:
3486 BUG();
3487 }
3488 return NULL;
3489}
3490
3491/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003492 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3493 * @mtd: MTD device structure
3494 * @retry_mode: the retry mode to use
3495 *
3496 * Some vendors supply a special command to shift the Vt threshold, to be used
3497 * when there are too many bitflips in a page (i.e., ECC error). After setting
3498 * a new threshold, the host should retry reading the page.
3499 */
3500static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3501{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003502 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08003503
3504 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3505
3506 if (retry_mode >= chip->read_retries)
3507 return -EINVAL;
3508
3509 if (!chip->setup_read_retry)
3510 return -EOPNOTSUPP;
3511
3512 return chip->setup_read_retry(mtd, retry_mode);
3513}
3514
3515/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003516 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003517 * @mtd: MTD device structure
3518 * @from: offset to read from
3519 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003520 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003521 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003522 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003523static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3524 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003525{
Brian Norrise47f3db2012-05-02 10:14:56 -07003526 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003527 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003528 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003529 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003530 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003531 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003532
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003533 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003534 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003535 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003536 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003537 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003539 chipnr = (int)(from >> chip->chip_shift);
3540 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003542 realpage = (int)(from >> chip->page_shift);
3543 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003545 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003547 buf = ops->datbuf;
3548 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003549 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003550
Florian Fainellif8ac0412010-09-07 13:23:43 +02003551 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003552 unsigned int ecc_failures = mtd->ecc_stats.failed;
3553
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003554 bytes = min(mtd->writesize - col, readlen);
3555 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003556
Kamal Dasu66507c72014-05-01 20:51:19 -04003557 if (!aligned)
3558 use_bufpoi = 1;
3559 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003560 use_bufpoi = !virt_addr_valid(buf) ||
3561 !IS_ALIGNED((unsigned long)buf,
3562 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003563 else
3564 use_bufpoi = 0;
3565
Brian Norris8b6e50c2011-05-25 14:59:01 -07003566 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003567 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003568 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003569
3570 if (use_bufpoi && aligned)
3571 pr_debug("%s: using read bounce buffer for buf@%p\n",
3572 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Brian Norrisba84fb52014-01-03 15:13:33 -08003574read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003575 /*
3576 * Now read the page into the buffer. Absent an error,
3577 * the read methods return max bitflips per ecc step.
3578 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003579 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07003580 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003581 oob_required,
3582 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003583 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3584 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003585 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003586 col, bytes, bufpoi,
3587 page);
David Woodhouse956e9442006-09-25 17:12:39 +01003588 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07003589 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003590 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003591 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003592 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003593 /* Invalidate page cache */
3594 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003595 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003596 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003597
3598 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003599 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003600 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003601 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003602 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003603 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003604 chip->pagebuf_bitflips = ret;
3605 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003606 /* Invalidate page cache */
3607 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003608 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003609 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003611
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003612 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003613 int toread = min(oobreadlen, max_oobsize);
3614
3615 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003616 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003617 oob, ops, toread);
3618 oobreadlen -= toread;
3619 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003620 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003621
3622 if (chip->options & NAND_NEED_READRDY) {
3623 /* Apply delay or wait for ready/busy pin */
3624 if (!chip->dev_ready)
3625 udelay(chip->chip_delay);
3626 else
3627 nand_wait_ready(mtd);
3628 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08003629
Brian Norrisba84fb52014-01-03 15:13:33 -08003630 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003631 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003632 retry_mode++;
3633 ret = nand_setup_read_retry(mtd,
3634 retry_mode);
3635 if (ret < 0)
3636 break;
3637
3638 /* Reset failures; retry */
3639 mtd->ecc_stats.failed = ecc_failures;
3640 goto read_retry;
3641 } else {
3642 /* No more retry modes; real failure */
3643 ecc_fail = true;
3644 }
3645 }
3646
3647 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003648 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003649 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003650 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003651 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003652 max_bitflips = max_t(unsigned int, max_bitflips,
3653 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003656 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003657
Brian Norrisba84fb52014-01-03 15:13:33 -08003658 /* Reset to retry mode 0 */
3659 if (retry_mode) {
3660 ret = nand_setup_read_retry(mtd, 0);
3661 if (ret < 0)
3662 break;
3663 retry_mode = 0;
3664 }
3665
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003666 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003667 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Brian Norris8b6e50c2011-05-25 14:59:01 -07003669 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 col = 0;
3671 /* Increment page address */
3672 realpage++;
3673
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003674 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 /* Check, if we cross a chip boundary */
3676 if (!page) {
3677 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003678 chip->select_chip(mtd, -1);
3679 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003682 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003684 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003685 if (oob)
3686 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Mike Dunn3f91e942012-04-25 12:06:09 -07003688 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003689 return ret;
3690
Brian Norrisb72f3df2013-12-03 11:04:14 -08003691 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003692 return -EBADMSG;
3693
Mike Dunnedbc45402012-04-25 12:06:11 -07003694 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003695}
3696
3697/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003698 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003699 * @mtd: mtd info structure
3700 * @chip: nand chip info structure
3701 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003702 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003703int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003704{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003705 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003706}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003707EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003708
3709/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003710 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003711 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003712 * @mtd: mtd info structure
3713 * @chip: nand chip info structure
3714 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003715 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003716int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3717 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003718{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003719 int length = mtd->oobsize;
3720 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3721 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003722 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003723 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003724
Boris Brezillon97d90da2017-11-30 18:01:29 +01003725 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3726 if (ret)
3727 return ret;
3728
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003729 for (i = 0; i < chip->ecc.steps; i++) {
3730 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003731 int ret;
3732
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003733 pos = eccsize + i * (eccsize + chunk);
3734 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003735 ret = nand_change_read_column_op(chip, pos,
3736 NULL, 0,
3737 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003738 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003739 ret = nand_read_page_op(chip, page, pos, NULL,
3740 0);
3741
3742 if (ret)
3743 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003744 } else
3745 sndrnd = 1;
3746 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003747
3748 ret = nand_read_data_op(chip, bufpoi, toread, false);
3749 if (ret)
3750 return ret;
3751
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003752 bufpoi += toread;
3753 length -= toread;
3754 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003755 if (length > 0) {
3756 ret = nand_read_data_op(chip, bufpoi, length, false);
3757 if (ret)
3758 return ret;
3759 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003760
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003761 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003762}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003763EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003764
3765/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003766 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003767 * @mtd: mtd info structure
3768 * @chip: nand chip info structure
3769 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003770 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003771int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003772{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003773 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3774 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003775}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003776EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003777
3778/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003779 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003780 * with syndrome - only for large page flash
3781 * @mtd: mtd info structure
3782 * @chip: nand chip info structure
3783 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003784 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003785int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3786 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003787{
3788 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3789 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003790 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003791 const uint8_t *bufpoi = chip->oob_poi;
3792
3793 /*
3794 * data-ecc-data-ecc ... ecc-oob
3795 * or
3796 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3797 */
3798 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3799 pos = steps * (eccsize + chunk);
3800 steps = 0;
3801 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003802 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003803
Boris Brezillon97d90da2017-11-30 18:01:29 +01003804 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3805 if (ret)
3806 return ret;
3807
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003808 for (i = 0; i < steps; i++) {
3809 if (sndcmd) {
3810 if (mtd->writesize <= 512) {
3811 uint32_t fill = 0xFFFFFFFF;
3812
3813 len = eccsize;
3814 while (len > 0) {
3815 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003816
3817 ret = nand_write_data_op(chip, &fill,
3818 num, false);
3819 if (ret)
3820 return ret;
3821
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003822 len -= num;
3823 }
3824 } else {
3825 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003826 ret = nand_change_write_column_op(chip, pos,
3827 NULL, 0,
3828 false);
3829 if (ret)
3830 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003831 }
3832 } else
3833 sndcmd = 1;
3834 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003835
3836 ret = nand_write_data_op(chip, bufpoi, len, false);
3837 if (ret)
3838 return ret;
3839
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003840 bufpoi += len;
3841 length -= len;
3842 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003843 if (length > 0) {
3844 ret = nand_write_data_op(chip, bufpoi, length, false);
3845 if (ret)
3846 return ret;
3847 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003848
Boris Brezillon97d90da2017-11-30 18:01:29 +01003849 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003850}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003851EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003852
3853/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003854 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003855 * @mtd: MTD device structure
3856 * @from: offset to read from
3857 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003859 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003861static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3862 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003864 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003865 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003866 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003867 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003868 int readlen = ops->ooblen;
3869 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003870 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003871 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Brian Norris289c0522011-07-19 10:06:09 -07003873 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303874 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Brian Norris041e4572011-06-23 16:45:24 -07003876 stats = mtd->ecc_stats;
3877
Boris BREZILLON29f10582016-03-07 10:46:52 +01003878 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003879
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003880 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003881 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003883 /* Shift to get page */
3884 realpage = (int)(from >> chip->page_shift);
3885 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Florian Fainellif8ac0412010-09-07 13:23:43 +02003887 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003888 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003889 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003890 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003891 ret = chip->ecc.read_oob(mtd, chip, page);
3892
3893 if (ret < 0)
3894 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003895
3896 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003897 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003898
Brian Norris5bc7c332013-03-13 09:51:31 -07003899 if (chip->options & NAND_NEED_READRDY) {
3900 /* Apply delay or wait for ready/busy pin */
3901 if (!chip->dev_ready)
3902 udelay(chip->chip_delay);
3903 else
3904 nand_wait_ready(mtd);
3905 }
3906
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003907 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3908
Vitaly Wool70145682006-11-03 18:20:38 +03003909 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003910 if (!readlen)
3911 break;
3912
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003913 /* Increment page address */
3914 realpage++;
3915
3916 page = realpage & chip->pagemask;
3917 /* Check, if we cross a chip boundary */
3918 if (!page) {
3919 chipnr++;
3920 chip->select_chip(mtd, -1);
3921 chip->select_chip(mtd, chipnr);
3922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003924 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003926 ops->oobretlen = ops->ooblen - readlen;
3927
3928 if (ret < 0)
3929 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003930
3931 if (mtd->ecc_stats.failed - stats.failed)
3932 return -EBADMSG;
3933
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003934 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935}
3936
3937/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003938 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003939 * @mtd: MTD device structure
3940 * @from: offset to read from
3941 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003943 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003945static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3946 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003948 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003949
3950 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003952 if (ops->mode != MTD_OPS_PLACE_OOB &&
3953 ops->mode != MTD_OPS_AUTO_OOB &&
3954 ops->mode != MTD_OPS_RAW)
3955 return -ENOTSUPP;
3956
Huang Shijie6a8214a2012-11-19 14:43:30 +08003957 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003959 if (!ops->datbuf)
3960 ret = nand_do_read_oob(mtd, from, ops);
3961 else
3962 ret = nand_do_read_ops(mtd, from, ops);
3963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003965 return ret;
3966}
3967
3968
3969/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003970 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003971 * @mtd: mtd info structure
3972 * @chip: nand chip info structure
3973 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003974 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003975 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003976 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003977 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003978 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003979int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
3980 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003981{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003982 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003983
Boris Brezillon25f815f2017-11-30 18:01:30 +01003984 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003985 if (ret)
3986 return ret;
3987
3988 if (oob_required) {
3989 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3990 false);
3991 if (ret)
3992 return ret;
3993 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003994
Boris Brezillon25f815f2017-11-30 18:01:30 +01003995 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003997EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003999/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004000 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004001 * @mtd: mtd info structure
4002 * @chip: nand chip info structure
4003 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004004 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004005 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004006 *
4007 * We need a special oob layout and handling even when ECC isn't checked.
4008 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004009static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004010 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004011 const uint8_t *buf, int oob_required,
4012 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004013{
4014 int eccsize = chip->ecc.size;
4015 int eccbytes = chip->ecc.bytes;
4016 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004017 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004018
Boris Brezillon25f815f2017-11-30 18:01:30 +01004019 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4020 if (ret)
4021 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004022
4023 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004024 ret = nand_write_data_op(chip, buf, eccsize, false);
4025 if (ret)
4026 return ret;
4027
David Brownell52ff49d2009-03-04 12:01:36 -08004028 buf += eccsize;
4029
4030 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004031 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4032 false);
4033 if (ret)
4034 return ret;
4035
David Brownell52ff49d2009-03-04 12:01:36 -08004036 oob += chip->ecc.prepad;
4037 }
4038
Boris Brezillon97d90da2017-11-30 18:01:29 +01004039 ret = nand_write_data_op(chip, oob, eccbytes, false);
4040 if (ret)
4041 return ret;
4042
David Brownell52ff49d2009-03-04 12:01:36 -08004043 oob += eccbytes;
4044
4045 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004046 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4047 false);
4048 if (ret)
4049 return ret;
4050
David Brownell52ff49d2009-03-04 12:01:36 -08004051 oob += chip->ecc.postpad;
4052 }
4053 }
4054
4055 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004056 if (size) {
4057 ret = nand_write_data_op(chip, oob, size, false);
4058 if (ret)
4059 return ret;
4060 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004061
Boris Brezillon25f815f2017-11-30 18:01:30 +01004062 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004063}
4064/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004065 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004066 * @mtd: mtd info structure
4067 * @chip: nand chip info structure
4068 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004069 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004070 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004071 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004072static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004073 const uint8_t *buf, int oob_required,
4074 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004075{
Boris Brezillon846031d2016-02-03 20:11:00 +01004076 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004077 int eccbytes = chip->ecc.bytes;
4078 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004079 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004080 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004081
Brian Norris7854d3f2011-06-23 14:12:08 -07004082 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004083 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
4084 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004085
Boris Brezillon846031d2016-02-03 20:11:00 +01004086 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4087 chip->ecc.total);
4088 if (ret)
4089 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004090
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004091 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004092}
4093
4094/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004095 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004096 * @mtd: mtd info structure
4097 * @chip: nand chip info structure
4098 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004099 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004100 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004101 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004102static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004103 const uint8_t *buf, int oob_required,
4104 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004105{
Boris Brezillon846031d2016-02-03 20:11:00 +01004106 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004107 int eccbytes = chip->ecc.bytes;
4108 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004109 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004110 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004111
Boris Brezillon25f815f2017-11-30 18:01:30 +01004112 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4113 if (ret)
4114 return ret;
4115
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004116 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
4117 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004118
4119 ret = nand_write_data_op(chip, p, eccsize, false);
4120 if (ret)
4121 return ret;
4122
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004123 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
4124 }
4125
Boris Brezillon846031d2016-02-03 20:11:00 +01004126 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4127 chip->ecc.total);
4128 if (ret)
4129 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004130
Boris Brezillon97d90da2017-11-30 18:01:29 +01004131 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4132 if (ret)
4133 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004134
Boris Brezillon25f815f2017-11-30 18:01:30 +01004135 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004136}
4137
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304138
4139/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004140 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304141 * @mtd: mtd info structure
4142 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004143 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304144 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004145 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304146 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004147 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304148 */
4149static int nand_write_subpage_hwecc(struct mtd_info *mtd,
4150 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07004151 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004152 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304153{
4154 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004155 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304156 int ecc_size = chip->ecc.size;
4157 int ecc_bytes = chip->ecc.bytes;
4158 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304159 uint32_t start_step = offset / ecc_size;
4160 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4161 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004162 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304163
Boris Brezillon25f815f2017-11-30 18:01:30 +01004164 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4165 if (ret)
4166 return ret;
4167
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304168 for (step = 0; step < ecc_steps; step++) {
4169 /* configure controller for WRITE access */
4170 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
4171
4172 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004173 ret = nand_write_data_op(chip, buf, ecc_size, false);
4174 if (ret)
4175 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304176
4177 /* mask ECC of un-touched subpages by padding 0xFF */
4178 if ((step < start_step) || (step > end_step))
4179 memset(ecc_calc, 0xff, ecc_bytes);
4180 else
Brian Norrisd6a950802013-08-08 17:16:36 -07004181 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304182
4183 /* mask OOB of un-touched subpages by padding 0xFF */
4184 /* if oob_required, preserve OOB metadata of written subpage */
4185 if (!oob_required || (step < start_step) || (step > end_step))
4186 memset(oob_buf, 0xff, oob_bytes);
4187
Brian Norrisd6a950802013-08-08 17:16:36 -07004188 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304189 ecc_calc += ecc_bytes;
4190 oob_buf += oob_bytes;
4191 }
4192
4193 /* copy calculated ECC for whole page to chip->buffer->oob */
4194 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004195 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004196 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4197 chip->ecc.total);
4198 if (ret)
4199 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304200
4201 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004202 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4203 if (ret)
4204 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304205
Boris Brezillon25f815f2017-11-30 18:01:30 +01004206 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304207}
4208
4209
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004210/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004211 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004212 * @mtd: mtd info structure
4213 * @chip: nand chip info structure
4214 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004215 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004216 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004217 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004218 * The hw generator calculates the error syndrome automatically. Therefore we
4219 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004220 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004221static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07004222 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004223 const uint8_t *buf, int oob_required,
4224 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004225{
4226 int i, eccsize = chip->ecc.size;
4227 int eccbytes = chip->ecc.bytes;
4228 int eccsteps = chip->ecc.steps;
4229 const uint8_t *p = buf;
4230 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004231 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004232
Boris Brezillon25f815f2017-11-30 18:01:30 +01004233 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4234 if (ret)
4235 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004236
4237 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004238 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004239
4240 ret = nand_write_data_op(chip, p, eccsize, false);
4241 if (ret)
4242 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004243
4244 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004245 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4246 false);
4247 if (ret)
4248 return ret;
4249
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004250 oob += chip->ecc.prepad;
4251 }
4252
4253 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004254
4255 ret = nand_write_data_op(chip, oob, eccbytes, false);
4256 if (ret)
4257 return ret;
4258
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004259 oob += eccbytes;
4260
4261 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004262 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4263 false);
4264 if (ret)
4265 return ret;
4266
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004267 oob += chip->ecc.postpad;
4268 }
4269 }
4270
4271 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004272 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004273 if (i) {
4274 ret = nand_write_data_op(chip, oob, i, false);
4275 if (ret)
4276 return ret;
4277 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004278
Boris Brezillon25f815f2017-11-30 18:01:30 +01004279 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004280}
4281
4282/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004283 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004284 * @mtd: MTD device structure
4285 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304286 * @offset: address offset within the page
4287 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004288 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004289 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004290 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004291 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004292 */
4293static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304294 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004295 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004296{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304297 int status, subpage;
4298
4299 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4300 chip->ecc.write_subpage)
4301 subpage = offset || (data_len < mtd->writesize);
4302 else
4303 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004304
David Woodhouse956e9442006-09-25 17:12:39 +01004305 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304306 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004307 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304308 else if (subpage)
4309 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004310 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004311 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004312 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
4313 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004314
4315 if (status < 0)
4316 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004317
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004318 return 0;
4319}
4320
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004321/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004322 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004323 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004324 * @oob: oob data buffer
4325 * @len: oob data write length
4326 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004327 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004328static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4329 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004330{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004331 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004332 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004333
4334 /*
4335 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4336 * data from a previous OOB read.
4337 */
4338 memset(chip->oob_poi, 0xff, mtd->oobsize);
4339
Florian Fainellif8ac0412010-09-07 13:23:43 +02004340 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004341
Brian Norris0612b9d2011-08-30 18:45:40 -07004342 case MTD_OPS_PLACE_OOB:
4343 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004344 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4345 return oob + len;
4346
Boris Brezillon846031d2016-02-03 20:11:00 +01004347 case MTD_OPS_AUTO_OOB:
4348 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4349 ops->ooboffs, len);
4350 BUG_ON(ret);
4351 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004352
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004353 default:
4354 BUG();
4355 }
4356 return NULL;
4357}
4358
Florian Fainellif8ac0412010-09-07 13:23:43 +02004359#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004360
4361/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004362 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004363 * @mtd: MTD device structure
4364 * @to: offset to write to
4365 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004366 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004367 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004368 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004369static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4370 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004371{
Corentin Labbe73600b62017-09-02 10:49:38 +02004372 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004373 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004374 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004375
4376 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004377 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004378
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004379 uint8_t *oob = ops->oobbuf;
4380 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304381 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004382 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004383
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004384 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004385 if (!writelen)
4386 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004387
Brian Norris8b6e50c2011-05-25 14:59:01 -07004388 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004389 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004390 pr_notice("%s: attempt to write non page aligned data\n",
4391 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004392 return -EINVAL;
4393 }
4394
Thomas Gleixner29072b92006-09-28 15:38:36 +02004395 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004396
Thomas Gleixner6a930962006-06-28 00:11:45 +02004397 chipnr = (int)(to >> chip->chip_shift);
4398 chip->select_chip(mtd, chipnr);
4399
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004400 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004401 if (nand_check_wp(mtd)) {
4402 ret = -EIO;
4403 goto err_out;
4404 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004405
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004406 realpage = (int)(to >> chip->page_shift);
4407 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004408
4409 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004410 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4411 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004412 chip->pagebuf = -1;
4413
Maxim Levitsky782ce792010-02-22 20:39:36 +02004414 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004415 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4416 ret = -EINVAL;
4417 goto err_out;
4418 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004419
Florian Fainellif8ac0412010-09-07 13:23:43 +02004420 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004421 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004422 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004423 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004424 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004425
Kamal Dasu66507c72014-05-01 20:51:19 -04004426 if (part_pagewr)
4427 use_bufpoi = 1;
4428 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004429 use_bufpoi = !virt_addr_valid(buf) ||
4430 !IS_ALIGNED((unsigned long)buf,
4431 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004432 else
4433 use_bufpoi = 0;
4434
4435 /* Partial page write?, or need to use bounce buffer */
4436 if (use_bufpoi) {
4437 pr_debug("%s: using write bounce buffer for buf@%p\n",
4438 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004439 if (part_pagewr)
4440 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004441 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004442 memset(chip->data_buf, 0xff, mtd->writesize);
4443 memcpy(&chip->data_buf[column], buf, bytes);
4444 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004445 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004446
Maxim Levitsky782ce792010-02-22 20:39:36 +02004447 if (unlikely(oob)) {
4448 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004449 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004450 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004451 } else {
4452 /* We still need to erase leftover OOB data */
4453 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004454 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004455
4456 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004457 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004458 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004459 if (ret)
4460 break;
4461
4462 writelen -= bytes;
4463 if (!writelen)
4464 break;
4465
Thomas Gleixner29072b92006-09-28 15:38:36 +02004466 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004467 buf += bytes;
4468 realpage++;
4469
4470 page = realpage & chip->pagemask;
4471 /* Check, if we cross a chip boundary */
4472 if (!page) {
4473 chipnr++;
4474 chip->select_chip(mtd, -1);
4475 chip->select_chip(mtd, chipnr);
4476 }
4477 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004478
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004479 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004480 if (unlikely(oob))
4481 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004482
4483err_out:
4484 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004485 return ret;
4486}
4487
4488/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004489 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004490 * @mtd: MTD device structure
4491 * @to: offset to write to
4492 * @len: number of bytes to write
4493 * @retlen: pointer to variable to store the number of written bytes
4494 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004495 *
4496 * NAND write with ECC. Used when performing writes in interrupt context, this
4497 * may for example be called by mtdoops when writing an oops while in panic.
4498 */
4499static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4500 size_t *retlen, const uint8_t *buf)
4501{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004502 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004503 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004504 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004505 int ret;
4506
Brian Norris8b6e50c2011-05-25 14:59:01 -07004507 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004508 panic_nand_get_device(chip, mtd, FL_WRITING);
4509
Brent Taylor30863e382017-10-30 22:32:45 -05004510 chip->select_chip(mtd, chipnr);
4511
4512 /* Wait for the device to get ready */
4513 panic_nand_wait(mtd, chip, 400);
4514
Brian Norris0ec56dc2015-02-28 02:02:30 -08004515 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004516 ops.len = len;
4517 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004518 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004519
Brian Norris4a89ff82011-08-30 18:45:45 -07004520 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004521
Brian Norris4a89ff82011-08-30 18:45:45 -07004522 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004523 return ret;
4524}
4525
4526/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004527 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004528 * @mtd: MTD device structure
4529 * @to: offset to write to
4530 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004531 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004532 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004533 */
4534static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4535 struct mtd_oob_ops *ops)
4536{
Adrian Hunter03736152007-01-31 17:58:29 +02004537 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004538 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Brian Norris289c0522011-07-19 10:06:09 -07004540 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304541 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Boris BREZILLON29f10582016-03-07 10:46:52 +01004543 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004544
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004546 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004547 pr_debug("%s: attempt to write past end of page\n",
4548 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 return -EINVAL;
4550 }
4551
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004552 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004553
4554 /*
4555 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4556 * of my DiskOnChip 2000 test units) will clear the whole data page too
4557 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4558 * it in the doc2000 driver in August 1999. dwmw2.
4559 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004560 nand_reset(chip, chipnr);
4561
4562 chip->select_chip(mtd, chipnr);
4563
4564 /* Shift to get page */
4565 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
4567 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004568 if (nand_check_wp(mtd)) {
4569 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004570 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004571 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004574 if (page == chip->pagebuf)
4575 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004577 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004578
Brian Norris0612b9d2011-08-30 18:45:40 -07004579 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07004580 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
4581 else
4582 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004583
Huang Shijieb0bb6902012-11-19 14:43:29 +08004584 chip->select_chip(mtd, -1);
4585
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004586 if (status)
4587 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
Vitaly Wool70145682006-11-03 18:20:38 +03004589 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004591 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004592}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004594/**
4595 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004596 * @mtd: MTD device structure
4597 * @to: offset to write to
4598 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004599 */
4600static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4601 struct mtd_oob_ops *ops)
4602{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004603 int ret = -ENOTSUPP;
4604
4605 ops->retlen = 0;
4606
Huang Shijie6a8214a2012-11-19 14:43:30 +08004607 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004608
Florian Fainellif8ac0412010-09-07 13:23:43 +02004609 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004610 case MTD_OPS_PLACE_OOB:
4611 case MTD_OPS_AUTO_OOB:
4612 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004613 break;
4614
4615 default:
4616 goto out;
4617 }
4618
4619 if (!ops->datbuf)
4620 ret = nand_do_write_oob(mtd, to, ops);
4621 else
4622 ret = nand_do_write_ops(mtd, to, ops);
4623
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004624out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004625 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 return ret;
4627}
4628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629/**
Brian Norris49c50b92014-05-06 16:02:19 -07004630 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004631 * @mtd: MTD device structure
4632 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 *
Brian Norris49c50b92014-05-06 16:02:19 -07004634 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 */
Brian Norris49c50b92014-05-06 16:02:19 -07004636static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004638 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004639 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004640
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004642 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004643
Boris Brezillon97d90da2017-11-30 18:01:29 +01004644 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
4646
4647/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004649 * @mtd: MTD device structure
4650 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004652 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004654static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655{
David Woodhousee0c7d762006-05-13 18:07:53 +01004656 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004658
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004660 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004661 * @mtd: MTD device structure
4662 * @instr: erase instruction
4663 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004665 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004667int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4668 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669{
Adrian Hunter69423d92008-12-10 13:37:21 +00004670 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004671 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00004672 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Brian Norris289c0522011-07-19 10:06:09 -07004674 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4675 __func__, (unsigned long long)instr->addr,
4676 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304678 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004682 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
4684 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004685 page = (int)(instr->addr >> chip->page_shift);
4686 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
4688 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004689 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
4691 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004692 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 /* Check, if it is write protected */
4695 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004696 pr_debug("%s: device is write protected!\n",
4697 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 instr->state = MTD_ERASE_FAILED;
4699 goto erase_exit;
4700 }
4701
4702 /* Loop through the pages */
4703 len = instr->len;
4704
4705 instr->state = MTD_ERASING;
4706
4707 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004708 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004709 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304710 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004711 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4712 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 instr->state = MTD_ERASE_FAILED;
4714 goto erase_exit;
4715 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004716
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004717 /*
4718 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004719 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004720 */
4721 if (page <= chip->pagebuf && chip->pagebuf <
4722 (page + pages_per_block))
4723 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
Brian Norris49c50b92014-05-06 16:02:19 -07004725 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
4727 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004728 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004729 pr_debug("%s: failed erase, page 0x%08x\n",
4730 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00004732 instr->fail_addr =
4733 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 goto erase_exit;
4735 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004738 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 page += pages_per_block;
4740
4741 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004742 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004744 chip->select_chip(mtd, -1);
4745 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 }
4747 }
4748 instr->state = MTD_ERASE_DONE;
4749
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004750erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
4752 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
4754 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004755 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 nand_release_device(mtd);
4757
David Woodhouse49defc02007-10-06 15:01:59 -04004758 /* Do call back function */
4759 if (!ret)
4760 mtd_erase_callback(instr);
4761
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 /* Return more or less happy */
4763 return ret;
4764}
4765
4766/**
4767 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004768 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004770 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004772static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773{
Brian Norris289c0522011-07-19 10:06:09 -07004774 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
4776 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004777 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004779 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
4781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004783 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004784 * @mtd: MTD device structure
4785 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004787static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304789 struct nand_chip *chip = mtd_to_nand(mtd);
4790 int chipnr = (int)(offs >> chip->chip_shift);
4791 int ret;
4792
4793 /* Select the NAND device */
4794 nand_get_device(mtd, FL_READING);
4795 chip->select_chip(mtd, chipnr);
4796
4797 ret = nand_block_checkbad(mtd, offs, 0);
4798
4799 chip->select_chip(mtd, -1);
4800 nand_release_device(mtd);
4801
4802 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803}
4804
4805/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004806 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004807 * @mtd: MTD device structure
4808 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004810static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 int ret;
4813
Florian Fainellif8ac0412010-09-07 13:23:43 +02004814 ret = nand_block_isbad(mtd, ofs);
4815 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004816 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 if (ret > 0)
4818 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004819 return ret;
4820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Brian Norris5a0edb22013-07-30 17:52:58 -07004822 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
4825/**
Zach Brown56718422017-01-10 13:30:20 -06004826 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4827 * @mtd: MTD device structure
4828 * @ofs: offset relative to mtd start
4829 * @len: length of mtd
4830 */
4831static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4832{
4833 struct nand_chip *chip = mtd_to_nand(mtd);
4834 u32 part_start_block;
4835 u32 part_end_block;
4836 u32 part_start_die;
4837 u32 part_end_die;
4838
4839 /*
4840 * max_bb_per_die and blocks_per_die used to determine
4841 * the maximum bad block count.
4842 */
4843 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4844 return -ENOTSUPP;
4845
4846 /* Get the start and end of the partition in erase blocks. */
4847 part_start_block = mtd_div_by_eb(ofs, mtd);
4848 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4849
4850 /* Get the start and end LUNs of the partition. */
4851 part_start_die = part_start_block / chip->blocks_per_die;
4852 part_end_die = part_end_block / chip->blocks_per_die;
4853
4854 /*
4855 * Look up the bad blocks per unit and multiply by the number of units
4856 * that the partition spans.
4857 */
4858 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4859}
4860
4861/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004862 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004863 * @mtd: MTD device structure
4864 * @chip: nand chip info structure
4865 * @addr: feature address.
4866 * @subfeature_param: the subfeature parameters, a four bytes array.
4867 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004868static int nand_default_set_features(struct mtd_info *mtd,
4869 struct nand_chip *chip, int addr,
4870 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004871{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004872 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004873}
4874
4875/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004876 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004877 * @mtd: MTD device structure
4878 * @chip: nand chip info structure
4879 * @addr: feature address.
4880 * @subfeature_param: the subfeature parameters, a four bytes array.
4881 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004882static int nand_default_get_features(struct mtd_info *mtd,
4883 struct nand_chip *chip, int addr,
4884 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004885{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004886 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004887}
4888
4889/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004890 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004891 * @mtd: MTD device structure
4892 * @chip: nand chip info structure
4893 * @addr: feature address.
4894 * @subfeature_param: the subfeature parameters, a four bytes array.
4895 *
4896 * Should be used by NAND controller drivers that do not support the SET/GET
4897 * FEATURES operations.
4898 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004899int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4900 int addr, u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004901{
4902 return -ENOTSUPP;
4903}
Miquel Raynalb9587582018-03-19 14:47:19 +01004904EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004905
4906/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004907 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004908 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004909 */
4910static int nand_suspend(struct mtd_info *mtd)
4911{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004912 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004913}
4914
4915/**
4916 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004917 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004918 */
4919static void nand_resume(struct mtd_info *mtd)
4920{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004921 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004922
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004923 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004924 nand_release_device(mtd);
4925 else
Brian Norrisd0370212011-07-19 10:06:08 -07004926 pr_err("%s called for a chip which is not in suspended state\n",
4927 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004928}
4929
Scott Branden72ea4032014-11-20 11:18:05 -08004930/**
4931 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4932 * prevent further operations
4933 * @mtd: MTD device structure
4934 */
4935static void nand_shutdown(struct mtd_info *mtd)
4936{
Brian Norris9ca641b2015-11-09 16:37:28 -08004937 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004938}
4939
Brian Norris8b6e50c2011-05-25 14:59:01 -07004940/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004941static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004942{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004943 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004946 if (!chip->chip_delay)
4947 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
4949 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004950 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004951 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004954 if (chip->waitfunc == NULL)
4955 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004957 if (!chip->select_chip)
4958 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004959
Huang Shijie4204ccc2013-08-16 10:10:07 +08004960 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004961 if (!chip->set_features)
4962 chip->set_features = nand_default_set_features;
4963 if (!chip->get_features)
4964 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004965
Brian Norris68e80782013-07-18 01:17:02 -07004966 /* If called twice, pointers that depend on busw may need to be reset */
4967 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004968 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
4969 if (!chip->read_word)
4970 chip->read_word = nand_read_word;
4971 if (!chip->block_bad)
4972 chip->block_bad = nand_block_bad;
4973 if (!chip->block_markbad)
4974 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004975 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004976 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004977 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4978 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004979 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004980 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004981 if (!chip->scan_bbt)
4982 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004983
4984 if (!chip->controller) {
4985 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02004986 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004987 }
4988
Masahiro Yamada477544c2017-03-30 17:15:05 +09004989 if (!chip->buf_align)
4990 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004991}
4992
Brian Norris8b6e50c2011-05-25 14:59:01 -07004993/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004994static void sanitize_string(uint8_t *s, size_t len)
4995{
4996 ssize_t i;
4997
Brian Norris8b6e50c2011-05-25 14:59:01 -07004998 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004999 s[len - 1] = 0;
5000
Brian Norris8b6e50c2011-05-25 14:59:01 -07005001 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005002 for (i = 0; i < len - 1; i++) {
5003 if (s[i] < ' ' || s[i] > 127)
5004 s[i] = '?';
5005 }
5006
Brian Norris8b6e50c2011-05-25 14:59:01 -07005007 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005008 strim(s);
5009}
5010
5011static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
5012{
5013 int i;
5014 while (len--) {
5015 crc ^= *p++ << 8;
5016 for (i = 0; i < 8; i++)
5017 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
5018 }
5019
5020 return crc;
5021}
5022
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005023/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005024static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
5025 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005026{
5027 struct onfi_ext_param_page *ep;
5028 struct onfi_ext_section *s;
5029 struct onfi_ext_ecc_info *ecc;
5030 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005031 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005032 int len;
5033 int i;
5034
5035 len = le16_to_cpu(p->ext_param_page_length) * 16;
5036 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005037 if (!ep)
5038 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005039
5040 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005041 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5042 if (ret)
5043 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005044
5045 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005046 ret = nand_change_read_column_op(chip,
5047 sizeof(*p) * p->num_of_param_pages,
5048 ep, len, true);
5049 if (ret)
5050 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005051
Boris Brezillon97d90da2017-11-30 18:01:29 +01005052 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005053 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5054 != le16_to_cpu(ep->crc))) {
5055 pr_debug("fail in the CRC.\n");
5056 goto ext_out;
5057 }
5058
5059 /*
5060 * Check the signature.
5061 * Do not strictly follow the ONFI spec, maybe changed in future.
5062 */
5063 if (strncmp(ep->sig, "EPPS", 4)) {
5064 pr_debug("The signature is invalid.\n");
5065 goto ext_out;
5066 }
5067
5068 /* find the ECC section. */
5069 cursor = (uint8_t *)(ep + 1);
5070 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5071 s = ep->sections + i;
5072 if (s->type == ONFI_SECTION_TYPE_2)
5073 break;
5074 cursor += s->length * 16;
5075 }
5076 if (i == ONFI_EXT_SECTION_MAX) {
5077 pr_debug("We can not find the ECC section.\n");
5078 goto ext_out;
5079 }
5080
5081 /* get the info we want. */
5082 ecc = (struct onfi_ext_ecc_info *)cursor;
5083
Brian Norris4ae7d222013-09-16 18:20:21 -07005084 if (!ecc->codeword_size) {
5085 pr_debug("Invalid codeword size\n");
5086 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005087 }
5088
Brian Norris4ae7d222013-09-16 18:20:21 -07005089 chip->ecc_strength_ds = ecc->ecc_bits;
5090 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005091 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005092
5093ext_out:
5094 kfree(ep);
5095 return ret;
5096}
5097
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005098/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005099 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005100 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005101static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005102{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005103 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005104 struct nand_onfi_params *p;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005105 char id[4];
5106 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005107
Brian Norris7854d3f2011-06-23 14:12:08 -07005108 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005109 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5110 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005111 return 0;
5112
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005113 /* ONFI chip: allocate a buffer to hold its parameter page */
5114 p = kzalloc(sizeof(*p), GFP_KERNEL);
5115 if (!p)
5116 return -ENOMEM;
5117
Boris Brezillon97d90da2017-11-30 18:01:29 +01005118 ret = nand_read_param_page_op(chip, 0, NULL, 0);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005119 if (ret) {
5120 ret = 0;
5121 goto free_onfi_param_page;
5122 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005123
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005124 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005125 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005126 if (ret) {
5127 ret = 0;
5128 goto free_onfi_param_page;
5129 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005130
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005131 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
5132 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005133 break;
5134 }
5135 }
5136
Brian Norrisc7f23a72013-08-13 10:51:55 -07005137 if (i == 3) {
5138 pr_err("Could not find valid ONFI parameter page; aborting\n");
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005139 goto free_onfi_param_page;
Brian Norrisc7f23a72013-08-13 10:51:55 -07005140 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005141
Brian Norris8b6e50c2011-05-25 14:59:01 -07005142 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005143 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08005144 if (val & (1 << 5))
Miquel Raynala97421c2018-03-19 14:47:27 +01005145 chip->parameters.onfi.version = 23;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005146 else if (val & (1 << 4))
Miquel Raynala97421c2018-03-19 14:47:27 +01005147 chip->parameters.onfi.version = 22;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005148 else if (val & (1 << 3))
Miquel Raynala97421c2018-03-19 14:47:27 +01005149 chip->parameters.onfi.version = 21;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005150 else if (val & (1 << 2))
Miquel Raynala97421c2018-03-19 14:47:27 +01005151 chip->parameters.onfi.version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005152 else if (val & (1 << 1))
Miquel Raynala97421c2018-03-19 14:47:27 +01005153 chip->parameters.onfi.version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005154
Miquel Raynala97421c2018-03-19 14:47:27 +01005155 if (!chip->parameters.onfi.version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005156 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005157 goto free_onfi_param_page;
5158 } else {
5159 ret = 1;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005160 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005161
5162 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5163 sanitize_string(p->model, sizeof(p->model));
Miquel Raynalf4531b22018-03-19 14:47:26 +01005164 strncpy(chip->parameters.model, p->model,
5165 sizeof(chip->parameters.model) - 1);
Brian Norris4355b702013-08-27 18:45:10 -07005166
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005167 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005168
5169 /*
5170 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5171 * (don't ask me who thought of this...). MTD assumes that these
5172 * dimensions will be power-of-2, so just truncate the remaining area.
5173 */
5174 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5175 mtd->erasesize *= mtd->writesize;
5176
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005177 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005178
5179 /* See erasesize comment */
5180 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005181 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005182 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005183
Zach Brown34da5f52017-01-10 13:30:21 -06005184 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5185 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5186
Miquel Raynala97421c2018-03-19 14:47:27 +01005187 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005188 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005189
Huang Shijie10c86ba2013-05-17 11:17:26 +08005190 if (p->ecc_bits != 0xff) {
5191 chip->ecc_strength_ds = p->ecc_bits;
5192 chip->ecc_step_ds = 512;
Miquel Raynala97421c2018-03-19 14:47:27 +01005193 } else if (chip->parameters.onfi.version >= 21 &&
5194 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005195
5196 /*
5197 * The nand_flash_detect_ext_param_page() uses the
5198 * Change Read Column command which maybe not supported
5199 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5200 * now. We do not replace user supplied command function.
5201 */
5202 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5203 chip->cmdfunc = nand_command_lp;
5204
5205 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005206 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005207 pr_warn("Failed to detect ONFI extended param page\n");
5208 } else {
5209 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005210 }
5211
Miquel Raynalf4531b22018-03-19 14:47:26 +01005212 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005213 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005214 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005215 bitmap_set(chip->parameters.get_feature_list,
5216 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5217 bitmap_set(chip->parameters.set_feature_list,
5218 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5219 }
Miquel Raynala97421c2018-03-19 14:47:27 +01005220 chip->parameters.onfi.tPROG = le16_to_cpu(p->t_prog);
5221 chip->parameters.onfi.tBERS = le16_to_cpu(p->t_bers);
5222 chip->parameters.onfi.tR = le16_to_cpu(p->t_r);
5223 chip->parameters.onfi.tCCS = le16_to_cpu(p->t_ccs);
5224 chip->parameters.onfi.async_timing_mode =
5225 le16_to_cpu(p->async_timing_mode);
5226 chip->parameters.onfi.vendor_revision =
5227 le16_to_cpu(p->vendor_revision);
5228 memcpy(chip->parameters.onfi.vendor, p->vendor,
5229 sizeof(p->vendor));
Miquel Raynalf4531b22018-03-19 14:47:26 +01005230
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005231free_onfi_param_page:
5232 kfree(p);
5233 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005234}
5235
5236/*
Huang Shijie91361812014-02-21 13:39:40 +08005237 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5238 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005239static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005240{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005241 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005242 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005243 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005244 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005245 char id[5];
5246 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005247
5248 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005249 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5250 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005251 return 0;
5252
Miquel Raynal480139d2018-03-19 14:47:30 +01005253 /* JEDEC chip: allocate a buffer to hold its parameter page */
5254 p = kzalloc(sizeof(*p), GFP_KERNEL);
5255 if (!p)
5256 return -ENOMEM;
5257
Boris Brezillon97d90da2017-11-30 18:01:29 +01005258 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005259 if (ret) {
5260 ret = 0;
5261 goto free_jedec_param_page;
5262 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005263
Huang Shijie91361812014-02-21 13:39:40 +08005264 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005265 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005266 if (ret) {
5267 ret = 0;
5268 goto free_jedec_param_page;
5269 }
Huang Shijie91361812014-02-21 13:39:40 +08005270
5271 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5272 le16_to_cpu(p->crc))
5273 break;
5274 }
5275
5276 if (i == 3) {
5277 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005278 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005279 }
5280
5281 /* Check version */
5282 val = le16_to_cpu(p->revision);
5283 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005284 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005285 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005286 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005287
Miquel Raynal480139d2018-03-19 14:47:30 +01005288 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005289 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005290 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005291 }
5292
5293 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5294 sanitize_string(p->model, sizeof(p->model));
Miquel Raynalf4531b22018-03-19 14:47:26 +01005295 strncpy(chip->parameters.model, p->model,
5296 sizeof(chip->parameters.model) - 1);
Huang Shijie91361812014-02-21 13:39:40 +08005297
5298 mtd->writesize = le32_to_cpu(p->byte_per_page);
5299
5300 /* Please reference to the comment for nand_flash_detect_onfi. */
5301 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5302 mtd->erasesize *= mtd->writesize;
5303
5304 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5305
5306 /* Please reference to the comment for nand_flash_detect_onfi. */
5307 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5308 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5309 chip->bits_per_cell = p->bits_per_cell;
5310
Miquel Raynal480139d2018-03-19 14:47:30 +01005311 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005312 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005313
5314 /* ECC info */
5315 ecc = &p->ecc_info[0];
5316
5317 if (ecc->codeword_size >= 9) {
5318 chip->ecc_strength_ds = ecc->ecc_bits;
5319 chip->ecc_step_ds = 1 << ecc->codeword_size;
5320 } else {
5321 pr_warn("Invalid codeword size\n");
5322 }
5323
Miquel Raynal480139d2018-03-19 14:47:30 +01005324free_jedec_param_page:
5325 kfree(p);
5326 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005327}
5328
5329/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005330 * nand_id_has_period - Check if an ID string has a given wraparound period
5331 * @id_data: the ID string
5332 * @arrlen: the length of the @id_data array
5333 * @period: the period of repitition
5334 *
5335 * Check if an ID string is repeated within a given sequence of bytes at
5336 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005337 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005338 * if the repetition has a period of @period; otherwise, returns zero.
5339 */
5340static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5341{
5342 int i, j;
5343 for (i = 0; i < period; i++)
5344 for (j = i + period; j < arrlen; j += period)
5345 if (id_data[i] != id_data[j])
5346 return 0;
5347 return 1;
5348}
5349
5350/*
5351 * nand_id_len - Get the length of an ID string returned by CMD_READID
5352 * @id_data: the ID string
5353 * @arrlen: the length of the @id_data array
5354
5355 * Returns the length of the ID string, according to known wraparound/trailing
5356 * zero patterns. If no pattern exists, returns the length of the array.
5357 */
5358static int nand_id_len(u8 *id_data, int arrlen)
5359{
5360 int last_nonzero, period;
5361
5362 /* Find last non-zero byte */
5363 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5364 if (id_data[last_nonzero])
5365 break;
5366
5367 /* All zeros */
5368 if (last_nonzero < 0)
5369 return 0;
5370
5371 /* Calculate wraparound period */
5372 for (period = 1; period < arrlen; period++)
5373 if (nand_id_has_period(id_data, arrlen, period))
5374 break;
5375
5376 /* There's a repeated pattern */
5377 if (period < arrlen)
5378 return period;
5379
5380 /* There are trailing zeros */
5381 if (last_nonzero < arrlen - 1)
5382 return last_nonzero + 1;
5383
5384 /* No pattern detected */
5385 return arrlen;
5386}
5387
Huang Shijie7db906b2013-09-25 14:58:11 +08005388/* Extract the bits of per cell from the 3rd byte of the extended ID */
5389static int nand_get_bits_per_cell(u8 cellinfo)
5390{
5391 int bits;
5392
5393 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5394 bits >>= NAND_CI_CELLTYPE_SHIFT;
5395 return bits + 1;
5396}
5397
Brian Norrise3b88bd2012-09-24 20:40:52 -07005398/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005399 * Many new NAND share similar device ID codes, which represent the size of the
5400 * chip. The rest of the parameters must be decoded according to generic or
5401 * manufacturer-specific "extended ID" decoding patterns.
5402 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005403void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005404{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005405 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005406 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005407 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005408 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005409 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005410 /* The 4th id byte is the important one */
5411 extid = id_data[3];
5412
Boris Brezillon01389b62016-06-08 10:30:18 +02005413 /* Calc pagesize */
5414 mtd->writesize = 1024 << (extid & 0x03);
5415 extid >>= 2;
5416 /* Calc oobsize */
5417 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5418 extid >>= 2;
5419 /* Calc blocksize. Blocksize is multiples of 64KiB */
5420 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5421 extid >>= 2;
5422 /* Get buswidth information */
5423 if (extid & 0x1)
5424 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005425}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005426EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005427
5428/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005429 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5430 * decodes a matching ID table entry and assigns the MTD size parameters for
5431 * the chip.
5432 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005433static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005434{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005435 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005436
5437 mtd->erasesize = type->erasesize;
5438 mtd->writesize = type->pagesize;
5439 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005440
Huang Shijie1c195e92013-09-25 14:58:12 +08005441 /* All legacy ID NAND are small-page, SLC */
5442 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005443}
5444
5445/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005446 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5447 * heuristic patterns using various detected parameters (e.g., manufacturer,
5448 * page size, cell-type information).
5449 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005450static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005451{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005452 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005453
5454 /* Set the bad block position */
5455 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5456 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5457 else
5458 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005459}
5460
Huang Shijieec6e87e2013-03-15 11:01:00 +08005461static inline bool is_full_id_nand(struct nand_flash_dev *type)
5462{
5463 return type->id_len;
5464}
5465
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005466static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005467 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005468{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005469 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005470 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005471
Huang Shijieec6e87e2013-03-15 11:01:00 +08005472 if (!strncmp(type->id, id_data, type->id_len)) {
5473 mtd->writesize = type->pagesize;
5474 mtd->erasesize = type->erasesize;
5475 mtd->oobsize = type->oobsize;
5476
Huang Shijie7db906b2013-09-25 14:58:11 +08005477 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005478 chip->chipsize = (uint64_t)type->chipsize << 20;
5479 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005480 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5481 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005482 chip->onfi_timing_mode_default =
5483 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005484
Miquel Raynalf4531b22018-03-19 14:47:26 +01005485 strncpy(chip->parameters.model, type->name,
5486 sizeof(chip->parameters.model) - 1);
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005487
Huang Shijieec6e87e2013-03-15 11:01:00 +08005488 return true;
5489 }
5490 return false;
5491}
5492
Brian Norris7e74c2d2012-09-24 20:40:49 -07005493/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005494 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5495 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5496 * table.
5497 */
5498static void nand_manufacturer_detect(struct nand_chip *chip)
5499{
5500 /*
5501 * Try manufacturer detection if available and use
5502 * nand_decode_ext_id() otherwise.
5503 */
5504 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005505 chip->manufacturer.desc->ops->detect) {
5506 /* The 3rd id byte holds MLC / multichip data */
5507 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005508 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005509 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005510 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005511 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005512}
5513
5514/*
5515 * Manufacturer initialization. This function is called for all NANDs including
5516 * ONFI and JEDEC compliant ones.
5517 * Manufacturer drivers should put all their specific initialization code in
5518 * their ->init() hook.
5519 */
5520static int nand_manufacturer_init(struct nand_chip *chip)
5521{
5522 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5523 !chip->manufacturer.desc->ops->init)
5524 return 0;
5525
5526 return chip->manufacturer.desc->ops->init(chip);
5527}
5528
5529/*
5530 * Manufacturer cleanup. This function is called for all NANDs including
5531 * ONFI and JEDEC compliant ones.
5532 * Manufacturer drivers should put all their specific cleanup code in their
5533 * ->cleanup() hook.
5534 */
5535static void nand_manufacturer_cleanup(struct nand_chip *chip)
5536{
5537 /* Release manufacturer private data */
5538 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5539 chip->manufacturer.desc->ops->cleanup)
5540 chip->manufacturer.desc->ops->cleanup(chip);
5541}
5542
5543/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005544 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005545 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005546static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005547{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005548 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005549 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005550 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005551 u8 *id_data = chip->id.data;
5552 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
Karl Beldanef89a882008-09-15 14:37:29 +02005554 /*
5555 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005556 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005557 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005558 ret = nand_reset(chip, 0);
5559 if (ret)
5560 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005561
5562 /* Select the device */
5563 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005564
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005566 ret = nand_readid_op(chip, 0, id_data, 2);
5567 if (ret)
5568 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569
5570 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005571 maf_id = id_data[0];
5572 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
Brian Norris8b6e50c2011-05-25 14:59:01 -07005574 /*
5575 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005576 * interface concerns can cause random data which looks like a
5577 * possibly credible NAND flash to appear. If the two results do
5578 * not match, ignore the device completely.
5579 */
5580
Brian Norris4aef9b72012-09-24 20:40:48 -07005581 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005582 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5583 if (ret)
5584 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005585
Boris Brezillon7f501f02016-05-24 19:20:05 +02005586 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005587 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005588 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005589 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005590 }
5591
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005592 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005593
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005594 /* Try to identify manufacturer */
5595 manufacturer = nand_get_manufacturer(maf_id);
5596 chip->manufacturer.desc = manufacturer;
5597
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005598 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005599 type = nand_flash_ids;
5600
Boris Brezillon29a198a2016-05-24 20:17:48 +02005601 /*
5602 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5603 * override it.
5604 * This is required to make sure initial NAND bus width set by the
5605 * NAND controller driver is coherent with the real NAND bus width
5606 * (extracted by auto-detection code).
5607 */
5608 busw = chip->options & NAND_BUSWIDTH_16;
5609
5610 /*
5611 * The flag is only set (never cleared), reset it to its default value
5612 * before starting auto-detection.
5613 */
5614 chip->options &= ~NAND_BUSWIDTH_16;
5615
Huang Shijieec6e87e2013-03-15 11:01:00 +08005616 for (; type->name != NULL; type++) {
5617 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005618 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005619 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005620 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005621 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005622 }
5623 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005624
Miquel Raynala97421c2018-03-19 14:47:27 +01005625 chip->parameters.onfi.version = 0;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005626 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005627 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005628 ret = nand_flash_detect_onfi(chip);
5629 if (ret < 0)
5630 return ret;
5631 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005632 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005633
5634 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005635 ret = nand_flash_detect_jedec(chip);
5636 if (ret < 0)
5637 return ret;
5638 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005639 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005640 }
5641
David Woodhouse5e81e882010-02-26 18:32:56 +00005642 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005643 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005644
Miquel Raynalf4531b22018-03-19 14:47:26 +01005645 strncpy(chip->parameters.model, type->name,
5646 sizeof(chip->parameters.model) - 1);
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005647
Adrian Hunter69423d92008-12-10 13:37:21 +00005648 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005649
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005650 if (!type->pagesize)
5651 nand_manufacturer_detect(chip);
5652 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005653 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005654
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005655 /* Get chip options */
5656 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005657
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005658ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005659 if (!mtd->name)
5660 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005661
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005662 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005663 WARN_ON(busw & NAND_BUSWIDTH_16);
5664 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005665 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5666 /*
5667 * Check, if buswidth is correct. Hardware drivers should set
5668 * chip correct!
5669 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005670 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005671 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005672 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5673 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005674 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5675 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005676 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005677 }
5678
Boris Brezillon7f501f02016-05-24 19:20:05 +02005679 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005680
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005681 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005682 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005683 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005684 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005685
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005686 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005687 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005688 if (chip->chipsize & 0xffffffff)
5689 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005690 else {
5691 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5692 chip->chip_shift += 32 - 1;
5693 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005694
Masahiro Yamada14157f82017-09-13 11:05:50 +09005695 if (chip->chip_shift - chip->page_shift > 16)
5696 chip->options |= NAND_ROW_ADDR_3;
5697
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005698 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005699 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005700
Brian Norris8b6e50c2011-05-25 14:59:01 -07005701 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005702 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5703 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005704
Ezequiel Garcia20171642013-11-25 08:30:31 -03005705 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005706 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005707 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5708 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005709 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005710 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005711 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005712 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005713}
5714
Boris Brezillond48f62b2016-04-01 14:54:32 +02005715static const char * const nand_ecc_modes[] = {
5716 [NAND_ECC_NONE] = "none",
5717 [NAND_ECC_SOFT] = "soft",
5718 [NAND_ECC_HW] = "hw",
5719 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5720 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005721 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005722};
5723
5724static int of_get_nand_ecc_mode(struct device_node *np)
5725{
5726 const char *pm;
5727 int err, i;
5728
5729 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5730 if (err < 0)
5731 return err;
5732
5733 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5734 if (!strcasecmp(pm, nand_ecc_modes[i]))
5735 return i;
5736
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005737 /*
5738 * For backward compatibility we support few obsoleted values that don't
5739 * have their mappings into nand_ecc_modes_t anymore (they were merged
5740 * with other enums).
5741 */
5742 if (!strcasecmp(pm, "soft_bch"))
5743 return NAND_ECC_SOFT;
5744
Boris Brezillond48f62b2016-04-01 14:54:32 +02005745 return -ENODEV;
5746}
5747
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005748static const char * const nand_ecc_algos[] = {
5749 [NAND_ECC_HAMMING] = "hamming",
5750 [NAND_ECC_BCH] = "bch",
5751};
5752
Boris Brezillond48f62b2016-04-01 14:54:32 +02005753static int of_get_nand_ecc_algo(struct device_node *np)
5754{
5755 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005756 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005757
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005758 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5759 if (!err) {
5760 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5761 if (!strcasecmp(pm, nand_ecc_algos[i]))
5762 return i;
5763 return -ENODEV;
5764 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005765
5766 /*
5767 * For backward compatibility we also read "nand-ecc-mode" checking
5768 * for some obsoleted values that were specifying ECC algorithm.
5769 */
5770 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5771 if (err < 0)
5772 return err;
5773
5774 if (!strcasecmp(pm, "soft"))
5775 return NAND_ECC_HAMMING;
5776 else if (!strcasecmp(pm, "soft_bch"))
5777 return NAND_ECC_BCH;
5778
5779 return -ENODEV;
5780}
5781
5782static int of_get_nand_ecc_step_size(struct device_node *np)
5783{
5784 int ret;
5785 u32 val;
5786
5787 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5788 return ret ? ret : val;
5789}
5790
5791static int of_get_nand_ecc_strength(struct device_node *np)
5792{
5793 int ret;
5794 u32 val;
5795
5796 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5797 return ret ? ret : val;
5798}
5799
5800static int of_get_nand_bus_width(struct device_node *np)
5801{
5802 u32 val;
5803
5804 if (of_property_read_u32(np, "nand-bus-width", &val))
5805 return 8;
5806
5807 switch (val) {
5808 case 8:
5809 case 16:
5810 return val;
5811 default:
5812 return -EIO;
5813 }
5814}
5815
5816static bool of_get_nand_on_flash_bbt(struct device_node *np)
5817{
5818 return of_property_read_bool(np, "nand-on-flash-bbt");
5819}
5820
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005821static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005822{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005823 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005824 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005825
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005826 if (!dn)
5827 return 0;
5828
Brian Norris5844fee2015-01-23 00:22:27 -08005829 if (of_get_nand_bus_width(dn) == 16)
5830 chip->options |= NAND_BUSWIDTH_16;
5831
5832 if (of_get_nand_on_flash_bbt(dn))
5833 chip->bbt_options |= NAND_BBT_USE_FLASH;
5834
5835 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005836 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005837 ecc_strength = of_get_nand_ecc_strength(dn);
5838 ecc_step = of_get_nand_ecc_step_size(dn);
5839
Brian Norris5844fee2015-01-23 00:22:27 -08005840 if (ecc_mode >= 0)
5841 chip->ecc.mode = ecc_mode;
5842
Rafał Miłecki79082452016-03-23 11:19:02 +01005843 if (ecc_algo >= 0)
5844 chip->ecc.algo = ecc_algo;
5845
Brian Norris5844fee2015-01-23 00:22:27 -08005846 if (ecc_strength >= 0)
5847 chip->ecc.strength = ecc_strength;
5848
5849 if (ecc_step > 0)
5850 chip->ecc.size = ecc_step;
5851
Boris Brezillonba78ee02016-06-08 17:04:22 +02005852 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5853 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5854
Brian Norris5844fee2015-01-23 00:22:27 -08005855 return 0;
5856}
5857
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005858/**
David Woodhouse3b85c322006-09-25 17:06:53 +01005859 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005860 * @mtd: MTD device structure
5861 * @maxchips: number of chips to scan for
5862 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005863 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005864 * This is the first phase of the normal nand_scan() function. It reads the
5865 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005866 *
5867 */
David Woodhouse5e81e882010-02-26 18:32:56 +00005868int nand_scan_ident(struct mtd_info *mtd, int maxchips,
5869 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005870{
Cai Zhiyongbb770822013-12-25 20:11:15 +08005871 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01005872 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08005873 int ret;
5874
Miquel Raynal17fa8042017-11-30 18:01:31 +01005875 /* Enforce the right timings for reset/detection */
5876 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5877
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005878 ret = nand_dt_init(chip);
5879 if (ret)
5880 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005881
Brian Norrisf7a8e382016-01-05 10:39:45 -08005882 if (!mtd->name && mtd->dev.parent)
5883 mtd->name = dev_name(mtd->dev.parent);
5884
Miquel Raynal8878b122017-11-09 14:16:45 +01005885 /*
5886 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5887 * populated.
5888 */
5889 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005890 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005891 * Default functions assigned for ->cmdfunc() and
5892 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005893 */
Miquel Raynal8878b122017-11-09 14:16:45 +01005894 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5895 pr_err("->cmd_ctrl() should be provided\n");
5896 return -EINVAL;
5897 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005898 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005899
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005900 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005901 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005902
5903 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005904 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005905 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005906 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005907 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005908 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005909 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 }
5911
Boris Brezillon7f501f02016-05-24 19:20:05 +02005912 nand_maf_id = chip->id.data[0];
5913 nand_dev_id = chip->id.data[1];
5914
Huang Shijie07300162012-11-09 16:23:45 +08005915 chip->select_chip(mtd, -1);
5916
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005917 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005918 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005919 u8 id[2];
5920
Karl Beldanef89a882008-09-15 14:37:29 +02005921 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005922 nand_reset(chip, i);
5923
5924 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005926 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005928 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08005929 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 break;
Huang Shijie07300162012-11-09 16:23:45 +08005931 }
5932 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 }
5934 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005935 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005936
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005938 chip->numchips = i;
5939 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
David Woodhouse3b85c322006-09-25 17:06:53 +01005941 return 0;
5942}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005943EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01005944
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005945static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
5946{
5947 struct nand_chip *chip = mtd_to_nand(mtd);
5948 struct nand_ecc_ctrl *ecc = &chip->ecc;
5949
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005950 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005951 return -EINVAL;
5952
5953 switch (ecc->algo) {
5954 case NAND_ECC_HAMMING:
5955 ecc->calculate = nand_calculate_ecc;
5956 ecc->correct = nand_correct_data;
5957 ecc->read_page = nand_read_page_swecc;
5958 ecc->read_subpage = nand_read_subpage;
5959 ecc->write_page = nand_write_page_swecc;
5960 ecc->read_page_raw = nand_read_page_raw;
5961 ecc->write_page_raw = nand_write_page_raw;
5962 ecc->read_oob = nand_read_oob_std;
5963 ecc->write_oob = nand_write_oob_std;
5964 if (!ecc->size)
5965 ecc->size = 256;
5966 ecc->bytes = 3;
5967 ecc->strength = 1;
5968 return 0;
5969 case NAND_ECC_BCH:
5970 if (!mtd_nand_has_bch()) {
5971 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
5972 return -EINVAL;
5973 }
5974 ecc->calculate = nand_bch_calculate_ecc;
5975 ecc->correct = nand_bch_correct_data;
5976 ecc->read_page = nand_read_page_swecc;
5977 ecc->read_subpage = nand_read_subpage;
5978 ecc->write_page = nand_write_page_swecc;
5979 ecc->read_page_raw = nand_read_page_raw;
5980 ecc->write_page_raw = nand_write_page_raw;
5981 ecc->read_oob = nand_read_oob_std;
5982 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02005983
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005984 /*
5985 * Board driver should supply ecc.size and ecc.strength
5986 * values to select how many bits are correctable.
5987 * Otherwise, default to 4 bits for large page devices.
5988 */
5989 if (!ecc->size && (mtd->oobsize >= 64)) {
5990 ecc->size = 512;
5991 ecc->strength = 4;
5992 }
5993
5994 /*
5995 * if no ecc placement scheme was provided pickup the default
5996 * large page one.
5997 */
5998 if (!mtd->ooblayout) {
5999 /* handle large page devices only */
6000 if (mtd->oobsize < 64) {
6001 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6002 return -EINVAL;
6003 }
6004
6005 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02006006
6007 }
6008
6009 /*
6010 * We can only maximize ECC config when the default layout is
6011 * used, otherwise we don't know how many bytes can really be
6012 * used.
6013 */
6014 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6015 ecc->options & NAND_ECC_MAXIMIZE) {
6016 int steps, bytes;
6017
6018 /* Always prefer 1k blocks over 512bytes ones */
6019 ecc->size = 1024;
6020 steps = mtd->writesize / ecc->size;
6021
6022 /* Reserve 2 bytes for the BBM */
6023 bytes = (mtd->oobsize - 2) / steps;
6024 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006025 }
6026
6027 /* See nand_bch_init() for details. */
6028 ecc->bytes = 0;
6029 ecc->priv = nand_bch_init(mtd);
6030 if (!ecc->priv) {
6031 WARN(1, "BCH ECC initialization failed!\n");
6032 return -EINVAL;
6033 }
6034 return 0;
6035 default:
6036 WARN(1, "Unsupported ECC algorithm!\n");
6037 return -EINVAL;
6038 }
6039}
6040
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006041/**
6042 * nand_check_ecc_caps - check the sanity of preset ECC settings
6043 * @chip: nand chip info structure
6044 * @caps: ECC caps info structure
6045 * @oobavail: OOB size that the ECC engine can use
6046 *
6047 * When ECC step size and strength are already set, check if they are supported
6048 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6049 * On success, the calculated ECC bytes is set.
6050 */
6051int nand_check_ecc_caps(struct nand_chip *chip,
6052 const struct nand_ecc_caps *caps, int oobavail)
6053{
6054 struct mtd_info *mtd = nand_to_mtd(chip);
6055 const struct nand_ecc_step_info *stepinfo;
6056 int preset_step = chip->ecc.size;
6057 int preset_strength = chip->ecc.strength;
6058 int nsteps, ecc_bytes;
6059 int i, j;
6060
6061 if (WARN_ON(oobavail < 0))
6062 return -EINVAL;
6063
6064 if (!preset_step || !preset_strength)
6065 return -ENODATA;
6066
6067 nsteps = mtd->writesize / preset_step;
6068
6069 for (i = 0; i < caps->nstepinfos; i++) {
6070 stepinfo = &caps->stepinfos[i];
6071
6072 if (stepinfo->stepsize != preset_step)
6073 continue;
6074
6075 for (j = 0; j < stepinfo->nstrengths; j++) {
6076 if (stepinfo->strengths[j] != preset_strength)
6077 continue;
6078
6079 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6080 preset_strength);
6081 if (WARN_ON_ONCE(ecc_bytes < 0))
6082 return ecc_bytes;
6083
6084 if (ecc_bytes * nsteps > oobavail) {
6085 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6086 preset_step, preset_strength);
6087 return -ENOSPC;
6088 }
6089
6090 chip->ecc.bytes = ecc_bytes;
6091
6092 return 0;
6093 }
6094 }
6095
6096 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6097 preset_step, preset_strength);
6098
6099 return -ENOTSUPP;
6100}
6101EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
6102
6103/**
6104 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6105 * @chip: nand chip info structure
6106 * @caps: ECC engine caps info structure
6107 * @oobavail: OOB size that the ECC engine can use
6108 *
6109 * If a chip's ECC requirement is provided, try to meet it with the least
6110 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6111 * On success, the chosen ECC settings are set.
6112 */
6113int nand_match_ecc_req(struct nand_chip *chip,
6114 const struct nand_ecc_caps *caps, int oobavail)
6115{
6116 struct mtd_info *mtd = nand_to_mtd(chip);
6117 const struct nand_ecc_step_info *stepinfo;
6118 int req_step = chip->ecc_step_ds;
6119 int req_strength = chip->ecc_strength_ds;
6120 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6121 int best_step, best_strength, best_ecc_bytes;
6122 int best_ecc_bytes_total = INT_MAX;
6123 int i, j;
6124
6125 if (WARN_ON(oobavail < 0))
6126 return -EINVAL;
6127
6128 /* No information provided by the NAND chip */
6129 if (!req_step || !req_strength)
6130 return -ENOTSUPP;
6131
6132 /* number of correctable bits the chip requires in a page */
6133 req_corr = mtd->writesize / req_step * req_strength;
6134
6135 for (i = 0; i < caps->nstepinfos; i++) {
6136 stepinfo = &caps->stepinfos[i];
6137 step_size = stepinfo->stepsize;
6138
6139 for (j = 0; j < stepinfo->nstrengths; j++) {
6140 strength = stepinfo->strengths[j];
6141
6142 /*
6143 * If both step size and strength are smaller than the
6144 * chip's requirement, it is not easy to compare the
6145 * resulted reliability.
6146 */
6147 if (step_size < req_step && strength < req_strength)
6148 continue;
6149
6150 if (mtd->writesize % step_size)
6151 continue;
6152
6153 nsteps = mtd->writesize / step_size;
6154
6155 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6156 if (WARN_ON_ONCE(ecc_bytes < 0))
6157 continue;
6158 ecc_bytes_total = ecc_bytes * nsteps;
6159
6160 if (ecc_bytes_total > oobavail ||
6161 strength * nsteps < req_corr)
6162 continue;
6163
6164 /*
6165 * We assume the best is to meet the chip's requrement
6166 * with the least number of ECC bytes.
6167 */
6168 if (ecc_bytes_total < best_ecc_bytes_total) {
6169 best_ecc_bytes_total = ecc_bytes_total;
6170 best_step = step_size;
6171 best_strength = strength;
6172 best_ecc_bytes = ecc_bytes;
6173 }
6174 }
6175 }
6176
6177 if (best_ecc_bytes_total == INT_MAX)
6178 return -ENOTSUPP;
6179
6180 chip->ecc.size = best_step;
6181 chip->ecc.strength = best_strength;
6182 chip->ecc.bytes = best_ecc_bytes;
6183
6184 return 0;
6185}
6186EXPORT_SYMBOL_GPL(nand_match_ecc_req);
6187
6188/**
6189 * nand_maximize_ecc - choose the max ECC strength available
6190 * @chip: nand chip info structure
6191 * @caps: ECC engine caps info structure
6192 * @oobavail: OOB size that the ECC engine can use
6193 *
6194 * Choose the max ECC strength that is supported on the controller, and can fit
6195 * within the chip's OOB. On success, the chosen ECC settings are set.
6196 */
6197int nand_maximize_ecc(struct nand_chip *chip,
6198 const struct nand_ecc_caps *caps, int oobavail)
6199{
6200 struct mtd_info *mtd = nand_to_mtd(chip);
6201 const struct nand_ecc_step_info *stepinfo;
6202 int step_size, strength, nsteps, ecc_bytes, corr;
6203 int best_corr = 0;
6204 int best_step = 0;
6205 int best_strength, best_ecc_bytes;
6206 int i, j;
6207
6208 if (WARN_ON(oobavail < 0))
6209 return -EINVAL;
6210
6211 for (i = 0; i < caps->nstepinfos; i++) {
6212 stepinfo = &caps->stepinfos[i];
6213 step_size = stepinfo->stepsize;
6214
6215 /* If chip->ecc.size is already set, respect it */
6216 if (chip->ecc.size && step_size != chip->ecc.size)
6217 continue;
6218
6219 for (j = 0; j < stepinfo->nstrengths; j++) {
6220 strength = stepinfo->strengths[j];
6221
6222 if (mtd->writesize % step_size)
6223 continue;
6224
6225 nsteps = mtd->writesize / step_size;
6226
6227 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6228 if (WARN_ON_ONCE(ecc_bytes < 0))
6229 continue;
6230
6231 if (ecc_bytes * nsteps > oobavail)
6232 continue;
6233
6234 corr = strength * nsteps;
6235
6236 /*
6237 * If the number of correctable bits is the same,
6238 * bigger step_size has more reliability.
6239 */
6240 if (corr > best_corr ||
6241 (corr == best_corr && step_size > best_step)) {
6242 best_corr = corr;
6243 best_step = step_size;
6244 best_strength = strength;
6245 best_ecc_bytes = ecc_bytes;
6246 }
6247 }
6248 }
6249
6250 if (!best_corr)
6251 return -ENOTSUPP;
6252
6253 chip->ecc.size = best_step;
6254 chip->ecc.strength = best_strength;
6255 chip->ecc.bytes = best_ecc_bytes;
6256
6257 return 0;
6258}
6259EXPORT_SYMBOL_GPL(nand_maximize_ecc);
6260
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006261/*
6262 * Check if the chip configuration meet the datasheet requirements.
6263
6264 * If our configuration corrects A bits per B bytes and the minimum
6265 * required correction level is X bits per Y bytes, then we must ensure
6266 * both of the following are true:
6267 *
6268 * (1) A / B >= X / Y
6269 * (2) A >= X
6270 *
6271 * Requirement (1) ensures we can correct for the required bitflip density.
6272 * Requirement (2) ensures we can correct even when all bitflips are clumped
6273 * in the same sector.
6274 */
6275static bool nand_ecc_strength_good(struct mtd_info *mtd)
6276{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006277 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006278 struct nand_ecc_ctrl *ecc = &chip->ecc;
6279 int corr, ds_corr;
6280
6281 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6282 /* Not enough information */
6283 return true;
6284
6285 /*
6286 * We get the number of corrected bits per page to compare
6287 * the correction density.
6288 */
6289 corr = (mtd->writesize * ecc->strength) / ecc->size;
6290 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6291
6292 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6293}
David Woodhouse3b85c322006-09-25 17:06:53 +01006294
6295/**
6296 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006297 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01006298 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006299 * This is the second phase of the normal nand_scan() function. It fills out
6300 * all the uninitialized function pointers with the defaults and scans for a
6301 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006302 */
6303int nand_scan_tail(struct mtd_info *mtd)
6304{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006305 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08006306 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006307 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006308
Brian Norrise2414f42012-02-06 13:44:00 -08006309 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006310 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006311 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006312 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006313 }
Brian Norrise2414f42012-02-06 13:44:00 -08006314
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006315 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006316 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006317 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006318
Boris Brezillonf84674b2017-06-02 12:18:24 +02006319 /*
6320 * FIXME: some NAND manufacturer drivers expect the first die to be
6321 * selected when manufacturer->init() is called. They should be fixed
6322 * to explictly select the relevant die when interacting with the NAND
6323 * chip.
6324 */
6325 chip->select_chip(mtd, 0);
6326 ret = nand_manufacturer_init(chip);
6327 chip->select_chip(mtd, -1);
6328 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006329 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006330
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006331 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006332 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006333
6334 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006335 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006336 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006337 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006338 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006339 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006342 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 break;
6344 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006345 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006346 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006347 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006349 /*
6350 * Expose the whole OOB area to users if ECC_NONE
6351 * is passed. We could do that for all kind of
6352 * ->oobsize, but we must keep the old large/small
6353 * page with ECC layout when ->oobsize <= 128 for
6354 * compatibility reasons.
6355 */
6356 if (ecc->mode == NAND_ECC_NONE) {
6357 mtd_set_ooblayout(mtd,
6358 &nand_ooblayout_lp_ops);
6359 break;
6360 }
6361
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006362 WARN(1, "No oob scheme defined for oobsize %d\n",
6363 mtd->oobsize);
6364 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006365 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 }
6367 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006368
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006369 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006370 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006371 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006372 */
David Woodhouse956e9442006-09-25 17:12:39 +01006373
Huang Shijie97de79e02013-10-18 14:20:53 +08006374 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006375 case NAND_ECC_HW_OOB_FIRST:
6376 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006377 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006378 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6379 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006380 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006381 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006382 if (!ecc->read_page)
6383 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006384
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006385 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006386 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006387 if (!ecc->read_page)
6388 ecc->read_page = nand_read_page_hwecc;
6389 if (!ecc->write_page)
6390 ecc->write_page = nand_write_page_hwecc;
6391 if (!ecc->read_page_raw)
6392 ecc->read_page_raw = nand_read_page_raw;
6393 if (!ecc->write_page_raw)
6394 ecc->write_page_raw = nand_write_page_raw;
6395 if (!ecc->read_oob)
6396 ecc->read_oob = nand_read_oob_std;
6397 if (!ecc->write_oob)
6398 ecc->write_oob = nand_write_oob_std;
6399 if (!ecc->read_subpage)
6400 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006401 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006402 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006403
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006404 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006405 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6406 (!ecc->read_page ||
6407 ecc->read_page == nand_read_page_hwecc ||
6408 !ecc->write_page ||
6409 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006410 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6411 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006412 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006413 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006414 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006415 if (!ecc->read_page)
6416 ecc->read_page = nand_read_page_syndrome;
6417 if (!ecc->write_page)
6418 ecc->write_page = nand_write_page_syndrome;
6419 if (!ecc->read_page_raw)
6420 ecc->read_page_raw = nand_read_page_raw_syndrome;
6421 if (!ecc->write_page_raw)
6422 ecc->write_page_raw = nand_write_page_raw_syndrome;
6423 if (!ecc->read_oob)
6424 ecc->read_oob = nand_read_oob_syndrome;
6425 if (!ecc->write_oob)
6426 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006427
Huang Shijie97de79e02013-10-18 14:20:53 +08006428 if (mtd->writesize >= ecc->size) {
6429 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006430 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6431 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006432 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006433 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006434 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006435 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006436 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6437 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006438 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006439 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006441 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006442 ret = nand_set_ecc_soft_ops(mtd);
6443 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006444 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006445 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006446 }
6447 break;
6448
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006449 case NAND_ECC_ON_DIE:
6450 if (!ecc->read_page || !ecc->write_page) {
6451 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6452 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006453 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006454 }
6455 if (!ecc->read_oob)
6456 ecc->read_oob = nand_read_oob_std;
6457 if (!ecc->write_oob)
6458 ecc->write_oob = nand_write_oob_std;
6459 break;
6460
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006461 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006462 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006463 ecc->read_page = nand_read_page_raw;
6464 ecc->write_page = nand_write_page_raw;
6465 ecc->read_oob = nand_read_oob_std;
6466 ecc->read_page_raw = nand_read_page_raw;
6467 ecc->write_page_raw = nand_write_page_raw;
6468 ecc->write_oob = nand_write_oob_std;
6469 ecc->size = mtd->writesize;
6470 ecc->bytes = 0;
6471 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006473
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006475 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6476 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006477 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006480 if (ecc->correct || ecc->calculate) {
6481 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6482 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6483 if (!ecc->calc_buf || !ecc->code_buf) {
6484 ret = -ENOMEM;
6485 goto err_nand_manuf_cleanup;
6486 }
6487 }
6488
Brian Norris9ce244b2011-08-30 18:45:37 -07006489 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006490 if (!ecc->read_oob_raw)
6491 ecc->read_oob_raw = ecc->read_oob;
6492 if (!ecc->write_oob_raw)
6493 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006494
Boris Brezillon846031d2016-02-03 20:11:00 +01006495 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006496 mtd->ecc_strength = ecc->strength;
6497 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006498
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006499 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006500 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006501 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006502 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006503 ecc->steps = mtd->writesize / ecc->size;
6504 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006505 WARN(1, "Invalid ECC parameters\n");
6506 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006507 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006509 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006510 if (ecc->total > mtd->oobsize) {
6511 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6512 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006513 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006514 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006515
Boris Brezillon846031d2016-02-03 20:11:00 +01006516 /*
6517 * The number of bytes available for a client to place data into
6518 * the out of band area.
6519 */
6520 ret = mtd_ooblayout_count_freebytes(mtd);
6521 if (ret < 0)
6522 ret = 0;
6523
6524 mtd->oobavail = ret;
6525
6526 /* ECC sanity check: warn if it's too weak */
6527 if (!nand_ecc_strength_good(mtd))
6528 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6529 mtd->name);
6530
Brian Norris8b6e50c2011-05-25 14:59:01 -07006531 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006532 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006533 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006534 case 2:
6535 mtd->subpage_sft = 1;
6536 break;
6537 case 4:
6538 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006539 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006540 mtd->subpage_sft = 2;
6541 break;
6542 }
6543 }
6544 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6545
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006546 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006547 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006550 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006552 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306553 switch (ecc->mode) {
6554 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306555 if (chip->page_shift > 9)
6556 chip->options |= NAND_SUBPAGE_READ;
6557 break;
6558
6559 default:
6560 break;
6561 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006562
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006564 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006565 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6566 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006567 mtd->_erase = nand_erase;
6568 mtd->_point = NULL;
6569 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006570 mtd->_panic_write = panic_nand_write;
6571 mtd->_read_oob = nand_read_oob;
6572 mtd->_write_oob = nand_write_oob;
6573 mtd->_sync = nand_sync;
6574 mtd->_lock = NULL;
6575 mtd->_unlock = NULL;
6576 mtd->_suspend = nand_suspend;
6577 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006578 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006579 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006580 mtd->_block_isbad = nand_block_isbad;
6581 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006582 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006583 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006585 /*
6586 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6587 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6588 * properly set.
6589 */
6590 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006591 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592
Boris Brezillonf84674b2017-06-02 12:18:24 +02006593 /* Initialize the ->data_interface field. */
6594 ret = nand_init_data_interface(chip);
6595 if (ret)
6596 goto err_nand_manuf_cleanup;
6597
6598 /* Enter fastest possible mode on all dies. */
6599 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006600 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006601 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006602 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006603 }
6604
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006605 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006606 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608
6609 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07006610 ret = chip->scan_bbt(mtd);
6611 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006612 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006613
Brian Norris44d41822017-05-01 17:04:50 -07006614 return 0;
6615
Boris Brezillonf84674b2017-06-02 12:18:24 +02006616
6617err_nand_manuf_cleanup:
6618 nand_manufacturer_cleanup(chip);
6619
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006620err_free_buf:
6621 kfree(chip->data_buf);
6622 kfree(ecc->code_buf);
6623 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006624
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006625 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006627EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628
Brian Norris8b6e50c2011-05-25 14:59:01 -07006629/*
6630 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006631 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07006632 * to call us from in-kernel code if the core NAND support is modular.
6633 */
David Woodhouse3b85c322006-09-25 17:06:53 +01006634#ifdef MODULE
6635#define caller_is_module() (1)
6636#else
6637#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06006638 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01006639#endif
6640
6641/**
6642 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07006643 * @mtd: MTD device structure
6644 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01006645 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006646 * This fills out all the uninitialized function pointers with the defaults.
6647 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006648 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006649 */
6650int nand_scan(struct mtd_info *mtd, int maxchips)
6651{
6652 int ret;
6653
David Woodhouse5e81e882010-02-26 18:32:56 +00006654 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01006655 if (!ret)
6656 ret = nand_scan_tail(mtd);
6657 return ret;
6658}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006659EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01006660
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006662 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6663 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006664 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006665void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006667 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006668 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006669 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6670
Jesper Juhlfa671642005-11-07 01:01:27 -08006671 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006672 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006673 kfree(chip->data_buf);
6674 kfree(chip->ecc.code_buf);
6675 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006676
6677 /* Free bad block descriptor memory */
6678 if (chip->badblock_pattern && chip->badblock_pattern->options
6679 & NAND_BBT_DYNAMICSTRUCT)
6680 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006681
6682 /* Free manufacturer priv data. */
6683 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006685EXPORT_SYMBOL_GPL(nand_cleanup);
6686
6687/**
6688 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6689 * held by the NAND device
6690 * @mtd: MTD device structure
6691 */
6692void nand_release(struct mtd_info *mtd)
6693{
6694 mtd_device_unregister(mtd);
6695 nand_cleanup(mtd_to_nand(mtd));
6696}
David Woodhousee0c7d762006-05-13 18:07:53 +01006697EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006698
David Woodhousee0c7d762006-05-13 18:07:53 +01006699MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006700MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6701MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006702MODULE_DESCRIPTION("Generic NAND flash driver code");