blob: fd0563fc4ad2ae093179efc40f00649455a56fae [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++) {
430 res = chip->ecc.read_oob(mtd, 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
2981 * @mtd: mtd info structure
2982 * @chip: nand chip info structure
2983 * @buf: buffer to store read data
2984 * @oob_required: caller requires OOB data read to chip->oob_poi
2985 * @page: page number to read
2986 *
2987 * Returns -ENOTSUPP unconditionally.
2988 */
2989int nand_read_page_raw_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
2990 u8 *buf, int oob_required, int page)
2991{
2992 return -ENOTSUPP;
2993}
2994EXPORT_SYMBOL(nand_read_page_raw_notsupp);
2995
2996/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002997 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002998 * @mtd: mtd info structure
2999 * @chip: nand chip info structure
3000 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003001 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003002 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003003 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003004 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003005 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003006int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
3007 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003008{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003009 int ret;
3010
Boris Brezillon25f815f2017-11-30 18:01:30 +01003011 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003012 if (ret)
3013 return ret;
3014
3015 if (oob_required) {
3016 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
3017 false);
3018 if (ret)
3019 return ret;
3020 }
3021
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003022 return 0;
3023}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003024EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003025
3026/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003027 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07003028 * @mtd: mtd info structure
3029 * @chip: nand chip info structure
3030 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003031 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003032 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003033 *
3034 * We need a special oob layout and handling even when OOB isn't used.
3035 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003036static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003037 struct nand_chip *chip, uint8_t *buf,
3038 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003039{
3040 int eccsize = chip->ecc.size;
3041 int eccbytes = chip->ecc.bytes;
3042 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003043 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003044
Boris Brezillon25f815f2017-11-30 18:01:30 +01003045 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3046 if (ret)
3047 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003048
3049 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003050 ret = nand_read_data_op(chip, buf, eccsize, false);
3051 if (ret)
3052 return ret;
3053
David Brownell52ff49d2009-03-04 12:01:36 -08003054 buf += eccsize;
3055
3056 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003057 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3058 false);
3059 if (ret)
3060 return ret;
3061
David Brownell52ff49d2009-03-04 12:01:36 -08003062 oob += chip->ecc.prepad;
3063 }
3064
Boris Brezillon97d90da2017-11-30 18:01:29 +01003065 ret = nand_read_data_op(chip, oob, eccbytes, false);
3066 if (ret)
3067 return ret;
3068
David Brownell52ff49d2009-03-04 12:01:36 -08003069 oob += eccbytes;
3070
3071 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003072 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3073 false);
3074 if (ret)
3075 return ret;
3076
David Brownell52ff49d2009-03-04 12:01:36 -08003077 oob += chip->ecc.postpad;
3078 }
3079 }
3080
3081 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003082 if (size) {
3083 ret = nand_read_data_op(chip, oob, size, false);
3084 if (ret)
3085 return ret;
3086 }
David Brownell52ff49d2009-03-04 12:01:36 -08003087
3088 return 0;
3089}
3090
3091/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003092 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003093 * @mtd: mtd info structure
3094 * @chip: nand chip info structure
3095 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003096 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003097 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003098 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003099static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003100 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
Boris Brezillon846031d2016-02-03 20:11:00 +01003102 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003103 int eccbytes = chip->ecc.bytes;
3104 int eccsteps = chip->ecc.steps;
3105 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003106 uint8_t *ecc_calc = chip->ecc.calc_buf;
3107 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003108 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003109
Brian Norris1fbb9382012-05-02 10:14:55 -07003110 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003111
3112 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
3113 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3114
Boris Brezillon846031d2016-02-03 20:11:00 +01003115 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3116 chip->ecc.total);
3117 if (ret)
3118 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003119
3120 eccsteps = chip->ecc.steps;
3121 p = buf;
3122
3123 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3124 int stat;
3125
3126 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003127 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003128 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003129 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003130 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003131 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3132 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003133 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003134 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003135}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303138 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003139 * @mtd: mtd info structure
3140 * @chip: nand chip info structure
3141 * @data_offs: offset of requested data within the page
3142 * @readlen: data length
3143 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003144 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003145 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003146static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003147 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
3148 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003149{
Boris Brezillon846031d2016-02-03 20:11:00 +01003150 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003151 uint8_t *p;
3152 int data_col_addr, i, gaps = 0;
3153 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3154 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003155 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003156 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003157 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003158
Brian Norris7854d3f2011-06-23 14:12:08 -07003159 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003160 start_step = data_offs / chip->ecc.size;
3161 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3162 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303163 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003164
Brian Norris8b6e50c2011-05-25 14:59:01 -07003165 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003166 datafrag_len = num_steps * chip->ecc.size;
3167 eccfrag_len = num_steps * chip->ecc.bytes;
3168
3169 data_col_addr = start_step * chip->ecc.size;
3170 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003171 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003172 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003173 if (ret)
3174 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003175
Brian Norris8b6e50c2011-05-25 14:59:01 -07003176 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003177 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003178 chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003179
Brian Norris8b6e50c2011-05-25 14:59:01 -07003180 /*
3181 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003182 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003183 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003184 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3185 if (ret)
3186 return ret;
3187
3188 if (oobregion.length < eccfrag_len)
3189 gaps = 1;
3190
Alexey Korolev3d459552008-05-15 17:23:18 +01003191 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003192 ret = nand_change_read_column_op(chip, mtd->writesize,
3193 chip->oob_poi, mtd->oobsize,
3194 false);
3195 if (ret)
3196 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003197 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003198 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003199 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003200 * about buswidth alignment in read_buf.
3201 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003202 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003203 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003204 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003205 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003206 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3207 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003208 aligned_len++;
3209
Boris Brezillon97d90da2017-11-30 18:01:29 +01003210 ret = nand_change_read_column_op(chip,
3211 mtd->writesize + aligned_pos,
3212 &chip->oob_poi[aligned_pos],
3213 aligned_len, false);
3214 if (ret)
3215 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003216 }
3217
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003218 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003219 chip->oob_poi, index, eccfrag_len);
3220 if (ret)
3221 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003222
3223 p = bufpoi + data_col_addr;
3224 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3225 int stat;
3226
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003227 stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i],
3228 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003229 if (stat == -EBADMSG &&
3230 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3231 /* check for empty pages with bitflips */
3232 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003233 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003234 chip->ecc.bytes,
3235 NULL, 0,
3236 chip->ecc.strength);
3237 }
3238
Mike Dunn3f91e942012-04-25 12:06:09 -07003239 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003240 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003241 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003242 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003243 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3244 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003245 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003246 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003247}
3248
3249/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003250 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003251 * @mtd: mtd info structure
3252 * @chip: nand chip info structure
3253 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003254 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003255 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003256 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003257 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003258 */
3259static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003260 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003261{
Boris Brezillon846031d2016-02-03 20:11:00 +01003262 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003263 int eccbytes = chip->ecc.bytes;
3264 int eccsteps = chip->ecc.steps;
3265 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003266 uint8_t *ecc_calc = chip->ecc.calc_buf;
3267 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003268 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003269
Boris Brezillon25f815f2017-11-30 18:01:30 +01003270 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3271 if (ret)
3272 return ret;
3273
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003274 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003275 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003276
3277 ret = nand_read_data_op(chip, p, eccsize, false);
3278 if (ret)
3279 return ret;
3280
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003281 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3282 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003283
3284 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3285 if (ret)
3286 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003287
Boris Brezillon846031d2016-02-03 20:11:00 +01003288 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3289 chip->ecc.total);
3290 if (ret)
3291 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003292
3293 eccsteps = chip->ecc.steps;
3294 p = buf;
3295
3296 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3297 int stat;
3298
3299 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003300 if (stat == -EBADMSG &&
3301 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3302 /* check for empty pages with bitflips */
3303 stat = nand_check_erased_ecc_chunk(p, eccsize,
3304 &ecc_code[i], eccbytes,
3305 NULL, 0,
3306 chip->ecc.strength);
3307 }
3308
Mike Dunn3f91e942012-04-25 12:06:09 -07003309 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003310 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003311 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003312 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003313 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3314 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003315 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003316 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003317}
3318
3319/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003320 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003321 * @mtd: mtd info structure
3322 * @chip: nand chip info structure
3323 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003324 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003325 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003326 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003327 * Hardware ECC for large page chips, require OOB to be read first. For this
3328 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3329 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3330 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3331 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003332 */
3333static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07003334 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003335{
Boris Brezillon846031d2016-02-03 20:11:00 +01003336 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003337 int eccbytes = chip->ecc.bytes;
3338 int eccsteps = chip->ecc.steps;
3339 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003340 uint8_t *ecc_code = chip->ecc.code_buf;
3341 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003342 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003343
3344 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003345 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3346 if (ret)
3347 return ret;
3348
3349 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3350 if (ret)
3351 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003352
Boris Brezillon846031d2016-02-03 20:11:00 +01003353 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3354 chip->ecc.total);
3355 if (ret)
3356 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003357
3358 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3359 int stat;
3360
Boris Brezillonec476362018-09-06 14:05:17 +02003361 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003362
3363 ret = nand_read_data_op(chip, p, eccsize, false);
3364 if (ret)
3365 return ret;
3366
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003367 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
3368
3369 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003370 if (stat == -EBADMSG &&
3371 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3372 /* check for empty pages with bitflips */
3373 stat = nand_check_erased_ecc_chunk(p, eccsize,
3374 &ecc_code[i], eccbytes,
3375 NULL, 0,
3376 chip->ecc.strength);
3377 }
3378
Mike Dunn3f91e942012-04-25 12:06:09 -07003379 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003380 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003381 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003382 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003383 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3384 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003385 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003386 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003387}
3388
3389/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003390 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003391 * @mtd: mtd info structure
3392 * @chip: nand chip info structure
3393 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003394 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003395 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003396 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003397 * The hw generator calculates the error syndrome automatically. Therefore we
3398 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003399 */
3400static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07003401 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003402{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003403 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003404 int eccbytes = chip->ecc.bytes;
3405 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003406 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003407 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003408 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003409 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003410
Boris Brezillon25f815f2017-11-30 18:01:30 +01003411 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3412 if (ret)
3413 return ret;
3414
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003415 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3416 int stat;
3417
Boris Brezillonec476362018-09-06 14:05:17 +02003418 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003419
3420 ret = nand_read_data_op(chip, p, eccsize, false);
3421 if (ret)
3422 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003423
3424 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003425 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3426 false);
3427 if (ret)
3428 return ret;
3429
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003430 oob += chip->ecc.prepad;
3431 }
3432
Boris Brezillonec476362018-09-06 14:05:17 +02003433 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003434
3435 ret = nand_read_data_op(chip, oob, eccbytes, false);
3436 if (ret)
3437 return ret;
3438
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003439 stat = chip->ecc.correct(mtd, p, oob, NULL);
3440
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003441 oob += eccbytes;
3442
3443 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003444 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3445 false);
3446 if (ret)
3447 return ret;
3448
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003449 oob += chip->ecc.postpad;
3450 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003451
3452 if (stat == -EBADMSG &&
3453 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3454 /* check for empty pages with bitflips */
3455 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3456 oob - eccpadbytes,
3457 eccpadbytes,
3458 NULL, 0,
3459 chip->ecc.strength);
3460 }
3461
3462 if (stat < 0) {
3463 mtd->ecc_stats.failed++;
3464 } else {
3465 mtd->ecc_stats.corrected += stat;
3466 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3467 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003468 }
3469
3470 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003471 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003472 if (i) {
3473 ret = nand_read_data_op(chip, oob, i, false);
3474 if (ret)
3475 return ret;
3476 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003477
Mike Dunn3f91e942012-04-25 12:06:09 -07003478 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003479}
3480
3481/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003482 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003483 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003484 * @oob: oob destination address
3485 * @ops: oob ops structure
3486 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003487 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003488static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003489 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003490{
Boris Brezillon846031d2016-02-03 20:11:00 +01003491 struct nand_chip *chip = mtd_to_nand(mtd);
3492 int ret;
3493
Florian Fainellif8ac0412010-09-07 13:23:43 +02003494 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003495
Brian Norris0612b9d2011-08-30 18:45:40 -07003496 case MTD_OPS_PLACE_OOB:
3497 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003498 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3499 return oob + len;
3500
Boris Brezillon846031d2016-02-03 20:11:00 +01003501 case MTD_OPS_AUTO_OOB:
3502 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3503 ops->ooboffs, len);
3504 BUG_ON(ret);
3505 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003506
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003507 default:
3508 BUG();
3509 }
3510 return NULL;
3511}
3512
3513/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003514 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3515 * @mtd: MTD device structure
3516 * @retry_mode: the retry mode to use
3517 *
3518 * Some vendors supply a special command to shift the Vt threshold, to be used
3519 * when there are too many bitflips in a page (i.e., ECC error). After setting
3520 * a new threshold, the host should retry reading the page.
3521 */
3522static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3523{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003524 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08003525
3526 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3527
3528 if (retry_mode >= chip->read_retries)
3529 return -EINVAL;
3530
3531 if (!chip->setup_read_retry)
3532 return -EOPNOTSUPP;
3533
3534 return chip->setup_read_retry(mtd, retry_mode);
3535}
3536
Boris Brezillon85e08e52018-07-27 09:44:17 +02003537static void nand_wait_readrdy(struct nand_chip *chip)
3538{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003539 const struct nand_sdr_timings *sdr;
3540
Boris Brezillon85e08e52018-07-27 09:44:17 +02003541 if (!(chip->options & NAND_NEED_READRDY))
3542 return;
3543
Boris Brezillon52f05b62018-07-27 09:44:18 +02003544 sdr = nand_get_sdr_timings(&chip->data_interface);
3545 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003546}
3547
Brian Norrisba84fb52014-01-03 15:13:33 -08003548/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003549 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003550 * @mtd: MTD device structure
3551 * @from: offset to read from
3552 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003553 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003554 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003555 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003556static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3557 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003558{
Brian Norrise47f3db2012-05-02 10:14:56 -07003559 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003560 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003561 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003562 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003563 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003564 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003565
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003566 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003567 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003568 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003569 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003570 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003572 chipnr = (int)(from >> chip->chip_shift);
3573 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003575 realpage = (int)(from >> chip->page_shift);
3576 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003578 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003580 buf = ops->datbuf;
3581 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003582 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003583
Florian Fainellif8ac0412010-09-07 13:23:43 +02003584 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003585 unsigned int ecc_failures = mtd->ecc_stats.failed;
3586
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003587 bytes = min(mtd->writesize - col, readlen);
3588 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003589
Kamal Dasu66507c72014-05-01 20:51:19 -04003590 if (!aligned)
3591 use_bufpoi = 1;
3592 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003593 use_bufpoi = !virt_addr_valid(buf) ||
3594 !IS_ALIGNED((unsigned long)buf,
3595 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003596 else
3597 use_bufpoi = 0;
3598
Brian Norris8b6e50c2011-05-25 14:59:01 -07003599 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003600 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003601 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003602
3603 if (use_bufpoi && aligned)
3604 pr_debug("%s: using read bounce buffer for buf@%p\n",
3605 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Brian Norrisba84fb52014-01-03 15:13:33 -08003607read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003608 /*
3609 * Now read the page into the buffer. Absent an error,
3610 * the read methods return max bitflips per ecc step.
3611 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003612 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07003613 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003614 oob_required,
3615 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003616 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3617 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003618 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08003619 col, bytes, bufpoi,
3620 page);
David Woodhouse956e9442006-09-25 17:12:39 +01003621 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07003622 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003623 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003624 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003625 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003626 /* Invalidate page cache */
3627 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003628 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003629 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003630
3631 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003632 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003633 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003634 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003635 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003636 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003637 chip->pagebuf_bitflips = ret;
3638 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003639 /* Invalidate page cache */
3640 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003641 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003642 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003644
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003645 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003646 int toread = min(oobreadlen, max_oobsize);
3647
3648 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003649 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003650 oob, ops, toread);
3651 oobreadlen -= toread;
3652 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003653 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003654
Boris Brezillon85e08e52018-07-27 09:44:17 +02003655 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003656
Brian Norrisba84fb52014-01-03 15:13:33 -08003657 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003658 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003659 retry_mode++;
3660 ret = nand_setup_read_retry(mtd,
3661 retry_mode);
3662 if (ret < 0)
3663 break;
3664
3665 /* Reset failures; retry */
3666 mtd->ecc_stats.failed = ecc_failures;
3667 goto read_retry;
3668 } else {
3669 /* No more retry modes; real failure */
3670 ecc_fail = true;
3671 }
3672 }
3673
3674 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003675 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003676 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003677 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003678 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003679 max_bitflips = max_t(unsigned int, max_bitflips,
3680 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003683 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003684
Brian Norrisba84fb52014-01-03 15:13:33 -08003685 /* Reset to retry mode 0 */
3686 if (retry_mode) {
3687 ret = nand_setup_read_retry(mtd, 0);
3688 if (ret < 0)
3689 break;
3690 retry_mode = 0;
3691 }
3692
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003693 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Brian Norris8b6e50c2011-05-25 14:59:01 -07003696 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 col = 0;
3698 /* Increment page address */
3699 realpage++;
3700
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003701 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 /* Check, if we cross a chip boundary */
3703 if (!page) {
3704 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003705 chip->select_chip(mtd, -1);
3706 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003709 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003711 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003712 if (oob)
3713 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Mike Dunn3f91e942012-04-25 12:06:09 -07003715 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003716 return ret;
3717
Brian Norrisb72f3df2013-12-03 11:04:14 -08003718 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003719 return -EBADMSG;
3720
Mike Dunnedbc45402012-04-25 12:06:11 -07003721 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003722}
3723
3724/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003725 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003726 * @mtd: mtd info structure
3727 * @chip: nand chip info structure
3728 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003729 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003730int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003731{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003732 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003733}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003734EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003735
3736/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003737 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003738 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003739 * @mtd: mtd info structure
3740 * @chip: nand chip info structure
3741 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003742 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003743int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3744 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003745{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003746 int length = mtd->oobsize;
3747 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3748 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003749 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003750 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003751
Boris Brezillon97d90da2017-11-30 18:01:29 +01003752 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3753 if (ret)
3754 return ret;
3755
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003756 for (i = 0; i < chip->ecc.steps; i++) {
3757 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003758 int ret;
3759
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003760 pos = eccsize + i * (eccsize + chunk);
3761 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003762 ret = nand_change_read_column_op(chip, pos,
3763 NULL, 0,
3764 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003765 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003766 ret = nand_read_page_op(chip, page, pos, NULL,
3767 0);
3768
3769 if (ret)
3770 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003771 } else
3772 sndrnd = 1;
3773 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003774
3775 ret = nand_read_data_op(chip, bufpoi, toread, false);
3776 if (ret)
3777 return ret;
3778
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003779 bufpoi += toread;
3780 length -= toread;
3781 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003782 if (length > 0) {
3783 ret = nand_read_data_op(chip, bufpoi, length, false);
3784 if (ret)
3785 return ret;
3786 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003787
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003788 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003789}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003790EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003791
3792/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003793 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003794 * @mtd: mtd info structure
3795 * @chip: nand chip info structure
3796 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003797 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003798int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003799{
Boris Brezillon97d90da2017-11-30 18:01:29 +01003800 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3801 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003802}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003803EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003804
3805/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003806 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003807 * with syndrome - only for large page flash
3808 * @mtd: mtd info structure
3809 * @chip: nand chip info structure
3810 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003811 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003812int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
3813 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003814{
3815 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3816 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003817 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003818 const uint8_t *bufpoi = chip->oob_poi;
3819
3820 /*
3821 * data-ecc-data-ecc ... ecc-oob
3822 * or
3823 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3824 */
3825 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3826 pos = steps * (eccsize + chunk);
3827 steps = 0;
3828 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003829 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003830
Boris Brezillon97d90da2017-11-30 18:01:29 +01003831 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3832 if (ret)
3833 return ret;
3834
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003835 for (i = 0; i < steps; i++) {
3836 if (sndcmd) {
3837 if (mtd->writesize <= 512) {
3838 uint32_t fill = 0xFFFFFFFF;
3839
3840 len = eccsize;
3841 while (len > 0) {
3842 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003843
3844 ret = nand_write_data_op(chip, &fill,
3845 num, false);
3846 if (ret)
3847 return ret;
3848
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003849 len -= num;
3850 }
3851 } else {
3852 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003853 ret = nand_change_write_column_op(chip, pos,
3854 NULL, 0,
3855 false);
3856 if (ret)
3857 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003858 }
3859 } else
3860 sndcmd = 1;
3861 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003862
3863 ret = nand_write_data_op(chip, bufpoi, len, false);
3864 if (ret)
3865 return ret;
3866
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003867 bufpoi += len;
3868 length -= len;
3869 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003870 if (length > 0) {
3871 ret = nand_write_data_op(chip, bufpoi, length, false);
3872 if (ret)
3873 return ret;
3874 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003875
Boris Brezillon97d90da2017-11-30 18:01:29 +01003876 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003877}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003878EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003879
3880/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003881 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003882 * @mtd: MTD device structure
3883 * @from: offset to read from
3884 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003886 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003888static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3889 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003891 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003892 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003893 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003894 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003895 int readlen = ops->ooblen;
3896 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003897 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003898 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Brian Norris289c0522011-07-19 10:06:09 -07003900 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303901 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
Brian Norris041e4572011-06-23 16:45:24 -07003903 stats = mtd->ecc_stats;
3904
Boris BREZILLON29f10582016-03-07 10:46:52 +01003905 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003906
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003907 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003908 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003910 /* Shift to get page */
3911 realpage = (int)(from >> chip->page_shift);
3912 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Florian Fainellif8ac0412010-09-07 13:23:43 +02003914 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003915 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003916 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003917 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003918 ret = chip->ecc.read_oob(mtd, chip, page);
3919
3920 if (ret < 0)
3921 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003922
3923 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003924 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003925
Boris Brezillon85e08e52018-07-27 09:44:17 +02003926 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003927
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003928 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3929
Vitaly Wool70145682006-11-03 18:20:38 +03003930 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003931 if (!readlen)
3932 break;
3933
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003934 /* Increment page address */
3935 realpage++;
3936
3937 page = realpage & chip->pagemask;
3938 /* Check, if we cross a chip boundary */
3939 if (!page) {
3940 chipnr++;
3941 chip->select_chip(mtd, -1);
3942 chip->select_chip(mtd, chipnr);
3943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08003945 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003947 ops->oobretlen = ops->ooblen - readlen;
3948
3949 if (ret < 0)
3950 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003951
3952 if (mtd->ecc_stats.failed - stats.failed)
3953 return -EBADMSG;
3954
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003955 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956}
3957
3958/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003959 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003960 * @mtd: MTD device structure
3961 * @from: offset to read from
3962 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003964 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003966static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3967 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003969 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003970
3971 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003973 if (ops->mode != MTD_OPS_PLACE_OOB &&
3974 ops->mode != MTD_OPS_AUTO_OOB &&
3975 ops->mode != MTD_OPS_RAW)
3976 return -ENOTSUPP;
3977
Huang Shijie6a8214a2012-11-19 14:43:30 +08003978 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003980 if (!ops->datbuf)
3981 ret = nand_do_read_oob(mtd, from, ops);
3982 else
3983 ret = nand_do_read_ops(mtd, from, ops);
3984
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003986 return ret;
3987}
3988
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003989/**
3990 * nand_write_page_raw_notsupp - dummy raw page write function
3991 * @mtd: mtd info structure
3992 * @chip: nand chip info structure
3993 * @buf: data buffer
3994 * @oob_required: must write chip->oob_poi to OOB
3995 * @page: page number to write
3996 *
3997 * Returns -ENOTSUPP unconditionally.
3998 */
3999int nand_write_page_raw_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4000 const u8 *buf, int oob_required, int page)
4001{
4002 return -ENOTSUPP;
4003}
4004EXPORT_SYMBOL(nand_write_page_raw_notsupp);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004005
4006/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004007 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004008 * @mtd: mtd info structure
4009 * @chip: nand chip info structure
4010 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004011 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004012 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004013 *
Brian Norris7854d3f2011-06-23 14:12:08 -07004014 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004015 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02004016int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
4017 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004018{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004019 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004020
Boris Brezillon25f815f2017-11-30 18:01:30 +01004021 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004022 if (ret)
4023 return ret;
4024
4025 if (oob_required) {
4026 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
4027 false);
4028 if (ret)
4029 return ret;
4030 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004031
Boris Brezillon25f815f2017-11-30 18:01:30 +01004032 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02004034EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004036/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004037 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004038 * @mtd: mtd info structure
4039 * @chip: nand chip info structure
4040 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004041 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004042 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004043 *
4044 * We need a special oob layout and handling even when ECC isn't checked.
4045 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004046static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004047 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004048 const uint8_t *buf, int oob_required,
4049 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004050{
4051 int eccsize = chip->ecc.size;
4052 int eccbytes = chip->ecc.bytes;
4053 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004054 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004055
Boris Brezillon25f815f2017-11-30 18:01:30 +01004056 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4057 if (ret)
4058 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004059
4060 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004061 ret = nand_write_data_op(chip, buf, eccsize, false);
4062 if (ret)
4063 return ret;
4064
David Brownell52ff49d2009-03-04 12:01:36 -08004065 buf += eccsize;
4066
4067 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004068 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4069 false);
4070 if (ret)
4071 return ret;
4072
David Brownell52ff49d2009-03-04 12:01:36 -08004073 oob += chip->ecc.prepad;
4074 }
4075
Boris Brezillon97d90da2017-11-30 18:01:29 +01004076 ret = nand_write_data_op(chip, oob, eccbytes, false);
4077 if (ret)
4078 return ret;
4079
David Brownell52ff49d2009-03-04 12:01:36 -08004080 oob += eccbytes;
4081
4082 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004083 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4084 false);
4085 if (ret)
4086 return ret;
4087
David Brownell52ff49d2009-03-04 12:01:36 -08004088 oob += chip->ecc.postpad;
4089 }
4090 }
4091
4092 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004093 if (size) {
4094 ret = nand_write_data_op(chip, oob, size, false);
4095 if (ret)
4096 return ret;
4097 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004098
Boris Brezillon25f815f2017-11-30 18:01:30 +01004099 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004100}
4101/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004102 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004103 * @mtd: mtd info structure
4104 * @chip: nand chip info structure
4105 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004106 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004107 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004108 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004109static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004110 const uint8_t *buf, int oob_required,
4111 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004112{
Boris Brezillon846031d2016-02-03 20:11:00 +01004113 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004114 int eccbytes = chip->ecc.bytes;
4115 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004116 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004117 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004118
Brian Norris7854d3f2011-06-23 14:12:08 -07004119 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004120 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
4121 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004122
Boris Brezillon846031d2016-02-03 20:11:00 +01004123 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4124 chip->ecc.total);
4125 if (ret)
4126 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004127
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004128 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004129}
4130
4131/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004132 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004133 * @mtd: mtd info structure
4134 * @chip: nand chip info structure
4135 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004136 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004137 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004138 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004139static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004140 const uint8_t *buf, int oob_required,
4141 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004142{
Boris Brezillon846031d2016-02-03 20:11:00 +01004143 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004144 int eccbytes = chip->ecc.bytes;
4145 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004146 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004147 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004148
Boris Brezillon25f815f2017-11-30 18:01:30 +01004149 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4150 if (ret)
4151 return ret;
4152
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004153 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004154 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004155
4156 ret = nand_write_data_op(chip, p, eccsize, false);
4157 if (ret)
4158 return ret;
4159
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004160 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
4161 }
4162
Boris Brezillon846031d2016-02-03 20:11:00 +01004163 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4164 chip->ecc.total);
4165 if (ret)
4166 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004167
Boris Brezillon97d90da2017-11-30 18:01:29 +01004168 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4169 if (ret)
4170 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004171
Boris Brezillon25f815f2017-11-30 18:01:30 +01004172 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004173}
4174
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304175
4176/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004177 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304178 * @mtd: mtd info structure
4179 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004180 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304181 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004182 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304183 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004184 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304185 */
4186static int nand_write_subpage_hwecc(struct mtd_info *mtd,
4187 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07004188 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004189 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304190{
4191 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004192 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304193 int ecc_size = chip->ecc.size;
4194 int ecc_bytes = chip->ecc.bytes;
4195 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304196 uint32_t start_step = offset / ecc_size;
4197 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4198 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004199 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304200
Boris Brezillon25f815f2017-11-30 18:01:30 +01004201 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4202 if (ret)
4203 return ret;
4204
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304205 for (step = 0; step < ecc_steps; step++) {
4206 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02004207 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304208
4209 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004210 ret = nand_write_data_op(chip, buf, ecc_size, false);
4211 if (ret)
4212 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304213
4214 /* mask ECC of un-touched subpages by padding 0xFF */
4215 if ((step < start_step) || (step > end_step))
4216 memset(ecc_calc, 0xff, ecc_bytes);
4217 else
Brian Norrisd6a950802013-08-08 17:16:36 -07004218 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304219
4220 /* mask OOB of un-touched subpages by padding 0xFF */
4221 /* if oob_required, preserve OOB metadata of written subpage */
4222 if (!oob_required || (step < start_step) || (step > end_step))
4223 memset(oob_buf, 0xff, oob_bytes);
4224
Brian Norrisd6a950802013-08-08 17:16:36 -07004225 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304226 ecc_calc += ecc_bytes;
4227 oob_buf += oob_bytes;
4228 }
4229
4230 /* copy calculated ECC for whole page to chip->buffer->oob */
4231 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004232 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004233 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4234 chip->ecc.total);
4235 if (ret)
4236 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304237
4238 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004239 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4240 if (ret)
4241 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304242
Boris Brezillon25f815f2017-11-30 18:01:30 +01004243 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304244}
4245
4246
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004247/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004248 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004249 * @mtd: mtd info structure
4250 * @chip: nand chip info structure
4251 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004252 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004253 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004254 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004255 * The hw generator calculates the error syndrome automatically. Therefore we
4256 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004257 */
Josh Wufdbad98d2012-06-25 18:07:45 +08004258static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07004259 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004260 const uint8_t *buf, int oob_required,
4261 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004262{
4263 int i, eccsize = chip->ecc.size;
4264 int eccbytes = chip->ecc.bytes;
4265 int eccsteps = chip->ecc.steps;
4266 const uint8_t *p = buf;
4267 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004268 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004269
Boris Brezillon25f815f2017-11-30 18:01:30 +01004270 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4271 if (ret)
4272 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004273
4274 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004275 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004276
4277 ret = nand_write_data_op(chip, p, eccsize, false);
4278 if (ret)
4279 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004280
4281 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004282 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4283 false);
4284 if (ret)
4285 return ret;
4286
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004287 oob += chip->ecc.prepad;
4288 }
4289
4290 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004291
4292 ret = nand_write_data_op(chip, oob, eccbytes, false);
4293 if (ret)
4294 return ret;
4295
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004296 oob += eccbytes;
4297
4298 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004299 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4300 false);
4301 if (ret)
4302 return ret;
4303
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004304 oob += chip->ecc.postpad;
4305 }
4306 }
4307
4308 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004309 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004310 if (i) {
4311 ret = nand_write_data_op(chip, oob, i, false);
4312 if (ret)
4313 return ret;
4314 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004315
Boris Brezillon25f815f2017-11-30 18:01:30 +01004316 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004317}
4318
4319/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004320 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004321 * @mtd: MTD device structure
4322 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304323 * @offset: address offset within the page
4324 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004325 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004326 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004327 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004328 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004329 */
4330static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304331 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004332 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004333{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304334 int status, subpage;
4335
4336 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4337 chip->ecc.write_subpage)
4338 subpage = offset || (data_len < mtd->writesize);
4339 else
4340 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004341
David Woodhouse956e9442006-09-25 17:12:39 +01004342 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304343 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004344 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304345 else if (subpage)
4346 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004347 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004348 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004349 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
4350 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004351
4352 if (status < 0)
4353 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004354
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004355 return 0;
4356}
4357
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004358/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004359 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004360 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004361 * @oob: oob data buffer
4362 * @len: oob data write length
4363 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004364 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004365static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4366 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004367{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004368 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004369 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004370
4371 /*
4372 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4373 * data from a previous OOB read.
4374 */
4375 memset(chip->oob_poi, 0xff, mtd->oobsize);
4376
Florian Fainellif8ac0412010-09-07 13:23:43 +02004377 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004378
Brian Norris0612b9d2011-08-30 18:45:40 -07004379 case MTD_OPS_PLACE_OOB:
4380 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004381 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4382 return oob + len;
4383
Boris Brezillon846031d2016-02-03 20:11:00 +01004384 case MTD_OPS_AUTO_OOB:
4385 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4386 ops->ooboffs, len);
4387 BUG_ON(ret);
4388 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004389
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004390 default:
4391 BUG();
4392 }
4393 return NULL;
4394}
4395
Florian Fainellif8ac0412010-09-07 13:23:43 +02004396#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004397
4398/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004399 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004400 * @mtd: MTD device structure
4401 * @to: offset to write to
4402 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004403 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004404 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004405 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004406static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4407 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004408{
Corentin Labbe73600b62017-09-02 10:49:38 +02004409 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004410 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004411 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004412
4413 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004414 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004415
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004416 uint8_t *oob = ops->oobbuf;
4417 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304418 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004419 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004420
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004421 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004422 if (!writelen)
4423 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004424
Brian Norris8b6e50c2011-05-25 14:59:01 -07004425 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004426 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004427 pr_notice("%s: attempt to write non page aligned data\n",
4428 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004429 return -EINVAL;
4430 }
4431
Thomas Gleixner29072b92006-09-28 15:38:36 +02004432 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004433
Thomas Gleixner6a930962006-06-28 00:11:45 +02004434 chipnr = (int)(to >> chip->chip_shift);
4435 chip->select_chip(mtd, chipnr);
4436
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004437 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004438 if (nand_check_wp(mtd)) {
4439 ret = -EIO;
4440 goto err_out;
4441 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004442
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004443 realpage = (int)(to >> chip->page_shift);
4444 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004445
4446 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004447 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4448 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004449 chip->pagebuf = -1;
4450
Maxim Levitsky782ce792010-02-22 20:39:36 +02004451 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004452 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4453 ret = -EINVAL;
4454 goto err_out;
4455 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004456
Florian Fainellif8ac0412010-09-07 13:23:43 +02004457 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004458 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004459 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004460 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004461 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004462
Kamal Dasu66507c72014-05-01 20:51:19 -04004463 if (part_pagewr)
4464 use_bufpoi = 1;
4465 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004466 use_bufpoi = !virt_addr_valid(buf) ||
4467 !IS_ALIGNED((unsigned long)buf,
4468 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004469 else
4470 use_bufpoi = 0;
4471
4472 /* Partial page write?, or need to use bounce buffer */
4473 if (use_bufpoi) {
4474 pr_debug("%s: using write bounce buffer for buf@%p\n",
4475 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004476 if (part_pagewr)
4477 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004478 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004479 memset(chip->data_buf, 0xff, mtd->writesize);
4480 memcpy(&chip->data_buf[column], buf, bytes);
4481 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004482 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004483
Maxim Levitsky782ce792010-02-22 20:39:36 +02004484 if (unlikely(oob)) {
4485 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004486 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004487 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004488 } else {
4489 /* We still need to erase leftover OOB data */
4490 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004491 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004492
4493 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004494 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004495 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004496 if (ret)
4497 break;
4498
4499 writelen -= bytes;
4500 if (!writelen)
4501 break;
4502
Thomas Gleixner29072b92006-09-28 15:38:36 +02004503 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004504 buf += bytes;
4505 realpage++;
4506
4507 page = realpage & chip->pagemask;
4508 /* Check, if we cross a chip boundary */
4509 if (!page) {
4510 chipnr++;
4511 chip->select_chip(mtd, -1);
4512 chip->select_chip(mtd, chipnr);
4513 }
4514 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004515
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004516 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004517 if (unlikely(oob))
4518 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004519
4520err_out:
4521 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004522 return ret;
4523}
4524
4525/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004526 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004527 * @mtd: MTD device structure
4528 * @to: offset to write to
4529 * @len: number of bytes to write
4530 * @retlen: pointer to variable to store the number of written bytes
4531 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004532 *
4533 * NAND write with ECC. Used when performing writes in interrupt context, this
4534 * may for example be called by mtdoops when writing an oops while in panic.
4535 */
4536static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4537 size_t *retlen, const uint8_t *buf)
4538{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004539 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004540 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004541 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004542 int ret;
4543
Brian Norris8b6e50c2011-05-25 14:59:01 -07004544 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004545 panic_nand_get_device(chip, mtd, FL_WRITING);
4546
Brent Taylor30863e382017-10-30 22:32:45 -05004547 chip->select_chip(mtd, chipnr);
4548
4549 /* Wait for the device to get ready */
4550 panic_nand_wait(mtd, chip, 400);
4551
Brian Norris0ec56dc2015-02-28 02:02:30 -08004552 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004553 ops.len = len;
4554 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004555 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004556
Brian Norris4a89ff82011-08-30 18:45:45 -07004557 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004558
Brian Norris4a89ff82011-08-30 18:45:45 -07004559 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004560 return ret;
4561}
4562
4563/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004564 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004565 * @mtd: MTD device structure
4566 * @to: offset to write to
4567 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004568 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004569 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004570 */
4571static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4572 struct mtd_oob_ops *ops)
4573{
Adrian Hunter03736152007-01-31 17:58:29 +02004574 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004575 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Brian Norris289c0522011-07-19 10:06:09 -07004577 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304578 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Boris BREZILLON29f10582016-03-07 10:46:52 +01004580 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004583 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004584 pr_debug("%s: attempt to write past end of page\n",
4585 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 return -EINVAL;
4587 }
4588
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004589 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004590
4591 /*
4592 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4593 * of my DiskOnChip 2000 test units) will clear the whole data page too
4594 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4595 * it in the doc2000 driver in August 1999. dwmw2.
4596 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004597 nand_reset(chip, chipnr);
4598
4599 chip->select_chip(mtd, chipnr);
4600
4601 /* Shift to get page */
4602 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
4604 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004605 if (nand_check_wp(mtd)) {
4606 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004607 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004608 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004611 if (page == chip->pagebuf)
4612 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004614 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004615
Brian Norris0612b9d2011-08-30 18:45:40 -07004616 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07004617 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
4618 else
4619 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004620
Huang Shijieb0bb6902012-11-19 14:43:29 +08004621 chip->select_chip(mtd, -1);
4622
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004623 if (status)
4624 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
Vitaly Wool70145682006-11-03 18:20:38 +03004626 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004628 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004631/**
4632 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004633 * @mtd: MTD device structure
4634 * @to: offset to write to
4635 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004636 */
4637static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4638 struct mtd_oob_ops *ops)
4639{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004640 int ret = -ENOTSUPP;
4641
4642 ops->retlen = 0;
4643
Huang Shijie6a8214a2012-11-19 14:43:30 +08004644 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004645
Florian Fainellif8ac0412010-09-07 13:23:43 +02004646 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004647 case MTD_OPS_PLACE_OOB:
4648 case MTD_OPS_AUTO_OOB:
4649 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004650 break;
4651
4652 default:
4653 goto out;
4654 }
4655
4656 if (!ops->datbuf)
4657 ret = nand_do_write_oob(mtd, to, ops);
4658 else
4659 ret = nand_do_write_ops(mtd, to, ops);
4660
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004661out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004662 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 return ret;
4664}
4665
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666/**
Brian Norris49c50b92014-05-06 16:02:19 -07004667 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004668 * @mtd: MTD device structure
4669 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 *
Brian Norris49c50b92014-05-06 16:02:19 -07004671 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 */
Brian Norris49c50b92014-05-06 16:02:19 -07004673static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004675 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004676 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004677
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004679 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004680
Boris Brezillon97d90da2017-11-30 18:01:29 +01004681 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682}
4683
4684/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004686 * @mtd: MTD device structure
4687 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004689 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004691static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692{
David Woodhousee0c7d762006-05-13 18:07:53 +01004693 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004695
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004697 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004698 * @mtd: MTD device structure
4699 * @instr: erase instruction
4700 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004702 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004704int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4705 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706{
Adrian Hunter69423d92008-12-10 13:37:21 +00004707 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004708 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00004709 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Brian Norris289c0522011-07-19 10:06:09 -07004711 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4712 __func__, (unsigned long long)instr->addr,
4713 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304715 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004719 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
4721 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004722 page = (int)(instr->addr >> chip->page_shift);
4723 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
4725 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004726 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
4728 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004729 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 /* Check, if it is write protected */
4732 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004733 pr_debug("%s: device is write protected!\n",
4734 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004735 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 goto erase_exit;
4737 }
4738
4739 /* Loop through the pages */
4740 len = instr->len;
4741
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004743 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004744 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304745 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004746 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4747 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004748 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 goto erase_exit;
4750 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004751
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004752 /*
4753 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004754 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004755 */
4756 if (page <= chip->pagebuf && chip->pagebuf <
4757 (page + pages_per_block))
4758 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
Brian Norris49c50b92014-05-06 16:02:19 -07004760 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
4762 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004763 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004764 pr_debug("%s: failed erase, page 0x%08x\n",
4765 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004766 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004767 instr->fail_addr =
4768 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 goto erase_exit;
4770 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004771
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004773 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 page += pages_per_block;
4775
4776 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004777 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004779 chip->select_chip(mtd, -1);
4780 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
4782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004784 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004785erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004788 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 nand_release_device(mtd);
4790
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 /* Return more or less happy */
4792 return ret;
4793}
4794
4795/**
4796 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004797 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004799 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004801static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802{
Brian Norris289c0522011-07-19 10:06:09 -07004803 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
4805 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004806 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004808 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809}
4810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004812 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004813 * @mtd: MTD device structure
4814 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004816static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304818 struct nand_chip *chip = mtd_to_nand(mtd);
4819 int chipnr = (int)(offs >> chip->chip_shift);
4820 int ret;
4821
4822 /* Select the NAND device */
4823 nand_get_device(mtd, FL_READING);
4824 chip->select_chip(mtd, chipnr);
4825
4826 ret = nand_block_checkbad(mtd, offs, 0);
4827
4828 chip->select_chip(mtd, -1);
4829 nand_release_device(mtd);
4830
4831 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832}
4833
4834/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004835 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004836 * @mtd: MTD device structure
4837 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004839static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 int ret;
4842
Florian Fainellif8ac0412010-09-07 13:23:43 +02004843 ret = nand_block_isbad(mtd, ofs);
4844 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004845 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 if (ret > 0)
4847 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004848 return ret;
4849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Brian Norris5a0edb22013-07-30 17:52:58 -07004851 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
4854/**
Zach Brown56718422017-01-10 13:30:20 -06004855 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4856 * @mtd: MTD device structure
4857 * @ofs: offset relative to mtd start
4858 * @len: length of mtd
4859 */
4860static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4861{
4862 struct nand_chip *chip = mtd_to_nand(mtd);
4863 u32 part_start_block;
4864 u32 part_end_block;
4865 u32 part_start_die;
4866 u32 part_end_die;
4867
4868 /*
4869 * max_bb_per_die and blocks_per_die used to determine
4870 * the maximum bad block count.
4871 */
4872 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4873 return -ENOTSUPP;
4874
4875 /* Get the start and end of the partition in erase blocks. */
4876 part_start_block = mtd_div_by_eb(ofs, mtd);
4877 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4878
4879 /* Get the start and end LUNs of the partition. */
4880 part_start_die = part_start_block / chip->blocks_per_die;
4881 part_end_die = part_end_block / chip->blocks_per_die;
4882
4883 /*
4884 * Look up the bad blocks per unit and multiply by the number of units
4885 * that the partition spans.
4886 */
4887 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4888}
4889
4890/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004891 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004892 * @mtd: MTD device structure
4893 * @chip: nand chip info structure
4894 * @addr: feature address.
4895 * @subfeature_param: the subfeature parameters, a four bytes array.
4896 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004897static int nand_default_set_features(struct mtd_info *mtd,
4898 struct nand_chip *chip, int addr,
4899 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004900{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004901 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004902}
4903
4904/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004905 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004906 * @mtd: MTD device structure
4907 * @chip: nand chip info structure
4908 * @addr: feature address.
4909 * @subfeature_param: the subfeature parameters, a four bytes array.
4910 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004911static int nand_default_get_features(struct mtd_info *mtd,
4912 struct nand_chip *chip, int addr,
4913 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004914{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004915 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004916}
4917
4918/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004919 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004920 * @mtd: MTD device structure
4921 * @chip: nand chip info structure
4922 * @addr: feature address.
4923 * @subfeature_param: the subfeature parameters, a four bytes array.
4924 *
4925 * Should be used by NAND controller drivers that do not support the SET/GET
4926 * FEATURES operations.
4927 */
Miquel Raynalb9587582018-03-19 14:47:19 +01004928int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip,
4929 int addr, u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004930{
4931 return -ENOTSUPP;
4932}
Miquel Raynalb9587582018-03-19 14:47:19 +01004933EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004934
4935/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004936 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004937 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004938 */
4939static int nand_suspend(struct mtd_info *mtd)
4940{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004941 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004942}
4943
4944/**
4945 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004946 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004947 */
4948static void nand_resume(struct mtd_info *mtd)
4949{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004950 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004951
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004952 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004953 nand_release_device(mtd);
4954 else
Brian Norrisd0370212011-07-19 10:06:08 -07004955 pr_err("%s called for a chip which is not in suspended state\n",
4956 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004957}
4958
Scott Branden72ea4032014-11-20 11:18:05 -08004959/**
4960 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4961 * prevent further operations
4962 * @mtd: MTD device structure
4963 */
4964static void nand_shutdown(struct mtd_info *mtd)
4965{
Brian Norris9ca641b2015-11-09 16:37:28 -08004966 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004967}
4968
Brian Norris8b6e50c2011-05-25 14:59:01 -07004969/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004970static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004971{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004972 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4973
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004975 if (!chip->chip_delay)
4976 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
4978 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004979 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004980 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
4982 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004983 if (chip->waitfunc == NULL)
4984 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004986 if (!chip->select_chip)
4987 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004988
Huang Shijie4204ccc2013-08-16 10:10:07 +08004989 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004990 if (!chip->set_features)
4991 chip->set_features = nand_default_set_features;
4992 if (!chip->get_features)
4993 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004994
Brian Norris68e80782013-07-18 01:17:02 -07004995 /* If called twice, pointers that depend on busw may need to be reset */
4996 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004997 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004998 if (!chip->block_bad)
4999 chip->block_bad = nand_block_bad;
5000 if (!chip->block_markbad)
5001 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07005002 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005003 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01005004 if (!chip->write_byte || chip->write_byte == nand_write_byte)
5005 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07005006 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005007 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02005008
5009 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02005010 chip->controller = &chip->dummy_controller;
5011 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02005012 }
5013
Masahiro Yamada477544c2017-03-30 17:15:05 +09005014 if (!chip->buf_align)
5015 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005016}
5017
Brian Norris8b6e50c2011-05-25 14:59:01 -07005018/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005019static void sanitize_string(uint8_t *s, size_t len)
5020{
5021 ssize_t i;
5022
Brian Norris8b6e50c2011-05-25 14:59:01 -07005023 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005024 s[len - 1] = 0;
5025
Brian Norris8b6e50c2011-05-25 14:59:01 -07005026 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005027 for (i = 0; i < len - 1; i++) {
5028 if (s[i] < ' ' || s[i] > 127)
5029 s[i] = '?';
5030 }
5031
Brian Norris8b6e50c2011-05-25 14:59:01 -07005032 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005033 strim(s);
5034}
5035
5036static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
5037{
5038 int i;
5039 while (len--) {
5040 crc ^= *p++ << 8;
5041 for (i = 0; i < 8; i++)
5042 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
5043 }
5044
5045 return crc;
5046}
5047
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005048/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005049static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
5050 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005051{
5052 struct onfi_ext_param_page *ep;
5053 struct onfi_ext_section *s;
5054 struct onfi_ext_ecc_info *ecc;
5055 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005056 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005057 int len;
5058 int i;
5059
5060 len = le16_to_cpu(p->ext_param_page_length) * 16;
5061 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005062 if (!ep)
5063 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005064
5065 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005066 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5067 if (ret)
5068 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005069
5070 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005071 ret = nand_change_read_column_op(chip,
5072 sizeof(*p) * p->num_of_param_pages,
5073 ep, len, true);
5074 if (ret)
5075 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005076
Boris Brezillon97d90da2017-11-30 18:01:29 +01005077 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005078 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5079 != le16_to_cpu(ep->crc))) {
5080 pr_debug("fail in the CRC.\n");
5081 goto ext_out;
5082 }
5083
5084 /*
5085 * Check the signature.
5086 * Do not strictly follow the ONFI spec, maybe changed in future.
5087 */
5088 if (strncmp(ep->sig, "EPPS", 4)) {
5089 pr_debug("The signature is invalid.\n");
5090 goto ext_out;
5091 }
5092
5093 /* find the ECC section. */
5094 cursor = (uint8_t *)(ep + 1);
5095 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5096 s = ep->sections + i;
5097 if (s->type == ONFI_SECTION_TYPE_2)
5098 break;
5099 cursor += s->length * 16;
5100 }
5101 if (i == ONFI_EXT_SECTION_MAX) {
5102 pr_debug("We can not find the ECC section.\n");
5103 goto ext_out;
5104 }
5105
5106 /* get the info we want. */
5107 ecc = (struct onfi_ext_ecc_info *)cursor;
5108
Brian Norris4ae7d222013-09-16 18:20:21 -07005109 if (!ecc->codeword_size) {
5110 pr_debug("Invalid codeword size\n");
5111 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005112 }
5113
Brian Norris4ae7d222013-09-16 18:20:21 -07005114 chip->ecc_strength_ds = ecc->ecc_bits;
5115 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005116 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005117
5118ext_out:
5119 kfree(ep);
5120 return ret;
5121}
5122
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005123/*
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005124 * Recover data with bit-wise majority
5125 */
5126static void nand_bit_wise_majority(const void **srcbufs,
5127 unsigned int nsrcbufs,
5128 void *dstbuf,
5129 unsigned int bufsize)
5130{
5131 int i, j, k;
5132
5133 for (i = 0; i < bufsize; i++) {
5134 u8 val = 0;
5135
5136 for (j = 0; j < 8; j++) {
5137 unsigned int cnt = 0;
5138
5139 for (k = 0; k < nsrcbufs; k++) {
5140 const u8 *srcbuf = srcbufs[k];
5141
5142 if (srcbuf[i] & BIT(j))
5143 cnt++;
5144 }
5145
5146 if (cnt > nsrcbufs / 2)
5147 val |= BIT(j);
5148 }
5149
5150 ((u8 *)dstbuf)[i] = val;
5151 }
5152}
5153
5154/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005155 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005156 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005157static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005158{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005159 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005160 struct nand_onfi_params *p;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005161 struct onfi_params *onfi;
5162 int onfi_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005163 char id[4];
5164 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005165
Brian Norris7854d3f2011-06-23 14:12:08 -07005166 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005167 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5168 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005169 return 0;
5170
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005171 /* ONFI chip: allocate a buffer to hold its parameter page */
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005172 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005173 if (!p)
5174 return -ENOMEM;
5175
Boris Brezillon97d90da2017-11-30 18:01:29 +01005176 ret = nand_read_param_page_op(chip, 0, NULL, 0);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005177 if (ret) {
5178 ret = 0;
5179 goto free_onfi_param_page;
5180 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005181
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005182 for (i = 0; i < 3; i++) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005183 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005184 if (ret) {
5185 ret = 0;
5186 goto free_onfi_param_page;
5187 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005188
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005189 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005190 le16_to_cpu(p->crc)) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005191 if (i)
5192 memcpy(p, &p[i], sizeof(*p));
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005193 break;
5194 }
5195 }
5196
Brian Norrisc7f23a72013-08-13 10:51:55 -07005197 if (i == 3) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005198 const void *srcbufs[3] = {p, p + 1, p + 2};
5199
5200 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5201 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5202 sizeof(*p));
5203
5204 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5205 le16_to_cpu(p->crc)) {
5206 pr_err("ONFI parameter recovery failed, aborting\n");
5207 goto free_onfi_param_page;
5208 }
Brian Norrisc7f23a72013-08-13 10:51:55 -07005209 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005210
Chris Packham00ce4e02018-06-25 10:44:44 +12005211 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5212 chip->manufacturer.desc->ops->fixup_onfi_param_page)
5213 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5214
Brian Norris8b6e50c2011-05-25 14:59:01 -07005215 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005216 val = le16_to_cpu(p->revision);
Chris Packham872b71f2018-06-25 10:44:45 +12005217 if (val & ONFI_VERSION_2_3)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005218 onfi_version = 23;
Chris Packham872b71f2018-06-25 10:44:45 +12005219 else if (val & ONFI_VERSION_2_2)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005220 onfi_version = 22;
Chris Packham872b71f2018-06-25 10:44:45 +12005221 else if (val & ONFI_VERSION_2_1)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005222 onfi_version = 21;
Chris Packham872b71f2018-06-25 10:44:45 +12005223 else if (val & ONFI_VERSION_2_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005224 onfi_version = 20;
Chris Packham872b71f2018-06-25 10:44:45 +12005225 else if (val & ONFI_VERSION_1_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005226 onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005227
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005228 if (!onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005229 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005230 goto free_onfi_param_page;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005231 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005232
5233 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5234 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005235 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5236 if (!chip->parameters.model) {
5237 ret = -ENOMEM;
5238 goto free_onfi_param_page;
5239 }
Brian Norris4355b702013-08-27 18:45:10 -07005240
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005241 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005242
5243 /*
5244 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5245 * (don't ask me who thought of this...). MTD assumes that these
5246 * dimensions will be power-of-2, so just truncate the remaining area.
5247 */
5248 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5249 mtd->erasesize *= mtd->writesize;
5250
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005251 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005252
5253 /* See erasesize comment */
5254 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005255 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005256 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005257
Zach Brown34da5f52017-01-10 13:30:21 -06005258 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5259 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5260
Miquel Raynala97421c2018-03-19 14:47:27 +01005261 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005262 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005263
Huang Shijie10c86ba2013-05-17 11:17:26 +08005264 if (p->ecc_bits != 0xff) {
5265 chip->ecc_strength_ds = p->ecc_bits;
5266 chip->ecc_step_ds = 512;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005267 } else if (onfi_version >= 21 &&
Miquel Raynala97421c2018-03-19 14:47:27 +01005268 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005269
5270 /*
5271 * The nand_flash_detect_ext_param_page() uses the
5272 * Change Read Column command which maybe not supported
5273 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5274 * now. We do not replace user supplied command function.
5275 */
5276 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5277 chip->cmdfunc = nand_command_lp;
5278
5279 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005280 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005281 pr_warn("Failed to detect ONFI extended param page\n");
5282 } else {
5283 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005284 }
5285
Miquel Raynalf4531b22018-03-19 14:47:26 +01005286 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005287 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005288 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005289 bitmap_set(chip->parameters.get_feature_list,
5290 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5291 bitmap_set(chip->parameters.set_feature_list,
5292 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5293 }
Miquel Raynalf4531b22018-03-19 14:47:26 +01005294
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005295 onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
5296 if (!onfi) {
5297 ret = -ENOMEM;
5298 goto free_model;
5299 }
5300
5301 onfi->version = onfi_version;
5302 onfi->tPROG = le16_to_cpu(p->t_prog);
5303 onfi->tBERS = le16_to_cpu(p->t_bers);
5304 onfi->tR = le16_to_cpu(p->t_r);
5305 onfi->tCCS = le16_to_cpu(p->t_ccs);
5306 onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
5307 onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
5308 memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
5309 chip->parameters.onfi = onfi;
5310
5311 /* Identification done, free the full ONFI parameter page and exit */
5312 kfree(p);
5313
5314 return 1;
5315
5316free_model:
5317 kfree(chip->parameters.model);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005318free_onfi_param_page:
5319 kfree(p);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005320
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005321 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005322}
5323
5324/*
Huang Shijie91361812014-02-21 13:39:40 +08005325 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5326 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005327static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005328{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005329 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005330 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005331 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005332 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005333 char id[5];
5334 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005335
5336 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005337 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5338 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005339 return 0;
5340
Miquel Raynal480139d2018-03-19 14:47:30 +01005341 /* JEDEC chip: allocate a buffer to hold its parameter page */
5342 p = kzalloc(sizeof(*p), GFP_KERNEL);
5343 if (!p)
5344 return -ENOMEM;
5345
Boris Brezillon97d90da2017-11-30 18:01:29 +01005346 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005347 if (ret) {
5348 ret = 0;
5349 goto free_jedec_param_page;
5350 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005351
Huang Shijie91361812014-02-21 13:39:40 +08005352 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005353 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005354 if (ret) {
5355 ret = 0;
5356 goto free_jedec_param_page;
5357 }
Huang Shijie91361812014-02-21 13:39:40 +08005358
5359 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5360 le16_to_cpu(p->crc))
5361 break;
5362 }
5363
5364 if (i == 3) {
5365 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005366 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005367 }
5368
5369 /* Check version */
5370 val = le16_to_cpu(p->revision);
5371 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005372 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005373 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005374 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005375
Miquel Raynal480139d2018-03-19 14:47:30 +01005376 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005377 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005378 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005379 }
5380
5381 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5382 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005383 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5384 if (!chip->parameters.model) {
5385 ret = -ENOMEM;
5386 goto free_jedec_param_page;
5387 }
Huang Shijie91361812014-02-21 13:39:40 +08005388
5389 mtd->writesize = le32_to_cpu(p->byte_per_page);
5390
5391 /* Please reference to the comment for nand_flash_detect_onfi. */
5392 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5393 mtd->erasesize *= mtd->writesize;
5394
5395 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5396
5397 /* Please reference to the comment for nand_flash_detect_onfi. */
5398 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5399 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5400 chip->bits_per_cell = p->bits_per_cell;
5401
Miquel Raynal480139d2018-03-19 14:47:30 +01005402 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005403 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005404
5405 /* ECC info */
5406 ecc = &p->ecc_info[0];
5407
5408 if (ecc->codeword_size >= 9) {
5409 chip->ecc_strength_ds = ecc->ecc_bits;
5410 chip->ecc_step_ds = 1 << ecc->codeword_size;
5411 } else {
5412 pr_warn("Invalid codeword size\n");
5413 }
5414
Miquel Raynal480139d2018-03-19 14:47:30 +01005415free_jedec_param_page:
5416 kfree(p);
5417 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005418}
5419
5420/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005421 * nand_id_has_period - Check if an ID string has a given wraparound period
5422 * @id_data: the ID string
5423 * @arrlen: the length of the @id_data array
5424 * @period: the period of repitition
5425 *
5426 * Check if an ID string is repeated within a given sequence of bytes at
5427 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005428 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005429 * if the repetition has a period of @period; otherwise, returns zero.
5430 */
5431static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5432{
5433 int i, j;
5434 for (i = 0; i < period; i++)
5435 for (j = i + period; j < arrlen; j += period)
5436 if (id_data[i] != id_data[j])
5437 return 0;
5438 return 1;
5439}
5440
5441/*
5442 * nand_id_len - Get the length of an ID string returned by CMD_READID
5443 * @id_data: the ID string
5444 * @arrlen: the length of the @id_data array
5445
5446 * Returns the length of the ID string, according to known wraparound/trailing
5447 * zero patterns. If no pattern exists, returns the length of the array.
5448 */
5449static int nand_id_len(u8 *id_data, int arrlen)
5450{
5451 int last_nonzero, period;
5452
5453 /* Find last non-zero byte */
5454 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5455 if (id_data[last_nonzero])
5456 break;
5457
5458 /* All zeros */
5459 if (last_nonzero < 0)
5460 return 0;
5461
5462 /* Calculate wraparound period */
5463 for (period = 1; period < arrlen; period++)
5464 if (nand_id_has_period(id_data, arrlen, period))
5465 break;
5466
5467 /* There's a repeated pattern */
5468 if (period < arrlen)
5469 return period;
5470
5471 /* There are trailing zeros */
5472 if (last_nonzero < arrlen - 1)
5473 return last_nonzero + 1;
5474
5475 /* No pattern detected */
5476 return arrlen;
5477}
5478
Huang Shijie7db906b2013-09-25 14:58:11 +08005479/* Extract the bits of per cell from the 3rd byte of the extended ID */
5480static int nand_get_bits_per_cell(u8 cellinfo)
5481{
5482 int bits;
5483
5484 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5485 bits >>= NAND_CI_CELLTYPE_SHIFT;
5486 return bits + 1;
5487}
5488
Brian Norrise3b88bd2012-09-24 20:40:52 -07005489/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005490 * Many new NAND share similar device ID codes, which represent the size of the
5491 * chip. The rest of the parameters must be decoded according to generic or
5492 * manufacturer-specific "extended ID" decoding patterns.
5493 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005494void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005495{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005496 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005497 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005498 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005499 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005500 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005501 /* The 4th id byte is the important one */
5502 extid = id_data[3];
5503
Boris Brezillon01389b62016-06-08 10:30:18 +02005504 /* Calc pagesize */
5505 mtd->writesize = 1024 << (extid & 0x03);
5506 extid >>= 2;
5507 /* Calc oobsize */
5508 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5509 extid >>= 2;
5510 /* Calc blocksize. Blocksize is multiples of 64KiB */
5511 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5512 extid >>= 2;
5513 /* Get buswidth information */
5514 if (extid & 0x1)
5515 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005516}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005517EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005518
5519/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005520 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5521 * decodes a matching ID table entry and assigns the MTD size parameters for
5522 * the chip.
5523 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005524static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005525{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005526 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005527
5528 mtd->erasesize = type->erasesize;
5529 mtd->writesize = type->pagesize;
5530 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005531
Huang Shijie1c195e92013-09-25 14:58:12 +08005532 /* All legacy ID NAND are small-page, SLC */
5533 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005534}
5535
5536/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005537 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5538 * heuristic patterns using various detected parameters (e.g., manufacturer,
5539 * page size, cell-type information).
5540 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005541static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005542{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005543 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005544
5545 /* Set the bad block position */
5546 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5547 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5548 else
5549 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005550}
5551
Huang Shijieec6e87e2013-03-15 11:01:00 +08005552static inline bool is_full_id_nand(struct nand_flash_dev *type)
5553{
5554 return type->id_len;
5555}
5556
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005557static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005558 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005559{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005560 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005561 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005562
Huang Shijieec6e87e2013-03-15 11:01:00 +08005563 if (!strncmp(type->id, id_data, type->id_len)) {
5564 mtd->writesize = type->pagesize;
5565 mtd->erasesize = type->erasesize;
5566 mtd->oobsize = type->oobsize;
5567
Huang Shijie7db906b2013-09-25 14:58:11 +08005568 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005569 chip->chipsize = (uint64_t)type->chipsize << 20;
5570 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005571 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5572 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005573 chip->onfi_timing_mode_default =
5574 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005575
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005576 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5577 if (!chip->parameters.model)
5578 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005579
Huang Shijieec6e87e2013-03-15 11:01:00 +08005580 return true;
5581 }
5582 return false;
5583}
5584
Brian Norris7e74c2d2012-09-24 20:40:49 -07005585/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005586 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5587 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5588 * table.
5589 */
5590static void nand_manufacturer_detect(struct nand_chip *chip)
5591{
5592 /*
5593 * Try manufacturer detection if available and use
5594 * nand_decode_ext_id() otherwise.
5595 */
5596 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005597 chip->manufacturer.desc->ops->detect) {
5598 /* The 3rd id byte holds MLC / multichip data */
5599 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005600 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005601 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005602 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005603 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005604}
5605
5606/*
5607 * Manufacturer initialization. This function is called for all NANDs including
5608 * ONFI and JEDEC compliant ones.
5609 * Manufacturer drivers should put all their specific initialization code in
5610 * their ->init() hook.
5611 */
5612static int nand_manufacturer_init(struct nand_chip *chip)
5613{
5614 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5615 !chip->manufacturer.desc->ops->init)
5616 return 0;
5617
5618 return chip->manufacturer.desc->ops->init(chip);
5619}
5620
5621/*
5622 * Manufacturer cleanup. This function is called for all NANDs including
5623 * ONFI and JEDEC compliant ones.
5624 * Manufacturer drivers should put all their specific cleanup code in their
5625 * ->cleanup() hook.
5626 */
5627static void nand_manufacturer_cleanup(struct nand_chip *chip)
5628{
5629 /* Release manufacturer private data */
5630 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5631 chip->manufacturer.desc->ops->cleanup)
5632 chip->manufacturer.desc->ops->cleanup(chip);
5633}
5634
5635/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005636 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005637 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005638static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005639{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005640 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005641 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005642 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005643 u8 *id_data = chip->id.data;
5644 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
Karl Beldanef89a882008-09-15 14:37:29 +02005646 /*
5647 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005648 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005649 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005650 ret = nand_reset(chip, 0);
5651 if (ret)
5652 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005653
5654 /* Select the device */
5655 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005658 ret = nand_readid_op(chip, 0, id_data, 2);
5659 if (ret)
5660 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661
5662 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005663 maf_id = id_data[0];
5664 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
Brian Norris8b6e50c2011-05-25 14:59:01 -07005666 /*
5667 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005668 * interface concerns can cause random data which looks like a
5669 * possibly credible NAND flash to appear. If the two results do
5670 * not match, ignore the device completely.
5671 */
5672
Brian Norris4aef9b72012-09-24 20:40:48 -07005673 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005674 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5675 if (ret)
5676 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005677
Boris Brezillon7f501f02016-05-24 19:20:05 +02005678 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005679 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005680 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005681 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005682 }
5683
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005684 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005685
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005686 /* Try to identify manufacturer */
5687 manufacturer = nand_get_manufacturer(maf_id);
5688 chip->manufacturer.desc = manufacturer;
5689
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005690 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005691 type = nand_flash_ids;
5692
Boris Brezillon29a198a2016-05-24 20:17:48 +02005693 /*
5694 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5695 * override it.
5696 * This is required to make sure initial NAND bus width set by the
5697 * NAND controller driver is coherent with the real NAND bus width
5698 * (extracted by auto-detection code).
5699 */
5700 busw = chip->options & NAND_BUSWIDTH_16;
5701
5702 /*
5703 * The flag is only set (never cleared), reset it to its default value
5704 * before starting auto-detection.
5705 */
5706 chip->options &= ~NAND_BUSWIDTH_16;
5707
Huang Shijieec6e87e2013-03-15 11:01:00 +08005708 for (; type->name != NULL; type++) {
5709 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005710 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005711 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005712 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005713 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005714 }
5715 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005716
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005717 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005718 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005719 ret = nand_flash_detect_onfi(chip);
5720 if (ret < 0)
5721 return ret;
5722 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005723 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005724
5725 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005726 ret = nand_flash_detect_jedec(chip);
5727 if (ret < 0)
5728 return ret;
5729 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005730 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005731 }
5732
David Woodhouse5e81e882010-02-26 18:32:56 +00005733 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005734 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005735
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005736 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5737 if (!chip->parameters.model)
5738 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005739
Adrian Hunter69423d92008-12-10 13:37:21 +00005740 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005741
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005742 if (!type->pagesize)
5743 nand_manufacturer_detect(chip);
5744 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005745 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005746
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005747 /* Get chip options */
5748 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005749
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005750ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005751 if (!mtd->name)
5752 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005753
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005754 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005755 WARN_ON(busw & NAND_BUSWIDTH_16);
5756 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005757 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5758 /*
5759 * Check, if buswidth is correct. Hardware drivers should set
5760 * chip correct!
5761 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005762 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005763 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005764 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5765 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005766 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5767 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005768 ret = -EINVAL;
5769
5770 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005771 }
5772
Boris Brezillon7f501f02016-05-24 19:20:05 +02005773 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005774
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005775 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005776 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005777 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005778 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005779
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005780 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005781 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005782 if (chip->chipsize & 0xffffffff)
5783 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005784 else {
5785 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5786 chip->chip_shift += 32 - 1;
5787 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005788
Masahiro Yamada14157f82017-09-13 11:05:50 +09005789 if (chip->chip_shift - chip->page_shift > 16)
5790 chip->options |= NAND_ROW_ADDR_3;
5791
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005792 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005793 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005794
Brian Norris8b6e50c2011-05-25 14:59:01 -07005795 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005796 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5797 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005798
Ezequiel Garcia20171642013-11-25 08:30:31 -03005799 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005800 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005801 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5802 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005803 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005804 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005805 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005806 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005807
5808free_detect_allocation:
5809 kfree(chip->parameters.model);
5810
5811 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005812}
5813
Boris Brezillond48f62b2016-04-01 14:54:32 +02005814static const char * const nand_ecc_modes[] = {
5815 [NAND_ECC_NONE] = "none",
5816 [NAND_ECC_SOFT] = "soft",
5817 [NAND_ECC_HW] = "hw",
5818 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5819 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005820 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005821};
5822
5823static int of_get_nand_ecc_mode(struct device_node *np)
5824{
5825 const char *pm;
5826 int err, i;
5827
5828 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5829 if (err < 0)
5830 return err;
5831
5832 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5833 if (!strcasecmp(pm, nand_ecc_modes[i]))
5834 return i;
5835
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005836 /*
5837 * For backward compatibility we support few obsoleted values that don't
5838 * have their mappings into nand_ecc_modes_t anymore (they were merged
5839 * with other enums).
5840 */
5841 if (!strcasecmp(pm, "soft_bch"))
5842 return NAND_ECC_SOFT;
5843
Boris Brezillond48f62b2016-04-01 14:54:32 +02005844 return -ENODEV;
5845}
5846
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005847static const char * const nand_ecc_algos[] = {
5848 [NAND_ECC_HAMMING] = "hamming",
5849 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02005850 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005851};
5852
Boris Brezillond48f62b2016-04-01 14:54:32 +02005853static int of_get_nand_ecc_algo(struct device_node *np)
5854{
5855 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005856 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005857
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005858 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5859 if (!err) {
5860 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5861 if (!strcasecmp(pm, nand_ecc_algos[i]))
5862 return i;
5863 return -ENODEV;
5864 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005865
5866 /*
5867 * For backward compatibility we also read "nand-ecc-mode" checking
5868 * for some obsoleted values that were specifying ECC algorithm.
5869 */
5870 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5871 if (err < 0)
5872 return err;
5873
5874 if (!strcasecmp(pm, "soft"))
5875 return NAND_ECC_HAMMING;
5876 else if (!strcasecmp(pm, "soft_bch"))
5877 return NAND_ECC_BCH;
5878
5879 return -ENODEV;
5880}
5881
5882static int of_get_nand_ecc_step_size(struct device_node *np)
5883{
5884 int ret;
5885 u32 val;
5886
5887 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5888 return ret ? ret : val;
5889}
5890
5891static int of_get_nand_ecc_strength(struct device_node *np)
5892{
5893 int ret;
5894 u32 val;
5895
5896 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5897 return ret ? ret : val;
5898}
5899
5900static int of_get_nand_bus_width(struct device_node *np)
5901{
5902 u32 val;
5903
5904 if (of_property_read_u32(np, "nand-bus-width", &val))
5905 return 8;
5906
5907 switch (val) {
5908 case 8:
5909 case 16:
5910 return val;
5911 default:
5912 return -EIO;
5913 }
5914}
5915
5916static bool of_get_nand_on_flash_bbt(struct device_node *np)
5917{
5918 return of_property_read_bool(np, "nand-on-flash-bbt");
5919}
5920
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005921static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005922{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005923 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005924 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005925
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005926 if (!dn)
5927 return 0;
5928
Brian Norris5844fee2015-01-23 00:22:27 -08005929 if (of_get_nand_bus_width(dn) == 16)
5930 chip->options |= NAND_BUSWIDTH_16;
5931
Stefan Agnerf922bd72018-06-24 23:27:23 +02005932 if (of_property_read_bool(dn, "nand-is-boot-medium"))
5933 chip->options |= NAND_IS_BOOT_MEDIUM;
5934
Brian Norris5844fee2015-01-23 00:22:27 -08005935 if (of_get_nand_on_flash_bbt(dn))
5936 chip->bbt_options |= NAND_BBT_USE_FLASH;
5937
5938 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005939 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005940 ecc_strength = of_get_nand_ecc_strength(dn);
5941 ecc_step = of_get_nand_ecc_step_size(dn);
5942
Brian Norris5844fee2015-01-23 00:22:27 -08005943 if (ecc_mode >= 0)
5944 chip->ecc.mode = ecc_mode;
5945
Rafał Miłecki79082452016-03-23 11:19:02 +01005946 if (ecc_algo >= 0)
5947 chip->ecc.algo = ecc_algo;
5948
Brian Norris5844fee2015-01-23 00:22:27 -08005949 if (ecc_strength >= 0)
5950 chip->ecc.strength = ecc_strength;
5951
5952 if (ecc_step > 0)
5953 chip->ecc.size = ecc_step;
5954
Boris Brezillonba78ee02016-06-08 17:04:22 +02005955 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5956 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5957
Brian Norris5844fee2015-01-23 00:22:27 -08005958 return 0;
5959}
5960
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005961/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005962 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005963 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005964 * @maxchips: number of chips to scan for
5965 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005966 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005967 * This is the first phase of the normal nand_scan() function. It reads the
5968 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005969 *
Miquel Raynal98732da2018-07-25 15:31:50 +02005970 * This helper used to be called directly from controller drivers that needed
5971 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5972 * prevented dynamic allocations during this phase which was unconvenient and
5973 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005974 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005975static int nand_scan_ident(struct nand_chip *chip, int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005976 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005977{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005978 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08005979 int i, nand_maf_id, nand_dev_id;
Brian Norris5844fee2015-01-23 00:22:27 -08005980 int ret;
5981
Miquel Raynal17fa8042017-11-30 18:01:31 +01005982 /* Enforce the right timings for reset/detection */
5983 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5984
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005985 ret = nand_dt_init(chip);
5986 if (ret)
5987 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005988
Brian Norrisf7a8e382016-01-05 10:39:45 -08005989 if (!mtd->name && mtd->dev.parent)
5990 mtd->name = dev_name(mtd->dev.parent);
5991
Miquel Raynal8878b122017-11-09 14:16:45 +01005992 /*
5993 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5994 * populated.
5995 */
5996 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005997 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005998 * Default functions assigned for ->cmdfunc() and
5999 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07006000 */
Miquel Raynal8878b122017-11-09 14:16:45 +01006001 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
6002 pr_err("->cmd_ctrl() should be provided\n");
6003 return -EINVAL;
6004 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07006005 }
Miquel Raynal8878b122017-11-09 14:16:45 +01006006
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006007 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02006008 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006009
6010 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02006011 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09006012 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00006013 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07006014 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006015 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09006016 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 }
6018
Boris Brezillon7f501f02016-05-24 19:20:05 +02006019 nand_maf_id = chip->id.data[0];
6020 nand_dev_id = chip->id.data[1];
6021
Huang Shijie07300162012-11-09 16:23:45 +08006022 chip->select_chip(mtd, -1);
6023
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006024 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01006025 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01006026 u8 id[2];
6027
Karl Beldanef89a882008-09-15 14:37:29 +02006028 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02006029 nand_reset(chip, i);
6030
6031 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01006033 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01006035 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Huang Shijie07300162012-11-09 16:23:45 +08006036 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 break;
Huang Shijie07300162012-11-09 16:23:45 +08006038 }
6039 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 }
6041 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03006042 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006043
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006045 chip->numchips = i;
6046 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
David Woodhouse3b85c322006-09-25 17:06:53 +01006048 return 0;
6049}
6050
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006051static void nand_scan_ident_cleanup(struct nand_chip *chip)
6052{
6053 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02006054 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006055}
6056
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006057static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
6058{
6059 struct nand_chip *chip = mtd_to_nand(mtd);
6060 struct nand_ecc_ctrl *ecc = &chip->ecc;
6061
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006062 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006063 return -EINVAL;
6064
6065 switch (ecc->algo) {
6066 case NAND_ECC_HAMMING:
6067 ecc->calculate = nand_calculate_ecc;
6068 ecc->correct = nand_correct_data;
6069 ecc->read_page = nand_read_page_swecc;
6070 ecc->read_subpage = nand_read_subpage;
6071 ecc->write_page = nand_write_page_swecc;
6072 ecc->read_page_raw = nand_read_page_raw;
6073 ecc->write_page_raw = nand_write_page_raw;
6074 ecc->read_oob = nand_read_oob_std;
6075 ecc->write_oob = nand_write_oob_std;
6076 if (!ecc->size)
6077 ecc->size = 256;
6078 ecc->bytes = 3;
6079 ecc->strength = 1;
6080 return 0;
6081 case NAND_ECC_BCH:
6082 if (!mtd_nand_has_bch()) {
6083 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
6084 return -EINVAL;
6085 }
6086 ecc->calculate = nand_bch_calculate_ecc;
6087 ecc->correct = nand_bch_correct_data;
6088 ecc->read_page = nand_read_page_swecc;
6089 ecc->read_subpage = nand_read_subpage;
6090 ecc->write_page = nand_write_page_swecc;
6091 ecc->read_page_raw = nand_read_page_raw;
6092 ecc->write_page_raw = nand_write_page_raw;
6093 ecc->read_oob = nand_read_oob_std;
6094 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02006095
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006096 /*
6097 * Board driver should supply ecc.size and ecc.strength
6098 * values to select how many bits are correctable.
6099 * Otherwise, default to 4 bits for large page devices.
6100 */
6101 if (!ecc->size && (mtd->oobsize >= 64)) {
6102 ecc->size = 512;
6103 ecc->strength = 4;
6104 }
6105
6106 /*
6107 * if no ecc placement scheme was provided pickup the default
6108 * large page one.
6109 */
6110 if (!mtd->ooblayout) {
6111 /* handle large page devices only */
6112 if (mtd->oobsize < 64) {
6113 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6114 return -EINVAL;
6115 }
6116
6117 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02006118
6119 }
6120
6121 /*
6122 * We can only maximize ECC config when the default layout is
6123 * used, otherwise we don't know how many bytes can really be
6124 * used.
6125 */
6126 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6127 ecc->options & NAND_ECC_MAXIMIZE) {
6128 int steps, bytes;
6129
6130 /* Always prefer 1k blocks over 512bytes ones */
6131 ecc->size = 1024;
6132 steps = mtd->writesize / ecc->size;
6133
6134 /* Reserve 2 bytes for the BBM */
6135 bytes = (mtd->oobsize - 2) / steps;
6136 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006137 }
6138
6139 /* See nand_bch_init() for details. */
6140 ecc->bytes = 0;
6141 ecc->priv = nand_bch_init(mtd);
6142 if (!ecc->priv) {
6143 WARN(1, "BCH ECC initialization failed!\n");
6144 return -EINVAL;
6145 }
6146 return 0;
6147 default:
6148 WARN(1, "Unsupported ECC algorithm!\n");
6149 return -EINVAL;
6150 }
6151}
6152
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006153/**
6154 * nand_check_ecc_caps - check the sanity of preset ECC settings
6155 * @chip: nand chip info structure
6156 * @caps: ECC caps info structure
6157 * @oobavail: OOB size that the ECC engine can use
6158 *
6159 * When ECC step size and strength are already set, check if they are supported
6160 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6161 * On success, the calculated ECC bytes is set.
6162 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306163static int
6164nand_check_ecc_caps(struct nand_chip *chip,
6165 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006166{
6167 struct mtd_info *mtd = nand_to_mtd(chip);
6168 const struct nand_ecc_step_info *stepinfo;
6169 int preset_step = chip->ecc.size;
6170 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306171 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006172 int i, j;
6173
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006174 for (i = 0; i < caps->nstepinfos; i++) {
6175 stepinfo = &caps->stepinfos[i];
6176
6177 if (stepinfo->stepsize != preset_step)
6178 continue;
6179
6180 for (j = 0; j < stepinfo->nstrengths; j++) {
6181 if (stepinfo->strengths[j] != preset_strength)
6182 continue;
6183
6184 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6185 preset_strength);
6186 if (WARN_ON_ONCE(ecc_bytes < 0))
6187 return ecc_bytes;
6188
6189 if (ecc_bytes * nsteps > oobavail) {
6190 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6191 preset_step, preset_strength);
6192 return -ENOSPC;
6193 }
6194
6195 chip->ecc.bytes = ecc_bytes;
6196
6197 return 0;
6198 }
6199 }
6200
6201 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6202 preset_step, preset_strength);
6203
6204 return -ENOTSUPP;
6205}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006206
6207/**
6208 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6209 * @chip: nand chip info structure
6210 * @caps: ECC engine caps info structure
6211 * @oobavail: OOB size that the ECC engine can use
6212 *
6213 * If a chip's ECC requirement is provided, try to meet it with the least
6214 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6215 * On success, the chosen ECC settings are set.
6216 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306217static int
6218nand_match_ecc_req(struct nand_chip *chip,
6219 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006220{
6221 struct mtd_info *mtd = nand_to_mtd(chip);
6222 const struct nand_ecc_step_info *stepinfo;
6223 int req_step = chip->ecc_step_ds;
6224 int req_strength = chip->ecc_strength_ds;
6225 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6226 int best_step, best_strength, best_ecc_bytes;
6227 int best_ecc_bytes_total = INT_MAX;
6228 int i, j;
6229
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006230 /* No information provided by the NAND chip */
6231 if (!req_step || !req_strength)
6232 return -ENOTSUPP;
6233
6234 /* number of correctable bits the chip requires in a page */
6235 req_corr = mtd->writesize / req_step * req_strength;
6236
6237 for (i = 0; i < caps->nstepinfos; i++) {
6238 stepinfo = &caps->stepinfos[i];
6239 step_size = stepinfo->stepsize;
6240
6241 for (j = 0; j < stepinfo->nstrengths; j++) {
6242 strength = stepinfo->strengths[j];
6243
6244 /*
6245 * If both step size and strength are smaller than the
6246 * chip's requirement, it is not easy to compare the
6247 * resulted reliability.
6248 */
6249 if (step_size < req_step && strength < req_strength)
6250 continue;
6251
6252 if (mtd->writesize % step_size)
6253 continue;
6254
6255 nsteps = mtd->writesize / step_size;
6256
6257 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6258 if (WARN_ON_ONCE(ecc_bytes < 0))
6259 continue;
6260 ecc_bytes_total = ecc_bytes * nsteps;
6261
6262 if (ecc_bytes_total > oobavail ||
6263 strength * nsteps < req_corr)
6264 continue;
6265
6266 /*
6267 * We assume the best is to meet the chip's requrement
6268 * with the least number of ECC bytes.
6269 */
6270 if (ecc_bytes_total < best_ecc_bytes_total) {
6271 best_ecc_bytes_total = ecc_bytes_total;
6272 best_step = step_size;
6273 best_strength = strength;
6274 best_ecc_bytes = ecc_bytes;
6275 }
6276 }
6277 }
6278
6279 if (best_ecc_bytes_total == INT_MAX)
6280 return -ENOTSUPP;
6281
6282 chip->ecc.size = best_step;
6283 chip->ecc.strength = best_strength;
6284 chip->ecc.bytes = best_ecc_bytes;
6285
6286 return 0;
6287}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006288
6289/**
6290 * nand_maximize_ecc - choose the max ECC strength available
6291 * @chip: nand chip info structure
6292 * @caps: ECC engine caps info structure
6293 * @oobavail: OOB size that the ECC engine can use
6294 *
6295 * Choose the max ECC strength that is supported on the controller, and can fit
6296 * within the chip's OOB. On success, the chosen ECC settings are set.
6297 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306298static int
6299nand_maximize_ecc(struct nand_chip *chip,
6300 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006301{
6302 struct mtd_info *mtd = nand_to_mtd(chip);
6303 const struct nand_ecc_step_info *stepinfo;
6304 int step_size, strength, nsteps, ecc_bytes, corr;
6305 int best_corr = 0;
6306 int best_step = 0;
6307 int best_strength, best_ecc_bytes;
6308 int i, j;
6309
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006310 for (i = 0; i < caps->nstepinfos; i++) {
6311 stepinfo = &caps->stepinfos[i];
6312 step_size = stepinfo->stepsize;
6313
6314 /* If chip->ecc.size is already set, respect it */
6315 if (chip->ecc.size && step_size != chip->ecc.size)
6316 continue;
6317
6318 for (j = 0; j < stepinfo->nstrengths; j++) {
6319 strength = stepinfo->strengths[j];
6320
6321 if (mtd->writesize % step_size)
6322 continue;
6323
6324 nsteps = mtd->writesize / step_size;
6325
6326 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6327 if (WARN_ON_ONCE(ecc_bytes < 0))
6328 continue;
6329
6330 if (ecc_bytes * nsteps > oobavail)
6331 continue;
6332
6333 corr = strength * nsteps;
6334
6335 /*
6336 * If the number of correctable bits is the same,
6337 * bigger step_size has more reliability.
6338 */
6339 if (corr > best_corr ||
6340 (corr == best_corr && step_size > best_step)) {
6341 best_corr = corr;
6342 best_step = step_size;
6343 best_strength = strength;
6344 best_ecc_bytes = ecc_bytes;
6345 }
6346 }
6347 }
6348
6349 if (!best_corr)
6350 return -ENOTSUPP;
6351
6352 chip->ecc.size = best_step;
6353 chip->ecc.strength = best_strength;
6354 chip->ecc.bytes = best_ecc_bytes;
6355
6356 return 0;
6357}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006358
Abhishek Sahu181ace92018-06-20 12:57:28 +05306359/**
6360 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
6361 * @chip: nand chip info structure
6362 * @caps: ECC engine caps info structure
6363 * @oobavail: OOB size that the ECC engine can use
6364 *
6365 * Choose the ECC configuration according to following logic
6366 *
6367 * 1. If both ECC step size and ECC strength are already set (usually by DT)
6368 * then check if it is supported by this controller.
6369 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
6370 * 3. Otherwise, try to match the ECC step size and ECC strength closest
6371 * to the chip's requirement. If available OOB size can't fit the chip
6372 * requirement then fallback to the maximum ECC step size and ECC strength.
6373 *
6374 * On success, the chosen ECC settings are set.
6375 */
6376int nand_ecc_choose_conf(struct nand_chip *chip,
6377 const struct nand_ecc_caps *caps, int oobavail)
6378{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306379 struct mtd_info *mtd = nand_to_mtd(chip);
6380
6381 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
6382 return -EINVAL;
6383
Abhishek Sahu181ace92018-06-20 12:57:28 +05306384 if (chip->ecc.size && chip->ecc.strength)
6385 return nand_check_ecc_caps(chip, caps, oobavail);
6386
6387 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
6388 return nand_maximize_ecc(chip, caps, oobavail);
6389
6390 if (!nand_match_ecc_req(chip, caps, oobavail))
6391 return 0;
6392
6393 return nand_maximize_ecc(chip, caps, oobavail);
6394}
6395EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
6396
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006397/*
6398 * Check if the chip configuration meet the datasheet requirements.
6399
6400 * If our configuration corrects A bits per B bytes and the minimum
6401 * required correction level is X bits per Y bytes, then we must ensure
6402 * both of the following are true:
6403 *
6404 * (1) A / B >= X / Y
6405 * (2) A >= X
6406 *
6407 * Requirement (1) ensures we can correct for the required bitflip density.
6408 * Requirement (2) ensures we can correct even when all bitflips are clumped
6409 * in the same sector.
6410 */
6411static bool nand_ecc_strength_good(struct mtd_info *mtd)
6412{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006413 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006414 struct nand_ecc_ctrl *ecc = &chip->ecc;
6415 int corr, ds_corr;
6416
6417 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6418 /* Not enough information */
6419 return true;
6420
6421 /*
6422 * We get the number of corrected bits per page to compare
6423 * the correction density.
6424 */
6425 corr = (mtd->writesize * ecc->strength) / ecc->size;
6426 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6427
6428 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6429}
David Woodhouse3b85c322006-09-25 17:06:53 +01006430
6431/**
Miquel Raynal98732da2018-07-25 15:31:50 +02006432 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006433 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01006434 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006435 * This is the second phase of the normal nand_scan() function. It fills out
6436 * all the uninitialized function pointers with the defaults and scans for a
6437 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006438 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006439static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01006440{
Boris Brezillon00ad3782018-09-06 14:05:14 +02006441 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08006442 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006443 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006444
Brian Norrise2414f42012-02-06 13:44:00 -08006445 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006446 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006447 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006448 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006449 }
Brian Norrise2414f42012-02-06 13:44:00 -08006450
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006451 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006452 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006453 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006454
Boris Brezillonf84674b2017-06-02 12:18:24 +02006455 /*
6456 * FIXME: some NAND manufacturer drivers expect the first die to be
6457 * selected when manufacturer->init() is called. They should be fixed
6458 * to explictly select the relevant die when interacting with the NAND
6459 * chip.
6460 */
6461 chip->select_chip(mtd, 0);
6462 ret = nand_manufacturer_init(chip);
6463 chip->select_chip(mtd, -1);
6464 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006465 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006466
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006467 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006468 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006469
6470 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006471 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006472 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006473 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006474 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006475 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006478 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 break;
6480 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006481 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006482 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006483 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006485 /*
6486 * Expose the whole OOB area to users if ECC_NONE
6487 * is passed. We could do that for all kind of
6488 * ->oobsize, but we must keep the old large/small
6489 * page with ECC layout when ->oobsize <= 128 for
6490 * compatibility reasons.
6491 */
6492 if (ecc->mode == NAND_ECC_NONE) {
6493 mtd_set_ooblayout(mtd,
6494 &nand_ooblayout_lp_ops);
6495 break;
6496 }
6497
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006498 WARN(1, "No oob scheme defined for oobsize %d\n",
6499 mtd->oobsize);
6500 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006501 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 }
6503 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006504
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006505 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006506 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006507 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006508 */
David Woodhouse956e9442006-09-25 17:12:39 +01006509
Huang Shijie97de79e02013-10-18 14:20:53 +08006510 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006511 case NAND_ECC_HW_OOB_FIRST:
6512 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006513 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006514 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6515 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006516 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006517 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006518 if (!ecc->read_page)
6519 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006520
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006521 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006522 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006523 if (!ecc->read_page)
6524 ecc->read_page = nand_read_page_hwecc;
6525 if (!ecc->write_page)
6526 ecc->write_page = nand_write_page_hwecc;
6527 if (!ecc->read_page_raw)
6528 ecc->read_page_raw = nand_read_page_raw;
6529 if (!ecc->write_page_raw)
6530 ecc->write_page_raw = nand_write_page_raw;
6531 if (!ecc->read_oob)
6532 ecc->read_oob = nand_read_oob_std;
6533 if (!ecc->write_oob)
6534 ecc->write_oob = nand_write_oob_std;
6535 if (!ecc->read_subpage)
6536 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006537 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006538 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006539
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006540 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006541 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6542 (!ecc->read_page ||
6543 ecc->read_page == nand_read_page_hwecc ||
6544 !ecc->write_page ||
6545 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006546 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6547 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006548 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006549 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006550 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006551 if (!ecc->read_page)
6552 ecc->read_page = nand_read_page_syndrome;
6553 if (!ecc->write_page)
6554 ecc->write_page = nand_write_page_syndrome;
6555 if (!ecc->read_page_raw)
6556 ecc->read_page_raw = nand_read_page_raw_syndrome;
6557 if (!ecc->write_page_raw)
6558 ecc->write_page_raw = nand_write_page_raw_syndrome;
6559 if (!ecc->read_oob)
6560 ecc->read_oob = nand_read_oob_syndrome;
6561 if (!ecc->write_oob)
6562 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006563
Huang Shijie97de79e02013-10-18 14:20:53 +08006564 if (mtd->writesize >= ecc->size) {
6565 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006566 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6567 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006568 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006569 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006570 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006571 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006572 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6573 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006574 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006575 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006577 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006578 ret = nand_set_ecc_soft_ops(mtd);
6579 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006580 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006581 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006582 }
6583 break;
6584
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006585 case NAND_ECC_ON_DIE:
6586 if (!ecc->read_page || !ecc->write_page) {
6587 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6588 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006589 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006590 }
6591 if (!ecc->read_oob)
6592 ecc->read_oob = nand_read_oob_std;
6593 if (!ecc->write_oob)
6594 ecc->write_oob = nand_write_oob_std;
6595 break;
6596
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006597 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006598 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006599 ecc->read_page = nand_read_page_raw;
6600 ecc->write_page = nand_write_page_raw;
6601 ecc->read_oob = nand_read_oob_std;
6602 ecc->read_page_raw = nand_read_page_raw;
6603 ecc->write_page_raw = nand_write_page_raw;
6604 ecc->write_oob = nand_write_oob_std;
6605 ecc->size = mtd->writesize;
6606 ecc->bytes = 0;
6607 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006609
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006611 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6612 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006613 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006616 if (ecc->correct || ecc->calculate) {
6617 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6618 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6619 if (!ecc->calc_buf || !ecc->code_buf) {
6620 ret = -ENOMEM;
6621 goto err_nand_manuf_cleanup;
6622 }
6623 }
6624
Brian Norris9ce244b2011-08-30 18:45:37 -07006625 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006626 if (!ecc->read_oob_raw)
6627 ecc->read_oob_raw = ecc->read_oob;
6628 if (!ecc->write_oob_raw)
6629 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006630
Boris Brezillon846031d2016-02-03 20:11:00 +01006631 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006632 mtd->ecc_strength = ecc->strength;
6633 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006634
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006635 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006636 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006637 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006638 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006639 ecc->steps = mtd->writesize / ecc->size;
6640 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006641 WARN(1, "Invalid ECC parameters\n");
6642 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006643 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006645 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006646 if (ecc->total > mtd->oobsize) {
6647 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6648 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006649 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006650 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006651
Boris Brezillon846031d2016-02-03 20:11:00 +01006652 /*
6653 * The number of bytes available for a client to place data into
6654 * the out of band area.
6655 */
6656 ret = mtd_ooblayout_count_freebytes(mtd);
6657 if (ret < 0)
6658 ret = 0;
6659
6660 mtd->oobavail = ret;
6661
6662 /* ECC sanity check: warn if it's too weak */
6663 if (!nand_ecc_strength_good(mtd))
6664 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6665 mtd->name);
6666
Brian Norris8b6e50c2011-05-25 14:59:01 -07006667 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006668 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006669 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006670 case 2:
6671 mtd->subpage_sft = 1;
6672 break;
6673 case 4:
6674 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006675 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006676 mtd->subpage_sft = 2;
6677 break;
6678 }
6679 }
6680 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6681
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006682 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006683 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006686 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006688 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306689 switch (ecc->mode) {
6690 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306691 if (chip->page_shift > 9)
6692 chip->options |= NAND_SUBPAGE_READ;
6693 break;
6694
6695 default:
6696 break;
6697 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006698
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006700 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006701 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6702 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006703 mtd->_erase = nand_erase;
6704 mtd->_point = NULL;
6705 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006706 mtd->_panic_write = panic_nand_write;
6707 mtd->_read_oob = nand_read_oob;
6708 mtd->_write_oob = nand_write_oob;
6709 mtd->_sync = nand_sync;
6710 mtd->_lock = NULL;
6711 mtd->_unlock = NULL;
6712 mtd->_suspend = nand_suspend;
6713 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006714 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006715 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006716 mtd->_block_isbad = nand_block_isbad;
6717 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006718 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006719 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006721 /*
6722 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6723 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6724 * properly set.
6725 */
6726 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006727 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728
Boris Brezillonf84674b2017-06-02 12:18:24 +02006729 /* Initialize the ->data_interface field. */
6730 ret = nand_init_data_interface(chip);
6731 if (ret)
6732 goto err_nand_manuf_cleanup;
6733
6734 /* Enter fastest possible mode on all dies. */
6735 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006736 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006737 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006738 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006739 }
6740
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006741 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006742 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744
6745 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02006746 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07006747 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006748 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006749
Brian Norris44d41822017-05-01 17:04:50 -07006750 return 0;
6751
Boris Brezillonf84674b2017-06-02 12:18:24 +02006752
6753err_nand_manuf_cleanup:
6754 nand_manufacturer_cleanup(chip);
6755
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006756err_free_buf:
6757 kfree(chip->data_buf);
6758 kfree(ecc->code_buf);
6759 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006760
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006761 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762}
6763
Miquel Raynal05b54c72018-07-19 01:05:46 +02006764static int nand_attach(struct nand_chip *chip)
6765{
6766 if (chip->controller->ops && chip->controller->ops->attach_chip)
6767 return chip->controller->ops->attach_chip(chip);
6768
6769 return 0;
6770}
6771
6772static void nand_detach(struct nand_chip *chip)
6773{
6774 if (chip->controller->ops && chip->controller->ops->detach_chip)
6775 chip->controller->ops->detach_chip(chip);
6776}
6777
David Woodhouse3b85c322006-09-25 17:06:53 +01006778/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006779 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006780 * @chip: NAND chip object
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006781 * @maxchips: number of chips to scan for. @nand_scan_ident() will not be run if
6782 * this parameter is zero (useful for specific drivers that must
6783 * handle this part of the process themselves, e.g docg4).
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006784 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01006785 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006786 * This fills out all the uninitialized function pointers with the defaults.
6787 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006788 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006789 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006790int nand_scan_with_ids(struct nand_chip *chip, int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006791 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01006792{
6793 int ret;
6794
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006795 if (maxchips) {
Boris Brezillon00ad3782018-09-06 14:05:14 +02006796 ret = nand_scan_ident(chip, maxchips, ids);
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006797 if (ret)
6798 return ret;
6799 }
Miquel Raynal05b54c72018-07-19 01:05:46 +02006800
6801 ret = nand_attach(chip);
6802 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006803 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006804
Boris Brezillon00ad3782018-09-06 14:05:14 +02006805 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006806 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006807 goto detach_chip;
6808
6809 return 0;
6810
6811detach_chip:
6812 nand_detach(chip);
6813cleanup_ident:
6814 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006815
David Woodhouse3b85c322006-09-25 17:06:53 +01006816 return ret;
6817}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006818EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006819
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006821 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6822 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006823 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006824void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006826 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006827 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006828 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6829
Jesper Juhlfa671642005-11-07 01:01:27 -08006830 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006831 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006832 kfree(chip->data_buf);
6833 kfree(chip->ecc.code_buf);
6834 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006835
6836 /* Free bad block descriptor memory */
6837 if (chip->badblock_pattern && chip->badblock_pattern->options
6838 & NAND_BBT_DYNAMICSTRUCT)
6839 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006840
6841 /* Free manufacturer priv data. */
6842 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006843
6844 /* Free controller specific allocations after chip identification */
6845 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006846
6847 /* Free identification phase allocations */
6848 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849}
Miquel Raynal05b54c72018-07-19 01:05:46 +02006850
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006851EXPORT_SYMBOL_GPL(nand_cleanup);
6852
6853/**
6854 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6855 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02006856 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006857 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02006858void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006859{
Boris Brezillon59ac2762018-09-06 14:05:15 +02006860 mtd_device_unregister(nand_to_mtd(chip));
6861 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006862}
David Woodhousee0c7d762006-05-13 18:07:53 +01006863EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006864
David Woodhousee0c7d762006-05-13 18:07:53 +01006865MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006866MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6867MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006868MODULE_DESCRIPTION("Generic NAND flash driver code");