blob: cc386ee64a1b379933434e736896f55f7932d74a [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_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700282 * @mtd: MTD device structure
283 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *
285 * Default select function for 1 chip devices.
286 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200287static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100289 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200290
291 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200293 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 break;
295 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297
298 default:
299 BUG();
300 }
301}
302
303/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100304 * nand_write_byte - [DEFAULT] write single byte to chip
305 * @mtd: MTD device structure
306 * @byte: value to write
307 *
308 * Default function to write a byte to I/O[7:0]
309 */
310static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
311{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100312 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100313
314 chip->write_buf(mtd, &byte, 1);
315}
316
317/**
318 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
319 * @mtd: MTD device structure
320 * @byte: value to write
321 *
322 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
323 */
324static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
325{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100326 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100327 uint16_t word = byte;
328
329 /*
330 * It's not entirely clear what should happen to I/O[15:8] when writing
331 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
332 *
333 * When the host supports a 16-bit bus width, only data is
334 * transferred at the 16-bit width. All address and command line
335 * transfers shall use only the lower 8-bits of the data bus. During
336 * command transfers, the host may place any value on the upper
337 * 8-bits of the data bus. During address transfers, the host shall
338 * set the upper 8-bits of the data bus to 00h.
339 *
Miquel Raynalb9587582018-03-19 14:47:19 +0100340 * One user of the write_byte callback is nand_set_features. The
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100341 * four parameters are specified to be written to I/O[7:0], but this is
342 * neither an address nor a command transfer. Let's assume a 0 on the
343 * upper I/O lines is OK.
344 */
345 chip->write_buf(mtd, (uint8_t *)&word, 2);
346}
347
348/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700350 * @mtd: MTD device structure
351 * @buf: data buffer
352 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700354 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200356static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100358 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Alexander Shiyan76413832013-04-13 09:32:13 +0400360 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000364 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700365 * @mtd: MTD device structure
366 * @buf: buffer to store date
367 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700369 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200371static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100373 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Alexander Shiyan76413832013-04-13 09:32:13 +0400375 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700380 * @mtd: MTD device structure
381 * @buf: data buffer
382 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700384 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200386static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100388 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000390
Alexander Shiyan76413832013-04-13 09:32:13 +0400391 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000395 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700396 * @mtd: MTD device structure
397 * @buf: buffer to store date
398 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700400 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200402static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100404 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Alexander Shiyan76413832013-04-13 09:32:13 +0400407 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700412 * @mtd: MTD device structure
413 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000415 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530417static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Masahiro Yamadac120e752017-03-23 05:07:01 +0900419 int page, page_end, res;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100420 struct nand_chip *chip = mtd_to_nand(mtd);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900421 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Brian Norris5fb15492011-05-31 16:31:21 -0700423 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700424 ofs += mtd->erasesize - mtd->writesize;
425
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100426 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900427 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100428
Masahiro Yamadac120e752017-03-23 05:07:01 +0900429 for (; page < page_end; page++) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200430 res = chip->ecc.read_oob(chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530431 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900432 return res;
433
434 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000435
Brian Norriscdbec052012-01-13 18:11:48 -0800436 if (likely(chip->badblockbits == 8))
437 res = bad != 0xFF;
438 else
439 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900440 if (res)
441 return res;
442 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200443
Masahiro Yamadac120e752017-03-23 05:07:01 +0900444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700448 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700449 * @mtd: MTD device structure
450 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700452 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700453 * specific driver. It provides the details for writing a bad block marker to a
454 * block.
455 */
456static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
457{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100458 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700459 struct mtd_oob_ops ops;
460 uint8_t buf[2] = { 0, 0 };
461 int ret = 0, res, i = 0;
462
Brian Norris0ec56dc2015-02-28 02:02:30 -0800463 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700464 ops.oobbuf = buf;
465 ops.ooboffs = chip->badblockpos;
466 if (chip->options & NAND_BUSWIDTH_16) {
467 ops.ooboffs &= ~0x01;
468 ops.len = ops.ooblen = 2;
469 } else {
470 ops.len = ops.ooblen = 1;
471 }
472 ops.mode = MTD_OPS_PLACE_OOB;
473
474 /* Write to first/last page(s) if necessary */
475 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
476 ofs += mtd->erasesize - mtd->writesize;
477 do {
478 res = nand_do_write_oob(mtd, ofs, &ops);
479 if (!ret)
480 ret = res;
481
482 i++;
483 ofs += mtd->writesize;
484 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
485
486 return ret;
487}
488
489/**
490 * nand_block_markbad_lowlevel - mark a block bad
491 * @mtd: MTD device structure
492 * @ofs: offset from device start
493 *
494 * This function performs the generic NAND bad block marking steps (i.e., bad
495 * block table(s) and/or marker(s)). We only allow the hardware driver to
496 * specify how to write bad block markers to OOB (chip->block_markbad).
497 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700498 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300499 *
Brian Norrise2414f42012-02-06 13:44:00 -0800500 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700501 * (2) write bad block marker to OOB area of affected block (unless flag
502 * NAND_BBT_NO_OOB_BBM is present)
503 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300504 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700505 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800506 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700508static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100510 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700511 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000512
Brian Norrisb32843b2013-07-30 17:52:59 -0700513 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800514 struct erase_info einfo;
515
516 /* Attempt erase before marking OOB */
517 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800518 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300519 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800520 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800521
Brian Norrisb32843b2013-07-30 17:52:59 -0700522 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800523 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700524 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300525 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200526 }
Brian Norrise2414f42012-02-06 13:44:00 -0800527
Brian Norrisb32843b2013-07-30 17:52:59 -0700528 /* Mark block bad in BBT */
529 if (chip->bbt) {
530 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800531 if (!ret)
532 ret = res;
533 }
534
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200535 if (!ret)
536 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300537
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200538 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000541/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700543 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700545 * Check, if the device is write protected. The function expects, that the
546 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100548static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100550 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100551 u8 status;
552 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200553
Brian Norris8b6e50c2011-05-25 14:59:01 -0700554 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200555 if (chip->options & NAND_BROKEN_XD)
556 return 0;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100559 ret = nand_status_op(chip, &status);
560 if (ret)
561 return ret;
562
563 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800567 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700568 * @mtd: MTD device structure
569 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300570 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800571 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300572 */
573static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
574{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100575 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300576
577 if (!chip->bbt)
578 return 0;
579 /* Return info from the table */
580 return nand_isreserved_bbt(mtd, ofs);
581}
582
583/**
584 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
585 * @mtd: MTD device structure
586 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700587 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 *
589 * Check, if the block is bad. Either by reading the bad block table or
590 * calling of the scan function.
591 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530592static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100594 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000595
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200596 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530597 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100600 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200603/**
604 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700605 * @mtd: MTD device structure
606 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200607 *
608 * Helper function for nand_wait_ready used when needing to wait in interrupt
609 * context.
610 */
611static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
612{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100613 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200614 int i;
615
616 /* Wait for the device to get ready */
617 for (i = 0; i < timeo; i++) {
618 if (chip->dev_ready(mtd))
619 break;
620 touch_softlockup_watchdog();
621 mdelay(1);
622 }
623}
624
Alex Smithb70af9b2015-10-06 14:52:07 +0100625/**
626 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200627 * @chip: NAND chip object
Alex Smithb70af9b2015-10-06 14:52:07 +0100628 *
629 * Wait for the ready pin after a command, and warn if a timeout occurs.
630 */
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200631void nand_wait_ready(struct nand_chip *chip)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000632{
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200633 struct mtd_info *mtd = nand_to_mtd(chip);
Alex Smithb70af9b2015-10-06 14:52:07 +0100634 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000635
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200636 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100637 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200638
Brian Norris7854d3f2011-06-23 14:12:08 -0700639 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100640 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000641 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200642 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300643 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100644 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000645 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100646
Brian Norris9ebfdf52016-03-04 17:19:23 -0800647 if (!chip->dev_ready(mtd))
648 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000649}
David Woodhouse4b648b02006-09-25 17:05:24 +0100650EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200653 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
654 * @mtd: MTD device structure
655 * @timeo: Timeout in ms
656 *
657 * Wait for status ready (i.e. command done) or timeout.
658 */
659static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
660{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100661 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100662 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200663
664 timeo = jiffies + msecs_to_jiffies(timeo);
665 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100666 u8 status;
667
668 ret = nand_read_data_op(chip, &status, sizeof(status), true);
669 if (ret)
670 return;
671
672 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200673 break;
674 touch_softlockup_watchdog();
675 } while (time_before(jiffies, timeo));
676};
677
678/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100679 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
680 * @chip: NAND chip structure
681 * @timeout_ms: Timeout in ms
682 *
683 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
684 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
685 * returned.
686 *
687 * This helper is intended to be used when the controller does not have access
688 * to the NAND R/B pin.
689 *
690 * Be aware that calling this helper from an ->exec_op() implementation means
691 * ->exec_op() must be re-entrant.
692 *
693 * Return 0 if the NAND chip is ready, a negative error otherwise.
694 */
695int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
696{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200697 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100698 u8 status = 0;
699 int ret;
700
701 if (!chip->exec_op)
702 return -ENOTSUPP;
703
Boris Brezillon3057fce2018-05-04 21:24:31 +0200704 /* Wait tWB before polling the STATUS reg. */
705 timings = nand_get_sdr_timings(&chip->data_interface);
706 ndelay(PSEC_TO_NSEC(timings->tWB_max));
707
Miquel Raynal8878b122017-11-09 14:16:45 +0100708 ret = nand_status_op(chip, NULL);
709 if (ret)
710 return ret;
711
712 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
713 do {
714 ret = nand_read_data_op(chip, &status, sizeof(status), true);
715 if (ret)
716 break;
717
718 if (status & NAND_STATUS_READY)
719 break;
720
721 /*
722 * Typical lowest execution time for a tR on most NANDs is 10us,
723 * use this as polling delay before doing something smarter (ie.
724 * deriving a delay from the timeout value, timeout_ms/ratio).
725 */
726 udelay(10);
727 } while (time_before(jiffies, timeout_ms));
728
729 /*
730 * We have to exit READ_STATUS mode in order to read real data on the
731 * bus in case the WAITRDY instruction is preceding a DATA_IN
732 * instruction.
733 */
734 nand_exit_status_op(chip);
735
736 if (ret)
737 return ret;
738
739 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
740};
741EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
742
743/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700745 * @mtd: MTD device structure
746 * @command: the command to be sent
747 * @column: the column address for this command, -1 if none
748 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700750 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200751 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200753static void nand_command(struct mtd_info *mtd, unsigned int command,
754 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100756 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200757 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Brian Norris8b6e50c2011-05-25 14:59:01 -0700759 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (command == NAND_CMD_SEQIN) {
761 int readcmd;
762
Joern Engel28318772006-05-22 23:18:05 +0200763 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200765 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 readcmd = NAND_CMD_READOOB;
767 } else if (column < 256) {
768 /* First 256 bytes --> READ0 */
769 readcmd = NAND_CMD_READ0;
770 } else {
771 column -= 256;
772 readcmd = NAND_CMD_READ1;
773 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200774 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200775 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100777 if (command != NAND_CMD_NONE)
778 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Brian Norris8b6e50c2011-05-25 14:59:01 -0700780 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200781 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
782 /* Serially input address */
783 if (column != -1) {
784 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800785 if (chip->options & NAND_BUSWIDTH_16 &&
786 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200787 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200788 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200789 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200791 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200792 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200793 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200794 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900795 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200796 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200797 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200798 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000799
800 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700801 * Program and erase have their own busy handlers status and sequential
802 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100803 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000805
Miquel Raynaldf467892017-11-08 17:00:27 +0100806 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 case NAND_CMD_PAGEPROG:
808 case NAND_CMD_ERASE1:
809 case NAND_CMD_ERASE2:
810 case NAND_CMD_SEQIN:
811 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900812 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900813 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return;
815
816 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200817 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200819 udelay(chip->chip_delay);
820 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200821 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200822 chip->cmd_ctrl(mtd,
823 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200824 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
825 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return;
827
David Woodhousee0c7d762006-05-13 18:07:53 +0100828 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200829 case NAND_CMD_READ0:
830 /*
831 * READ0 is sometimes used to exit GET STATUS mode. When this
832 * is the case no address cycles are requested, and we can use
833 * this information to detect that we should not wait for the
834 * device to be ready.
835 */
836 if (column == -1 && page_addr == -1)
837 return;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000840 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * If we don't have access to the busy pin, we apply the given
842 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100843 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200844 if (!chip->dev_ready) {
845 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700849 /*
850 * Apply this short delay always to ensure that we do wait tWB in
851 * any case on any machine.
852 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100853 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000854
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200855 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200858static void nand_ccs_delay(struct nand_chip *chip)
859{
860 /*
861 * The controller already takes care of waiting for tCCS when the RNDIN
862 * or RNDOUT command is sent, return directly.
863 */
864 if (!(chip->options & NAND_WAIT_TCCS))
865 return;
866
867 /*
868 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
869 * (which should be safe for all NANDs).
870 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100871 if (chip->setup_data_interface)
872 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200873 else
874 ndelay(500);
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/**
878 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700879 * @mtd: MTD device structure
880 * @command: the command to be sent
881 * @column: the column address for this command, -1 if none
882 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200884 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700885 * devices. We don't have the separate regions as we have in the small page
886 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200888static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
889 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100891 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* Emulate NAND_CMD_READOOB */
894 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200895 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 command = NAND_CMD_READ0;
897 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000898
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200899 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100900 if (command != NAND_CMD_NONE)
901 chip->cmd_ctrl(mtd, command,
902 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200905 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /* Serially input address */
908 if (column != -1) {
909 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800910 if (chip->options & NAND_BUSWIDTH_16 &&
911 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200913 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200914 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200915
Brian Norrisf5b88de2016-10-03 09:49:35 -0700916 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200917 if (!nand_opcode_8bits(command))
918 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200921 chip->cmd_ctrl(mtd, page_addr, ctrl);
922 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200923 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900924 if (chip->options & NAND_ROW_ADDR_3)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200925 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200926 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200929 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000930
931 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700932 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100933 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000934 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000936
Miquel Raynaldf467892017-11-08 17:00:27 +0100937 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 case NAND_CMD_CACHEDPROG:
939 case NAND_CMD_PAGEPROG:
940 case NAND_CMD_ERASE1:
941 case NAND_CMD_ERASE2:
942 case NAND_CMD_SEQIN:
943 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900944 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900945 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000946 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200948 case NAND_CMD_RNDIN:
949 nand_ccs_delay(chip);
950 return;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200953 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200955 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200956 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
957 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
958 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
959 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200960 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
961 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return;
963
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200964 case NAND_CMD_RNDOUT:
965 /* No ready / busy check necessary */
966 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
967 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
968 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
969 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200970
971 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200972 return;
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200975 /*
976 * READ0 is sometimes used to exit GET STATUS mode. When this
977 * is the case no address cycles are requested, and we can use
978 * this information to detect that READSTART should not be
979 * issued.
980 */
981 if (column == -1 && page_addr == -1)
982 return;
983
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200984 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
985 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
986 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
987 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000988
David Woodhousee0c7d762006-05-13 18:07:53 +0100989 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000991 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700993 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100994 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200995 if (!chip->dev_ready) {
996 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Thomas Gleixner3b887752005-02-22 21:56:49 +00001000
Brian Norris8b6e50c2011-05-25 14:59:01 -07001001 /*
1002 * Apply this short delay always to ensure that we do wait tWB in
1003 * any case on any machine.
1004 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001005 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +00001006
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001007 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001011 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001012 * @chip: the nand chip descriptor
1013 * @mtd: MTD device structure
1014 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001015 *
1016 * Used when in panic, no locks are taken.
1017 */
1018static void panic_nand_get_device(struct nand_chip *chip,
1019 struct mtd_info *mtd, int new_state)
1020{
Brian Norris7854d3f2011-06-23 14:12:08 -07001021 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001022 chip->controller->active = chip;
1023 chip->state = new_state;
1024}
1025
1026/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001028 * @mtd: MTD device structure
1029 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 *
1031 * Get the device and lock it for exclusive access
1032 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001033static int
Huang Shijie6a8214a2012-11-19 14:43:30 +08001034nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001036 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001037 spinlock_t *lock = &chip->controller->lock;
1038 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +01001039 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001040retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001041 spin_lock(lock);
1042
vimal singhb8b3ee92009-07-09 20:41:22 +05301043 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001044 if (!chip->controller->active)
1045 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001046
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001047 if (chip->controller->active == chip && chip->state == FL_READY) {
1048 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001049 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01001050 return 0;
1051 }
1052 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001053 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1054 chip->state = FL_PM_SUSPENDED;
1055 spin_unlock(lock);
1056 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001057 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001058 }
1059 set_current_state(TASK_UNINTERRUPTIBLE);
1060 add_wait_queue(wq, &wait);
1061 spin_unlock(lock);
1062 schedule();
1063 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 goto retry;
1065}
1066
1067/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001068 * panic_nand_wait - [GENERIC] wait until the command is done
1069 * @mtd: MTD device structure
1070 * @chip: NAND chip structure
1071 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001072 *
1073 * Wait for command done. This is a helper function for nand_wait used when
1074 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001075 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001076 */
1077static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1078 unsigned long timeo)
1079{
1080 int i;
1081 for (i = 0; i < timeo; i++) {
1082 if (chip->dev_ready) {
1083 if (chip->dev_ready(mtd))
1084 break;
1085 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001086 int ret;
1087 u8 status;
1088
1089 ret = nand_read_data_op(chip, &status, sizeof(status),
1090 true);
1091 if (ret)
1092 return;
1093
1094 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001095 break;
1096 }
1097 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001098 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001099}
1100
1101/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001102 * nand_wait - [DEFAULT] wait until the command is done
1103 * @mtd: MTD device structure
1104 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001106 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001107 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001108static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110
Alex Smithb70af9b2015-10-06 14:52:07 +01001111 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001112 u8 status;
1113 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Brian Norris8b6e50c2011-05-25 14:59:01 -07001115 /*
1116 * Apply this short delay always to ensure that we do wait tWB in any
1117 * case on any machine.
1118 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001119 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Boris Brezillon97d90da2017-11-30 18:01:29 +01001121 ret = nand_status_op(chip, NULL);
1122 if (ret)
1123 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001125 if (in_interrupt() || oops_in_progress)
1126 panic_nand_wait(mtd, chip, timeo);
1127 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001128 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001129 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001130 if (chip->dev_ready) {
1131 if (chip->dev_ready(mtd))
1132 break;
1133 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001134 ret = nand_read_data_op(chip, &status,
1135 sizeof(status), true);
1136 if (ret)
1137 return ret;
1138
1139 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001140 break;
1141 }
1142 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001143 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001145
Boris Brezillon97d90da2017-11-30 18:01:29 +01001146 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1147 if (ret)
1148 return ret;
1149
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001150 /* This can happen if in case of timeout or buggy dev_ready */
1151 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return status;
1153}
1154
Miquel Raynal789157e2018-03-19 14:47:28 +01001155static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +01001156{
Miquel Raynal789157e2018-03-19 14:47:28 +01001157 return (chip->parameters.supports_set_get_features &&
1158 test_bit(addr, chip->parameters.get_feature_list));
1159}
1160
1161static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1162{
1163 return (chip->parameters.supports_set_get_features &&
1164 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +01001165}
1166
1167/**
1168 * nand_get_features - wrapper to perform a GET_FEATURE
1169 * @chip: NAND chip info structure
1170 * @addr: feature address
1171 * @subfeature_param: the subfeature parameters, a four bytes array
1172 *
1173 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1174 * operation cannot be handled.
1175 */
1176int nand_get_features(struct nand_chip *chip, int addr,
1177 u8 *subfeature_param)
1178{
1179 struct mtd_info *mtd = nand_to_mtd(chip);
1180
Miquel Raynal789157e2018-03-19 14:47:28 +01001181 if (!nand_supports_get_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001182 return -ENOTSUPP;
1183
1184 return chip->get_features(mtd, chip, addr, subfeature_param);
1185}
1186EXPORT_SYMBOL_GPL(nand_get_features);
1187
1188/**
1189 * nand_set_features - wrapper to perform a SET_FEATURE
1190 * @chip: NAND chip info structure
1191 * @addr: feature address
1192 * @subfeature_param: the subfeature parameters, a four bytes array
1193 *
1194 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1195 * operation cannot be handled.
1196 */
1197int nand_set_features(struct nand_chip *chip, int addr,
1198 u8 *subfeature_param)
1199{
1200 struct mtd_info *mtd = nand_to_mtd(chip);
1201
Miquel Raynal789157e2018-03-19 14:47:28 +01001202 if (!nand_supports_set_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001203 return -ENOTSUPP;
1204
1205 return chip->set_features(mtd, chip, addr, subfeature_param);
1206}
1207EXPORT_SYMBOL_GPL(nand_set_features);
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001210 * nand_reset_data_interface - Reset data interface and timings
1211 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001212 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001213 *
1214 * Reset the Data interface and timings to ONFI mode 0.
1215 *
1216 * Returns 0 for success or negative error code otherwise.
1217 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001218static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001219{
1220 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001221 int ret;
1222
1223 if (!chip->setup_data_interface)
1224 return 0;
1225
1226 /*
1227 * The ONFI specification says:
1228 * "
1229 * To transition from NV-DDR or NV-DDR2 to the SDR data
1230 * interface, the host shall use the Reset (FFh) command
1231 * using SDR timing mode 0. A device in any timing mode is
1232 * required to recognize Reset (FFh) command issued in SDR
1233 * timing mode 0.
1234 * "
1235 *
1236 * Configure the data interface in SDR mode and set the
1237 * timings to timing mode 0.
1238 */
1239
Miquel Raynal17fa8042017-11-30 18:01:31 +01001240 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1241 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001242 if (ret)
1243 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1244
1245 return ret;
1246}
1247
1248/**
1249 * nand_setup_data_interface - Setup the best data interface and timings
1250 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001251 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001252 *
1253 * Find and configure the best data interface and NAND timings supported by
1254 * the chip and the driver.
1255 * First tries to retrieve supported timing modes from ONFI information,
1256 * and if the NAND chip does not support ONFI, relies on the
1257 * ->onfi_timing_mode_default specified in the nand_ids table.
1258 *
1259 * Returns 0 for success or negative error code otherwise.
1260 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001261static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001262{
1263 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal97baea12018-03-19 14:47:20 +01001264 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1265 chip->onfi_timing_mode_default,
1266 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001267 int ret;
1268
Miquel Raynal17fa8042017-11-30 18:01:31 +01001269 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001270 return 0;
1271
Miquel Raynal993447b2018-03-19 14:47:21 +01001272 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +01001273 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Miquel Raynal29714d62018-03-19 14:47:23 +01001274 chip->select_chip(mtd, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001275 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1276 tmode_param);
Miquel Raynal29714d62018-03-19 14:47:23 +01001277 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001278 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +01001279 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001280 }
1281
Miquel Raynal97baea12018-03-19 14:47:20 +01001282 /* Change the mode on the controller side */
Miquel Raynal17fa8042017-11-30 18:01:31 +01001283 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +01001284 if (ret)
1285 return ret;
1286
1287 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +01001288 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +01001289 return 0;
1290
1291 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
1292 chip->select_chip(mtd, chipnr);
1293 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1294 tmode_param);
1295 chip->select_chip(mtd, -1);
1296 if (ret)
1297 goto err_reset_chip;
1298
1299 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1300 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1301 chip->onfi_timing_mode_default);
1302 goto err_reset_chip;
1303 }
1304
1305 return 0;
1306
1307err_reset_chip:
1308 /*
1309 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1310 * timing mode.
1311 */
1312 nand_reset_data_interface(chip, chipnr);
1313 chip->select_chip(mtd, chipnr);
1314 nand_reset_op(chip);
1315 chip->select_chip(mtd, -1);
1316
Boris Brezillond8e725d2016-09-15 10:32:50 +02001317 return ret;
1318}
1319
1320/**
1321 * nand_init_data_interface - find the best data interface and timings
1322 * @chip: The NAND chip
1323 *
1324 * Find the best data interface and NAND timings supported by the chip
1325 * and the driver.
1326 * First tries to retrieve supported timing modes from ONFI information,
1327 * and if the NAND chip does not support ONFI, relies on the
1328 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1329 * function nand_chip->data_interface is initialized with the best timing mode
1330 * available.
1331 *
1332 * Returns 0 for success or negative error code otherwise.
1333 */
1334static int nand_init_data_interface(struct nand_chip *chip)
1335{
1336 struct mtd_info *mtd = nand_to_mtd(chip);
1337 int modes, mode, ret;
1338
1339 if (!chip->setup_data_interface)
1340 return 0;
1341
1342 /*
1343 * First try to identify the best timings from ONFI parameters and
1344 * if the NAND does not support ONFI, fallback to the default ONFI
1345 * timing mode.
1346 */
1347 modes = onfi_get_async_timing_mode(chip);
1348 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1349 if (!chip->onfi_timing_mode_default)
1350 return 0;
1351
1352 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1353 }
1354
Boris Brezillond8e725d2016-09-15 10:32:50 +02001355
1356 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001357 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001358 if (ret)
1359 continue;
1360
Miquel Raynald787b8b2017-12-22 18:12:41 +01001361 /*
1362 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1363 * controller supports the requested timings.
1364 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001365 ret = chip->setup_data_interface(mtd,
1366 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001367 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001368 if (!ret) {
1369 chip->onfi_timing_mode_default = mode;
1370 break;
1371 }
1372 }
1373
1374 return 0;
1375}
1376
Boris Brezillond8e725d2016-09-15 10:32:50 +02001377/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001378 * nand_fill_column_cycles - fill the column cycles of an address
1379 * @chip: The NAND chip
1380 * @addrs: Array of address cycles to fill
1381 * @offset_in_page: The offset in the page
1382 *
1383 * Fills the first or the first two bytes of the @addrs field depending
1384 * on the NAND bus width and the page size.
1385 *
1386 * Returns the number of cycles needed to encode the column, or a negative
1387 * error code in case one of the arguments is invalid.
1388 */
1389static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1390 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Miquel Raynal8878b122017-11-09 14:16:45 +01001392 struct mtd_info *mtd = nand_to_mtd(chip);
1393
1394 /* Make sure the offset is less than the actual page size. */
1395 if (offset_in_page > mtd->writesize + mtd->oobsize)
1396 return -EINVAL;
1397
1398 /*
1399 * On small page NANDs, there's a dedicated command to access the OOB
1400 * area, and the column address is relative to the start of the OOB
1401 * area, not the start of the page. Asjust the address accordingly.
1402 */
1403 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1404 offset_in_page -= mtd->writesize;
1405
1406 /*
1407 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1408 * wide, then it must be divided by 2.
1409 */
1410 if (chip->options & NAND_BUSWIDTH_16) {
1411 if (WARN_ON(offset_in_page % 2))
1412 return -EINVAL;
1413
1414 offset_in_page /= 2;
1415 }
1416
1417 addrs[0] = offset_in_page;
1418
1419 /*
1420 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1421 * need 2
1422 */
1423 if (mtd->writesize <= 512)
1424 return 1;
1425
1426 addrs[1] = offset_in_page >> 8;
1427
1428 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Miquel Raynal8878b122017-11-09 14:16:45 +01001431static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1432 unsigned int offset_in_page, void *buf,
1433 unsigned int len)
1434{
1435 struct mtd_info *mtd = nand_to_mtd(chip);
1436 const struct nand_sdr_timings *sdr =
1437 nand_get_sdr_timings(&chip->data_interface);
1438 u8 addrs[4];
1439 struct nand_op_instr instrs[] = {
1440 NAND_OP_CMD(NAND_CMD_READ0, 0),
1441 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1442 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1443 PSEC_TO_NSEC(sdr->tRR_min)),
1444 NAND_OP_DATA_IN(len, buf, 0),
1445 };
1446 struct nand_operation op = NAND_OPERATION(instrs);
1447 int ret;
1448
1449 /* Drop the DATA_IN instruction if len is set to 0. */
1450 if (!len)
1451 op.ninstrs--;
1452
1453 if (offset_in_page >= mtd->writesize)
1454 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1455 else if (offset_in_page >= 256 &&
1456 !(chip->options & NAND_BUSWIDTH_16))
1457 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1458
1459 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1460 if (ret < 0)
1461 return ret;
1462
1463 addrs[1] = page;
1464 addrs[2] = page >> 8;
1465
1466 if (chip->options & NAND_ROW_ADDR_3) {
1467 addrs[3] = page >> 16;
1468 instrs[1].ctx.addr.naddrs++;
1469 }
1470
1471 return nand_exec_op(chip, &op);
1472}
1473
1474static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1475 unsigned int offset_in_page, void *buf,
1476 unsigned int len)
1477{
1478 const struct nand_sdr_timings *sdr =
1479 nand_get_sdr_timings(&chip->data_interface);
1480 u8 addrs[5];
1481 struct nand_op_instr instrs[] = {
1482 NAND_OP_CMD(NAND_CMD_READ0, 0),
1483 NAND_OP_ADDR(4, addrs, 0),
1484 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1485 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1486 PSEC_TO_NSEC(sdr->tRR_min)),
1487 NAND_OP_DATA_IN(len, buf, 0),
1488 };
1489 struct nand_operation op = NAND_OPERATION(instrs);
1490 int ret;
1491
1492 /* Drop the DATA_IN instruction if len is set to 0. */
1493 if (!len)
1494 op.ninstrs--;
1495
1496 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1497 if (ret < 0)
1498 return ret;
1499
1500 addrs[2] = page;
1501 addrs[3] = page >> 8;
1502
1503 if (chip->options & NAND_ROW_ADDR_3) {
1504 addrs[4] = page >> 16;
1505 instrs[1].ctx.addr.naddrs++;
1506 }
1507
1508 return nand_exec_op(chip, &op);
1509}
1510
1511/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001512 * nand_read_page_op - Do a READ PAGE operation
1513 * @chip: The NAND chip
1514 * @page: page to read
1515 * @offset_in_page: offset within the page
1516 * @buf: buffer used to store the data
1517 * @len: length of the buffer
1518 *
1519 * This function issues a READ PAGE operation.
1520 * This function does not select/unselect the CS line.
1521 *
1522 * Returns 0 on success, a negative error code otherwise.
1523 */
1524int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1525 unsigned int offset_in_page, void *buf, unsigned int len)
1526{
1527 struct mtd_info *mtd = nand_to_mtd(chip);
1528
1529 if (len && !buf)
1530 return -EINVAL;
1531
1532 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1533 return -EINVAL;
1534
Miquel Raynal8878b122017-11-09 14:16:45 +01001535 if (chip->exec_op) {
1536 if (mtd->writesize > 512)
1537 return nand_lp_exec_read_page_op(chip, page,
1538 offset_in_page, buf,
1539 len);
1540
1541 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1542 buf, len);
1543 }
1544
Boris Brezillon97d90da2017-11-30 18:01:29 +01001545 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1546 if (len)
1547 chip->read_buf(mtd, buf, len);
1548
1549 return 0;
1550}
1551EXPORT_SYMBOL_GPL(nand_read_page_op);
1552
1553/**
1554 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1555 * @chip: The NAND chip
1556 * @page: parameter page to read
1557 * @buf: buffer used to store the data
1558 * @len: length of the buffer
1559 *
1560 * This function issues a READ PARAMETER PAGE operation.
1561 * This function does not select/unselect the CS line.
1562 *
1563 * Returns 0 on success, a negative error code otherwise.
1564 */
1565static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1566 unsigned int len)
1567{
1568 struct mtd_info *mtd = nand_to_mtd(chip);
1569 unsigned int i;
1570 u8 *p = buf;
1571
1572 if (len && !buf)
1573 return -EINVAL;
1574
Miquel Raynal8878b122017-11-09 14:16:45 +01001575 if (chip->exec_op) {
1576 const struct nand_sdr_timings *sdr =
1577 nand_get_sdr_timings(&chip->data_interface);
1578 struct nand_op_instr instrs[] = {
1579 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1580 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1581 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1582 PSEC_TO_NSEC(sdr->tRR_min)),
1583 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1584 };
1585 struct nand_operation op = NAND_OPERATION(instrs);
1586
1587 /* Drop the DATA_IN instruction if len is set to 0. */
1588 if (!len)
1589 op.ninstrs--;
1590
1591 return nand_exec_op(chip, &op);
1592 }
1593
Boris Brezillon97d90da2017-11-30 18:01:29 +01001594 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1595 for (i = 0; i < len; i++)
1596 p[i] = chip->read_byte(mtd);
1597
1598 return 0;
1599}
1600
1601/**
1602 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1603 * @chip: The NAND chip
1604 * @offset_in_page: offset within the page
1605 * @buf: buffer used to store the data
1606 * @len: length of the buffer
1607 * @force_8bit: force 8-bit bus access
1608 *
1609 * This function issues a CHANGE READ COLUMN operation.
1610 * This function does not select/unselect the CS line.
1611 *
1612 * Returns 0 on success, a negative error code otherwise.
1613 */
1614int nand_change_read_column_op(struct nand_chip *chip,
1615 unsigned int offset_in_page, void *buf,
1616 unsigned int len, bool force_8bit)
1617{
1618 struct mtd_info *mtd = nand_to_mtd(chip);
1619
1620 if (len && !buf)
1621 return -EINVAL;
1622
1623 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1624 return -EINVAL;
1625
Miquel Raynal8878b122017-11-09 14:16:45 +01001626 /* Small page NANDs do not support column change. */
1627 if (mtd->writesize <= 512)
1628 return -ENOTSUPP;
1629
1630 if (chip->exec_op) {
1631 const struct nand_sdr_timings *sdr =
1632 nand_get_sdr_timings(&chip->data_interface);
1633 u8 addrs[2] = {};
1634 struct nand_op_instr instrs[] = {
1635 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1636 NAND_OP_ADDR(2, addrs, 0),
1637 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1638 PSEC_TO_NSEC(sdr->tCCS_min)),
1639 NAND_OP_DATA_IN(len, buf, 0),
1640 };
1641 struct nand_operation op = NAND_OPERATION(instrs);
1642 int ret;
1643
1644 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1645 if (ret < 0)
1646 return ret;
1647
1648 /* Drop the DATA_IN instruction if len is set to 0. */
1649 if (!len)
1650 op.ninstrs--;
1651
1652 instrs[3].ctx.data.force_8bit = force_8bit;
1653
1654 return nand_exec_op(chip, &op);
1655 }
1656
Boris Brezillon97d90da2017-11-30 18:01:29 +01001657 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1658 if (len)
1659 chip->read_buf(mtd, buf, len);
1660
1661 return 0;
1662}
1663EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1664
1665/**
1666 * nand_read_oob_op - Do a READ OOB operation
1667 * @chip: The NAND chip
1668 * @page: page to read
1669 * @offset_in_oob: offset within the OOB area
1670 * @buf: buffer used to store the data
1671 * @len: length of the buffer
1672 *
1673 * This function issues a READ OOB operation.
1674 * This function does not select/unselect the CS line.
1675 *
1676 * Returns 0 on success, a negative error code otherwise.
1677 */
1678int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1679 unsigned int offset_in_oob, void *buf, unsigned int len)
1680{
1681 struct mtd_info *mtd = nand_to_mtd(chip);
1682
1683 if (len && !buf)
1684 return -EINVAL;
1685
1686 if (offset_in_oob + len > mtd->oobsize)
1687 return -EINVAL;
1688
Miquel Raynal8878b122017-11-09 14:16:45 +01001689 if (chip->exec_op)
1690 return nand_read_page_op(chip, page,
1691 mtd->writesize + offset_in_oob,
1692 buf, len);
1693
Boris Brezillon97d90da2017-11-30 18:01:29 +01001694 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1695 if (len)
1696 chip->read_buf(mtd, buf, len);
1697
1698 return 0;
1699}
1700EXPORT_SYMBOL_GPL(nand_read_oob_op);
1701
Miquel Raynal8878b122017-11-09 14:16:45 +01001702static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1703 unsigned int offset_in_page, const void *buf,
1704 unsigned int len, bool prog)
1705{
1706 struct mtd_info *mtd = nand_to_mtd(chip);
1707 const struct nand_sdr_timings *sdr =
1708 nand_get_sdr_timings(&chip->data_interface);
1709 u8 addrs[5] = {};
1710 struct nand_op_instr instrs[] = {
1711 /*
1712 * The first instruction will be dropped if we're dealing
1713 * with a large page NAND and adjusted if we're dealing
1714 * with a small page NAND and the page offset is > 255.
1715 */
1716 NAND_OP_CMD(NAND_CMD_READ0, 0),
1717 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1718 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1719 NAND_OP_DATA_OUT(len, buf, 0),
1720 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1721 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1722 };
1723 struct nand_operation op = NAND_OPERATION(instrs);
1724 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1725 int ret;
1726 u8 status;
1727
1728 if (naddrs < 0)
1729 return naddrs;
1730
1731 addrs[naddrs++] = page;
1732 addrs[naddrs++] = page >> 8;
1733 if (chip->options & NAND_ROW_ADDR_3)
1734 addrs[naddrs++] = page >> 16;
1735
1736 instrs[2].ctx.addr.naddrs = naddrs;
1737
1738 /* Drop the last two instructions if we're not programming the page. */
1739 if (!prog) {
1740 op.ninstrs -= 2;
1741 /* Also drop the DATA_OUT instruction if empty. */
1742 if (!len)
1743 op.ninstrs--;
1744 }
1745
1746 if (mtd->writesize <= 512) {
1747 /*
1748 * Small pages need some more tweaking: we have to adjust the
1749 * first instruction depending on the page offset we're trying
1750 * to access.
1751 */
1752 if (offset_in_page >= mtd->writesize)
1753 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1754 else if (offset_in_page >= 256 &&
1755 !(chip->options & NAND_BUSWIDTH_16))
1756 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1757 } else {
1758 /*
1759 * Drop the first command if we're dealing with a large page
1760 * NAND.
1761 */
1762 op.instrs++;
1763 op.ninstrs--;
1764 }
1765
1766 ret = nand_exec_op(chip, &op);
1767 if (!prog || ret)
1768 return ret;
1769
1770 ret = nand_status_op(chip, &status);
1771 if (ret)
1772 return ret;
1773
1774 return status;
1775}
1776
Boris Brezillon97d90da2017-11-30 18:01:29 +01001777/**
1778 * nand_prog_page_begin_op - starts a PROG PAGE operation
1779 * @chip: The NAND chip
1780 * @page: page to write
1781 * @offset_in_page: offset within the page
1782 * @buf: buffer containing the data to write to the page
1783 * @len: length of the buffer
1784 *
1785 * This function issues the first half of a PROG PAGE operation.
1786 * This function does not select/unselect the CS line.
1787 *
1788 * Returns 0 on success, a negative error code otherwise.
1789 */
1790int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1791 unsigned int offset_in_page, const void *buf,
1792 unsigned int len)
1793{
1794 struct mtd_info *mtd = nand_to_mtd(chip);
1795
1796 if (len && !buf)
1797 return -EINVAL;
1798
1799 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1800 return -EINVAL;
1801
Miquel Raynal8878b122017-11-09 14:16:45 +01001802 if (chip->exec_op)
1803 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1804 len, false);
1805
Boris Brezillon97d90da2017-11-30 18:01:29 +01001806 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1807
1808 if (buf)
1809 chip->write_buf(mtd, buf, len);
1810
1811 return 0;
1812}
1813EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1814
1815/**
1816 * nand_prog_page_end_op - ends a PROG PAGE operation
1817 * @chip: The NAND chip
1818 *
1819 * This function issues the second half of a PROG PAGE operation.
1820 * This function does not select/unselect the CS line.
1821 *
1822 * Returns 0 on success, a negative error code otherwise.
1823 */
1824int nand_prog_page_end_op(struct nand_chip *chip)
1825{
1826 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001827 int ret;
1828 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001829
Miquel Raynal8878b122017-11-09 14:16:45 +01001830 if (chip->exec_op) {
1831 const struct nand_sdr_timings *sdr =
1832 nand_get_sdr_timings(&chip->data_interface);
1833 struct nand_op_instr instrs[] = {
1834 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1835 PSEC_TO_NSEC(sdr->tWB_max)),
1836 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1837 };
1838 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001839
Miquel Raynal8878b122017-11-09 14:16:45 +01001840 ret = nand_exec_op(chip, &op);
1841 if (ret)
1842 return ret;
1843
1844 ret = nand_status_op(chip, &status);
1845 if (ret)
1846 return ret;
1847 } else {
1848 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1849 ret = chip->waitfunc(mtd, chip);
1850 if (ret < 0)
1851 return ret;
1852
1853 status = ret;
1854 }
1855
Boris Brezillon97d90da2017-11-30 18:01:29 +01001856 if (status & NAND_STATUS_FAIL)
1857 return -EIO;
1858
1859 return 0;
1860}
1861EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1862
1863/**
1864 * nand_prog_page_op - Do a full PROG PAGE operation
1865 * @chip: The NAND chip
1866 * @page: page to write
1867 * @offset_in_page: offset within the page
1868 * @buf: buffer containing the data to write to the page
1869 * @len: length of the buffer
1870 *
1871 * This function issues a full PROG PAGE operation.
1872 * This function does not select/unselect the CS line.
1873 *
1874 * Returns 0 on success, a negative error code otherwise.
1875 */
1876int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1877 unsigned int offset_in_page, const void *buf,
1878 unsigned int len)
1879{
1880 struct mtd_info *mtd = nand_to_mtd(chip);
1881 int status;
1882
1883 if (!len || !buf)
1884 return -EINVAL;
1885
1886 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1887 return -EINVAL;
1888
Miquel Raynal8878b122017-11-09 14:16:45 +01001889 if (chip->exec_op) {
1890 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1891 len, true);
1892 } else {
1893 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1894 chip->write_buf(mtd, buf, len);
1895 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1896 status = chip->waitfunc(mtd, chip);
1897 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001898
Boris Brezillon97d90da2017-11-30 18:01:29 +01001899 if (status & NAND_STATUS_FAIL)
1900 return -EIO;
1901
1902 return 0;
1903}
1904EXPORT_SYMBOL_GPL(nand_prog_page_op);
1905
1906/**
1907 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1908 * @chip: The NAND chip
1909 * @offset_in_page: offset within the page
1910 * @buf: buffer containing the data to send to the NAND
1911 * @len: length of the buffer
1912 * @force_8bit: force 8-bit bus access
1913 *
1914 * This function issues a CHANGE WRITE COLUMN operation.
1915 * This function does not select/unselect the CS line.
1916 *
1917 * Returns 0 on success, a negative error code otherwise.
1918 */
1919int nand_change_write_column_op(struct nand_chip *chip,
1920 unsigned int offset_in_page,
1921 const void *buf, unsigned int len,
1922 bool force_8bit)
1923{
1924 struct mtd_info *mtd = nand_to_mtd(chip);
1925
1926 if (len && !buf)
1927 return -EINVAL;
1928
1929 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1930 return -EINVAL;
1931
Miquel Raynal8878b122017-11-09 14:16:45 +01001932 /* Small page NANDs do not support column change. */
1933 if (mtd->writesize <= 512)
1934 return -ENOTSUPP;
1935
1936 if (chip->exec_op) {
1937 const struct nand_sdr_timings *sdr =
1938 nand_get_sdr_timings(&chip->data_interface);
1939 u8 addrs[2];
1940 struct nand_op_instr instrs[] = {
1941 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1942 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1943 NAND_OP_DATA_OUT(len, buf, 0),
1944 };
1945 struct nand_operation op = NAND_OPERATION(instrs);
1946 int ret;
1947
1948 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1949 if (ret < 0)
1950 return ret;
1951
1952 instrs[2].ctx.data.force_8bit = force_8bit;
1953
1954 /* Drop the DATA_OUT instruction if len is set to 0. */
1955 if (!len)
1956 op.ninstrs--;
1957
1958 return nand_exec_op(chip, &op);
1959 }
1960
Boris Brezillon97d90da2017-11-30 18:01:29 +01001961 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1962 if (len)
1963 chip->write_buf(mtd, buf, len);
1964
1965 return 0;
1966}
1967EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1968
1969/**
1970 * nand_readid_op - Do a READID operation
1971 * @chip: The NAND chip
1972 * @addr: address cycle to pass after the READID command
1973 * @buf: buffer used to store the ID
1974 * @len: length of the buffer
1975 *
1976 * This function sends a READID command and reads back the ID returned by the
1977 * NAND.
1978 * This function does not select/unselect the CS line.
1979 *
1980 * Returns 0 on success, a negative error code otherwise.
1981 */
1982int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1983 unsigned int len)
1984{
1985 struct mtd_info *mtd = nand_to_mtd(chip);
1986 unsigned int i;
1987 u8 *id = buf;
1988
1989 if (len && !buf)
1990 return -EINVAL;
1991
Miquel Raynal8878b122017-11-09 14:16:45 +01001992 if (chip->exec_op) {
1993 const struct nand_sdr_timings *sdr =
1994 nand_get_sdr_timings(&chip->data_interface);
1995 struct nand_op_instr instrs[] = {
1996 NAND_OP_CMD(NAND_CMD_READID, 0),
1997 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1998 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1999 };
2000 struct nand_operation op = NAND_OPERATION(instrs);
2001
2002 /* Drop the DATA_IN instruction if len is set to 0. */
2003 if (!len)
2004 op.ninstrs--;
2005
2006 return nand_exec_op(chip, &op);
2007 }
2008
Boris Brezillon97d90da2017-11-30 18:01:29 +01002009 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
2010
2011 for (i = 0; i < len; i++)
2012 id[i] = chip->read_byte(mtd);
2013
2014 return 0;
2015}
2016EXPORT_SYMBOL_GPL(nand_readid_op);
2017
2018/**
2019 * nand_status_op - Do a STATUS operation
2020 * @chip: The NAND chip
2021 * @status: out variable to store the NAND status
2022 *
2023 * This function sends a STATUS command and reads back the status returned by
2024 * the NAND.
2025 * This function does not select/unselect the CS line.
2026 *
2027 * Returns 0 on success, a negative error code otherwise.
2028 */
2029int nand_status_op(struct nand_chip *chip, u8 *status)
2030{
2031 struct mtd_info *mtd = nand_to_mtd(chip);
2032
Miquel Raynal8878b122017-11-09 14:16:45 +01002033 if (chip->exec_op) {
2034 const struct nand_sdr_timings *sdr =
2035 nand_get_sdr_timings(&chip->data_interface);
2036 struct nand_op_instr instrs[] = {
2037 NAND_OP_CMD(NAND_CMD_STATUS,
2038 PSEC_TO_NSEC(sdr->tADL_min)),
2039 NAND_OP_8BIT_DATA_IN(1, status, 0),
2040 };
2041 struct nand_operation op = NAND_OPERATION(instrs);
2042
2043 if (!status)
2044 op.ninstrs--;
2045
2046 return nand_exec_op(chip, &op);
2047 }
2048
Boris Brezillon97d90da2017-11-30 18:01:29 +01002049 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
2050 if (status)
2051 *status = chip->read_byte(mtd);
2052
2053 return 0;
2054}
2055EXPORT_SYMBOL_GPL(nand_status_op);
2056
2057/**
2058 * nand_exit_status_op - Exit a STATUS operation
2059 * @chip: The NAND chip
2060 *
2061 * This function sends a READ0 command to cancel the effect of the STATUS
2062 * command to avoid reading only the status until a new read command is sent.
2063 *
2064 * This function does not select/unselect the CS line.
2065 *
2066 * Returns 0 on success, a negative error code otherwise.
2067 */
2068int nand_exit_status_op(struct nand_chip *chip)
2069{
2070 struct mtd_info *mtd = nand_to_mtd(chip);
2071
Miquel Raynal8878b122017-11-09 14:16:45 +01002072 if (chip->exec_op) {
2073 struct nand_op_instr instrs[] = {
2074 NAND_OP_CMD(NAND_CMD_READ0, 0),
2075 };
2076 struct nand_operation op = NAND_OPERATION(instrs);
2077
2078 return nand_exec_op(chip, &op);
2079 }
2080
Boris Brezillon97d90da2017-11-30 18:01:29 +01002081 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
2082
2083 return 0;
2084}
2085EXPORT_SYMBOL_GPL(nand_exit_status_op);
2086
2087/**
2088 * nand_erase_op - Do an erase operation
2089 * @chip: The NAND chip
2090 * @eraseblock: block to erase
2091 *
2092 * This function sends an ERASE command and waits for the NAND to be ready
2093 * before returning.
2094 * This function does not select/unselect the CS line.
2095 *
2096 * Returns 0 on success, a negative error code otherwise.
2097 */
2098int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2099{
2100 struct mtd_info *mtd = nand_to_mtd(chip);
2101 unsigned int page = eraseblock <<
2102 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002103 int ret;
2104 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002105
Miquel Raynal8878b122017-11-09 14:16:45 +01002106 if (chip->exec_op) {
2107 const struct nand_sdr_timings *sdr =
2108 nand_get_sdr_timings(&chip->data_interface);
2109 u8 addrs[3] = { page, page >> 8, page >> 16 };
2110 struct nand_op_instr instrs[] = {
2111 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2112 NAND_OP_ADDR(2, addrs, 0),
2113 NAND_OP_CMD(NAND_CMD_ERASE2,
2114 PSEC_TO_MSEC(sdr->tWB_max)),
2115 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2116 };
2117 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002118
Miquel Raynal8878b122017-11-09 14:16:45 +01002119 if (chip->options & NAND_ROW_ADDR_3)
2120 instrs[1].ctx.addr.naddrs++;
2121
2122 ret = nand_exec_op(chip, &op);
2123 if (ret)
2124 return ret;
2125
2126 ret = nand_status_op(chip, &status);
2127 if (ret)
2128 return ret;
2129 } else {
2130 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2131 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2132
2133 ret = chip->waitfunc(mtd, chip);
2134 if (ret < 0)
2135 return ret;
2136
2137 status = ret;
2138 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002139
2140 if (status & NAND_STATUS_FAIL)
2141 return -EIO;
2142
2143 return 0;
2144}
2145EXPORT_SYMBOL_GPL(nand_erase_op);
2146
2147/**
2148 * nand_set_features_op - Do a SET FEATURES operation
2149 * @chip: The NAND chip
2150 * @feature: feature id
2151 * @data: 4 bytes of data
2152 *
2153 * This function sends a SET FEATURES command and waits for the NAND to be
2154 * ready before returning.
2155 * This function does not select/unselect the CS line.
2156 *
2157 * Returns 0 on success, a negative error code otherwise.
2158 */
2159static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2160 const void *data)
2161{
2162 struct mtd_info *mtd = nand_to_mtd(chip);
2163 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002164 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002165
Miquel Raynal8878b122017-11-09 14:16:45 +01002166 if (chip->exec_op) {
2167 const struct nand_sdr_timings *sdr =
2168 nand_get_sdr_timings(&chip->data_interface);
2169 struct nand_op_instr instrs[] = {
2170 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2171 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2172 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2173 PSEC_TO_NSEC(sdr->tWB_max)),
2174 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2175 };
2176 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002177
Boris Brezillon782d1962018-05-11 14:44:07 +02002178 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01002179 }
2180
Boris Brezillon782d1962018-05-11 14:44:07 +02002181 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
2182 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2183 chip->write_byte(mtd, params[i]);
2184
2185 ret = chip->waitfunc(mtd, chip);
2186 if (ret < 0)
2187 return ret;
2188
2189 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002190 return -EIO;
2191
2192 return 0;
2193}
2194
2195/**
2196 * nand_get_features_op - Do a GET FEATURES operation
2197 * @chip: The NAND chip
2198 * @feature: feature id
2199 * @data: 4 bytes of data
2200 *
2201 * This function sends a GET FEATURES command and waits for the NAND to be
2202 * ready before returning.
2203 * This function does not select/unselect the CS line.
2204 *
2205 * Returns 0 on success, a negative error code otherwise.
2206 */
2207static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2208 void *data)
2209{
2210 struct mtd_info *mtd = nand_to_mtd(chip);
2211 u8 *params = data;
2212 int i;
2213
Miquel Raynal8878b122017-11-09 14:16:45 +01002214 if (chip->exec_op) {
2215 const struct nand_sdr_timings *sdr =
2216 nand_get_sdr_timings(&chip->data_interface);
2217 struct nand_op_instr instrs[] = {
2218 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2219 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2220 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2221 PSEC_TO_NSEC(sdr->tRR_min)),
2222 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2223 data, 0),
2224 };
2225 struct nand_operation op = NAND_OPERATION(instrs);
2226
2227 return nand_exec_op(chip, &op);
2228 }
2229
Boris Brezillon97d90da2017-11-30 18:01:29 +01002230 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
2231 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2232 params[i] = chip->read_byte(mtd);
2233
2234 return 0;
2235}
2236
Boris Brezillon52f05b62018-07-27 09:44:18 +02002237static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
2238 unsigned int delay_ns)
2239{
2240 if (chip->exec_op) {
2241 struct nand_op_instr instrs[] = {
2242 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
2243 PSEC_TO_NSEC(delay_ns)),
2244 };
2245 struct nand_operation op = NAND_OPERATION(instrs);
2246
2247 return nand_exec_op(chip, &op);
2248 }
2249
2250 /* Apply delay or wait for ready/busy pin */
2251 if (!chip->dev_ready)
2252 udelay(chip->chip_delay);
2253 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02002254 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02002255
2256 return 0;
2257}
2258
Boris Brezillon97d90da2017-11-30 18:01:29 +01002259/**
2260 * nand_reset_op - Do a reset operation
2261 * @chip: The NAND chip
2262 *
2263 * This function sends a RESET command and waits for the NAND to be ready
2264 * before returning.
2265 * This function does not select/unselect the CS line.
2266 *
2267 * Returns 0 on success, a negative error code otherwise.
2268 */
2269int nand_reset_op(struct nand_chip *chip)
2270{
2271 struct mtd_info *mtd = nand_to_mtd(chip);
2272
Miquel Raynal8878b122017-11-09 14:16:45 +01002273 if (chip->exec_op) {
2274 const struct nand_sdr_timings *sdr =
2275 nand_get_sdr_timings(&chip->data_interface);
2276 struct nand_op_instr instrs[] = {
2277 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2278 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2279 };
2280 struct nand_operation op = NAND_OPERATION(instrs);
2281
2282 return nand_exec_op(chip, &op);
2283 }
2284
Boris Brezillon97d90da2017-11-30 18:01:29 +01002285 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2286
2287 return 0;
2288}
2289EXPORT_SYMBOL_GPL(nand_reset_op);
2290
2291/**
2292 * nand_read_data_op - Read data from the NAND
2293 * @chip: The NAND chip
2294 * @buf: buffer used to store the data
2295 * @len: length of the buffer
2296 * @force_8bit: force 8-bit bus access
2297 *
2298 * This function does a raw data read on the bus. Usually used after launching
2299 * another NAND operation like nand_read_page_op().
2300 * This function does not select/unselect the CS line.
2301 *
2302 * Returns 0 on success, a negative error code otherwise.
2303 */
2304int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2305 bool force_8bit)
2306{
2307 struct mtd_info *mtd = nand_to_mtd(chip);
2308
2309 if (!len || !buf)
2310 return -EINVAL;
2311
Miquel Raynal8878b122017-11-09 14:16:45 +01002312 if (chip->exec_op) {
2313 struct nand_op_instr instrs[] = {
2314 NAND_OP_DATA_IN(len, buf, 0),
2315 };
2316 struct nand_operation op = NAND_OPERATION(instrs);
2317
2318 instrs[0].ctx.data.force_8bit = force_8bit;
2319
2320 return nand_exec_op(chip, &op);
2321 }
2322
Boris Brezillon97d90da2017-11-30 18:01:29 +01002323 if (force_8bit) {
2324 u8 *p = buf;
2325 unsigned int i;
2326
2327 for (i = 0; i < len; i++)
2328 p[i] = chip->read_byte(mtd);
2329 } else {
2330 chip->read_buf(mtd, buf, len);
2331 }
2332
2333 return 0;
2334}
2335EXPORT_SYMBOL_GPL(nand_read_data_op);
2336
2337/**
2338 * nand_write_data_op - Write data from the NAND
2339 * @chip: The NAND chip
2340 * @buf: buffer containing the data to send on the bus
2341 * @len: length of the buffer
2342 * @force_8bit: force 8-bit bus access
2343 *
2344 * This function does a raw data write on the bus. Usually used after launching
2345 * another NAND operation like nand_write_page_begin_op().
2346 * This function does not select/unselect the CS line.
2347 *
2348 * Returns 0 on success, a negative error code otherwise.
2349 */
2350int nand_write_data_op(struct nand_chip *chip, const void *buf,
2351 unsigned int len, bool force_8bit)
2352{
2353 struct mtd_info *mtd = nand_to_mtd(chip);
2354
2355 if (!len || !buf)
2356 return -EINVAL;
2357
Miquel Raynal8878b122017-11-09 14:16:45 +01002358 if (chip->exec_op) {
2359 struct nand_op_instr instrs[] = {
2360 NAND_OP_DATA_OUT(len, buf, 0),
2361 };
2362 struct nand_operation op = NAND_OPERATION(instrs);
2363
2364 instrs[0].ctx.data.force_8bit = force_8bit;
2365
2366 return nand_exec_op(chip, &op);
2367 }
2368
Boris Brezillon97d90da2017-11-30 18:01:29 +01002369 if (force_8bit) {
2370 const u8 *p = buf;
2371 unsigned int i;
2372
2373 for (i = 0; i < len; i++)
2374 chip->write_byte(mtd, p[i]);
2375 } else {
2376 chip->write_buf(mtd, buf, len);
2377 }
2378
2379 return 0;
2380}
2381EXPORT_SYMBOL_GPL(nand_write_data_op);
2382
2383/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002384 * struct nand_op_parser_ctx - Context used by the parser
2385 * @instrs: array of all the instructions that must be addressed
2386 * @ninstrs: length of the @instrs array
2387 * @subop: Sub-operation to be passed to the NAND controller
2388 *
2389 * This structure is used by the core to split NAND operations into
2390 * sub-operations that can be handled by the NAND controller.
2391 */
2392struct nand_op_parser_ctx {
2393 const struct nand_op_instr *instrs;
2394 unsigned int ninstrs;
2395 struct nand_subop subop;
2396};
2397
2398/**
2399 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2400 * @pat: the parser pattern element that matches @instr
2401 * @instr: pointer to the instruction to check
2402 * @start_offset: this is an in/out parameter. If @instr has already been
2403 * split, then @start_offset is the offset from which to start
2404 * (either an address cycle or an offset in the data buffer).
2405 * Conversely, if the function returns true (ie. instr must be
2406 * split), this parameter is updated to point to the first
2407 * data/address cycle that has not been taken care of.
2408 *
2409 * Some NAND controllers are limited and cannot send X address cycles with a
2410 * unique operation, or cannot read/write more than Y bytes at the same time.
2411 * In this case, split the instruction that does not fit in a single
2412 * controller-operation into two or more chunks.
2413 *
2414 * Returns true if the instruction must be split, false otherwise.
2415 * The @start_offset parameter is also updated to the offset at which the next
2416 * bundle of instruction must start (if an address or a data instruction).
2417 */
2418static bool
2419nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2420 const struct nand_op_instr *instr,
2421 unsigned int *start_offset)
2422{
2423 switch (pat->type) {
2424 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002425 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002426 break;
2427
2428 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002429 pat->ctx.addr.maxcycles) {
2430 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002431 return true;
2432 }
2433 break;
2434
2435 case NAND_OP_DATA_IN_INSTR:
2436 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002437 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002438 break;
2439
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002440 if (instr->ctx.data.len - *start_offset >
2441 pat->ctx.data.maxlen) {
2442 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002443 return true;
2444 }
2445 break;
2446
2447 default:
2448 break;
2449 }
2450
2451 return false;
2452}
2453
2454/**
2455 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2456 * remaining in the parser context
2457 * @pat: the pattern to test
2458 * @ctx: the parser context structure to match with the pattern @pat
2459 *
2460 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2461 * Returns true if this is the case, false ortherwise. When true is returned,
2462 * @ctx->subop is updated with the set of instructions to be passed to the
2463 * controller driver.
2464 */
2465static bool
2466nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2467 struct nand_op_parser_ctx *ctx)
2468{
2469 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2470 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2471 const struct nand_op_instr *instr = ctx->subop.instrs;
2472 unsigned int i, ninstrs;
2473
2474 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2475 /*
2476 * The pattern instruction does not match the operation
2477 * instruction. If the instruction is marked optional in the
2478 * pattern definition, we skip the pattern element and continue
2479 * to the next one. If the element is mandatory, there's no
2480 * match and we can return false directly.
2481 */
2482 if (instr->type != pat->elems[i].type) {
2483 if (!pat->elems[i].optional)
2484 return false;
2485
2486 continue;
2487 }
2488
2489 /*
2490 * Now check the pattern element constraints. If the pattern is
2491 * not able to handle the whole instruction in a single step,
2492 * we have to split it.
2493 * The last_instr_end_off value comes back updated to point to
2494 * the position where we have to split the instruction (the
2495 * start of the next subop chunk).
2496 */
2497 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2498 &instr_offset)) {
2499 ninstrs++;
2500 i++;
2501 break;
2502 }
2503
2504 instr++;
2505 ninstrs++;
2506 instr_offset = 0;
2507 }
2508
2509 /*
2510 * This can happen if all instructions of a pattern are optional.
2511 * Still, if there's not at least one instruction handled by this
2512 * pattern, this is not a match, and we should try the next one (if
2513 * any).
2514 */
2515 if (!ninstrs)
2516 return false;
2517
2518 /*
2519 * We had a match on the pattern head, but the pattern may be longer
2520 * than the instructions we're asked to execute. We need to make sure
2521 * there's no mandatory elements in the pattern tail.
2522 */
2523 for (; i < pat->nelems; i++) {
2524 if (!pat->elems[i].optional)
2525 return false;
2526 }
2527
2528 /*
2529 * We have a match: update the subop structure accordingly and return
2530 * true.
2531 */
2532 ctx->subop.ninstrs = ninstrs;
2533 ctx->subop.last_instr_end_off = instr_offset;
2534
2535 return true;
2536}
2537
2538#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2539static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2540{
2541 const struct nand_op_instr *instr;
2542 char *prefix = " ";
2543 unsigned int i;
2544
2545 pr_debug("executing subop:\n");
2546
2547 for (i = 0; i < ctx->ninstrs; i++) {
2548 instr = &ctx->instrs[i];
2549
2550 if (instr == &ctx->subop.instrs[0])
2551 prefix = " ->";
2552
2553 switch (instr->type) {
2554 case NAND_OP_CMD_INSTR:
2555 pr_debug("%sCMD [0x%02x]\n", prefix,
2556 instr->ctx.cmd.opcode);
2557 break;
2558 case NAND_OP_ADDR_INSTR:
2559 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2560 instr->ctx.addr.naddrs,
2561 instr->ctx.addr.naddrs < 64 ?
2562 instr->ctx.addr.naddrs : 64,
2563 instr->ctx.addr.addrs);
2564 break;
2565 case NAND_OP_DATA_IN_INSTR:
2566 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2567 instr->ctx.data.len,
2568 instr->ctx.data.force_8bit ?
2569 ", force 8-bit" : "");
2570 break;
2571 case NAND_OP_DATA_OUT_INSTR:
2572 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2573 instr->ctx.data.len,
2574 instr->ctx.data.force_8bit ?
2575 ", force 8-bit" : "");
2576 break;
2577 case NAND_OP_WAITRDY_INSTR:
2578 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2579 instr->ctx.waitrdy.timeout_ms);
2580 break;
2581 }
2582
2583 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2584 prefix = " ";
2585 }
2586}
2587#else
2588static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2589{
2590 /* NOP */
2591}
2592#endif
2593
2594/**
2595 * nand_op_parser_exec_op - exec_op parser
2596 * @chip: the NAND chip
2597 * @parser: patterns description provided by the controller driver
2598 * @op: the NAND operation to address
2599 * @check_only: when true, the function only checks if @op can be handled but
2600 * does not execute the operation
2601 *
2602 * Helper function designed to ease integration of NAND controller drivers that
2603 * only support a limited set of instruction sequences. The supported sequences
2604 * are described in @parser, and the framework takes care of splitting @op into
2605 * multiple sub-operations (if required) and pass them back to the ->exec()
2606 * callback of the matching pattern if @check_only is set to false.
2607 *
2608 * NAND controller drivers should call this function from their own ->exec_op()
2609 * implementation.
2610 *
2611 * Returns 0 on success, a negative error code otherwise. A failure can be
2612 * caused by an unsupported operation (none of the supported patterns is able
2613 * to handle the requested operation), or an error returned by one of the
2614 * matching pattern->exec() hook.
2615 */
2616int nand_op_parser_exec_op(struct nand_chip *chip,
2617 const struct nand_op_parser *parser,
2618 const struct nand_operation *op, bool check_only)
2619{
2620 struct nand_op_parser_ctx ctx = {
2621 .subop.instrs = op->instrs,
2622 .instrs = op->instrs,
2623 .ninstrs = op->ninstrs,
2624 };
2625 unsigned int i;
2626
2627 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2628 int ret;
2629
2630 for (i = 0; i < parser->npatterns; i++) {
2631 const struct nand_op_parser_pattern *pattern;
2632
2633 pattern = &parser->patterns[i];
2634 if (!nand_op_parser_match_pat(pattern, &ctx))
2635 continue;
2636
2637 nand_op_parser_trace(&ctx);
2638
2639 if (check_only)
2640 break;
2641
2642 ret = pattern->exec(chip, &ctx.subop);
2643 if (ret)
2644 return ret;
2645
2646 break;
2647 }
2648
2649 if (i == parser->npatterns) {
2650 pr_debug("->exec_op() parser: pattern not found!\n");
2651 return -ENOTSUPP;
2652 }
2653
2654 /*
2655 * Update the context structure by pointing to the start of the
2656 * next subop.
2657 */
2658 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2659 if (ctx.subop.last_instr_end_off)
2660 ctx.subop.instrs -= 1;
2661
2662 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2663 }
2664
2665 return 0;
2666}
2667EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2668
2669static bool nand_instr_is_data(const struct nand_op_instr *instr)
2670{
2671 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2672 instr->type == NAND_OP_DATA_OUT_INSTR);
2673}
2674
2675static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2676 unsigned int instr_idx)
2677{
2678 return subop && instr_idx < subop->ninstrs;
2679}
2680
Miquel Raynal760c4352018-07-19 00:09:12 +02002681static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2682 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002683{
2684 if (instr_idx)
2685 return 0;
2686
2687 return subop->first_instr_start_off;
2688}
2689
2690/**
2691 * nand_subop_get_addr_start_off - Get the start offset in an address array
2692 * @subop: The entire sub-operation
2693 * @instr_idx: Index of the instruction inside the sub-operation
2694 *
2695 * During driver development, one could be tempted to directly use the
2696 * ->addr.addrs field of address instructions. This is wrong as address
2697 * instructions might be split.
2698 *
2699 * Given an address instruction, returns the offset of the first cycle to issue.
2700 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002701unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2702 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002703{
Miquel Raynal760c4352018-07-19 00:09:12 +02002704 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2705 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2706 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002707
2708 return nand_subop_get_start_off(subop, instr_idx);
2709}
2710EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2711
2712/**
2713 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2714 * @subop: The entire sub-operation
2715 * @instr_idx: Index of the instruction inside the sub-operation
2716 *
2717 * During driver development, one could be tempted to directly use the
2718 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2719 * might be split.
2720 *
2721 * Given an address instruction, returns the number of address cycle to issue.
2722 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002723unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2724 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002725{
2726 int start_off, end_off;
2727
Miquel Raynal760c4352018-07-19 00:09:12 +02002728 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2729 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2730 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002731
2732 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2733
2734 if (instr_idx == subop->ninstrs - 1 &&
2735 subop->last_instr_end_off)
2736 end_off = subop->last_instr_end_off;
2737 else
2738 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2739
2740 return end_off - start_off;
2741}
2742EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2743
2744/**
2745 * nand_subop_get_data_start_off - Get the start offset in a data array
2746 * @subop: The entire sub-operation
2747 * @instr_idx: Index of the instruction inside the sub-operation
2748 *
2749 * During driver development, one could be tempted to directly use the
2750 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2751 * instructions might be split.
2752 *
2753 * Given a data instruction, returns the offset to start from.
2754 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002755unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2756 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002757{
Miquel Raynal760c4352018-07-19 00:09:12 +02002758 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2759 !nand_instr_is_data(&subop->instrs[instr_idx])))
2760 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002761
2762 return nand_subop_get_start_off(subop, instr_idx);
2763}
2764EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2765
2766/**
2767 * nand_subop_get_data_len - Get the number of bytes to retrieve
2768 * @subop: The entire sub-operation
2769 * @instr_idx: Index of the instruction inside the sub-operation
2770 *
2771 * During driver development, one could be tempted to directly use the
2772 * ->data->len field of a data instruction. This is wrong as data instructions
2773 * might be split.
2774 *
2775 * Returns the length of the chunk of data to send/receive.
2776 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002777unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2778 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002779{
2780 int start_off = 0, end_off;
2781
Miquel Raynal760c4352018-07-19 00:09:12 +02002782 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2783 !nand_instr_is_data(&subop->instrs[instr_idx])))
2784 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002785
2786 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2787
2788 if (instr_idx == subop->ninstrs - 1 &&
2789 subop->last_instr_end_off)
2790 end_off = subop->last_instr_end_off;
2791 else
2792 end_off = subop->instrs[instr_idx].ctx.data.len;
2793
2794 return end_off - start_off;
2795}
2796EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002799 * nand_reset - Reset and initialize a NAND device
2800 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002801 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002802 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002803 * Save the timings data structure, then apply SDR timings mode 0 (see
2804 * nand_reset_data_interface for details), do the reset operation, and
2805 * apply back the previous timings.
2806 *
2807 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002808 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002809int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002810{
2811 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal17fa8042017-11-30 18:01:31 +01002812 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002813 int ret;
2814
Boris Brezillon104e4422017-03-16 09:35:58 +01002815 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002816 if (ret)
2817 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002818
Boris Brezillon73f907f2016-10-24 16:46:20 +02002819 /*
2820 * The CS line has to be released before we can apply the new NAND
2821 * interface settings, hence this weird ->select_chip() dance.
2822 */
2823 chip->select_chip(mtd, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002824 ret = nand_reset_op(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02002825 chip->select_chip(mtd, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002826 if (ret)
2827 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002828
Miquel Raynal107b7d62018-03-19 14:47:25 +01002829 /*
2830 * A nand_reset_data_interface() put both the NAND chip and the NAND
2831 * controller in timings mode 0. If the default mode for this chip is
2832 * also 0, no need to proceed to the change again. Plus, at probe time,
2833 * nand_setup_data_interface() uses ->set/get_features() which would
2834 * fail anyway as the parameter page is not available yet.
2835 */
2836 if (!chip->onfi_timing_mode_default)
2837 return 0;
2838
Miquel Raynal17fa8042017-11-30 18:01:31 +01002839 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002840 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002841 if (ret)
2842 return ret;
2843
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002844 return 0;
2845}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002846EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002847
2848/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002849 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2850 * @buf: buffer to test
2851 * @len: buffer length
2852 * @bitflips_threshold: maximum number of bitflips
2853 *
2854 * Check if a buffer contains only 0xff, which means the underlying region
2855 * has been erased and is ready to be programmed.
2856 * The bitflips_threshold specify the maximum number of bitflips before
2857 * considering the region is not erased.
2858 * Note: The logic of this function has been extracted from the memweight
2859 * implementation, except that nand_check_erased_buf function exit before
2860 * testing the whole buffer if the number of bitflips exceed the
2861 * bitflips_threshold value.
2862 *
2863 * Returns a positive number of bitflips less than or equal to
2864 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2865 * threshold.
2866 */
2867static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2868{
2869 const unsigned char *bitmap = buf;
2870 int bitflips = 0;
2871 int weight;
2872
2873 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2874 len--, bitmap++) {
2875 weight = hweight8(*bitmap);
2876 bitflips += BITS_PER_BYTE - weight;
2877 if (unlikely(bitflips > bitflips_threshold))
2878 return -EBADMSG;
2879 }
2880
2881 for (; len >= sizeof(long);
2882 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002883 unsigned long d = *((unsigned long *)bitmap);
2884 if (d == ~0UL)
2885 continue;
2886 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002887 bitflips += BITS_PER_LONG - weight;
2888 if (unlikely(bitflips > bitflips_threshold))
2889 return -EBADMSG;
2890 }
2891
2892 for (; len > 0; len--, bitmap++) {
2893 weight = hweight8(*bitmap);
2894 bitflips += BITS_PER_BYTE - weight;
2895 if (unlikely(bitflips > bitflips_threshold))
2896 return -EBADMSG;
2897 }
2898
2899 return bitflips;
2900}
2901
2902/**
2903 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2904 * 0xff data
2905 * @data: data buffer to test
2906 * @datalen: data length
2907 * @ecc: ECC buffer
2908 * @ecclen: ECC length
2909 * @extraoob: extra OOB buffer
2910 * @extraooblen: extra OOB length
2911 * @bitflips_threshold: maximum number of bitflips
2912 *
2913 * Check if a data buffer and its associated ECC and OOB data contains only
2914 * 0xff pattern, which means the underlying region has been erased and is
2915 * ready to be programmed.
2916 * The bitflips_threshold specify the maximum number of bitflips before
2917 * considering the region as not erased.
2918 *
2919 * Note:
2920 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2921 * different from the NAND page size. When fixing bitflips, ECC engines will
2922 * report the number of errors per chunk, and the NAND core infrastructure
2923 * expect you to return the maximum number of bitflips for the whole page.
2924 * This is why you should always use this function on a single chunk and
2925 * not on the whole page. After checking each chunk you should update your
2926 * max_bitflips value accordingly.
2927 * 2/ When checking for bitflips in erased pages you should not only check
2928 * the payload data but also their associated ECC data, because a user might
2929 * have programmed almost all bits to 1 but a few. In this case, we
2930 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2931 * this case.
2932 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2933 * data are protected by the ECC engine.
2934 * It could also be used if you support subpages and want to attach some
2935 * extra OOB data to an ECC chunk.
2936 *
2937 * Returns a positive number of bitflips less than or equal to
2938 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2939 * threshold. In case of success, the passed buffers are filled with 0xff.
2940 */
2941int nand_check_erased_ecc_chunk(void *data, int datalen,
2942 void *ecc, int ecclen,
2943 void *extraoob, int extraooblen,
2944 int bitflips_threshold)
2945{
2946 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2947
2948 data_bitflips = nand_check_erased_buf(data, datalen,
2949 bitflips_threshold);
2950 if (data_bitflips < 0)
2951 return data_bitflips;
2952
2953 bitflips_threshold -= data_bitflips;
2954
2955 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2956 if (ecc_bitflips < 0)
2957 return ecc_bitflips;
2958
2959 bitflips_threshold -= ecc_bitflips;
2960
2961 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2962 bitflips_threshold);
2963 if (extraoob_bitflips < 0)
2964 return extraoob_bitflips;
2965
2966 if (data_bitflips)
2967 memset(data, 0xff, datalen);
2968
2969 if (ecc_bitflips)
2970 memset(ecc, 0xff, ecclen);
2971
2972 if (extraoob_bitflips)
2973 memset(extraoob, 0xff, extraooblen);
2974
2975 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2976}
2977EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2978
2979/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002980 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002981 * @chip: nand chip info structure
2982 * @buf: buffer to store read data
2983 * @oob_required: caller requires OOB data read to chip->oob_poi
2984 * @page: page number to read
2985 *
2986 * Returns -ENOTSUPP unconditionally.
2987 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002988int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2989 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002990{
2991 return -ENOTSUPP;
2992}
2993EXPORT_SYMBOL(nand_read_page_raw_notsupp);
2994
2995/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002996 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002997 * @chip: nand chip info structure
2998 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002999 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003000 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003001 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003002 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003003 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003004int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
3005 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003006{
Boris Brezillonb9761682018-09-06 14:05:20 +02003007 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003008 int ret;
3009
Boris Brezillon25f815f2017-11-30 18:01:30 +01003010 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003011 if (ret)
3012 return ret;
3013
3014 if (oob_required) {
3015 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
3016 false);
3017 if (ret)
3018 return ret;
3019 }
3020
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003021 return 0;
3022}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003023EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003024
3025/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003026 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07003027 * @chip: nand chip info structure
3028 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003029 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003030 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003031 *
3032 * We need a special oob layout and handling even when OOB isn't used.
3033 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003034static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07003035 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003036{
Boris Brezillonb9761682018-09-06 14:05:20 +02003037 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003038 int eccsize = chip->ecc.size;
3039 int eccbytes = chip->ecc.bytes;
3040 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003041 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003042
Boris Brezillon25f815f2017-11-30 18:01:30 +01003043 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3044 if (ret)
3045 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003046
3047 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003048 ret = nand_read_data_op(chip, buf, eccsize, false);
3049 if (ret)
3050 return ret;
3051
David Brownell52ff49d2009-03-04 12:01:36 -08003052 buf += eccsize;
3053
3054 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003055 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3056 false);
3057 if (ret)
3058 return ret;
3059
David Brownell52ff49d2009-03-04 12:01:36 -08003060 oob += chip->ecc.prepad;
3061 }
3062
Boris Brezillon97d90da2017-11-30 18:01:29 +01003063 ret = nand_read_data_op(chip, oob, eccbytes, false);
3064 if (ret)
3065 return ret;
3066
David Brownell52ff49d2009-03-04 12:01:36 -08003067 oob += eccbytes;
3068
3069 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003070 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3071 false);
3072 if (ret)
3073 return ret;
3074
David Brownell52ff49d2009-03-04 12:01:36 -08003075 oob += chip->ecc.postpad;
3076 }
3077 }
3078
3079 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003080 if (size) {
3081 ret = nand_read_data_op(chip, oob, size, false);
3082 if (ret)
3083 return ret;
3084 }
David Brownell52ff49d2009-03-04 12:01:36 -08003085
3086 return 0;
3087}
3088
3089/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003090 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003091 * @chip: nand chip info structure
3092 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003093 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003094 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003095 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003096static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
3097 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Boris Brezillonb9761682018-09-06 14:05:20 +02003099 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003100 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003101 int eccbytes = chip->ecc.bytes;
3102 int eccsteps = chip->ecc.steps;
3103 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003104 uint8_t *ecc_calc = chip->ecc.calc_buf;
3105 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003106 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003107
Boris Brezillonb9761682018-09-06 14:05:20 +02003108 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003109
3110 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003111 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003112
Boris Brezillon846031d2016-02-03 20:11:00 +01003113 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3114 chip->ecc.total);
3115 if (ret)
3116 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003117
3118 eccsteps = chip->ecc.steps;
3119 p = buf;
3120
3121 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3122 int stat;
3123
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003124 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003125 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003126 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003127 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003128 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003129 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3130 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003131 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003132 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003133}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303136 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003137 * @chip: nand chip info structure
3138 * @data_offs: offset of requested data within the page
3139 * @readlen: data length
3140 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003141 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003142 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003143static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
3144 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003145{
Boris Brezillonb9761682018-09-06 14:05:20 +02003146 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003147 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003148 uint8_t *p;
3149 int data_col_addr, i, gaps = 0;
3150 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3151 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003152 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003153 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003154 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003155
Brian Norris7854d3f2011-06-23 14:12:08 -07003156 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003157 start_step = data_offs / chip->ecc.size;
3158 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3159 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303160 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003161
Brian Norris8b6e50c2011-05-25 14:59:01 -07003162 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003163 datafrag_len = num_steps * chip->ecc.size;
3164 eccfrag_len = num_steps * chip->ecc.bytes;
3165
3166 data_col_addr = start_step * chip->ecc.size;
3167 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003168 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003169 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003170 if (ret)
3171 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003172
Brian Norris8b6e50c2011-05-25 14:59:01 -07003173 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003174 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003175 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003176
Brian Norris8b6e50c2011-05-25 14:59:01 -07003177 /*
3178 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003179 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003180 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003181 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3182 if (ret)
3183 return ret;
3184
3185 if (oobregion.length < eccfrag_len)
3186 gaps = 1;
3187
Alexey Korolev3d459552008-05-15 17:23:18 +01003188 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003189 ret = nand_change_read_column_op(chip, mtd->writesize,
3190 chip->oob_poi, mtd->oobsize,
3191 false);
3192 if (ret)
3193 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003194 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003195 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003196 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003197 * about buswidth alignment in read_buf.
3198 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003199 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003200 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003201 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003202 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003203 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3204 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003205 aligned_len++;
3206
Boris Brezillon97d90da2017-11-30 18:01:29 +01003207 ret = nand_change_read_column_op(chip,
3208 mtd->writesize + aligned_pos,
3209 &chip->oob_poi[aligned_pos],
3210 aligned_len, false);
3211 if (ret)
3212 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003213 }
3214
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003215 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003216 chip->oob_poi, index, eccfrag_len);
3217 if (ret)
3218 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003219
3220 p = bufpoi + data_col_addr;
3221 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3222 int stat;
3223
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003224 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003225 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003226 if (stat == -EBADMSG &&
3227 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3228 /* check for empty pages with bitflips */
3229 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003230 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003231 chip->ecc.bytes,
3232 NULL, 0,
3233 chip->ecc.strength);
3234 }
3235
Mike Dunn3f91e942012-04-25 12:06:09 -07003236 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003237 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003238 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003239 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003240 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3241 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003242 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003243 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003244}
3245
3246/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003247 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003248 * @chip: nand chip info structure
3249 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003250 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003251 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003252 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003253 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003254 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003255static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
3256 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003257{
Boris Brezillonb9761682018-09-06 14:05:20 +02003258 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003259 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003260 int eccbytes = chip->ecc.bytes;
3261 int eccsteps = chip->ecc.steps;
3262 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003263 uint8_t *ecc_calc = chip->ecc.calc_buf;
3264 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003265 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003266
Boris Brezillon25f815f2017-11-30 18:01:30 +01003267 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3268 if (ret)
3269 return ret;
3270
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003271 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003272 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003273
3274 ret = nand_read_data_op(chip, p, eccsize, false);
3275 if (ret)
3276 return ret;
3277
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003278 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003279 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003280
3281 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3282 if (ret)
3283 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003284
Boris Brezillon846031d2016-02-03 20:11:00 +01003285 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3286 chip->ecc.total);
3287 if (ret)
3288 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003289
3290 eccsteps = chip->ecc.steps;
3291 p = buf;
3292
3293 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3294 int stat;
3295
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003296 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003297 if (stat == -EBADMSG &&
3298 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3299 /* check for empty pages with bitflips */
3300 stat = nand_check_erased_ecc_chunk(p, eccsize,
3301 &ecc_code[i], eccbytes,
3302 NULL, 0,
3303 chip->ecc.strength);
3304 }
3305
Mike Dunn3f91e942012-04-25 12:06:09 -07003306 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003307 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003308 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003309 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003310 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3311 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003312 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003313 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003314}
3315
3316/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003317 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003318 * @chip: nand chip info structure
3319 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003320 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003321 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003322 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003323 * Hardware ECC for large page chips, require OOB to be read first. For this
3324 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3325 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3326 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3327 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003328 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003329static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
3330 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003331{
Boris Brezillonb9761682018-09-06 14:05:20 +02003332 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003333 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003334 int eccbytes = chip->ecc.bytes;
3335 int eccsteps = chip->ecc.steps;
3336 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003337 uint8_t *ecc_code = chip->ecc.code_buf;
3338 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003339 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003340
3341 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003342 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3343 if (ret)
3344 return ret;
3345
3346 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3347 if (ret)
3348 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003349
Boris Brezillon846031d2016-02-03 20:11:00 +01003350 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3351 chip->ecc.total);
3352 if (ret)
3353 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003354
3355 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3356 int stat;
3357
Boris Brezillonec476362018-09-06 14:05:17 +02003358 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003359
3360 ret = nand_read_data_op(chip, p, eccsize, false);
3361 if (ret)
3362 return ret;
3363
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003364 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003365
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003366 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003367 if (stat == -EBADMSG &&
3368 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3369 /* check for empty pages with bitflips */
3370 stat = nand_check_erased_ecc_chunk(p, eccsize,
3371 &ecc_code[i], eccbytes,
3372 NULL, 0,
3373 chip->ecc.strength);
3374 }
3375
Mike Dunn3f91e942012-04-25 12:06:09 -07003376 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003377 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003378 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003379 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003380 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3381 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003382 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003383 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003384}
3385
3386/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003387 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003388 * @chip: nand chip info structure
3389 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003390 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003391 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003392 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003393 * The hw generator calculates the error syndrome automatically. Therefore we
3394 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003395 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003396static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3397 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003398{
Boris Brezillonb9761682018-09-06 14:05:20 +02003399 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003400 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003401 int eccbytes = chip->ecc.bytes;
3402 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003403 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003404 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003405 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003406 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003407
Boris Brezillon25f815f2017-11-30 18:01:30 +01003408 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3409 if (ret)
3410 return ret;
3411
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003412 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3413 int stat;
3414
Boris Brezillonec476362018-09-06 14:05:17 +02003415 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003416
3417 ret = nand_read_data_op(chip, p, eccsize, false);
3418 if (ret)
3419 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003420
3421 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003422 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3423 false);
3424 if (ret)
3425 return ret;
3426
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003427 oob += chip->ecc.prepad;
3428 }
3429
Boris Brezillonec476362018-09-06 14:05:17 +02003430 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003431
3432 ret = nand_read_data_op(chip, oob, eccbytes, false);
3433 if (ret)
3434 return ret;
3435
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003436 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003437
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003438 oob += eccbytes;
3439
3440 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003441 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3442 false);
3443 if (ret)
3444 return ret;
3445
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003446 oob += chip->ecc.postpad;
3447 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003448
3449 if (stat == -EBADMSG &&
3450 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3451 /* check for empty pages with bitflips */
3452 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3453 oob - eccpadbytes,
3454 eccpadbytes,
3455 NULL, 0,
3456 chip->ecc.strength);
3457 }
3458
3459 if (stat < 0) {
3460 mtd->ecc_stats.failed++;
3461 } else {
3462 mtd->ecc_stats.corrected += stat;
3463 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3464 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003465 }
3466
3467 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003468 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003469 if (i) {
3470 ret = nand_read_data_op(chip, oob, i, false);
3471 if (ret)
3472 return ret;
3473 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003474
Mike Dunn3f91e942012-04-25 12:06:09 -07003475 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003476}
3477
3478/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003479 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003480 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003481 * @oob: oob destination address
3482 * @ops: oob ops structure
3483 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003484 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003485static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003486 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003487{
Boris Brezillon846031d2016-02-03 20:11:00 +01003488 struct nand_chip *chip = mtd_to_nand(mtd);
3489 int ret;
3490
Florian Fainellif8ac0412010-09-07 13:23:43 +02003491 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003492
Brian Norris0612b9d2011-08-30 18:45:40 -07003493 case MTD_OPS_PLACE_OOB:
3494 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003495 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3496 return oob + len;
3497
Boris Brezillon846031d2016-02-03 20:11:00 +01003498 case MTD_OPS_AUTO_OOB:
3499 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3500 ops->ooboffs, len);
3501 BUG_ON(ret);
3502 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003503
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003504 default:
3505 BUG();
3506 }
3507 return NULL;
3508}
3509
3510/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003511 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3512 * @mtd: MTD device structure
3513 * @retry_mode: the retry mode to use
3514 *
3515 * Some vendors supply a special command to shift the Vt threshold, to be used
3516 * when there are too many bitflips in a page (i.e., ECC error). After setting
3517 * a new threshold, the host should retry reading the page.
3518 */
3519static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3520{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003521 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08003522
3523 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3524
3525 if (retry_mode >= chip->read_retries)
3526 return -EINVAL;
3527
3528 if (!chip->setup_read_retry)
3529 return -EOPNOTSUPP;
3530
3531 return chip->setup_read_retry(mtd, retry_mode);
3532}
3533
Boris Brezillon85e08e52018-07-27 09:44:17 +02003534static void nand_wait_readrdy(struct nand_chip *chip)
3535{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003536 const struct nand_sdr_timings *sdr;
3537
Boris Brezillon85e08e52018-07-27 09:44:17 +02003538 if (!(chip->options & NAND_NEED_READRDY))
3539 return;
3540
Boris Brezillon52f05b62018-07-27 09:44:18 +02003541 sdr = nand_get_sdr_timings(&chip->data_interface);
3542 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003543}
3544
Brian Norrisba84fb52014-01-03 15:13:33 -08003545/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003546 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003547 * @mtd: MTD device structure
3548 * @from: offset to read from
3549 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003550 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003551 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003552 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003553static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3554 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003555{
Brian Norrise47f3db2012-05-02 10:14:56 -07003556 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003557 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003558 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003559 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003560 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003561 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003562
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003563 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003564 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003565 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003566 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003567 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003569 chipnr = (int)(from >> chip->chip_shift);
3570 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003572 realpage = (int)(from >> chip->page_shift);
3573 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003575 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003577 buf = ops->datbuf;
3578 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003579 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003580
Florian Fainellif8ac0412010-09-07 13:23:43 +02003581 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003582 unsigned int ecc_failures = mtd->ecc_stats.failed;
3583
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003584 bytes = min(mtd->writesize - col, readlen);
3585 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003586
Kamal Dasu66507c72014-05-01 20:51:19 -04003587 if (!aligned)
3588 use_bufpoi = 1;
3589 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003590 use_bufpoi = !virt_addr_valid(buf) ||
3591 !IS_ALIGNED((unsigned long)buf,
3592 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003593 else
3594 use_bufpoi = 0;
3595
Brian Norris8b6e50c2011-05-25 14:59:01 -07003596 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003597 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003598 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003599
3600 if (use_bufpoi && aligned)
3601 pr_debug("%s: using read bounce buffer for buf@%p\n",
3602 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Brian Norrisba84fb52014-01-03 15:13:33 -08003604read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003605 /*
3606 * Now read the page into the buffer. Absent an error,
3607 * the read methods return max bitflips per ecc step.
3608 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003609 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003610 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003611 oob_required,
3612 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003613 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3614 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003615 ret = chip->ecc.read_subpage(chip, col, bytes,
3616 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003617 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003618 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003619 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003620 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003621 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003622 /* Invalidate page cache */
3623 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003624 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003625 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003626
3627 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003628 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003629 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003630 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003631 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003632 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003633 chip->pagebuf_bitflips = ret;
3634 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003635 /* Invalidate page cache */
3636 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003637 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003638 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003640
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003641 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003642 int toread = min(oobreadlen, max_oobsize);
3643
3644 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003645 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003646 oob, ops, toread);
3647 oobreadlen -= toread;
3648 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003649 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003650
Boris Brezillon85e08e52018-07-27 09:44:17 +02003651 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003652
Brian Norrisba84fb52014-01-03 15:13:33 -08003653 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003654 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003655 retry_mode++;
3656 ret = nand_setup_read_retry(mtd,
3657 retry_mode);
3658 if (ret < 0)
3659 break;
3660
3661 /* Reset failures; retry */
3662 mtd->ecc_stats.failed = ecc_failures;
3663 goto read_retry;
3664 } else {
3665 /* No more retry modes; real failure */
3666 ecc_fail = true;
3667 }
3668 }
3669
3670 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003671 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003672 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003673 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003674 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003675 max_bitflips = max_t(unsigned int, max_bitflips,
3676 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003679 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003680
Brian Norrisba84fb52014-01-03 15:13:33 -08003681 /* Reset to retry mode 0 */
3682 if (retry_mode) {
3683 ret = nand_setup_read_retry(mtd, 0);
3684 if (ret < 0)
3685 break;
3686 retry_mode = 0;
3687 }
3688
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003689 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003690 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Brian Norris8b6e50c2011-05-25 14:59:01 -07003692 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 col = 0;
3694 /* Increment page address */
3695 realpage++;
3696
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003697 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 /* Check, if we cross a chip boundary */
3699 if (!page) {
3700 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003701 chip->select_chip(mtd, -1);
3702 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003705 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003707 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003708 if (oob)
3709 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
Mike Dunn3f91e942012-04-25 12:06:09 -07003711 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003712 return ret;
3713
Brian Norrisb72f3df2013-12-03 11:04:14 -08003714 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003715 return -EBADMSG;
3716
Mike Dunnedbc45402012-04-25 12:06:11 -07003717 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003718}
3719
3720/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003721 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003722 * @chip: nand chip info structure
3723 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003724 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003725int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003726{
Boris Brezillonb9761682018-09-06 14:05:20 +02003727 struct mtd_info *mtd = nand_to_mtd(chip);
3728
Boris Brezillon97d90da2017-11-30 18:01:29 +01003729 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003730}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003731EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003732
3733/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003734 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003735 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003736 * @chip: nand chip info structure
3737 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003738 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003739int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003740{
Boris Brezillonb9761682018-09-06 14:05:20 +02003741 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003742 int length = mtd->oobsize;
3743 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3744 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003745 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003746 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003747
Boris Brezillon97d90da2017-11-30 18:01:29 +01003748 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3749 if (ret)
3750 return ret;
3751
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003752 for (i = 0; i < chip->ecc.steps; i++) {
3753 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003754 int ret;
3755
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003756 pos = eccsize + i * (eccsize + chunk);
3757 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003758 ret = nand_change_read_column_op(chip, pos,
3759 NULL, 0,
3760 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003761 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003762 ret = nand_read_page_op(chip, page, pos, NULL,
3763 0);
3764
3765 if (ret)
3766 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003767 } else
3768 sndrnd = 1;
3769 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003770
3771 ret = nand_read_data_op(chip, bufpoi, toread, false);
3772 if (ret)
3773 return ret;
3774
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003775 bufpoi += toread;
3776 length -= toread;
3777 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003778 if (length > 0) {
3779 ret = nand_read_data_op(chip, bufpoi, length, false);
3780 if (ret)
3781 return ret;
3782 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003783
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003784 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003785}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003786EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003787
3788/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003789 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003790 * @chip: nand chip info structure
3791 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003792 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003793int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003794{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003795 struct mtd_info *mtd = nand_to_mtd(chip);
3796
Boris Brezillon97d90da2017-11-30 18:01:29 +01003797 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3798 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003799}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003800EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003801
3802/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003803 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003804 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003805 * @chip: nand chip info structure
3806 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003807 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003808int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003809{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003810 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003811 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3812 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003813 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003814 const uint8_t *bufpoi = chip->oob_poi;
3815
3816 /*
3817 * data-ecc-data-ecc ... ecc-oob
3818 * or
3819 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3820 */
3821 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3822 pos = steps * (eccsize + chunk);
3823 steps = 0;
3824 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003825 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003826
Boris Brezillon97d90da2017-11-30 18:01:29 +01003827 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3828 if (ret)
3829 return ret;
3830
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003831 for (i = 0; i < steps; i++) {
3832 if (sndcmd) {
3833 if (mtd->writesize <= 512) {
3834 uint32_t fill = 0xFFFFFFFF;
3835
3836 len = eccsize;
3837 while (len > 0) {
3838 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003839
3840 ret = nand_write_data_op(chip, &fill,
3841 num, false);
3842 if (ret)
3843 return ret;
3844
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003845 len -= num;
3846 }
3847 } else {
3848 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003849 ret = nand_change_write_column_op(chip, pos,
3850 NULL, 0,
3851 false);
3852 if (ret)
3853 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003854 }
3855 } else
3856 sndcmd = 1;
3857 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003858
3859 ret = nand_write_data_op(chip, bufpoi, len, false);
3860 if (ret)
3861 return ret;
3862
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003863 bufpoi += len;
3864 length -= len;
3865 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003866 if (length > 0) {
3867 ret = nand_write_data_op(chip, bufpoi, length, false);
3868 if (ret)
3869 return ret;
3870 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003871
Boris Brezillon97d90da2017-11-30 18:01:29 +01003872 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003873}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003874EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003875
3876/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003877 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003878 * @mtd: MTD device structure
3879 * @from: offset to read from
3880 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003882 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003884static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3885 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003887 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003888 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003889 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003890 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003891 int readlen = ops->ooblen;
3892 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003893 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003894 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Brian Norris289c0522011-07-19 10:06:09 -07003896 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303897 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Brian Norris041e4572011-06-23 16:45:24 -07003899 stats = mtd->ecc_stats;
3900
Boris BREZILLON29f10582016-03-07 10:46:52 +01003901 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003902
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003903 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003904 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003906 /* Shift to get page */
3907 realpage = (int)(from >> chip->page_shift);
3908 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Florian Fainellif8ac0412010-09-07 13:23:43 +02003910 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003911 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003912 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003913 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003914 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003915
3916 if (ret < 0)
3917 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003918
3919 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003920 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003921
Boris Brezillon85e08e52018-07-27 09:44:17 +02003922 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003923
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003924 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3925
Vitaly Wool70145682006-11-03 18:20:38 +03003926 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003927 if (!readlen)
3928 break;
3929
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003930 /* Increment page address */
3931 realpage++;
3932
3933 page = realpage & chip->pagemask;
3934 /* Check, if we cross a chip boundary */
3935 if (!page) {
3936 chipnr++;
3937 chip->select_chip(mtd, -1);
3938 chip->select_chip(mtd, chipnr);
3939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003941 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003943 ops->oobretlen = ops->ooblen - readlen;
3944
3945 if (ret < 0)
3946 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003947
3948 if (mtd->ecc_stats.failed - stats.failed)
3949 return -EBADMSG;
3950
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003951 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952}
3953
3954/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003955 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003956 * @mtd: MTD device structure
3957 * @from: offset to read from
3958 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003960 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003962static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3963 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003965 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003966
3967 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003969 if (ops->mode != MTD_OPS_PLACE_OOB &&
3970 ops->mode != MTD_OPS_AUTO_OOB &&
3971 ops->mode != MTD_OPS_RAW)
3972 return -ENOTSUPP;
3973
Huang Shijie6a8214a2012-11-19 14:43:30 +08003974 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003976 if (!ops->datbuf)
3977 ret = nand_do_read_oob(mtd, from, ops);
3978 else
3979 ret = nand_do_read_ops(mtd, from, ops);
3980
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003982 return ret;
3983}
3984
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003985/**
3986 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003987 * @chip: nand chip info structure
3988 * @buf: data buffer
3989 * @oob_required: must write chip->oob_poi to OOB
3990 * @page: page number to write
3991 *
3992 * Returns -ENOTSUPP unconditionally.
3993 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003994int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3995 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003996{
3997 return -ENOTSUPP;
3998}
3999EXPORT_SYMBOL(nand_write_page_raw_notsupp);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004000
4001/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004002 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004003 * @chip: nand chip info structure
4004 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004005 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004006 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004007 *
Brian Norris7854d3f2011-06-23 14:12:08 -07004008 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004009 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004010int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
4011 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004012{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004013 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004014 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004015
Boris Brezillon25f815f2017-11-30 18:01:30 +01004016 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004017 if (ret)
4018 return ret;
4019
4020 if (oob_required) {
4021 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
4022 false);
4023 if (ret)
4024 return ret;
4025 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004026
Boris Brezillon25f815f2017-11-30 18:01:30 +01004027 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02004029EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004031/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004032 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004033 * @chip: nand chip info structure
4034 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004035 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004036 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004037 *
4038 * We need a special oob layout and handling even when ECC isn't checked.
4039 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004040static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004041 const uint8_t *buf, int oob_required,
4042 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004043{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004044 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004045 int eccsize = chip->ecc.size;
4046 int eccbytes = chip->ecc.bytes;
4047 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004048 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004049
Boris Brezillon25f815f2017-11-30 18:01:30 +01004050 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4051 if (ret)
4052 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004053
4054 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004055 ret = nand_write_data_op(chip, buf, eccsize, false);
4056 if (ret)
4057 return ret;
4058
David Brownell52ff49d2009-03-04 12:01:36 -08004059 buf += eccsize;
4060
4061 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004062 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4063 false);
4064 if (ret)
4065 return ret;
4066
David Brownell52ff49d2009-03-04 12:01:36 -08004067 oob += chip->ecc.prepad;
4068 }
4069
Boris Brezillon97d90da2017-11-30 18:01:29 +01004070 ret = nand_write_data_op(chip, oob, eccbytes, false);
4071 if (ret)
4072 return ret;
4073
David Brownell52ff49d2009-03-04 12:01:36 -08004074 oob += eccbytes;
4075
4076 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004077 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4078 false);
4079 if (ret)
4080 return ret;
4081
David Brownell52ff49d2009-03-04 12:01:36 -08004082 oob += chip->ecc.postpad;
4083 }
4084 }
4085
4086 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004087 if (size) {
4088 ret = nand_write_data_op(chip, oob, size, false);
4089 if (ret)
4090 return ret;
4091 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004092
Boris Brezillon25f815f2017-11-30 18:01:30 +01004093 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004094}
4095/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004096 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004097 * @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 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004102static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
4103 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004104{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004105 struct mtd_info *mtd = nand_to_mtd(chip);
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
Brian Norris7854d3f2011-06-23 14:12:08 -07004112 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004113 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004114 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004115
Boris Brezillon846031d2016-02-03 20:11:00 +01004116 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4117 chip->ecc.total);
4118 if (ret)
4119 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004120
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004121 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004122}
4123
4124/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004125 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004126 * @chip: nand chip info structure
4127 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004128 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004129 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004130 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004131static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
4132 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004133{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004134 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004135 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004136 int eccbytes = chip->ecc.bytes;
4137 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004138 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004139 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004140
Boris Brezillon25f815f2017-11-30 18:01:30 +01004141 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4142 if (ret)
4143 return ret;
4144
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004145 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004146 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004147
4148 ret = nand_write_data_op(chip, p, eccsize, false);
4149 if (ret)
4150 return ret;
4151
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004152 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004153 }
4154
Boris Brezillon846031d2016-02-03 20:11:00 +01004155 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4156 chip->ecc.total);
4157 if (ret)
4158 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004159
Boris Brezillon97d90da2017-11-30 18:01:29 +01004160 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4161 if (ret)
4162 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004163
Boris Brezillon25f815f2017-11-30 18:01:30 +01004164 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004165}
4166
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304167
4168/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004169 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304170 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004171 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304172 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004173 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304174 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004175 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304176 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004177static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
4178 uint32_t data_len, const uint8_t *buf,
4179 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304180{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004181 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304182 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004183 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304184 int ecc_size = chip->ecc.size;
4185 int ecc_bytes = chip->ecc.bytes;
4186 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304187 uint32_t start_step = offset / ecc_size;
4188 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4189 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004190 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304191
Boris Brezillon25f815f2017-11-30 18:01:30 +01004192 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4193 if (ret)
4194 return ret;
4195
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304196 for (step = 0; step < ecc_steps; step++) {
4197 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02004198 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304199
4200 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004201 ret = nand_write_data_op(chip, buf, ecc_size, false);
4202 if (ret)
4203 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304204
4205 /* mask ECC of un-touched subpages by padding 0xFF */
4206 if ((step < start_step) || (step > end_step))
4207 memset(ecc_calc, 0xff, ecc_bytes);
4208 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004209 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304210
4211 /* mask OOB of un-touched subpages by padding 0xFF */
4212 /* if oob_required, preserve OOB metadata of written subpage */
4213 if (!oob_required || (step < start_step) || (step > end_step))
4214 memset(oob_buf, 0xff, oob_bytes);
4215
Brian Norrisd6a950802013-08-08 17:16:36 -07004216 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304217 ecc_calc += ecc_bytes;
4218 oob_buf += oob_bytes;
4219 }
4220
4221 /* copy calculated ECC for whole page to chip->buffer->oob */
4222 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004223 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004224 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4225 chip->ecc.total);
4226 if (ret)
4227 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304228
4229 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004230 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4231 if (ret)
4232 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304233
Boris Brezillon25f815f2017-11-30 18:01:30 +01004234 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304235}
4236
4237
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004238/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004239 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004240 * @chip: nand chip info structure
4241 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004242 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004243 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004244 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004245 * The hw generator calculates the error syndrome automatically. Therefore we
4246 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004247 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004248static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
4249 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004250{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004251 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004252 int i, eccsize = chip->ecc.size;
4253 int eccbytes = chip->ecc.bytes;
4254 int eccsteps = chip->ecc.steps;
4255 const uint8_t *p = buf;
4256 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004257 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004258
Boris Brezillon25f815f2017-11-30 18:01:30 +01004259 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4260 if (ret)
4261 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004262
4263 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004264 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004265
4266 ret = nand_write_data_op(chip, p, eccsize, false);
4267 if (ret)
4268 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004269
4270 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004271 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4272 false);
4273 if (ret)
4274 return ret;
4275
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004276 oob += chip->ecc.prepad;
4277 }
4278
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004279 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004280
4281 ret = nand_write_data_op(chip, oob, eccbytes, false);
4282 if (ret)
4283 return ret;
4284
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004285 oob += eccbytes;
4286
4287 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004288 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4289 false);
4290 if (ret)
4291 return ret;
4292
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004293 oob += chip->ecc.postpad;
4294 }
4295 }
4296
4297 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004298 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004299 if (i) {
4300 ret = nand_write_data_op(chip, oob, i, false);
4301 if (ret)
4302 return ret;
4303 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004304
Boris Brezillon25f815f2017-11-30 18:01:30 +01004305 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004306}
4307
4308/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004309 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004310 * @mtd: MTD device structure
4311 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304312 * @offset: address offset within the page
4313 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004314 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004315 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004316 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004317 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004318 */
4319static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304320 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004321 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004322{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304323 int status, subpage;
4324
4325 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4326 chip->ecc.write_subpage)
4327 subpage = offset || (data_len < mtd->writesize);
4328 else
4329 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004330
David Woodhouse956e9442006-09-25 17:12:39 +01004331 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004332 status = chip->ecc.write_page_raw(chip, buf, oob_required,
4333 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304334 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004335 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
4336 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004337 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004338 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004339
4340 if (status < 0)
4341 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004342
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004343 return 0;
4344}
4345
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004346/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004347 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004348 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004349 * @oob: oob data buffer
4350 * @len: oob data write length
4351 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004352 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004353static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4354 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004355{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004356 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004357 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004358
4359 /*
4360 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4361 * data from a previous OOB read.
4362 */
4363 memset(chip->oob_poi, 0xff, mtd->oobsize);
4364
Florian Fainellif8ac0412010-09-07 13:23:43 +02004365 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004366
Brian Norris0612b9d2011-08-30 18:45:40 -07004367 case MTD_OPS_PLACE_OOB:
4368 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004369 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4370 return oob + len;
4371
Boris Brezillon846031d2016-02-03 20:11:00 +01004372 case MTD_OPS_AUTO_OOB:
4373 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4374 ops->ooboffs, len);
4375 BUG_ON(ret);
4376 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004377
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004378 default:
4379 BUG();
4380 }
4381 return NULL;
4382}
4383
Florian Fainellif8ac0412010-09-07 13:23:43 +02004384#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004385
4386/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004387 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004388 * @mtd: MTD device structure
4389 * @to: offset to write to
4390 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004391 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004392 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004393 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004394static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4395 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004396{
Corentin Labbe73600b62017-09-02 10:49:38 +02004397 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004398 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004399 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004400
4401 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004402 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004403
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004404 uint8_t *oob = ops->oobbuf;
4405 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304406 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004407 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004408
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004409 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004410 if (!writelen)
4411 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004412
Brian Norris8b6e50c2011-05-25 14:59:01 -07004413 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004414 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004415 pr_notice("%s: attempt to write non page aligned data\n",
4416 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004417 return -EINVAL;
4418 }
4419
Thomas Gleixner29072b92006-09-28 15:38:36 +02004420 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004421
Thomas Gleixner6a930962006-06-28 00:11:45 +02004422 chipnr = (int)(to >> chip->chip_shift);
4423 chip->select_chip(mtd, chipnr);
4424
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004425 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004426 if (nand_check_wp(mtd)) {
4427 ret = -EIO;
4428 goto err_out;
4429 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004430
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004431 realpage = (int)(to >> chip->page_shift);
4432 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004433
4434 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004435 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4436 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004437 chip->pagebuf = -1;
4438
Maxim Levitsky782ce792010-02-22 20:39:36 +02004439 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004440 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4441 ret = -EINVAL;
4442 goto err_out;
4443 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004444
Florian Fainellif8ac0412010-09-07 13:23:43 +02004445 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004446 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004447 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004448 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004449 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004450
Kamal Dasu66507c72014-05-01 20:51:19 -04004451 if (part_pagewr)
4452 use_bufpoi = 1;
4453 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004454 use_bufpoi = !virt_addr_valid(buf) ||
4455 !IS_ALIGNED((unsigned long)buf,
4456 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004457 else
4458 use_bufpoi = 0;
4459
4460 /* Partial page write?, or need to use bounce buffer */
4461 if (use_bufpoi) {
4462 pr_debug("%s: using write bounce buffer for buf@%p\n",
4463 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004464 if (part_pagewr)
4465 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004466 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004467 memset(chip->data_buf, 0xff, mtd->writesize);
4468 memcpy(&chip->data_buf[column], buf, bytes);
4469 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004470 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004471
Maxim Levitsky782ce792010-02-22 20:39:36 +02004472 if (unlikely(oob)) {
4473 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004474 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004475 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004476 } else {
4477 /* We still need to erase leftover OOB data */
4478 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004479 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004480
4481 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004482 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004483 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004484 if (ret)
4485 break;
4486
4487 writelen -= bytes;
4488 if (!writelen)
4489 break;
4490
Thomas Gleixner29072b92006-09-28 15:38:36 +02004491 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004492 buf += bytes;
4493 realpage++;
4494
4495 page = realpage & chip->pagemask;
4496 /* Check, if we cross a chip boundary */
4497 if (!page) {
4498 chipnr++;
4499 chip->select_chip(mtd, -1);
4500 chip->select_chip(mtd, chipnr);
4501 }
4502 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004503
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004504 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004505 if (unlikely(oob))
4506 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004507
4508err_out:
4509 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004510 return ret;
4511}
4512
4513/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004514 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004515 * @mtd: MTD device structure
4516 * @to: offset to write to
4517 * @len: number of bytes to write
4518 * @retlen: pointer to variable to store the number of written bytes
4519 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004520 *
4521 * NAND write with ECC. Used when performing writes in interrupt context, this
4522 * may for example be called by mtdoops when writing an oops while in panic.
4523 */
4524static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4525 size_t *retlen, const uint8_t *buf)
4526{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004527 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004528 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004529 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004530 int ret;
4531
Brian Norris8b6e50c2011-05-25 14:59:01 -07004532 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004533 panic_nand_get_device(chip, mtd, FL_WRITING);
4534
Brent Taylor30863e382017-10-30 22:32:45 -05004535 chip->select_chip(mtd, chipnr);
4536
4537 /* Wait for the device to get ready */
4538 panic_nand_wait(mtd, chip, 400);
4539
Brian Norris0ec56dc2015-02-28 02:02:30 -08004540 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004541 ops.len = len;
4542 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004543 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004544
Brian Norris4a89ff82011-08-30 18:45:45 -07004545 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004546
Brian Norris4a89ff82011-08-30 18:45:45 -07004547 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004548 return ret;
4549}
4550
4551/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004552 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004553 * @mtd: MTD device structure
4554 * @to: offset to write to
4555 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004556 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004557 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004558 */
4559static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4560 struct mtd_oob_ops *ops)
4561{
Adrian Hunter03736152007-01-31 17:58:29 +02004562 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004563 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Brian Norris289c0522011-07-19 10:06:09 -07004565 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304566 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
Boris BREZILLON29f10582016-03-07 10:46:52 +01004568 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004571 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004572 pr_debug("%s: attempt to write past end of page\n",
4573 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 return -EINVAL;
4575 }
4576
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004577 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004578
4579 /*
4580 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4581 * of my DiskOnChip 2000 test units) will clear the whole data page too
4582 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4583 * it in the doc2000 driver in August 1999. dwmw2.
4584 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004585 nand_reset(chip, chipnr);
4586
4587 chip->select_chip(mtd, chipnr);
4588
4589 /* Shift to get page */
4590 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
4592 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004593 if (nand_check_wp(mtd)) {
4594 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004595 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004596 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004599 if (page == chip->pagebuf)
4600 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004602 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004603
Brian Norris0612b9d2011-08-30 18:45:40 -07004604 if (ops->mode == MTD_OPS_RAW)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004605 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
Brian Norris9ce244b2011-08-30 18:45:37 -07004606 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004607 status = chip->ecc.write_oob(chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004608
Huang Shijieb0bb6902012-11-19 14:43:29 +08004609 chip->select_chip(mtd, -1);
4610
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004611 if (status)
4612 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Vitaly Wool70145682006-11-03 18:20:38 +03004614 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004616 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004617}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004619/**
4620 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004621 * @mtd: MTD device structure
4622 * @to: offset to write to
4623 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004624 */
4625static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4626 struct mtd_oob_ops *ops)
4627{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004628 int ret = -ENOTSUPP;
4629
4630 ops->retlen = 0;
4631
Huang Shijie6a8214a2012-11-19 14:43:30 +08004632 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004633
Florian Fainellif8ac0412010-09-07 13:23:43 +02004634 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004635 case MTD_OPS_PLACE_OOB:
4636 case MTD_OPS_AUTO_OOB:
4637 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004638 break;
4639
4640 default:
4641 goto out;
4642 }
4643
4644 if (!ops->datbuf)
4645 ret = nand_do_write_oob(mtd, to, ops);
4646 else
4647 ret = nand_do_write_ops(mtd, to, ops);
4648
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004649out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004650 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 return ret;
4652}
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654/**
Brian Norris49c50b92014-05-06 16:02:19 -07004655 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004656 * @mtd: MTD device structure
4657 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 *
Brian Norris49c50b92014-05-06 16:02:19 -07004659 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 */
Brian Norris49c50b92014-05-06 16:02:19 -07004661static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004663 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004664 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004665
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004667 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004668
Boris Brezillon97d90da2017-11-30 18:01:29 +01004669 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670}
4671
4672/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004674 * @mtd: MTD device structure
4675 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004677 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004679static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680{
David Woodhousee0c7d762006-05-13 18:07:53 +01004681 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004685 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004686 * @mtd: MTD device structure
4687 * @instr: erase instruction
4688 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004690 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004692int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4693 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
Adrian Hunter69423d92008-12-10 13:37:21 +00004695 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004696 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00004697 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
Brian Norris289c0522011-07-19 10:06:09 -07004699 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4700 __func__, (unsigned long long)instr->addr,
4701 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304703 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004707 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004710 page = (int)(instr->addr >> chip->page_shift);
4711 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
4713 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004714 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
4716 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004717 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 /* Check, if it is write protected */
4720 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004721 pr_debug("%s: device is write protected!\n",
4722 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004723 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 goto erase_exit;
4725 }
4726
4727 /* Loop through the pages */
4728 len = instr->len;
4729
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004731 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004732 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304733 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004734 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4735 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004736 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 goto erase_exit;
4738 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004739
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004740 /*
4741 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004742 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004743 */
4744 if (page <= chip->pagebuf && chip->pagebuf <
4745 (page + pages_per_block))
4746 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
Brian Norris49c50b92014-05-06 16:02:19 -07004748 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
4750 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004751 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004752 pr_debug("%s: failed erase, page 0x%08x\n",
4753 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004754 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004755 instr->fail_addr =
4756 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 goto erase_exit;
4758 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004761 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 page += pages_per_block;
4763
4764 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004765 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004767 chip->select_chip(mtd, -1);
4768 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 }
4770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004772 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004773erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004776 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 nand_release_device(mtd);
4778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 /* Return more or less happy */
4780 return ret;
4781}
4782
4783/**
4784 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004785 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004787 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004789static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
Brian Norris289c0522011-07-19 10:06:09 -07004791 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
4793 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004794 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004796 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004800 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004801 * @mtd: MTD device structure
4802 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004804static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304806 struct nand_chip *chip = mtd_to_nand(mtd);
4807 int chipnr = (int)(offs >> chip->chip_shift);
4808 int ret;
4809
4810 /* Select the NAND device */
4811 nand_get_device(mtd, FL_READING);
4812 chip->select_chip(mtd, chipnr);
4813
4814 ret = nand_block_checkbad(mtd, offs, 0);
4815
4816 chip->select_chip(mtd, -1);
4817 nand_release_device(mtd);
4818
4819 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820}
4821
4822/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004823 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004824 * @mtd: MTD device structure
4825 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004827static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 int ret;
4830
Florian Fainellif8ac0412010-09-07 13:23:43 +02004831 ret = nand_block_isbad(mtd, ofs);
4832 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004833 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 if (ret > 0)
4835 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004836 return ret;
4837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Brian Norris5a0edb22013-07-30 17:52:58 -07004839 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840}
4841
4842/**
Zach Brown56718422017-01-10 13:30:20 -06004843 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4844 * @mtd: MTD device structure
4845 * @ofs: offset relative to mtd start
4846 * @len: length of mtd
4847 */
4848static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4849{
4850 struct nand_chip *chip = mtd_to_nand(mtd);
4851 u32 part_start_block;
4852 u32 part_end_block;
4853 u32 part_start_die;
4854 u32 part_end_die;
4855
4856 /*
4857 * max_bb_per_die and blocks_per_die used to determine
4858 * the maximum bad block count.
4859 */
4860 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4861 return -ENOTSUPP;
4862
4863 /* Get the start and end of the partition in erase blocks. */
4864 part_start_block = mtd_div_by_eb(ofs, mtd);
4865 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4866
4867 /* Get the start and end LUNs of the partition. */
4868 part_start_die = part_start_block / chip->blocks_per_die;
4869 part_end_die = part_end_block / chip->blocks_per_die;
4870
4871 /*
4872 * Look up the bad blocks per unit and multiply by the number of units
4873 * that the partition spans.
4874 */
4875 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4876}
4877
4878/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004879 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004880 * @mtd: MTD device structure
4881 * @chip: nand chip info structure
4882 * @addr: feature address.
4883 * @subfeature_param: the subfeature parameters, a four bytes array.
4884 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004885static int nand_default_set_features(struct mtd_info *mtd,
4886 struct nand_chip *chip, int addr,
4887 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004888{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004889 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004890}
4891
4892/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004893 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004894 * @mtd: MTD device structure
4895 * @chip: nand chip info structure
4896 * @addr: feature address.
4897 * @subfeature_param: the subfeature parameters, a four bytes array.
4898 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004899static int nand_default_get_features(struct mtd_info *mtd,
4900 struct nand_chip *chip, int addr,
4901 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004902{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004903 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004904}
4905
4906/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004907 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004908 * @mtd: MTD device structure
4909 * @chip: nand chip info structure
4910 * @addr: feature address.
4911 * @subfeature_param: the subfeature parameters, a four bytes array.
4912 *
4913 * Should be used by NAND controller drivers that do not support the SET/GET
4914 * FEATURES operations.
4915 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004916int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4917 int addr, u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004918{
4919 return -ENOTSUPP;
4920}
Miquel Raynalb9587582018-03-19 14:47:19 +01004921EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004922
4923/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004924 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004925 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004926 */
4927static int nand_suspend(struct mtd_info *mtd)
4928{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004929 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004930}
4931
4932/**
4933 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004934 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004935 */
4936static void nand_resume(struct mtd_info *mtd)
4937{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004938 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004939
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004940 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004941 nand_release_device(mtd);
4942 else
Brian Norrisd0370212011-07-19 10:06:08 -07004943 pr_err("%s called for a chip which is not in suspended state\n",
4944 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004945}
4946
Scott Branden72ea4032014-11-20 11:18:05 -08004947/**
4948 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4949 * prevent further operations
4950 * @mtd: MTD device structure
4951 */
4952static void nand_shutdown(struct mtd_info *mtd)
4953{
Brian Norris9ca641b2015-11-09 16:37:28 -08004954 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004955}
4956
Brian Norris8b6e50c2011-05-25 14:59:01 -07004957/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004958static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004959{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004960 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004963 if (!chip->chip_delay)
4964 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
4966 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004967 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004968 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
4970 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004971 if (chip->waitfunc == NULL)
4972 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004974 if (!chip->select_chip)
4975 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004976
Huang Shijie4204ccc2013-08-16 10:10:07 +08004977 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004978 if (!chip->set_features)
4979 chip->set_features = nand_default_set_features;
4980 if (!chip->get_features)
4981 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004982
Brian Norris68e80782013-07-18 01:17:02 -07004983 /* If called twice, pointers that depend on busw may need to be reset */
4984 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004985 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004986 if (!chip->block_bad)
4987 chip->block_bad = nand_block_bad;
4988 if (!chip->block_markbad)
4989 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004990 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004991 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004992 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4993 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004994 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004995 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004996
4997 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004998 chip->controller = &chip->dummy_controller;
4999 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02005000 }
5001
Masahiro Yamada477544c2017-03-30 17:15:05 +09005002 if (!chip->buf_align)
5003 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005004}
5005
Brian Norris8b6e50c2011-05-25 14:59:01 -07005006/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005007static void sanitize_string(uint8_t *s, size_t len)
5008{
5009 ssize_t i;
5010
Brian Norris8b6e50c2011-05-25 14:59:01 -07005011 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005012 s[len - 1] = 0;
5013
Brian Norris8b6e50c2011-05-25 14:59:01 -07005014 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005015 for (i = 0; i < len - 1; i++) {
5016 if (s[i] < ' ' || s[i] > 127)
5017 s[i] = '?';
5018 }
5019
Brian Norris8b6e50c2011-05-25 14:59:01 -07005020 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005021 strim(s);
5022}
5023
5024static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
5025{
5026 int i;
5027 while (len--) {
5028 crc ^= *p++ << 8;
5029 for (i = 0; i < 8; i++)
5030 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
5031 }
5032
5033 return crc;
5034}
5035
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005036/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005037static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
5038 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005039{
5040 struct onfi_ext_param_page *ep;
5041 struct onfi_ext_section *s;
5042 struct onfi_ext_ecc_info *ecc;
5043 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005044 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005045 int len;
5046 int i;
5047
5048 len = le16_to_cpu(p->ext_param_page_length) * 16;
5049 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005050 if (!ep)
5051 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005052
5053 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005054 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5055 if (ret)
5056 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005057
5058 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005059 ret = nand_change_read_column_op(chip,
5060 sizeof(*p) * p->num_of_param_pages,
5061 ep, len, true);
5062 if (ret)
5063 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005064
Boris Brezillon97d90da2017-11-30 18:01:29 +01005065 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005066 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5067 != le16_to_cpu(ep->crc))) {
5068 pr_debug("fail in the CRC.\n");
5069 goto ext_out;
5070 }
5071
5072 /*
5073 * Check the signature.
5074 * Do not strictly follow the ONFI spec, maybe changed in future.
5075 */
5076 if (strncmp(ep->sig, "EPPS", 4)) {
5077 pr_debug("The signature is invalid.\n");
5078 goto ext_out;
5079 }
5080
5081 /* find the ECC section. */
5082 cursor = (uint8_t *)(ep + 1);
5083 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5084 s = ep->sections + i;
5085 if (s->type == ONFI_SECTION_TYPE_2)
5086 break;
5087 cursor += s->length * 16;
5088 }
5089 if (i == ONFI_EXT_SECTION_MAX) {
5090 pr_debug("We can not find the ECC section.\n");
5091 goto ext_out;
5092 }
5093
5094 /* get the info we want. */
5095 ecc = (struct onfi_ext_ecc_info *)cursor;
5096
Brian Norris4ae7d222013-09-16 18:20:21 -07005097 if (!ecc->codeword_size) {
5098 pr_debug("Invalid codeword size\n");
5099 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005100 }
5101
Brian Norris4ae7d222013-09-16 18:20:21 -07005102 chip->ecc_strength_ds = ecc->ecc_bits;
5103 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005104 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005105
5106ext_out:
5107 kfree(ep);
5108 return ret;
5109}
5110
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005111/*
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005112 * Recover data with bit-wise majority
5113 */
5114static void nand_bit_wise_majority(const void **srcbufs,
5115 unsigned int nsrcbufs,
5116 void *dstbuf,
5117 unsigned int bufsize)
5118{
5119 int i, j, k;
5120
5121 for (i = 0; i < bufsize; i++) {
5122 u8 val = 0;
5123
5124 for (j = 0; j < 8; j++) {
5125 unsigned int cnt = 0;
5126
5127 for (k = 0; k < nsrcbufs; k++) {
5128 const u8 *srcbuf = srcbufs[k];
5129
5130 if (srcbuf[i] & BIT(j))
5131 cnt++;
5132 }
5133
5134 if (cnt > nsrcbufs / 2)
5135 val |= BIT(j);
5136 }
5137
5138 ((u8 *)dstbuf)[i] = val;
5139 }
5140}
5141
5142/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005143 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005144 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005145static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005146{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005147 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005148 struct nand_onfi_params *p;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005149 struct onfi_params *onfi;
5150 int onfi_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005151 char id[4];
5152 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005153
Brian Norris7854d3f2011-06-23 14:12:08 -07005154 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005155 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5156 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005157 return 0;
5158
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005159 /* ONFI chip: allocate a buffer to hold its parameter page */
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005160 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005161 if (!p)
5162 return -ENOMEM;
5163
Boris Brezillon97d90da2017-11-30 18:01:29 +01005164 ret = nand_read_param_page_op(chip, 0, NULL, 0);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005165 if (ret) {
5166 ret = 0;
5167 goto free_onfi_param_page;
5168 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005169
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005170 for (i = 0; i < 3; i++) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005171 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005172 if (ret) {
5173 ret = 0;
5174 goto free_onfi_param_page;
5175 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005176
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005177 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005178 le16_to_cpu(p->crc)) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005179 if (i)
5180 memcpy(p, &p[i], sizeof(*p));
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005181 break;
5182 }
5183 }
5184
Brian Norrisc7f23a72013-08-13 10:51:55 -07005185 if (i == 3) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005186 const void *srcbufs[3] = {p, p + 1, p + 2};
5187
5188 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5189 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5190 sizeof(*p));
5191
5192 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5193 le16_to_cpu(p->crc)) {
5194 pr_err("ONFI parameter recovery failed, aborting\n");
5195 goto free_onfi_param_page;
5196 }
Brian Norrisc7f23a72013-08-13 10:51:55 -07005197 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005198
Chris Packham00ce4e02018-06-25 10:44:44 +12005199 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5200 chip->manufacturer.desc->ops->fixup_onfi_param_page)
5201 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5202
Brian Norris8b6e50c2011-05-25 14:59:01 -07005203 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005204 val = le16_to_cpu(p->revision);
Chris Packham872b71f2018-06-25 10:44:45 +12005205 if (val & ONFI_VERSION_2_3)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005206 onfi_version = 23;
Chris Packham872b71f2018-06-25 10:44:45 +12005207 else if (val & ONFI_VERSION_2_2)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005208 onfi_version = 22;
Chris Packham872b71f2018-06-25 10:44:45 +12005209 else if (val & ONFI_VERSION_2_1)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005210 onfi_version = 21;
Chris Packham872b71f2018-06-25 10:44:45 +12005211 else if (val & ONFI_VERSION_2_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005212 onfi_version = 20;
Chris Packham872b71f2018-06-25 10:44:45 +12005213 else if (val & ONFI_VERSION_1_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005214 onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005215
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005216 if (!onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005217 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005218 goto free_onfi_param_page;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005219 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005220
5221 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5222 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005223 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5224 if (!chip->parameters.model) {
5225 ret = -ENOMEM;
5226 goto free_onfi_param_page;
5227 }
Brian Norris4355b702013-08-27 18:45:10 -07005228
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005229 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005230
5231 /*
5232 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5233 * (don't ask me who thought of this...). MTD assumes that these
5234 * dimensions will be power-of-2, so just truncate the remaining area.
5235 */
5236 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5237 mtd->erasesize *= mtd->writesize;
5238
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005239 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005240
5241 /* See erasesize comment */
5242 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005243 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005244 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005245
Zach Brown34da5f52017-01-10 13:30:21 -06005246 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5247 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5248
Miquel Raynala97421c2018-03-19 14:47:27 +01005249 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005250 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005251
Huang Shijie10c86ba2013-05-17 11:17:26 +08005252 if (p->ecc_bits != 0xff) {
5253 chip->ecc_strength_ds = p->ecc_bits;
5254 chip->ecc_step_ds = 512;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005255 } else if (onfi_version >= 21 &&
Miquel Raynala97421c2018-03-19 14:47:27 +01005256 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005257
5258 /*
5259 * The nand_flash_detect_ext_param_page() uses the
5260 * Change Read Column command which maybe not supported
5261 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5262 * now. We do not replace user supplied command function.
5263 */
5264 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5265 chip->cmdfunc = nand_command_lp;
5266
5267 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005268 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005269 pr_warn("Failed to detect ONFI extended param page\n");
5270 } else {
5271 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005272 }
5273
Miquel Raynalf4531b22018-03-19 14:47:26 +01005274 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005275 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005276 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005277 bitmap_set(chip->parameters.get_feature_list,
5278 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5279 bitmap_set(chip->parameters.set_feature_list,
5280 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5281 }
Miquel Raynalf4531b22018-03-19 14:47:26 +01005282
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005283 onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
5284 if (!onfi) {
5285 ret = -ENOMEM;
5286 goto free_model;
5287 }
5288
5289 onfi->version = onfi_version;
5290 onfi->tPROG = le16_to_cpu(p->t_prog);
5291 onfi->tBERS = le16_to_cpu(p->t_bers);
5292 onfi->tR = le16_to_cpu(p->t_r);
5293 onfi->tCCS = le16_to_cpu(p->t_ccs);
5294 onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
5295 onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
5296 memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
5297 chip->parameters.onfi = onfi;
5298
5299 /* Identification done, free the full ONFI parameter page and exit */
5300 kfree(p);
5301
5302 return 1;
5303
5304free_model:
5305 kfree(chip->parameters.model);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005306free_onfi_param_page:
5307 kfree(p);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005308
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005309 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005310}
5311
5312/*
Huang Shijie91361812014-02-21 13:39:40 +08005313 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5314 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005315static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005316{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005317 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005318 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005319 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005320 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005321 char id[5];
5322 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005323
5324 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005325 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5326 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005327 return 0;
5328
Miquel Raynal480139d2018-03-19 14:47:30 +01005329 /* JEDEC chip: allocate a buffer to hold its parameter page */
5330 p = kzalloc(sizeof(*p), GFP_KERNEL);
5331 if (!p)
5332 return -ENOMEM;
5333
Boris Brezillon97d90da2017-11-30 18:01:29 +01005334 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005335 if (ret) {
5336 ret = 0;
5337 goto free_jedec_param_page;
5338 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005339
Huang Shijie91361812014-02-21 13:39:40 +08005340 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005341 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005342 if (ret) {
5343 ret = 0;
5344 goto free_jedec_param_page;
5345 }
Huang Shijie91361812014-02-21 13:39:40 +08005346
5347 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5348 le16_to_cpu(p->crc))
5349 break;
5350 }
5351
5352 if (i == 3) {
5353 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005354 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005355 }
5356
5357 /* Check version */
5358 val = le16_to_cpu(p->revision);
5359 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005360 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005361 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005362 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005363
Miquel Raynal480139d2018-03-19 14:47:30 +01005364 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005365 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005366 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005367 }
5368
5369 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5370 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005371 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5372 if (!chip->parameters.model) {
5373 ret = -ENOMEM;
5374 goto free_jedec_param_page;
5375 }
Huang Shijie91361812014-02-21 13:39:40 +08005376
5377 mtd->writesize = le32_to_cpu(p->byte_per_page);
5378
5379 /* Please reference to the comment for nand_flash_detect_onfi. */
5380 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5381 mtd->erasesize *= mtd->writesize;
5382
5383 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5384
5385 /* Please reference to the comment for nand_flash_detect_onfi. */
5386 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5387 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5388 chip->bits_per_cell = p->bits_per_cell;
5389
Miquel Raynal480139d2018-03-19 14:47:30 +01005390 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005391 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005392
5393 /* ECC info */
5394 ecc = &p->ecc_info[0];
5395
5396 if (ecc->codeword_size >= 9) {
5397 chip->ecc_strength_ds = ecc->ecc_bits;
5398 chip->ecc_step_ds = 1 << ecc->codeword_size;
5399 } else {
5400 pr_warn("Invalid codeword size\n");
5401 }
5402
Miquel Raynal480139d2018-03-19 14:47:30 +01005403free_jedec_param_page:
5404 kfree(p);
5405 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005406}
5407
5408/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005409 * nand_id_has_period - Check if an ID string has a given wraparound period
5410 * @id_data: the ID string
5411 * @arrlen: the length of the @id_data array
5412 * @period: the period of repitition
5413 *
5414 * Check if an ID string is repeated within a given sequence of bytes at
5415 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005416 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005417 * if the repetition has a period of @period; otherwise, returns zero.
5418 */
5419static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5420{
5421 int i, j;
5422 for (i = 0; i < period; i++)
5423 for (j = i + period; j < arrlen; j += period)
5424 if (id_data[i] != id_data[j])
5425 return 0;
5426 return 1;
5427}
5428
5429/*
5430 * nand_id_len - Get the length of an ID string returned by CMD_READID
5431 * @id_data: the ID string
5432 * @arrlen: the length of the @id_data array
5433
5434 * Returns the length of the ID string, according to known wraparound/trailing
5435 * zero patterns. If no pattern exists, returns the length of the array.
5436 */
5437static int nand_id_len(u8 *id_data, int arrlen)
5438{
5439 int last_nonzero, period;
5440
5441 /* Find last non-zero byte */
5442 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5443 if (id_data[last_nonzero])
5444 break;
5445
5446 /* All zeros */
5447 if (last_nonzero < 0)
5448 return 0;
5449
5450 /* Calculate wraparound period */
5451 for (period = 1; period < arrlen; period++)
5452 if (nand_id_has_period(id_data, arrlen, period))
5453 break;
5454
5455 /* There's a repeated pattern */
5456 if (period < arrlen)
5457 return period;
5458
5459 /* There are trailing zeros */
5460 if (last_nonzero < arrlen - 1)
5461 return last_nonzero + 1;
5462
5463 /* No pattern detected */
5464 return arrlen;
5465}
5466
Huang Shijie7db906b2013-09-25 14:58:11 +08005467/* Extract the bits of per cell from the 3rd byte of the extended ID */
5468static int nand_get_bits_per_cell(u8 cellinfo)
5469{
5470 int bits;
5471
5472 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5473 bits >>= NAND_CI_CELLTYPE_SHIFT;
5474 return bits + 1;
5475}
5476
Brian Norrise3b88bd2012-09-24 20:40:52 -07005477/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005478 * Many new NAND share similar device ID codes, which represent the size of the
5479 * chip. The rest of the parameters must be decoded according to generic or
5480 * manufacturer-specific "extended ID" decoding patterns.
5481 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005482void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005483{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005484 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005485 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005486 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005487 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005488 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005489 /* The 4th id byte is the important one */
5490 extid = id_data[3];
5491
Boris Brezillon01389b62016-06-08 10:30:18 +02005492 /* Calc pagesize */
5493 mtd->writesize = 1024 << (extid & 0x03);
5494 extid >>= 2;
5495 /* Calc oobsize */
5496 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5497 extid >>= 2;
5498 /* Calc blocksize. Blocksize is multiples of 64KiB */
5499 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5500 extid >>= 2;
5501 /* Get buswidth information */
5502 if (extid & 0x1)
5503 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005504}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005505EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005506
5507/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005508 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5509 * decodes a matching ID table entry and assigns the MTD size parameters for
5510 * the chip.
5511 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005512static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005513{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005514 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005515
5516 mtd->erasesize = type->erasesize;
5517 mtd->writesize = type->pagesize;
5518 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005519
Huang Shijie1c195e92013-09-25 14:58:12 +08005520 /* All legacy ID NAND are small-page, SLC */
5521 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005522}
5523
5524/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005525 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5526 * heuristic patterns using various detected parameters (e.g., manufacturer,
5527 * page size, cell-type information).
5528 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005529static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005530{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005531 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005532
5533 /* Set the bad block position */
5534 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5535 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5536 else
5537 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005538}
5539
Huang Shijieec6e87e2013-03-15 11:01:00 +08005540static inline bool is_full_id_nand(struct nand_flash_dev *type)
5541{
5542 return type->id_len;
5543}
5544
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005545static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005546 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005547{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005548 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005549 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005550
Huang Shijieec6e87e2013-03-15 11:01:00 +08005551 if (!strncmp(type->id, id_data, type->id_len)) {
5552 mtd->writesize = type->pagesize;
5553 mtd->erasesize = type->erasesize;
5554 mtd->oobsize = type->oobsize;
5555
Huang Shijie7db906b2013-09-25 14:58:11 +08005556 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005557 chip->chipsize = (uint64_t)type->chipsize << 20;
5558 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005559 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5560 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005561 chip->onfi_timing_mode_default =
5562 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005563
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005564 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5565 if (!chip->parameters.model)
5566 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005567
Huang Shijieec6e87e2013-03-15 11:01:00 +08005568 return true;
5569 }
5570 return false;
5571}
5572
Brian Norris7e74c2d2012-09-24 20:40:49 -07005573/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005574 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5575 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5576 * table.
5577 */
5578static void nand_manufacturer_detect(struct nand_chip *chip)
5579{
5580 /*
5581 * Try manufacturer detection if available and use
5582 * nand_decode_ext_id() otherwise.
5583 */
5584 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005585 chip->manufacturer.desc->ops->detect) {
5586 /* The 3rd id byte holds MLC / multichip data */
5587 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005588 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005589 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005590 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005591 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005592}
5593
5594/*
5595 * Manufacturer initialization. This function is called for all NANDs including
5596 * ONFI and JEDEC compliant ones.
5597 * Manufacturer drivers should put all their specific initialization code in
5598 * their ->init() hook.
5599 */
5600static int nand_manufacturer_init(struct nand_chip *chip)
5601{
5602 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5603 !chip->manufacturer.desc->ops->init)
5604 return 0;
5605
5606 return chip->manufacturer.desc->ops->init(chip);
5607}
5608
5609/*
5610 * Manufacturer cleanup. This function is called for all NANDs including
5611 * ONFI and JEDEC compliant ones.
5612 * Manufacturer drivers should put all their specific cleanup code in their
5613 * ->cleanup() hook.
5614 */
5615static void nand_manufacturer_cleanup(struct nand_chip *chip)
5616{
5617 /* Release manufacturer private data */
5618 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5619 chip->manufacturer.desc->ops->cleanup)
5620 chip->manufacturer.desc->ops->cleanup(chip);
5621}
5622
5623/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005624 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005625 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005626static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005627{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005628 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005629 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005630 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005631 u8 *id_data = chip->id.data;
5632 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
Karl Beldanef89a882008-09-15 14:37:29 +02005634 /*
5635 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005636 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005637 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005638 ret = nand_reset(chip, 0);
5639 if (ret)
5640 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005641
5642 /* Select the device */
5643 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005644
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005646 ret = nand_readid_op(chip, 0, id_data, 2);
5647 if (ret)
5648 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
5650 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005651 maf_id = id_data[0];
5652 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
Brian Norris8b6e50c2011-05-25 14:59:01 -07005654 /*
5655 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005656 * interface concerns can cause random data which looks like a
5657 * possibly credible NAND flash to appear. If the two results do
5658 * not match, ignore the device completely.
5659 */
5660
Brian Norris4aef9b72012-09-24 20:40:48 -07005661 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005662 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5663 if (ret)
5664 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005665
Boris Brezillon7f501f02016-05-24 19:20:05 +02005666 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005667 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005668 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005669 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005670 }
5671
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005672 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005673
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005674 /* Try to identify manufacturer */
5675 manufacturer = nand_get_manufacturer(maf_id);
5676 chip->manufacturer.desc = manufacturer;
5677
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005678 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005679 type = nand_flash_ids;
5680
Boris Brezillon29a198a2016-05-24 20:17:48 +02005681 /*
5682 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5683 * override it.
5684 * This is required to make sure initial NAND bus width set by the
5685 * NAND controller driver is coherent with the real NAND bus width
5686 * (extracted by auto-detection code).
5687 */
5688 busw = chip->options & NAND_BUSWIDTH_16;
5689
5690 /*
5691 * The flag is only set (never cleared), reset it to its default value
5692 * before starting auto-detection.
5693 */
5694 chip->options &= ~NAND_BUSWIDTH_16;
5695
Huang Shijieec6e87e2013-03-15 11:01:00 +08005696 for (; type->name != NULL; type++) {
5697 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005698 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005699 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005700 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005701 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005702 }
5703 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005704
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005705 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005706 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005707 ret = nand_flash_detect_onfi(chip);
5708 if (ret < 0)
5709 return ret;
5710 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005711 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005712
5713 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005714 ret = nand_flash_detect_jedec(chip);
5715 if (ret < 0)
5716 return ret;
5717 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005718 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005719 }
5720
David Woodhouse5e81e882010-02-26 18:32:56 +00005721 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005722 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005723
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005724 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5725 if (!chip->parameters.model)
5726 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005727
Adrian Hunter69423d92008-12-10 13:37:21 +00005728 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005729
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005730 if (!type->pagesize)
5731 nand_manufacturer_detect(chip);
5732 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005733 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005734
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005735 /* Get chip options */
5736 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005737
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005738ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005739 if (!mtd->name)
5740 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005741
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005742 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005743 WARN_ON(busw & NAND_BUSWIDTH_16);
5744 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005745 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5746 /*
5747 * Check, if buswidth is correct. Hardware drivers should set
5748 * chip correct!
5749 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005750 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005751 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005752 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5753 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005754 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5755 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005756 ret = -EINVAL;
5757
5758 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005759 }
5760
Boris Brezillon7f501f02016-05-24 19:20:05 +02005761 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005762
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005763 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005764 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005765 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005766 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005767
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005768 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005769 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005770 if (chip->chipsize & 0xffffffff)
5771 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005772 else {
5773 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5774 chip->chip_shift += 32 - 1;
5775 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005776
Masahiro Yamada14157f82017-09-13 11:05:50 +09005777 if (chip->chip_shift - chip->page_shift > 16)
5778 chip->options |= NAND_ROW_ADDR_3;
5779
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005780 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005781 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005782
Brian Norris8b6e50c2011-05-25 14:59:01 -07005783 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005784 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5785 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005786
Ezequiel Garcia20171642013-11-25 08:30:31 -03005787 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005788 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005789 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5790 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005791 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005792 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005793 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005794 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005795
5796free_detect_allocation:
5797 kfree(chip->parameters.model);
5798
5799 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005800}
5801
Boris Brezillond48f62b2016-04-01 14:54:32 +02005802static const char * const nand_ecc_modes[] = {
5803 [NAND_ECC_NONE] = "none",
5804 [NAND_ECC_SOFT] = "soft",
5805 [NAND_ECC_HW] = "hw",
5806 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5807 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005808 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005809};
5810
5811static int of_get_nand_ecc_mode(struct device_node *np)
5812{
5813 const char *pm;
5814 int err, i;
5815
5816 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5817 if (err < 0)
5818 return err;
5819
5820 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5821 if (!strcasecmp(pm, nand_ecc_modes[i]))
5822 return i;
5823
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005824 /*
5825 * For backward compatibility we support few obsoleted values that don't
5826 * have their mappings into nand_ecc_modes_t anymore (they were merged
5827 * with other enums).
5828 */
5829 if (!strcasecmp(pm, "soft_bch"))
5830 return NAND_ECC_SOFT;
5831
Boris Brezillond48f62b2016-04-01 14:54:32 +02005832 return -ENODEV;
5833}
5834
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005835static const char * const nand_ecc_algos[] = {
5836 [NAND_ECC_HAMMING] = "hamming",
5837 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02005838 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005839};
5840
Boris Brezillond48f62b2016-04-01 14:54:32 +02005841static int of_get_nand_ecc_algo(struct device_node *np)
5842{
5843 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005844 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005845
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005846 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5847 if (!err) {
5848 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5849 if (!strcasecmp(pm, nand_ecc_algos[i]))
5850 return i;
5851 return -ENODEV;
5852 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005853
5854 /*
5855 * For backward compatibility we also read "nand-ecc-mode" checking
5856 * for some obsoleted values that were specifying ECC algorithm.
5857 */
5858 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5859 if (err < 0)
5860 return err;
5861
5862 if (!strcasecmp(pm, "soft"))
5863 return NAND_ECC_HAMMING;
5864 else if (!strcasecmp(pm, "soft_bch"))
5865 return NAND_ECC_BCH;
5866
5867 return -ENODEV;
5868}
5869
5870static int of_get_nand_ecc_step_size(struct device_node *np)
5871{
5872 int ret;
5873 u32 val;
5874
5875 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5876 return ret ? ret : val;
5877}
5878
5879static int of_get_nand_ecc_strength(struct device_node *np)
5880{
5881 int ret;
5882 u32 val;
5883
5884 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5885 return ret ? ret : val;
5886}
5887
5888static int of_get_nand_bus_width(struct device_node *np)
5889{
5890 u32 val;
5891
5892 if (of_property_read_u32(np, "nand-bus-width", &val))
5893 return 8;
5894
5895 switch (val) {
5896 case 8:
5897 case 16:
5898 return val;
5899 default:
5900 return -EIO;
5901 }
5902}
5903
5904static bool of_get_nand_on_flash_bbt(struct device_node *np)
5905{
5906 return of_property_read_bool(np, "nand-on-flash-bbt");
5907}
5908
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005909static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005910{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005911 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005912 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005913
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005914 if (!dn)
5915 return 0;
5916
Brian Norris5844fee2015-01-23 00:22:27 -08005917 if (of_get_nand_bus_width(dn) == 16)
5918 chip->options |= NAND_BUSWIDTH_16;
5919
Stefan Agnerf922bd72018-06-24 23:27:23 +02005920 if (of_property_read_bool(dn, "nand-is-boot-medium"))
5921 chip->options |= NAND_IS_BOOT_MEDIUM;
5922
Brian Norris5844fee2015-01-23 00:22:27 -08005923 if (of_get_nand_on_flash_bbt(dn))
5924 chip->bbt_options |= NAND_BBT_USE_FLASH;
5925
5926 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005927 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005928 ecc_strength = of_get_nand_ecc_strength(dn);
5929 ecc_step = of_get_nand_ecc_step_size(dn);
5930
Brian Norris5844fee2015-01-23 00:22:27 -08005931 if (ecc_mode >= 0)
5932 chip->ecc.mode = ecc_mode;
5933
Rafał Miłecki79082452016-03-23 11:19:02 +01005934 if (ecc_algo >= 0)
5935 chip->ecc.algo = ecc_algo;
5936
Brian Norris5844fee2015-01-23 00:22:27 -08005937 if (ecc_strength >= 0)
5938 chip->ecc.strength = ecc_strength;
5939
5940 if (ecc_step > 0)
5941 chip->ecc.size = ecc_step;
5942
Boris Brezillonba78ee02016-06-08 17:04:22 +02005943 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5944 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5945
Brian Norris5844fee2015-01-23 00:22:27 -08005946 return 0;
5947}
5948
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005949/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005950 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005951 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005952 * @maxchips: number of chips to scan for
5953 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005954 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005955 * This is the first phase of the normal nand_scan() function. It reads the
5956 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005957 *
Miquel Raynal98732da2018-07-25 15:31:50 +02005958 * This helper used to be called directly from controller drivers that needed
5959 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5960 * prevented dynamic allocations during this phase which was unconvenient and
5961 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005962 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005963static int nand_scan_ident(struct nand_chip *chip, int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005964 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005965{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005966 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08005967 int i, nand_maf_id, nand_dev_id;
Brian Norris5844fee2015-01-23 00:22:27 -08005968 int ret;
5969
Miquel Raynal17fa8042017-11-30 18:01:31 +01005970 /* Enforce the right timings for reset/detection */
5971 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5972
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005973 ret = nand_dt_init(chip);
5974 if (ret)
5975 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005976
Brian Norrisf7a8e382016-01-05 10:39:45 -08005977 if (!mtd->name && mtd->dev.parent)
5978 mtd->name = dev_name(mtd->dev.parent);
5979
Miquel Raynal8878b122017-11-09 14:16:45 +01005980 /*
5981 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5982 * populated.
5983 */
5984 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005985 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005986 * Default functions assigned for ->cmdfunc() and
5987 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005988 */
Miquel Raynal8878b122017-11-09 14:16:45 +01005989 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5990 pr_err("->cmd_ctrl() should be provided\n");
5991 return -EINVAL;
5992 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005993 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005994
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005995 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005996 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005997
5998 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005999 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09006000 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00006001 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07006002 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006003 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09006004 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 }
6006
Boris Brezillon7f501f02016-05-24 19:20:05 +02006007 nand_maf_id = chip->id.data[0];
6008 nand_dev_id = chip->id.data[1];
6009
Huang Shijie07300162012-11-09 16:23:45 +08006010 chip->select_chip(mtd, -1);
6011
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006012 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01006013 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01006014 u8 id[2];
6015
Karl Beldanef89a882008-09-15 14:37:29 +02006016 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02006017 nand_reset(chip, i);
6018
6019 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01006021 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01006023 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08006024 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 break;
Huang Shijie07300162012-11-09 16:23:45 +08006026 }
6027 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 }
6029 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03006030 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006031
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006033 chip->numchips = i;
6034 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
David Woodhouse3b85c322006-09-25 17:06:53 +01006036 return 0;
6037}
6038
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006039static void nand_scan_ident_cleanup(struct nand_chip *chip)
6040{
6041 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02006042 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006043}
6044
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006045static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
6046{
6047 struct nand_chip *chip = mtd_to_nand(mtd);
6048 struct nand_ecc_ctrl *ecc = &chip->ecc;
6049
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006050 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006051 return -EINVAL;
6052
6053 switch (ecc->algo) {
6054 case NAND_ECC_HAMMING:
6055 ecc->calculate = nand_calculate_ecc;
6056 ecc->correct = nand_correct_data;
6057 ecc->read_page = nand_read_page_swecc;
6058 ecc->read_subpage = nand_read_subpage;
6059 ecc->write_page = nand_write_page_swecc;
6060 ecc->read_page_raw = nand_read_page_raw;
6061 ecc->write_page_raw = nand_write_page_raw;
6062 ecc->read_oob = nand_read_oob_std;
6063 ecc->write_oob = nand_write_oob_std;
6064 if (!ecc->size)
6065 ecc->size = 256;
6066 ecc->bytes = 3;
6067 ecc->strength = 1;
6068 return 0;
6069 case NAND_ECC_BCH:
6070 if (!mtd_nand_has_bch()) {
6071 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
6072 return -EINVAL;
6073 }
6074 ecc->calculate = nand_bch_calculate_ecc;
6075 ecc->correct = nand_bch_correct_data;
6076 ecc->read_page = nand_read_page_swecc;
6077 ecc->read_subpage = nand_read_subpage;
6078 ecc->write_page = nand_write_page_swecc;
6079 ecc->read_page_raw = nand_read_page_raw;
6080 ecc->write_page_raw = nand_write_page_raw;
6081 ecc->read_oob = nand_read_oob_std;
6082 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02006083
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006084 /*
6085 * Board driver should supply ecc.size and ecc.strength
6086 * values to select how many bits are correctable.
6087 * Otherwise, default to 4 bits for large page devices.
6088 */
6089 if (!ecc->size && (mtd->oobsize >= 64)) {
6090 ecc->size = 512;
6091 ecc->strength = 4;
6092 }
6093
6094 /*
6095 * if no ecc placement scheme was provided pickup the default
6096 * large page one.
6097 */
6098 if (!mtd->ooblayout) {
6099 /* handle large page devices only */
6100 if (mtd->oobsize < 64) {
6101 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6102 return -EINVAL;
6103 }
6104
6105 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02006106
6107 }
6108
6109 /*
6110 * We can only maximize ECC config when the default layout is
6111 * used, otherwise we don't know how many bytes can really be
6112 * used.
6113 */
6114 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6115 ecc->options & NAND_ECC_MAXIMIZE) {
6116 int steps, bytes;
6117
6118 /* Always prefer 1k blocks over 512bytes ones */
6119 ecc->size = 1024;
6120 steps = mtd->writesize / ecc->size;
6121
6122 /* Reserve 2 bytes for the BBM */
6123 bytes = (mtd->oobsize - 2) / steps;
6124 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006125 }
6126
6127 /* See nand_bch_init() for details. */
6128 ecc->bytes = 0;
6129 ecc->priv = nand_bch_init(mtd);
6130 if (!ecc->priv) {
6131 WARN(1, "BCH ECC initialization failed!\n");
6132 return -EINVAL;
6133 }
6134 return 0;
6135 default:
6136 WARN(1, "Unsupported ECC algorithm!\n");
6137 return -EINVAL;
6138 }
6139}
6140
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006141/**
6142 * nand_check_ecc_caps - check the sanity of preset ECC settings
6143 * @chip: nand chip info structure
6144 * @caps: ECC caps info structure
6145 * @oobavail: OOB size that the ECC engine can use
6146 *
6147 * When ECC step size and strength are already set, check if they are supported
6148 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6149 * On success, the calculated ECC bytes is set.
6150 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306151static int
6152nand_check_ecc_caps(struct nand_chip *chip,
6153 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006154{
6155 struct mtd_info *mtd = nand_to_mtd(chip);
6156 const struct nand_ecc_step_info *stepinfo;
6157 int preset_step = chip->ecc.size;
6158 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306159 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006160 int i, j;
6161
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006162 for (i = 0; i < caps->nstepinfos; i++) {
6163 stepinfo = &caps->stepinfos[i];
6164
6165 if (stepinfo->stepsize != preset_step)
6166 continue;
6167
6168 for (j = 0; j < stepinfo->nstrengths; j++) {
6169 if (stepinfo->strengths[j] != preset_strength)
6170 continue;
6171
6172 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6173 preset_strength);
6174 if (WARN_ON_ONCE(ecc_bytes < 0))
6175 return ecc_bytes;
6176
6177 if (ecc_bytes * nsteps > oobavail) {
6178 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6179 preset_step, preset_strength);
6180 return -ENOSPC;
6181 }
6182
6183 chip->ecc.bytes = ecc_bytes;
6184
6185 return 0;
6186 }
6187 }
6188
6189 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6190 preset_step, preset_strength);
6191
6192 return -ENOTSUPP;
6193}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006194
6195/**
6196 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6197 * @chip: nand chip info structure
6198 * @caps: ECC engine caps info structure
6199 * @oobavail: OOB size that the ECC engine can use
6200 *
6201 * If a chip's ECC requirement is provided, try to meet it with the least
6202 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6203 * On success, the chosen ECC settings are set.
6204 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306205static int
6206nand_match_ecc_req(struct nand_chip *chip,
6207 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006208{
6209 struct mtd_info *mtd = nand_to_mtd(chip);
6210 const struct nand_ecc_step_info *stepinfo;
6211 int req_step = chip->ecc_step_ds;
6212 int req_strength = chip->ecc_strength_ds;
6213 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6214 int best_step, best_strength, best_ecc_bytes;
6215 int best_ecc_bytes_total = INT_MAX;
6216 int i, j;
6217
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006218 /* No information provided by the NAND chip */
6219 if (!req_step || !req_strength)
6220 return -ENOTSUPP;
6221
6222 /* number of correctable bits the chip requires in a page */
6223 req_corr = mtd->writesize / req_step * req_strength;
6224
6225 for (i = 0; i < caps->nstepinfos; i++) {
6226 stepinfo = &caps->stepinfos[i];
6227 step_size = stepinfo->stepsize;
6228
6229 for (j = 0; j < stepinfo->nstrengths; j++) {
6230 strength = stepinfo->strengths[j];
6231
6232 /*
6233 * If both step size and strength are smaller than the
6234 * chip's requirement, it is not easy to compare the
6235 * resulted reliability.
6236 */
6237 if (step_size < req_step && strength < req_strength)
6238 continue;
6239
6240 if (mtd->writesize % step_size)
6241 continue;
6242
6243 nsteps = mtd->writesize / step_size;
6244
6245 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6246 if (WARN_ON_ONCE(ecc_bytes < 0))
6247 continue;
6248 ecc_bytes_total = ecc_bytes * nsteps;
6249
6250 if (ecc_bytes_total > oobavail ||
6251 strength * nsteps < req_corr)
6252 continue;
6253
6254 /*
6255 * We assume the best is to meet the chip's requrement
6256 * with the least number of ECC bytes.
6257 */
6258 if (ecc_bytes_total < best_ecc_bytes_total) {
6259 best_ecc_bytes_total = ecc_bytes_total;
6260 best_step = step_size;
6261 best_strength = strength;
6262 best_ecc_bytes = ecc_bytes;
6263 }
6264 }
6265 }
6266
6267 if (best_ecc_bytes_total == INT_MAX)
6268 return -ENOTSUPP;
6269
6270 chip->ecc.size = best_step;
6271 chip->ecc.strength = best_strength;
6272 chip->ecc.bytes = best_ecc_bytes;
6273
6274 return 0;
6275}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006276
6277/**
6278 * nand_maximize_ecc - choose the max ECC strength available
6279 * @chip: nand chip info structure
6280 * @caps: ECC engine caps info structure
6281 * @oobavail: OOB size that the ECC engine can use
6282 *
6283 * Choose the max ECC strength that is supported on the controller, and can fit
6284 * within the chip's OOB. On success, the chosen ECC settings are set.
6285 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306286static int
6287nand_maximize_ecc(struct nand_chip *chip,
6288 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006289{
6290 struct mtd_info *mtd = nand_to_mtd(chip);
6291 const struct nand_ecc_step_info *stepinfo;
6292 int step_size, strength, nsteps, ecc_bytes, corr;
6293 int best_corr = 0;
6294 int best_step = 0;
6295 int best_strength, best_ecc_bytes;
6296 int i, j;
6297
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006298 for (i = 0; i < caps->nstepinfos; i++) {
6299 stepinfo = &caps->stepinfos[i];
6300 step_size = stepinfo->stepsize;
6301
6302 /* If chip->ecc.size is already set, respect it */
6303 if (chip->ecc.size && step_size != chip->ecc.size)
6304 continue;
6305
6306 for (j = 0; j < stepinfo->nstrengths; j++) {
6307 strength = stepinfo->strengths[j];
6308
6309 if (mtd->writesize % step_size)
6310 continue;
6311
6312 nsteps = mtd->writesize / step_size;
6313
6314 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6315 if (WARN_ON_ONCE(ecc_bytes < 0))
6316 continue;
6317
6318 if (ecc_bytes * nsteps > oobavail)
6319 continue;
6320
6321 corr = strength * nsteps;
6322
6323 /*
6324 * If the number of correctable bits is the same,
6325 * bigger step_size has more reliability.
6326 */
6327 if (corr > best_corr ||
6328 (corr == best_corr && step_size > best_step)) {
6329 best_corr = corr;
6330 best_step = step_size;
6331 best_strength = strength;
6332 best_ecc_bytes = ecc_bytes;
6333 }
6334 }
6335 }
6336
6337 if (!best_corr)
6338 return -ENOTSUPP;
6339
6340 chip->ecc.size = best_step;
6341 chip->ecc.strength = best_strength;
6342 chip->ecc.bytes = best_ecc_bytes;
6343
6344 return 0;
6345}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006346
Abhishek Sahu181ace92018-06-20 12:57:28 +05306347/**
6348 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
6349 * @chip: nand chip info structure
6350 * @caps: ECC engine caps info structure
6351 * @oobavail: OOB size that the ECC engine can use
6352 *
6353 * Choose the ECC configuration according to following logic
6354 *
6355 * 1. If both ECC step size and ECC strength are already set (usually by DT)
6356 * then check if it is supported by this controller.
6357 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
6358 * 3. Otherwise, try to match the ECC step size and ECC strength closest
6359 * to the chip's requirement. If available OOB size can't fit the chip
6360 * requirement then fallback to the maximum ECC step size and ECC strength.
6361 *
6362 * On success, the chosen ECC settings are set.
6363 */
6364int nand_ecc_choose_conf(struct nand_chip *chip,
6365 const struct nand_ecc_caps *caps, int oobavail)
6366{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306367 struct mtd_info *mtd = nand_to_mtd(chip);
6368
6369 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
6370 return -EINVAL;
6371
Abhishek Sahu181ace92018-06-20 12:57:28 +05306372 if (chip->ecc.size && chip->ecc.strength)
6373 return nand_check_ecc_caps(chip, caps, oobavail);
6374
6375 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
6376 return nand_maximize_ecc(chip, caps, oobavail);
6377
6378 if (!nand_match_ecc_req(chip, caps, oobavail))
6379 return 0;
6380
6381 return nand_maximize_ecc(chip, caps, oobavail);
6382}
6383EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
6384
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006385/*
6386 * Check if the chip configuration meet the datasheet requirements.
6387
6388 * If our configuration corrects A bits per B bytes and the minimum
6389 * required correction level is X bits per Y bytes, then we must ensure
6390 * both of the following are true:
6391 *
6392 * (1) A / B >= X / Y
6393 * (2) A >= X
6394 *
6395 * Requirement (1) ensures we can correct for the required bitflip density.
6396 * Requirement (2) ensures we can correct even when all bitflips are clumped
6397 * in the same sector.
6398 */
6399static bool nand_ecc_strength_good(struct mtd_info *mtd)
6400{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006401 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006402 struct nand_ecc_ctrl *ecc = &chip->ecc;
6403 int corr, ds_corr;
6404
6405 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6406 /* Not enough information */
6407 return true;
6408
6409 /*
6410 * We get the number of corrected bits per page to compare
6411 * the correction density.
6412 */
6413 corr = (mtd->writesize * ecc->strength) / ecc->size;
6414 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6415
6416 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6417}
David Woodhouse3b85c322006-09-25 17:06:53 +01006418
6419/**
Miquel Raynal98732da2018-07-25 15:31:50 +02006420 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006421 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01006422 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006423 * This is the second phase of the normal nand_scan() function. It fills out
6424 * all the uninitialized function pointers with the defaults and scans for a
6425 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006426 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006427static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01006428{
Boris Brezillon00ad3782018-09-06 14:05:14 +02006429 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08006430 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006431 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006432
Brian Norrise2414f42012-02-06 13:44:00 -08006433 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006434 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006435 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006436 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006437 }
Brian Norrise2414f42012-02-06 13:44:00 -08006438
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006439 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006440 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006441 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006442
Boris Brezillonf84674b2017-06-02 12:18:24 +02006443 /*
6444 * FIXME: some NAND manufacturer drivers expect the first die to be
6445 * selected when manufacturer->init() is called. They should be fixed
6446 * to explictly select the relevant die when interacting with the NAND
6447 * chip.
6448 */
6449 chip->select_chip(mtd, 0);
6450 ret = nand_manufacturer_init(chip);
6451 chip->select_chip(mtd, -1);
6452 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006453 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006454
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006455 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006456 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006457
6458 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006459 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006460 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006461 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006462 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006463 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006466 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 break;
6468 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006469 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006470 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006471 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006473 /*
6474 * Expose the whole OOB area to users if ECC_NONE
6475 * is passed. We could do that for all kind of
6476 * ->oobsize, but we must keep the old large/small
6477 * page with ECC layout when ->oobsize <= 128 for
6478 * compatibility reasons.
6479 */
6480 if (ecc->mode == NAND_ECC_NONE) {
6481 mtd_set_ooblayout(mtd,
6482 &nand_ooblayout_lp_ops);
6483 break;
6484 }
6485
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006486 WARN(1, "No oob scheme defined for oobsize %d\n",
6487 mtd->oobsize);
6488 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006489 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 }
6491 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006492
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006493 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006494 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006495 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006496 */
David Woodhouse956e9442006-09-25 17:12:39 +01006497
Huang Shijie97de79e02013-10-18 14:20:53 +08006498 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006499 case NAND_ECC_HW_OOB_FIRST:
6500 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006501 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006502 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6503 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006504 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006505 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006506 if (!ecc->read_page)
6507 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006508
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006509 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006510 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006511 if (!ecc->read_page)
6512 ecc->read_page = nand_read_page_hwecc;
6513 if (!ecc->write_page)
6514 ecc->write_page = nand_write_page_hwecc;
6515 if (!ecc->read_page_raw)
6516 ecc->read_page_raw = nand_read_page_raw;
6517 if (!ecc->write_page_raw)
6518 ecc->write_page_raw = nand_write_page_raw;
6519 if (!ecc->read_oob)
6520 ecc->read_oob = nand_read_oob_std;
6521 if (!ecc->write_oob)
6522 ecc->write_oob = nand_write_oob_std;
6523 if (!ecc->read_subpage)
6524 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006525 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006526 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006527
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006528 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006529 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6530 (!ecc->read_page ||
6531 ecc->read_page == nand_read_page_hwecc ||
6532 !ecc->write_page ||
6533 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006534 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6535 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006536 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006537 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006538 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006539 if (!ecc->read_page)
6540 ecc->read_page = nand_read_page_syndrome;
6541 if (!ecc->write_page)
6542 ecc->write_page = nand_write_page_syndrome;
6543 if (!ecc->read_page_raw)
6544 ecc->read_page_raw = nand_read_page_raw_syndrome;
6545 if (!ecc->write_page_raw)
6546 ecc->write_page_raw = nand_write_page_raw_syndrome;
6547 if (!ecc->read_oob)
6548 ecc->read_oob = nand_read_oob_syndrome;
6549 if (!ecc->write_oob)
6550 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006551
Huang Shijie97de79e02013-10-18 14:20:53 +08006552 if (mtd->writesize >= ecc->size) {
6553 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006554 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6555 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006556 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006557 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006558 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006559 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006560 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6561 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006562 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006563 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006565 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006566 ret = nand_set_ecc_soft_ops(mtd);
6567 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006568 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006569 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006570 }
6571 break;
6572
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006573 case NAND_ECC_ON_DIE:
6574 if (!ecc->read_page || !ecc->write_page) {
6575 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6576 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006577 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006578 }
6579 if (!ecc->read_oob)
6580 ecc->read_oob = nand_read_oob_std;
6581 if (!ecc->write_oob)
6582 ecc->write_oob = nand_write_oob_std;
6583 break;
6584
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006585 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006586 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006587 ecc->read_page = nand_read_page_raw;
6588 ecc->write_page = nand_write_page_raw;
6589 ecc->read_oob = nand_read_oob_std;
6590 ecc->read_page_raw = nand_read_page_raw;
6591 ecc->write_page_raw = nand_write_page_raw;
6592 ecc->write_oob = nand_write_oob_std;
6593 ecc->size = mtd->writesize;
6594 ecc->bytes = 0;
6595 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006597
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006599 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6600 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006601 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006604 if (ecc->correct || ecc->calculate) {
6605 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6606 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6607 if (!ecc->calc_buf || !ecc->code_buf) {
6608 ret = -ENOMEM;
6609 goto err_nand_manuf_cleanup;
6610 }
6611 }
6612
Brian Norris9ce244b2011-08-30 18:45:37 -07006613 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006614 if (!ecc->read_oob_raw)
6615 ecc->read_oob_raw = ecc->read_oob;
6616 if (!ecc->write_oob_raw)
6617 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006618
Boris Brezillon846031d2016-02-03 20:11:00 +01006619 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006620 mtd->ecc_strength = ecc->strength;
6621 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006622
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006623 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006624 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006625 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006626 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006627 ecc->steps = mtd->writesize / ecc->size;
6628 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006629 WARN(1, "Invalid ECC parameters\n");
6630 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006631 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006633 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006634 if (ecc->total > mtd->oobsize) {
6635 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6636 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006637 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006638 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006639
Boris Brezillon846031d2016-02-03 20:11:00 +01006640 /*
6641 * The number of bytes available for a client to place data into
6642 * the out of band area.
6643 */
6644 ret = mtd_ooblayout_count_freebytes(mtd);
6645 if (ret < 0)
6646 ret = 0;
6647
6648 mtd->oobavail = ret;
6649
6650 /* ECC sanity check: warn if it's too weak */
6651 if (!nand_ecc_strength_good(mtd))
6652 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6653 mtd->name);
6654
Brian Norris8b6e50c2011-05-25 14:59:01 -07006655 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006656 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006657 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006658 case 2:
6659 mtd->subpage_sft = 1;
6660 break;
6661 case 4:
6662 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006663 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006664 mtd->subpage_sft = 2;
6665 break;
6666 }
6667 }
6668 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6669
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006670 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006671 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006674 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006676 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306677 switch (ecc->mode) {
6678 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306679 if (chip->page_shift > 9)
6680 chip->options |= NAND_SUBPAGE_READ;
6681 break;
6682
6683 default:
6684 break;
6685 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006686
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006688 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006689 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6690 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006691 mtd->_erase = nand_erase;
6692 mtd->_point = NULL;
6693 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006694 mtd->_panic_write = panic_nand_write;
6695 mtd->_read_oob = nand_read_oob;
6696 mtd->_write_oob = nand_write_oob;
6697 mtd->_sync = nand_sync;
6698 mtd->_lock = NULL;
6699 mtd->_unlock = NULL;
6700 mtd->_suspend = nand_suspend;
6701 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006702 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006703 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006704 mtd->_block_isbad = nand_block_isbad;
6705 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006706 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006707 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006709 /*
6710 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6711 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6712 * properly set.
6713 */
6714 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006715 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
Boris Brezillonf84674b2017-06-02 12:18:24 +02006717 /* Initialize the ->data_interface field. */
6718 ret = nand_init_data_interface(chip);
6719 if (ret)
6720 goto err_nand_manuf_cleanup;
6721
6722 /* Enter fastest possible mode on all dies. */
6723 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006724 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006725 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006726 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006727 }
6728
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006729 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006730 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732
6733 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02006734 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07006735 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006736 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006737
Brian Norris44d41822017-05-01 17:04:50 -07006738 return 0;
6739
Boris Brezillonf84674b2017-06-02 12:18:24 +02006740
6741err_nand_manuf_cleanup:
6742 nand_manufacturer_cleanup(chip);
6743
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006744err_free_buf:
6745 kfree(chip->data_buf);
6746 kfree(ecc->code_buf);
6747 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006748
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006749 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750}
6751
Miquel Raynal05b54c72018-07-19 01:05:46 +02006752static int nand_attach(struct nand_chip *chip)
6753{
6754 if (chip->controller->ops && chip->controller->ops->attach_chip)
6755 return chip->controller->ops->attach_chip(chip);
6756
6757 return 0;
6758}
6759
6760static void nand_detach(struct nand_chip *chip)
6761{
6762 if (chip->controller->ops && chip->controller->ops->detach_chip)
6763 chip->controller->ops->detach_chip(chip);
6764}
6765
David Woodhouse3b85c322006-09-25 17:06:53 +01006766/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006767 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006768 * @chip: NAND chip object
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006769 * @maxchips: number of chips to scan for. @nand_scan_ident() will not be run if
6770 * this parameter is zero (useful for specific drivers that must
6771 * handle this part of the process themselves, e.g docg4).
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006772 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01006773 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006774 * This fills out all the uninitialized function pointers with the defaults.
6775 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006776 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006777 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006778int nand_scan_with_ids(struct nand_chip *chip, int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006779 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01006780{
6781 int ret;
6782
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006783 if (maxchips) {
Boris Brezillon00ad3782018-09-06 14:05:14 +02006784 ret = nand_scan_ident(chip, maxchips, ids);
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006785 if (ret)
6786 return ret;
6787 }
Miquel Raynal05b54c72018-07-19 01:05:46 +02006788
6789 ret = nand_attach(chip);
6790 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006791 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006792
Boris Brezillon00ad3782018-09-06 14:05:14 +02006793 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006794 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006795 goto detach_chip;
6796
6797 return 0;
6798
6799detach_chip:
6800 nand_detach(chip);
6801cleanup_ident:
6802 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006803
David Woodhouse3b85c322006-09-25 17:06:53 +01006804 return ret;
6805}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006806EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006807
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006809 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6810 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006811 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006812void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006814 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006815 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006816 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6817
Jesper Juhlfa671642005-11-07 01:01:27 -08006818 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006819 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006820 kfree(chip->data_buf);
6821 kfree(chip->ecc.code_buf);
6822 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006823
6824 /* Free bad block descriptor memory */
6825 if (chip->badblock_pattern && chip->badblock_pattern->options
6826 & NAND_BBT_DYNAMICSTRUCT)
6827 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006828
6829 /* Free manufacturer priv data. */
6830 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006831
6832 /* Free controller specific allocations after chip identification */
6833 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006834
6835 /* Free identification phase allocations */
6836 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837}
Miquel Raynal05b54c72018-07-19 01:05:46 +02006838
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006839EXPORT_SYMBOL_GPL(nand_cleanup);
6840
6841/**
6842 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6843 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02006844 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006845 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02006846void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006847{
Boris Brezillon59ac2762018-09-06 14:05:15 +02006848 mtd_device_unregister(nand_to_mtd(chip));
6849 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006850}
David Woodhousee0c7d762006-05-13 18:07:53 +01006851EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006852
David Woodhousee0c7d762006-05-13 18:07:53 +01006853MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006854MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6855MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006856MODULE_DESCRIPTION("Generic NAND flash driver code");